Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 21966:5023b913e2ba
Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Mon, 22 Jun 2015 10:16:27 -0700 |
parents | 9d15f06f3537 |
children | ff6f34159b8a |
comparison
equal
deleted
inserted
replaced
21965:0103d237f6c3 | 21966:5023b913e2ba |
---|---|
27 import java.io.*; | 27 import java.io.*; |
28 import java.lang.ref.*; | 28 import java.lang.ref.*; |
29 import java.net.*; | 29 import java.net.*; |
30 import java.util.*; | 30 import java.util.*; |
31 | 31 |
32 import com.oracle.truffle.api.*; | |
32 import com.oracle.truffle.api.instrument.*; | 33 import com.oracle.truffle.api.instrument.*; |
33 | 34 |
34 /** | 35 /** |
35 * Representation of a guest language source code unit and its contents. Sources originate in | 36 * Representation of a guest language source code unit and its contents. Sources originate in |
36 * several ways: | 37 * several ways: |
214 * @param fileName | 215 * @param fileName |
215 * @return canonical representation of the file's contents. | 216 * @return canonical representation of the file's contents. |
216 * @throws IOException if the file cannot be found | 217 * @throws IOException if the file cannot be found |
217 */ | 218 */ |
218 public static Source fromFileName(CharSequence chars, String fileName) throws IOException { | 219 public static Source fromFileName(CharSequence chars, String fileName) throws IOException { |
220 CompilerAsserts.neverPartOfCompilation(); | |
219 | 221 |
220 final WeakReference<Source> nameRef = nameToSource.get(fileName); | 222 final WeakReference<Source> nameRef = nameToSource.get(fileName); |
221 Source source = nameRef == null ? null : nameRef.get(); | 223 Source source = nameRef == null ? null : nameRef.get(); |
222 if (source == null) { | 224 if (source == null) { |
223 final File file = new File(fileName); | 225 final File file = new File(fileName); |
240 * @param chars textual source code | 242 * @param chars textual source code |
241 * @param description a note about the origin, for error messages and debugging | 243 * @param description a note about the origin, for error messages and debugging |
242 * @return a newly created, non-indexed source representation | 244 * @return a newly created, non-indexed source representation |
243 */ | 245 */ |
244 public static Source fromText(CharSequence chars, String description) { | 246 public static Source fromText(CharSequence chars, String description) { |
247 CompilerAsserts.neverPartOfCompilation(); | |
248 | |
245 assert chars != null; | 249 assert chars != null; |
246 final LiteralSource source = new LiteralSource(description, chars.toString()); | 250 final LiteralSource source = new LiteralSource(description, chars.toString()); |
247 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); | 251 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); |
248 return source; | 252 return source; |
249 } | 253 } |
254 * | 258 * |
255 * @param description a note about the origin, for error messages and debugging | 259 * @param description a note about the origin, for error messages and debugging |
256 * @return a newly created, non-indexed, initially empty, appendable source representation | 260 * @return a newly created, non-indexed, initially empty, appendable source representation |
257 */ | 261 */ |
258 public static Source fromAppendableText(String description) { | 262 public static Source fromAppendableText(String description) { |
263 CompilerAsserts.neverPartOfCompilation(); | |
264 | |
259 final Source source = new AppendableLiteralSource(description); | 265 final Source source = new AppendableLiteralSource(description); |
260 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); | 266 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); |
261 return source; | 267 return source; |
262 } | 268 } |
263 | 269 |
269 * @param chars textual source code | 275 * @param chars textual source code |
270 * @param name string to use for indexing/lookup | 276 * @param name string to use for indexing/lookup |
271 * @return a newly created, source representation | 277 * @return a newly created, source representation |
272 */ | 278 */ |
273 public static Source fromNamedText(CharSequence chars, String name) { | 279 public static Source fromNamedText(CharSequence chars, String name) { |
280 CompilerAsserts.neverPartOfCompilation(); | |
281 | |
274 final Source source = new LiteralSource(name, chars.toString()); | 282 final Source source = new LiteralSource(name, chars.toString()); |
275 nameToSource.put(name, new WeakReference<>(source)); | 283 nameToSource.put(name, new WeakReference<>(source)); |
276 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); | 284 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); |
277 return source; | 285 return source; |
278 } | 286 } |
285 * | 293 * |
286 * @param name string to use for indexing/lookup | 294 * @param name string to use for indexing/lookup |
287 * @return a newly created, indexed, initially empty, appendable source representation | 295 * @return a newly created, indexed, initially empty, appendable source representation |
288 */ | 296 */ |
289 public static Source fromNamedAppendableText(String name) { | 297 public static Source fromNamedAppendableText(String name) { |
298 CompilerAsserts.neverPartOfCompilation(); | |
299 | |
290 final Source source = new AppendableLiteralSource(name); | 300 final Source source = new AppendableLiteralSource(name); |
291 nameToSource.put(name, new WeakReference<>(source)); | 301 nameToSource.put(name, new WeakReference<>(source)); |
292 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); | 302 notifyNewSource(source).tagAs(Tags.FROM_LITERAL); |
293 return source; | 303 return source; |
294 } | 304 } |
302 * @param length the number of characters in the sub-range | 312 * @param length the number of characters in the sub-range |
303 * @return a new instance representing a sub-range of another Source | 313 * @return a new instance representing a sub-range of another Source |
304 * @throws IllegalArgumentException if the specified sub-range is not contained in the base | 314 * @throws IllegalArgumentException if the specified sub-range is not contained in the base |
305 */ | 315 */ |
306 public static Source subSource(Source base, int baseCharIndex, int length) { | 316 public static Source subSource(Source base, int baseCharIndex, int length) { |
317 CompilerAsserts.neverPartOfCompilation(); | |
318 | |
307 final SubSource subSource = SubSource.create(base, baseCharIndex, length); | 319 final SubSource subSource = SubSource.create(base, baseCharIndex, length); |
308 return subSource; | 320 return subSource; |
309 } | 321 } |
310 | 322 |
311 /** | 323 /** |
316 * @param baseCharIndex 0-based index of the first character of the sub-range | 328 * @param baseCharIndex 0-based index of the first character of the sub-range |
317 * @return a new instance representing a sub-range at the end of another Source | 329 * @return a new instance representing a sub-range at the end of another Source |
318 * @throws IllegalArgumentException if the index is out of range | 330 * @throws IllegalArgumentException if the index is out of range |
319 */ | 331 */ |
320 public static Source subSource(Source base, int baseCharIndex) { | 332 public static Source subSource(Source base, int baseCharIndex) { |
333 CompilerAsserts.neverPartOfCompilation(); | |
334 | |
321 return subSource(base, baseCharIndex, base.getLength() - baseCharIndex); | 335 return subSource(base, baseCharIndex, base.getLength() - baseCharIndex); |
322 } | 336 } |
323 | 337 |
324 /** | 338 /** |
325 * Creates a source whose contents will be read immediately from a URL and cached. | 339 * Creates a source whose contents will be read immediately from a URL and cached. |
328 * @param description identifies the origin, possibly useful for debugging | 342 * @param description identifies the origin, possibly useful for debugging |
329 * @return a newly created, non-indexed source representation | 343 * @return a newly created, non-indexed source representation |
330 * @throws IOException if reading fails | 344 * @throws IOException if reading fails |
331 */ | 345 */ |
332 public static Source fromURL(URL url, String description) throws IOException { | 346 public static Source fromURL(URL url, String description) throws IOException { |
347 CompilerAsserts.neverPartOfCompilation(); | |
348 | |
333 final URLSource source = URLSource.get(url, description); | 349 final URLSource source = URLSource.get(url, description); |
334 notifyNewSource(source).tagAs(Tags.FROM_URL); | 350 notifyNewSource(source).tagAs(Tags.FROM_URL); |
335 return source; | 351 return source; |
336 } | 352 } |
337 | 353 |
342 * @param description a note about the origin, possibly useful for debugging | 358 * @param description a note about the origin, possibly useful for debugging |
343 * @return a newly created, non-indexed source representation | 359 * @return a newly created, non-indexed source representation |
344 * @throws IOException if reading fails | 360 * @throws IOException if reading fails |
345 */ | 361 */ |
346 public static Source fromReader(Reader reader, String description) throws IOException { | 362 public static Source fromReader(Reader reader, String description) throws IOException { |
363 CompilerAsserts.neverPartOfCompilation(); | |
364 | |
347 final LiteralSource source = new LiteralSource(description, read(reader)); | 365 final LiteralSource source = new LiteralSource(description, read(reader)); |
348 notifyNewSource(source).tagAs(Tags.FROM_READER); | 366 notifyNewSource(source).tagAs(Tags.FROM_READER); |
349 return source; | 367 return source; |
350 } | 368 } |
351 | 369 |
375 * @param description a note about the origin, possibly useful for debugging | 393 * @param description a note about the origin, possibly useful for debugging |
376 * @param decoder how to decode the bytes into Java strings | 394 * @param decoder how to decode the bytes into Java strings |
377 * @return a newly created, non-indexed source representation | 395 * @return a newly created, non-indexed source representation |
378 */ | 396 */ |
379 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) { | 397 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) { |
398 CompilerAsserts.neverPartOfCompilation(); | |
399 | |
380 final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder); | 400 final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder); |
381 notifyNewSource(source).tagAs(Tags.FROM_BYTES); | 401 notifyNewSource(source).tagAs(Tags.FROM_BYTES); |
382 return source; | 402 return source; |
383 } | 403 } |
384 | 404 |