map(dir -> new DirRepository(dir, true))
+ .toList();
+ return switch (list.size()) {
+ case 0 -> null;
+ case 1 -> list.getFirst();
+ default -> new LinkedRepository(list.toArray(ModuleRepository.NO_REPOS));
+ };
+ }
+
+ /**
+ * A snapshot of what the compiler is holding on to.
+ *
+ * An embedding host that keeps a compiler alive across many compilations - a language server
+ * is the obvious one - needs some way to see whether it is accumulating. These are the cheap
+ * numbers: taking them costs a field read and a repository listing, so a host can log one per
+ * compilation without measuring itself instead of the compiler.
+ *
+ * @param modules how many modules the configured repository offers
+ * @param constants how many constants are interned in the current pool
+ * @param invalidations how many times cached type information has been invalidated
+ * @param heapBytes used heap, which is the JVM's figure and not the compiler's alone
+ */
+ public record Footprint(int modules, int constants, int invalidations, long heapBytes) {
+ @Override
+ public String toString() {
+ return "modules=" + modules + ", constants=" + constants
+ + ", invalidations=" + invalidations
+ + ", heap=" + (heapBytes / (1024 * 1024)) + "MB";
+ }
+ }
+
+ /**
+ * Take a {@link Footprint} without selecting a compilation's pool. This does not initialize
+ * the runtime; pool counts are zero. Use {@link #footprint(Compilation)} for compiler counts.
+ *
+ * @return the snapshot; the counts are zero where nothing has been configured or built yet
+ */
+ public Footprint footprint() {
+ return footprint(null);
+ }
+
+ /**
+ * Take a passive snapshot of a compilation's pool and the configured repository.
+ *
+ * @param compilation the compilation to measure, or null for repository and heap counts only
+ *
+ * @return the snapshot; pool counts are zero if no file structure was built
+ */
+ public Footprint footprint(Compilation compilation) {
+ var pool = compilation == null ? null : compilation.pool();
+ var runtime = Runtime.getRuntime();
+ return new Footprint(cfgRepo == null ? 0 : cfgRepo.getModuleNames().size(), pool == null ? 0 : pool.size(),
+ pool == null ? 0 : pool.getInvalidationCount(), runtime.totalMemory() - runtime.freeMemory());
+ }
+
/**
* @return true if configured
* @throws IllegalStateException if not configured
@@ -91,9 +214,15 @@ private boolean verifyConfigured() {
// attempt to auto-configure
String home = System.getenv("XDK_HOME");
if (home != null) {
- File dir = new File(new File(home), "lib");
- if (dir.isDirectory()) {
- configure(new DirRepository(dir, true), null);
+ // an XDK keeps its libraries in lib/ and the two modules the compiler bootstraps
+ // against - the turtle, mack.xtclang.org, and the native bridge - in javatools/.
+ // Configuring only lib/ produced a repository that could never compile anything:
+ // every compile failed in prelinkSystemLibraries with "Unable to load module:
+ // mack.xtclang.org", reported as an internal error with no location
+ var dirHome = new File(home);
+ var repo = repoOver(new File(dirHome, "lib"), new File(dirHome, "javatools"));
+ if (repo != null) {
+ configure(repo, null);
}
}
@@ -126,6 +255,20 @@ public Connector ensureConnector() {
}
}
+ /**
+ * Shut down this instance's interpreter, if it has one, so that its runtime can be collected;
+ * a later run starts another. The singleton's interpreter lasts as long as the JVM; this is for
+ * the tests, which make instances of their own.
+ */
+ void shutdownInterpreter() {
+ synchronized (LOCK) {
+ if (connector != null) {
+ connector.shutdown();
+ connector = null;
+ }
+ }
+ }
+
/**
* Diagnostics for a host that keeps the runtime across runs: the external consoles that runs
* registered and that are still registered. A run releases its console when it ends, so this is
@@ -139,6 +282,20 @@ public int registeredConsoles() {
: 0;
}
+ /**
+ * Diagnostics for a host that keeps the runtime across runs: the threads of the runtime's own
+ * pool, which runs share. The pool is fixed in size, so this is at most one thread per core.
+ * A thread that a pool thread starts, such as the JDK's for a CompletableFuture's timeout,
+ * joins the pool's thread group but is not one of these; JIT runs have no pool.
+ *
+ * @return the number of threads in the runtime's pool
+ */
+ public int poolThreads() {
+ return ensureConnector() instanceof InterpreterConnector interpreter
+ ? interpreter.getNativeContainer().f_runtime.f_executorXVM.getPoolSize()
+ : 0;
+ }
+
// ----- API -----------------------------------------------------------------------------------
/**
@@ -149,13 +306,16 @@ public static EmbeddingSupport instance() {
}
/**
- * Provide configuration necessary for the underlying Ecstasy tools and libraries. Must be
- * called exactly one time before any other method.
+ * Provide configuration for the underlying Ecstasy tools and libraries before compiling or
+ * running a module. Repeating the same configuration is allowed; a conflicting configuration
+ * is rejected. Passive configuration and footprint queries do not require initialization.
*
* @param coreRepo the ModuleRepository to load the core libraries from
* @param customInjector (optional) "module:class" name of a custom injector implementation to
* use to provide injectable resources in lieu of the default injector
* for this implementation
+ *
+ * @return this configured host support instance
*/
public EmbeddingSupport configure(ModuleRepository coreRepo, String customInjector) {
synchronized (LOCK) {
@@ -195,14 +355,33 @@ public String getConfiguredInjector() {
}
/**
- * @return the ConstantPool of the core Ecstasy libraries used by the runtime Connector instance
- * that is instantiated by EmbeddingSupport
+ * Obtain the constant pool of the runtime, starting one if it is not running yet.
+ *
+ *
This is not the pool a compilation used - that belongs to the {@link Compilation} it
+ * produced. Asking for this one boots an interpreter: a connector builds a NativeContainer,
+ * which loads a native template for every core module, so it needs the whole library and not
+ * just the part the compiler bootstraps against. The old name said "get" and read like an
+ * accessor.
+ *
+ * @return the runtime's constant pool
*/
- public ConstantPool getConstantPool() {
+ public ConstantPool ensureRuntimePool() {
verifyConfigured();
return ensureConnector().getConstantPool();
}
+ /**
+ * Obtain the runtime's constant pool, starting the runtime if necessary.
+ *
+ * @return the runtime's constant pool
+ * @deprecated use {@link #ensureRuntimePool()} to make runtime initialization explicit;
+ * use {@link Compilation#pool()} for a compilation's pool
+ */
+ @Deprecated
+ public ConstantPool getConstantPool() {
+ return ensureRuntimePool();
+ }
+
// ----- compiler support ----------------------------------------------------------------------
/**
@@ -210,33 +389,770 @@ public ConstantPool getConstantPool() {
*
* @param source the source code for an entire module to compile
* @param input (optional) the module repository to read any required modules from
- * @param errs (optional) the ErrorListener to log any compiler messages to
+ * @param errs the ErrorListener to log any compiler messages to
*
* @return the resulting ModuleStructure, or null if a compiler error occurred
*
* @see ModuleCompiler#compile(String, ModuleRepository, ErrorListener)
*/
- public ModuleStructure compile(String source, ModuleRepository input, ErrorListener errs) {
+ public ModuleStructure compile(String source, ModuleRepository input, @NotNull ErrorListener errs) {
+ return compile(new Source(source), input, errs);
+ }
+
+ /**
+ * Compile a module held in memory, as a named document.
+ *
+ *
A diagnostic's identity includes the name of the source it came from, so a host holding
+ * several documents that are not on disk - an editor's unsaved buffers - has to be able to
+ * tell them apart. Without a name, two documents with a problem at the same offset produce
+ * the same identity and a listener that deduplicates discards the second. The name belongs to
+ * the document rather than to the act of compiling it, which is why this takes a
+ * {@link Source}: {@code new Source(text, uri)} says it once, where a second String parameter
+ * would sit next to the first and be silently swappable with it.
+ *
+ * @param source the source to compile, carrying whatever name it was created with
+ * @param input (optional) the module repository to read any required modules from
+ * @param errs the ErrorListener to log any compiler messages to
+ *
+ * @return the resulting ModuleStructure, or null if a compiler error occurred
+ */
+ public ModuleStructure compile(Source source, ModuleRepository input, @NotNull ErrorListener errs) {
+ return compileModule(source, input, errs).module();
+ }
+
+ /**
+ * The outcome of compiling a source or module source tree.
+ *
+ *
A failed compilation used to answer with nothing but null, which threw away everything the
+ * attempt had built. That is most of what a host wants when it fails: the structures a
+ * verification error was raised against still exist, and the pool they were interned in is
+ * the only way to reach them - to ask a type what building its TypeInfo had to say, for
+ * instance.
+ *
+ *
Recovered source trees have not entered compiler passes. Traverse their children using
+ * each root's Source; child parent pointers may not yet be installed. Binding maps are
+ * immutable identity snapshots: keys and values compare by reference, not structural equality.
+ *
+ * @param module the compiled module, or null if the compilation did not get that far
+ * @param file the file structure that was built, or null if it did not get that far
+ * @param ast the assembled source tree, or null if loading/parsing failed; linked
+ * member trees retain their original Sources and positions
+ * @param sourceTrees available per-source syntax, including recovered trees on parse failure;
+ * these are structural facts, not a promise of semantic validity
+ * @param callBindings immutable call facts keyed by surviving invocation identity; compiler
+ * objects remain worker-owned and must be copied before concurrent use
+ * @param functionBindings validated function signatures with no selected runtime method;
+ * ownership is the same as for callBindings
+ * @param constructorBindings validated constructor and argument provenance; the same ownership
+ * rules apply, with no additional state on the construction AST
+ * @param initializerBindings successful constant-initializer facts anchored to surviving source
+ * properties; no speculative clone nodes or contexts are retained
+ */
+ public record Compilation(ModuleStructure module, FileStructure file, StatementBlock ast,
+ List sourceTrees,
+ Map callBindings,
+ Map functionBindings,
+ Map constructorBindings,
+ Map initializerBindings) {
+ public Compilation {
+ sourceTrees = List.copyOf(sourceTrees);
+ callBindings = identitySnapshot(callBindings);
+ functionBindings = identitySnapshot(functionBindings);
+ constructorBindings = identitySnapshot(constructorBindings);
+ initializerBindings = identitySnapshot(initializerBindings);
+ }
+
+ /**
+ * Retain hosts that supply constructor bindings without folded initializer facts.
+ *
+ * @param module the compiled module, or null when compilation did not succeed
+ * @param file the file structure produced by the attempt, or null if none was built
+ * @param ast the assembled source root, or null if loading or parsing failed
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ * @param callBindings validated method-call facts keyed by invocation identity
+ * @param functionBindings validated function-call facts keyed by invocation identity
+ * @param constructorBindings validated constructor-call facts keyed by construction identity
+ */
+ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast,
+ List sourceTrees, Map callBindings,
+ Map functionBindings,
+ Map constructorBindings) {
+ this(module, file, ast, sourceTrees, callBindings, functionBindings, constructorBindings, Map.of());
+ }
+
+ /**
+ * Retain hosts that supply method and function bindings.
+ *
+ * @param module the compiled module, or null when compilation did not succeed
+ * @param file the file structure produced by the attempt, or null if none was built
+ * @param ast the assembled source root, or null if loading or parsing failed
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ * @param callBindings validated method-call facts keyed by invocation identity
+ * @param functionBindings validated function-call facts keyed by invocation identity
+ */
+ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast,
+ List sourceTrees, Map callBindings,
+ Map functionBindings) {
+ this(module, file, ast, sourceTrees, callBindings, functionBindings, Map.of());
+ }
+
+ /**
+ * Retain hosts that supply only statically selected method calls.
+ *
+ * @param module the compiled module, or null when compilation did not succeed
+ * @param file the file structure produced by the attempt, or null if none was built
+ * @param ast the assembled source root, or null if loading or parsing failed
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ * @param callBindings validated method-call facts keyed by invocation identity
+ */
+ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast,
+ List sourceTrees, Map callBindings) {
+ this(module, file, ast, sourceTrees, callBindings, Map.of());
+ }
+
+ /**
+ * Retain the construction API for hosts supplying structural source trees.
+ *
+ * @param module the compiled module, or null when compilation did not succeed
+ * @param file the file structure produced by the attempt, or null if none was built
+ * @param ast the assembled source root, or null if loading or parsing failed
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ */
+ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, List sourceTrees) {
+ this(module, file, ast, sourceTrees, Map.of());
+ }
+
+ /**
+ * Retain the original construction API for hosts supplying one assembled tree.
+ *
+ * @param module the compiled module, or null when compilation did not succeed
+ * @param file the file structure produced by the attempt, or null if none was built
+ * @param ast the assembled source root, or null if loading or parsing failed
+ */
+ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast) {
+ this(module, file, ast, ast == null ? List.of() : List.of(ast));
+ }
+
+ /**
+ * Represent file structures without a successful module or retained source tree.
+ *
+ * @param file the file structure produced so far
+ *
+ * @return a partial compilation
+ */
+ public static Compilation forFile(FileStructure file) {
+ return new Compilation(null, requireNonNull(file, "file"), null);
+ }
+
+ /**
+ * @return true iff a module came out of it
+ */
+ public boolean succeeded() {
+ return module != null;
+ }
+
+ /**
+ * @return the pool this compilation interned into, or null if there was no compilation
+ */
+ public ConstantPool pool() {
+ return file == null ? null : file.getConstantPool();
+ }
+
+ /**
+ * Walk the assembled source after successful loading/parsing.
+ *
+ * A host that wants to say where something is has to come through here: a
+ * {@link org.xvm.asm.Component} knows its name, its kind and its children, and nothing
+ * about the text it was written in. Only the AST carries positions.
+ *
+ * @return the assembled source root, or null after loading/parsing errors; use
+ * {@link #sourceTrees()} for available structural syntax in that case
+ */
+ public StatementBlock parsed() {
+ return ast;
+ }
+ }
+
+ /**
+ * Compile a module held in memory, and answer with everything the attempt produced.
+ *
+ * @param source the source to compile, carrying whatever name it was created with
+ * @param input (optional) the module repository to read any required modules from
+ * @param errs the ErrorListener to log any compiler messages to
+ *
+ * @return the outcome; never null, though its parts may be
+ */
+ public Compilation compileModule(Source source, ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(source, "source");
+ return compileModule(listener -> ParsedSources.parse(source, listener), input, errs);
+ }
+
+ /**
+ * Compile a module's source tree using the same discovery, resource association and parse-tree
+ * assembly as the CLI. The returned AST includes the member files with their original Sources.
+ * Hosts may subclass {@link ModuleInfo} and override its protected {@code readSource(File)}
+ * and {@code sourceEntries(File)} hooks to supply a consistent snapshot of unsaved text and
+ * source membership. The default provider discovers and reads files from disk.
+ *
+ * @param sources a fresh ModuleInfo for this attempt; do not reuse a previously parsed tree
+ * @param input the optional repository of compiled dependencies
+ * @param errs the listener for this compilation
+ *
+ * @return the outcome, including the linked AST when source loading succeeded
+ */
+ public Compilation compileModule(ModuleInfo sources, ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(sources, "sources");
+ return compileModule(listener -> ParsedSources.parse(sources, listener), input, errs);
+ }
+
+ /**
+ * A successfully resolved declaration graph, before expression validation and code generation.
+ * It contains no executable artifact. The calling worker exclusively owns these structures and
+ * may inspect TypeInfo with its listener; ordinary compilation still validates every body.
+ *
+ * @param file the file structure containing the resolved declarations
+ * @param ast the assembled source tree
+ * @param sourceTrees the available per-source trees, retained in source order
+ */
+ public record DeclarationAnalysis(FileStructure file, StatementBlock ast, List sourceTrees) {
+ public DeclarationAnalysis {
+ requireNonNull(file, "file");
+ requireNonNull(ast, "ast");
+ sourceTrees = List.copyOf(sourceTrees);
+ }
+
+ /**
+ * Obtain the pool that owns these resolved declarations.
+ *
+ * @return the constant pool owned by this declaration analysis
+ */
+ public ConstantPool pool() {
+ return file.getConstantPool();
+ }
+ }
+
+ /**
+ * Resolve one source's declarations in a fresh attempt. Body errors are outside this operation's
+ * scope; syntax, linkage, declaration errors and cancellation prevent a result.
+ *
+ * @param source the source to parse
+ * @param input optional compiled dependencies
+ * @param errs the host listener
+ * @return resolved declarations, never a compiled module
+ */
+ public Optional analyzeDeclarations(Source source, ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(source, "source");
+ return analyzeDeclarations(listener -> ParsedSources.parse(source, listener), input, errs);
+ }
+
+ /**
+ * Resolve a module tree's declarations using the same source/resource overlay contract as
+ * {@link #compileModule(ModuleInfo, ModuleRepository, ErrorListener)}. Use a fresh ModuleInfo.
+ *
+ * @param sources the module's source tree
+ * @param input optional compiled dependencies
+ * @param errs the host listener
+ * @return resolved declarations, empty after any declaration failure or cancellation
+ */
+ public Optional analyzeDeclarations(ModuleInfo sources, ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(sources, "sources");
+ return analyzeDeclarations(listener -> ParsedSources.parse(sources, listener), input, errs);
+ }
+
+ private Optional analyzeDeclarations(Function parse,
+ ModuleRepository input, ErrorListener errs) {
+ var attempt = runCompiler(parse, input, errs, CursorBinding.Collector.NONE, CompilationGoal.DECLARATIONS);
+ var compiler = attempt.compiler();
+ return attempt.succeeded()
+ ? Optional.of(new DeclarationAnalysis(compiler.file, compiler.ast, compiler.sourceTrees))
+ : Optional.empty();
+ }
+
+ /**
+ * Facts retained by an explicit incomplete-source analysis, never a compiled module.
+ *
+ * The available source syntax and sites may be unvalidated. A child expression supplies a
+ * semantic fact only if validation succeeded (isValidated and a fitting TypeFit) and its
+ * resolved target/type is available. Failed validation can leave placeholder types. An absent
+ * pool means semantic analysis did not start. Cursor bindings copy visible scope and candidate
+ * signatures/mappings fitted to written arguments; they never select the incomplete operation
+ * or invent a missing argument/result. Consumers must copy facts while exclusively
+ * owning the attempt, as with Compilation; ASTs and pools are not concurrent query objects.
+ * Binding maps preserve reference identity for both keys and values and reject null entries.
+ *
+ * @param sourceTrees available source syntax, including unvalidated recovered trees
+ * @param sites incomplete source sites retained by this attempt
+ * @param pool the attempt's constant pool, or null if semantic analysis did not start
+ * @param callBindings validated method calls keyed by source invocation identity
+ * @param cursorBindings visible scope and candidate facts keyed by incomplete site identity
+ * @param functionBindings validated function signatures keyed by source invocation identity
+ */
+ public record PartialAnalysis(List sourceTrees, List sites,
+ @Nullable ConstantPool pool,
+ Map callBindings,
+ Map cursorBindings,
+ Map functionBindings) {
+ public PartialAnalysis {
+ sourceTrees = List.copyOf(sourceTrees);
+ sites = List.copyOf(sites);
+ cursorBindings = identitySnapshot(cursorBindings);
+ callBindings = identitySnapshot(callBindings);
+ functionBindings = identitySnapshot(functionBindings);
+ }
+
+ /**
+ * Create partial facts with cursor and selected-method bindings.
+ *
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ * @param sites the retained incomplete source sites
+ * @param pool the attempt's pool, or null if semantic analysis did not start
+ * @param callBindings validated method-call facts keyed by invocation identity
+ * @param cursorBindings visible scope and candidate facts keyed by incomplete-site identity
+ */
+ public PartialAnalysis(List sourceTrees, List sites,
+ @Nullable ConstantPool pool,
+ Map callBindings,
+ Map cursorBindings) {
+ this(sourceTrees, sites, pool, callBindings, cursorBindings, Map.of());
+ }
+
+ /**
+ * Create partial facts with call and receiver information only.
+ *
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ * @param sites the retained incomplete source sites
+ * @param pool the attempt's pool, or null if semantic analysis did not start
+ * @param callBindings validated method-call facts keyed by invocation identity
+ */
+ public PartialAnalysis(List sourceTrees, List sites,
+ @Nullable ConstantPool pool, Map callBindings) {
+ this(sourceTrees, sites, pool, callBindings, Map.of());
+ }
+
+ /**
+ * Create partial facts with syntax and receiver information only.
+ *
+ * @param sourceTrees the available per-source syntax trees, including recovered trees
+ * @param sites the retained incomplete source sites
+ * @param pool the attempt's pool, or null if semantic analysis did not start
+ */
+ public PartialAnalysis(List sourceTrees, List sites, @Nullable ConstantPool pool) {
+ this(sourceTrees, sites, pool, Map.of());
+ }
+ }
+
+ /**
+ * Analyze the intact prefix of one trailing incomplete expression statement in a single
+ * source module. Supports a member-access dot or unfinished call at EOF, with complete
+ * preceding declarations/statements and arguments. Other syntax errors prevent semantic
+ * analysis; returns, assignments, incomplete nested arguments and module member files are
+ * outside this bounded contract. Complete input has no incomplete site and is not compiled.
+ *
+ * Parsing reports immediately to the host and respects its budget/cancellation. Only the
+ * recognized EOF boundary may enter compiler passes. The incomplete statement validates its
+ * intact children in the real method context, then fails validation before method emission.
+ * Its repeated EOF diagnostic stops the compiler internally without being delivered twice
+ * to the host. Other diagnostics and unexpected failures remain visible.
+ *
+ * @param source a fresh named source for this attempt
+ * @param input the optional repository of compiled dependencies
+ * @param errs the host's diagnostic listener
+ *
+ * @return partial facts with no success/module artifact contract
+ */
+ public PartialAnalysis analyzeIncomplete(Source source, ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(source, "source");
+ return analyzeIncomplete(listener -> {
+ var tree = Parser.forPartialAnalysis(source, listener).parseSource();
+ return new ParsedSources(tree, List.of(tree));
+ }, Parser.UNEXPECTED_EOF, input, errs);
+ }
+
+ /**
+ * Analyze an incomplete member access or call at an explicit source position, including before
+ * existing closing braces or a semicolon. Source text is unchanged and later declarations retain
+ * their positions. The cursor must come from {@link Source#getPosition()} for this exact text.
+ *
+ *
Expression queries support standalone statements, assignment and initializer values, single
+ * return values, and nested call arguments, including named arguments and existing closing
+ * parentheses. Member tokens retain their completion prefix. A cursor before a call's closing
+ * parenthesis inspects its receiver and written arguments even when the selected syntax is
+ * complete. The selected member or call itself is not validated. Bare-name prefixes and empty
+ * statement boundaries expose visible scope; a named argument awaiting its value retains its
+ * label. Candidate signatures infer types from written arguments. Compound and conditional
+ * values, and arguments following the cursor, retain their real contexts.
+ *
+ *
Recovery retains missing call/group/tuple parentheses and index/list/map brackets at
+ * statement or outer-delimiter boundaries. Declaration values can retain missing terminators,
+ * parameter defaults can retain a missing closing parenthesis before a body, and a cursor at
+ * EOF can retain missing block braces. Recovery does not invent operands, declaration names or
+ * types, or literal contents, and does not repair unrelated errors.
+ *
+ *
Construction queries reuse normal receiver/type preparation, including virtual, inner and
+ * annotated types, required-type and argument-derived class inference, and parenthesized array
+ * initializers. Anonymous construction prepares the retained declaration and fits its own or
+ * superclass constructors without emitting forwarding constructors or running capture analysis.
+ * Single-dimensional array brackets fit the fixed-size constructor's size parameter, including
+ * an empty/final-name slot and a missing closing bracket. A following supplier is parsed for
+ * recovery but is outside this prefix proof. Multidimensional construction remains unsupported.
+ *
+ *
Declaration queries resolve member, return and method-parameter type prefixes through the
+ * enclosing compiler scope. Flat qualified names resolve their qualifier and visible nested
+ * types, replacing only the final written identifier. Unqualified empty parameter slots are
+ * supported. An unfinished header retains its written name and range but registers no method,
+ * property component or parameters. Written leaf names inside parameterized and compound types
+ * participate, including bounded missing angle/group closers; these are visible-type suggestions,
+ * not proof of generic constraints. Empty type/qualified slots, selected qualifier tokens,
+ * parameterized qualifiers, generic base names and function/sequence types are supported.
+ *
+ *
Generic-method, multiple-return and type-composition headers query their enclosing scope.
+ * Unregistered formals hide outer names without acquiring invented identities. A root module
+ * header registers only its written namespace/core import; its incomplete body and compositions
+ * cannot emit or contribute facts. Other syntax errors prevent semantic analysis, and cursors
+ * outside supported boundaries yield no site. Partial facts never promise executable output.
+ *
+ * @param source a fresh named source whose text remains unchanged during this attempt
+ * @param cursor the encoded source position at which to inspect incomplete syntax
+ * @param input compiled dependencies, or null to use only the configured core repository
+ * @param errs the host's diagnostic listener and abort policy
+ *
+ * @return retained source trees and available cursor/binding facts, without an executable module
+ */
+ public PartialAnalysis analyzeIncomplete(Source source, long cursor, ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(source, "source");
+ return analyzeIncomplete(listener -> {
+ var tree = Parser.forPartialAnalysis(source, cursor, listener).parseSource();
+ return new ParsedSources(tree, List.of(tree));
+ }, Parser.INCOMPLETE_EXPRESSION, input, errs);
+ }
+
+ /**
+ * Analyze an explicit cursor in one file of a module snapshot. Uses the same source loading,
+ * overlay and resource contract as {@link #compileModule(ModuleInfo, ModuleRepository, ErrorListener)}.
+ * The cursor refers to the selected file's snapshot, which may contain unsaved text. Neither
+ * this ModuleInfo nor its parsed trees may be reused for another attempt or normal compilation.
+ * Only the recognized cursor diagnostic is deferred during assembly; other source errors abort it.
+ *
+ * @param sources a fresh module source snapshot for this attempt
+ * @param sourceFile the file in that snapshot containing the cursor
+ * @param cursor the selected position in the owning Source snapshot
+ * @param input the optional repository of compiled dependencies
+ * @param errs the diagnostic listener and abort policy for this operation
+ *
+ * @return retained syntax and any facts proved at the cursor; never a compiled module
+ */
+ public PartialAnalysis analyzeIncomplete(ModuleInfo sources, File sourceFile, long cursor,
+ ModuleRepository input, @NotNull ErrorListener errs) {
+ requireNonNull(sources, "sources");
+ var name = resolveFile(requireNonNull(sourceFile, "sourceFile")).getPath();
+ return analyzeIncomplete(listener -> {
+ var assembly = ErrorListener.cancellable(ErrorListener.collecting(listener::log), listener::isAbortDesired);
+ var root = sources.getSourceTree(assembly, (source, nodeErrors) -> {
+ if (!name.equals(source.getFileName())) {
+ return new Parser(source, nodeErrors).parseSource();
+ }
+ ErrorListener partial = ErrorListener.cancellable(ErrorListener.collecting(error -> {
+ if (error.getCode().equals(Parser.INCOMPLETE_EXPRESSION)) {
+ listener.log(error);
+ } else {
+ nodeErrors.log(error);
+ }
+ }), listener::isAbortDesired);
+ return Parser.forPartialAnalysis(source, cursor, partial).parseSource();
+ });
+ return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), sources.getParsedSources());
+ }, Parser.INCOMPLETE_EXPRESSION, input, errs);
+ }
+
+ private PartialAnalysis analyzeIncomplete(Function parse, String boundaryCode,
+ ModuleRepository input, ErrorListener errs) {
+ verifyConfigured();
+ requireNonNull(errs, "errs");
+ Set delivered = new HashSet<>();
+ var host = ErrorListener.cancellable(ErrorListener.collecting(error -> {
+ if (delivered.add(error.genUID())) {
+ errs.log(error);
+ }
+ }), errs::isAbortDesired);
+ var syntaxErrors = new ErrorList(ErrorList.UNLIMITED);
+ ParsedSources parsed;
+ try {
+ if (host.isAbortDesired()) {
+ return new PartialAnalysis(List.of(), List.of(), null);
+ }
+ parsed = parse.apply(ErrorListener.tee(syntaxErrors, host));
+ } catch (CompilerException e) {
+ return new PartialAnalysis(List.of(), List.of(), null);
+ } catch (RuntimeException | AssertionError e) {
+ host.error(ERR_INTERNAL, NOWHERE, e, "Incomplete-source parsing failed");
+ return new PartialAnalysis(List.of(), List.of(), null);
+ }
+
+ if (parsed.root() == null) {
+ return new PartialAnalysis(parsed.sources(), List.of(), null);
+ }
+ var sites = incompleteSites(parsed.root()).distinct().toList();
+ if (sites.size() != 1 || syntaxErrors.getErrors().stream().anyMatch(error ->
+ error.getSeverity().isAtLeast(ERROR) && !error.getCode().equals(boundaryCode))) {
+ return new PartialAnalysis(parsed.sources(), List.of(), null);
+ }
+
+ var cursors = new CursorBinding.Collector();
+ var attempt = runCompiler(listener -> parsed, input, host, cursors, CompilationGoal.PARTIAL).compiler().result();
+ // Anonymous construction can replace its deferred body with an owned class/validation
+ // clone. Publish only surviving syntax, never the pre-validation cursor identity.
+ // Primary-constructor defaults can expose the same surviving node through both the
+ // written parameter and generated property getter. Publish each identity only once.
+ var surviving = incompleteSites(parsed.root()).distinct().toList();
+ return new PartialAnalysis(parsed.sources(), surviving, attempt.pool(),
+ attempt.callBindings(), cursors.finish(parsed.sources()), attempt.functionBindings());
+ }
+
+ /** Parsed children exist before parent links; expose the innermost unfinished operations. */
+ private static Stream incompleteSites(AstNode node) {
+ var nested = PartialSyntax.children(node).flatMap(EmbeddingSupport::incompleteSites);
+ if (node instanceof IncompleteStatement site) {
+ var descendants = nested.toList();
+ return descendants.isEmpty() ? Stream.of(site) : descendants.stream();
+ }
+ return nested;
+ }
+
+ /** Preserve node identity, reject null entries and detach from the collector. */
+ private static Map identitySnapshot(Map source) {
+ var copy = new IdentityHashMap();
+ source.forEach((key, value) -> copy.put(requireNonNull(key), requireNonNull(value)));
+ return Collections.unmodifiableMap(copy);
+ }
+
+ /** Available source trees, with an assembled root only when parsing and loading succeeded. */
+ private record ParsedSources(StatementBlock root, List sources) {
+ /** Keep recovered syntax for diagnostics even when errors prevent compiling the root. */
+ static ParsedSources parse(Source source, ErrorListener errs) {
+ var tree = new Parser(source, errs).parseSource();
+ return new ParsedSources(errs.hasSeriousErrors() ? null : tree, List.of(tree));
+ }
+
+ /** Assemble the module before reading the per-source trees populated during parsing. */
+ static ParsedSources parse(ModuleInfo sources, ErrorListener errs) {
+ var root = sources.getSourceTree(errs);
+ return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), sources.getParsedSources());
+ }
+ }
+
+ private Compilation compileModule(Function parse, ModuleRepository input, ErrorListener errs) {
+ return runCompiler(parse, input, errs, CursorBinding.Collector.NONE, CompilationGoal.MODULE).compiler().result();
+ }
+
+ /**
+ * Selects declaration resolution alone, the full validation and emission pipeline, or that
+ * pipeline up to its first error, as a partial analysis needs: the diagnostic of its incomplete
+ * site is what stops the method that holds it from emitting.
+ */
+ private enum CompilationGoal { DECLARATIONS, MODULE, PARTIAL }
+
+ /** Keeps partial compiler state alongside whether the requested goal completed successfully. */
+ private record CompilerAttempt(EmbeddingCompiler compiler, boolean succeeded) {}
+
+ private CompilerAttempt runCompiler(Function parse,
+ ModuleRepository input, ErrorListener errs,
+ CursorBinding.Collector cursors, CompilationGoal goal) {
verifyConfigured();
- return new ModuleCompiler(cfgRepo).compile(source, input, errs);
+ requireNonNull(errs, "errs");
+ var compiler = new EmbeddingCompiler(parse, input, cfgRepo, errs, cursors, goal);
+ try {
+ return new CompilerAttempt(compiler, !errs.isAbortDesired() && compiler.process() == 0
+ && !errs.hasSeriousErrors() && !errs.isAbortDesired());
+ } catch (LauncherException e) {
+ // Expected aborts already have diagnostics or a cancellation request.
+ if (!errs.hasSeriousErrors() && !errs.isAbortDesired()) {
+ errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed");
+ }
+ } catch (RuntimeException | AssertionError e) {
+ // An earlier source error must not hide an unexpected compiler failure.
+ errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed");
+ }
+ return new CompilerAttempt(compiler, false);
}
/**
- * Compile a module that is in a file, against the configured core repository.
+ * Compile a module that is in a file or directory, against the configured core repository.
*
- * @param file the module source file
+ * @param file the location of the module source code on disk, either the module source file
+ * or the directory containing a single .x file and nested contents thereof
* @param input (optional) the module repository to read any required modules from
* @param output (optional) the module repository to write any compiled modules to
- * @param errs (optional) the ErrorListener to log any compiler messages to
+ * @param errs the ErrorListener to log any compiler messages to
*
* @return true if the compilation succeeded and the result was placed into the output
*
* @see ModuleCompiler#compile(File, ModuleRepository, ModuleRepository, ErrorListener)
*/
- public boolean compile(File file, ModuleRepository input, ModuleRepository output,
- ErrorListener errs) {
+ public boolean compile(File file, ModuleRepository input, ModuleRepository output, @NotNull ErrorListener errs) {
verifyConfigured();
- return new ModuleCompiler(cfgRepo).compile(file, input, output, errs);
+ requireNonNull(errs, "errs");
+ ModuleStructure module;
+ try {
+ module = compileModule(new ModuleInfo(file, false), input, errs).module();
+ } catch (IllegalArgumentException e) {
+ errs.error(ERR_INTERNAL, NOWHERE, e, "Unable to locate module " + file);
+ return false;
+ }
+ return ModuleCompiler.storeCompiled(module, output, errs);
+ }
+
+ /**
+ * Adapter that supplies the source and repositories to the standard compiler pipeline and
+ * captures its single compiled module instead of writing it to disk.
+ */
+ private static class EmbeddingCompiler
+ extends org.xvm.tool.Compiler {
+ private final Function parse;
+ private final InvocationBinding.Collector bindings = new InvocationBinding.Collector();
+ private final CursorBinding.Collector cursors;
+ private final CompilationGoal goal;
+
+ private final ModuleRepository inRepo;
+ private final ModuleRepository coreRepo;
+ private ModuleStructure module;
+ private FileStructure file;
+ private StatementBlock ast;
+ private List sourceTrees = List.of();
+
+ /**
+ * Everything this attempt produced.
+ *
+ * Each part is present when the attempt got that far and null when it did not, which the
+ * fields already say: the module is only ever assigned on success, and the file structure
+ * and the AST only once they exist. So there is nothing for the caller to decide, and no
+ * success flag to pass in and get wrong.
+ *
+ * @return the outcome; never null, though its parts may be
+ */
+ Compilation result() {
+ var facts = bindings.finishFacts(ast == null ? List.of() : List.of(ast));
+ return new Compilation(module, file, ast, sourceTrees, facts.methods(), facts.functions(),
+ facts.constructors(), facts.initializers());
+ }
+
+ /**
+ * @return the file structure this compilation built, which exists whether or not the
+ * compilation went on to succeed
+ */
+ FileStructure getFileStructure() {
+ return file;
+ }
+
+ @Override
+ public boolean isAbortDesired() {
+ // Cancellation must also stop between phases; stopping only an individual compiler
+ // loop would let the following phase run against an unfinished predecessor.
+ return super.isAbortDesired() || m_errors.isAbortDesired();
+ }
+
+ protected EmbeddingCompiler(Function parse,
+ ModuleRepository input, ModuleRepository core, ErrorListener errs,
+ CursorBinding.Collector cursors, CompilationGoal goal) {
+ super(CompilerOptions.builder().build(), SILENT_CONSOLE, errs);
+
+ this.parse = parse;
+ this.cursors = cursors;
+ this.goal = goal;
+ this.inRepo = input;
+ this.coreRepo = core;
+ }
+
+ @Override
+ protected int process() {
+ var repoLib = ensureLibraryRepo();
+ checkErrors("repository setup");
+
+ prelinkSystemLibraries(repoLib);
+ checkErrors("system library linking");
+
+ // The launcher stops the next compilation stage on any error. Each stage still goes on
+ // past its own errors, subject to the host's budget and cancellation, and so reports
+ // all of them: a stage logging to the launcher directly would stop at its first error,
+ // and a changed method would be an error in one of its callers instead of in each. A
+ // partial analysis is the exception, which stops at its incomplete site's error.
+ var errs = ErrorListener.cancellable(ErrorListener.collecting(this::log), m_errors::isAbortDesired);
+ StatementBlock block;
+ try {
+ var parsed = parse.apply(errs);
+ block = this.ast = parsed.root();
+ this.sourceTrees = parsed.sources();
+ } catch (CompilerException e) {
+ return 1;
+ }
+ if (checkErrors("source parsing") != 0) {
+ return 1;
+ }
+
+ if (block == null) {
+ error(ERR_MODULE_SOURCE, NOWHERE);
+ return 1;
+ }
+
+ if (block.getStatements().isEmpty()) {
+ block.log(this, ERROR, ERR_MODULE_SOURCE);
+ return checkErrors("source parsing");
+ }
+ var stmt = block.getStatements().getLast();
+ if (!(stmt instanceof TypeCompositionStatement stmtModule) || stmtModule.getCategory().getId() != Id.MODULE) {
+ stmt.log(this, ERROR, ERR_MODULE_SOURCE);
+ return checkErrors("source parsing");
+ }
+
+ var compiler = new Compiler(stmtModule, goal == CompilationGoal.PARTIAL ? this : errs, bindings, cursors);
+ var struct = compiler.generateInitialFileStructure();
+ this.file = struct;
+ if (struct == null || checkErrors("module creation") != 0) {
+ return 1;
+ }
+
+ try {
+ repoLib.storeModule(struct.getModule());
+ } catch (IOException e) {
+ log(ERROR, e, "I/O exception storing module: {}", struct.getModule().getName());
+ return 1;
+ }
+
+ if (goal == CompilationGoal.DECLARATIONS) {
+ resolveDeclarations(List.of(compiler), repoLib);
+ return hasSeriousErrors() || isAbortDesired() ? 1 : 0;
+ }
+
+ int result = super.compile(List.of(compiler), repoLib);
+ if (result == 0 && !isAbortDesired()) {
+ this.module = struct.getModule();
+ }
+ return result;
+ }
+
+ @Override
+ protected int compile(List compilers, ModuleRepository repoLib) {
+ throw new IllegalStateException("This method must not be called");
+ }
+
+ @Override
+ protected ModuleRepository configureLibraryRepo(List ignore) {
+ var build = new BuildRepository();
+ return inRepo == null || inRepo == coreRepo
+ ? new LinkedRepository(true, build, coreRepo)
+ : new LinkedRepository(true, build, inRepo, coreRepo);
+ }
+
+ @Override
+ protected int emitModules(List allNodes, ModuleRepository ignore) {
+ throw new IllegalStateException("This method must not be called");
+ }
+
+ /**
+ * @return the result of the compilation
+ */
+ protected ModuleStructure getModule() {
+ return module;
+ }
}
/**
@@ -268,7 +1184,7 @@ public interface ConsoleInput {
* @return input that reads lines from the specified reader and ignores "suppressEcho"
*/
static ConsoleInput of(BufferedReader reader) {
- Objects.requireNonNull(reader, "reader");
+ requireNonNull(reader, "reader");
return _ -> reader.readLine();
}
}
@@ -286,7 +1202,7 @@ public record EntryPoint(String name, List args) {
public static final EntryPoint RUN = new EntryPoint("run");
public EntryPoint {
- Objects.requireNonNull(name, "name");
+ requireNonNull(name, "name");
args = List.copyOf(args);
}
@@ -347,7 +1263,7 @@ public interface Control
/**
* Create a runtime container and execute the provided module.
*
- * A limited set of injections are made available to the module, including the console, clock,
+ * A limited set of injections are made available to the module, including the console, clock,
* and other "safe" injectable types. The FileSystem is provided as detailed by the "rootDir"
* parameter.
*
@@ -358,7 +1274,7 @@ public interface Control
* task-specific directory under "./.runner" that is deleted when the
* returned Control is closed
* @param injections (optional) additional "String" and "String[]" injections
- * @param errs (optional) a means for the container to report uncaught exceptions and
+ * @param errs a means for the container to report uncaught exceptions and
* other errors
*
* @return a Control object for the running module
@@ -376,7 +1292,7 @@ public Control run(
/**
* Create a runtime container and execute the specified module.
*
- * The "customerInjector" option allows the caller to indicate an Ecstasy Injector class that
+ *
The "customerInjector" option allows the caller to indicate an Ecstasy Injector class that
* will be loaded into its own container, and provided with the full set of injectable resources
* that Ecstasy supports, also including any provided String injections; in turn, that
* implementation provides the injections that will be available to the specified module within
@@ -394,7 +1310,7 @@ public Control run(
* @param customInjector (optional) "module:class" name of a custom injector implementation to
* use to provide injectable resources; when used, the "rootDir" value is
* ignored
- * @param errs (optional) a means for the container to report uncaught exceptions and
+ * @param errs a means for the container to report uncaught exceptions and
* other errors
*
* @return a Control object for the running module, or null if it could not be started, in
@@ -408,7 +1324,7 @@ public Control run(
File rootDir,
Map> injections,
String customInjector,
- ErrorListener errs) {
+ @NotNull ErrorListener errs) {
if (injections != null && !injections.isEmpty() || customInjector != null) {
throw new UnsupportedOperationException("Custom injectors are not implemented yet");
}
@@ -434,34 +1350,64 @@ public Control run(
* as an empty line. Used only with a console
* @param rootDir (optional) the root directory for the application's file system, as
* described for {@link #run(ModuleStructure, PrintWriter, File, Map, ErrorListener)}
- * @param errs (optional) a means for the container to report uncaught exceptions and
- * other errors
+ * @param errs a means for the container to report uncaught exceptions and other errors
*
* @return a Control object for the running module, or null if it could not be started, in
* which case the reason is reported to "errs"
*/
public Control runMethod(
- ModuleRepository input,
- String moduleName,
- Version version,
- EntryPoint entry,
- PrintWriter console,
- ConsoleInput consoleIn,
- File rootDir,
- ErrorListener errs) {
- Objects.requireNonNull(entry, "entry");
- Objects.requireNonNull(consoleIn, "consoleIn");
+ ModuleRepository input,
+ String moduleName,
+ Version version,
+ EntryPoint entry,
+ PrintWriter console,
+ ConsoleInput consoleIn,
+ File rootDir,
+ @NotNull ErrorListener errs) {
+ return runMethod(input, moduleName, version, entry, console, consoleIn, rootDir, null, errs);
+ }
+
+ /**
+ * Create a runtime container and invoke the specified entry method of a module, as
+ * {@link #runMethod(ModuleRepository, String, Version, EntryPoint, PrintWriter, ConsoleInput, File, ErrorListener)}
+ * does, under a debugger.
+ *
+ * The debugger, such as a {@link SessionDebugger}, is set on the runtime before the module
+ * starts, and replaced by {@link Debugger#NONE} when the run ends or is closed, which releases
+ * a program it holds stopped. Every run of this EmbeddingSupport shares that runtime, so a run
+ * with a debugger is meant to be its only run. Debugging needs the interpreter.
+ *
+ * @param debugger (optional) the debugger for this run, or null to run without one
+ *
+ * @return a Control object for the running module, or null if it could not be started, in
+ * which case the reason is reported to "errs"
+ *
+ * @throws UnsupportedOperationException if a debugger is given and the JIT is used
+ */
+ public Control runMethod(
+ ModuleRepository input,
+ String moduleName,
+ Version version,
+ EntryPoint entry,
+ PrintWriter console,
+ ConsoleInput consoleIn,
+ File rootDir,
+ Debugger debugger,
+ @NotNull ErrorListener errs) {
+ if (debugger != null && useJit()) {
+ throw new UnsupportedOperationException("Debugging requires the interpreter");
+ }
+ requireNonNull(entry, "entry");
+ requireNonNull(consoleIn, "consoleIn");
verifyConfigured();
+ requireNonNull(errs, "errs");
- ModuleRepository repository = new LinkedRepository(input, cfgRepo);
- ModuleStructure module = version == null
+ var repository = new LinkedRepository(input, cfgRepo);
+ var module = version == null
? repository.loadModule(moduleName)
: repository.loadModule(moduleName, version, true);
if (module == null) {
- if (errs != null) {
- errs.log(ERROR, version == null ? ERR_NO_APP_MODULE : ERR_NO_APP_MODULE_VER,
- new Object[] {moduleName, version}, null);
- }
+ errs.error(version == null ? ERR_NO_APP_MODULE : ERR_NO_APP_MODULE_VER, NOWHERE, moduleName, version);
return null;
}
@@ -470,20 +1416,17 @@ public Control runMethod(
}
try {
- Connector connector = ensureConnector();
+ var connector = ensureConnector();
return useJit()
? JitControl.create(connector, module, repository, console, rootDir, errs)
: InterpreterControl.create(connector, module, repository, entry, console,
- consoleIn, rootDir, errs);
+ consoleIn, rootDir, debugger, errs);
} catch (RuntimeException | AssertionError e) {
// an AssertionError is an Error, so the RuntimeException guard alone let a tripped
// assertion in the structure code past this report and out to the host. Errors are
// not caught wholesale: a VirtualMachineError says the JVM is in trouble, not that
// this module failed to start, and handling one is not something to rely on
- if (errs != null) {
- errs.log(ERROR, ERR_CREATE_APP_CONTAINER,
- new Object[] {e, "Unable to start " + moduleName}, module);
- }
+ errs.error(ERR_CREATE_APP_CONTAINER, at(module), e, "Unable to start " + moduleName);
return null;
}
}
@@ -590,4 +1533,8 @@ static boolean passesArguments(MethodStructure method, EntryPoint entry) {
* "%1" - why the module's entry method cannot be run, as the command-line runner reports it
*/
public static final String ERR_ENTRY_METHOD = "EMB-7";
+ /**
+ * The source does not contain a module declaration.
+ */
+ public static final String ERR_MODULE_SOURCE = "EMB-8";
}
diff --git a/javatools/src/main/java/org/xvm/api/InterpreterConnector.java b/javatools/src/main/java/org/xvm/api/InterpreterConnector.java
index a10281da91c..c6b98fcc1bc 100644
--- a/javatools/src/main/java/org/xvm/api/InterpreterConnector.java
+++ b/javatools/src/main/java/org/xvm/api/InterpreterConnector.java
@@ -18,8 +18,11 @@
import org.xvm.runtime.NativeContainer;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.Runtime;
+import org.xvm.runtime.ServiceContext;
import org.xvm.runtime.Utils;
+import org.xvm.runtime.template._native.temporal.xNanosTimer;
+
import org.xvm.runtime.template.text.xString;
/**
@@ -71,6 +74,20 @@ public void setDebugger(Debugger debugger) {
f_runtime.setDebugger(debugger);
}
+ /**
+ * Replace the debugger if it is the expected one, as a run does with its own when it ends.
+ *
+ * @param expected the debugger to replace
+ * @param replacement the debugger to use instead
+ */
+ public void replaceDebugger(Debugger expected, Debugger replacement) {
+ synchronized (f_runtime) {
+ if (f_runtime.getDebugger() == expected) {
+ f_runtime.setDebugger(replacement);
+ }
+ }
+ }
+
/**
* @return true if no service of this connector's runtime is running or about to
*/
@@ -164,6 +181,18 @@ public int join()
return nResult;
}
+ @Override
+ public void shutdown() {
+ // the native container's timers keep their pending alarms on the JVM-wide Java timer, and
+ // with them this runtime, until they are due
+ for (ServiceContext service : f_containerNative.getServices()) {
+ if (service.getService() instanceof xNanosTimer.TimerHandle timer) {
+ timer.cancelAlarms();
+ }
+ }
+ f_runtime.shutdownXVM();
+ }
+
// ----- data fields ---------------------------------------------------------------------------
/**
diff --git a/javatools/src/main/java/org/xvm/api/InterpreterControl.java b/javatools/src/main/java/org/xvm/api/InterpreterControl.java
index bb0f412475f..3bd32d5b7a6 100644
--- a/javatools/src/main/java/org/xvm/api/InterpreterControl.java
+++ b/javatools/src/main/java/org/xvm/api/InterpreterControl.java
@@ -38,13 +38,12 @@
import org.xvm.runtime.template._native.mgmt.xCoreRepository;
import org.xvm.runtime.template._native.reflect.xRTModuleTemplate;
+import static org.xvm.asm.ErrorListener.at;
+
import static org.xvm.api.EmbeddingSupport.ERR_ENTRY_METHOD;
import static org.xvm.api.EmbeddingSupport.ERR_UNHANDLED_EXCEPTION;
import static org.xvm.api.EmbeddingSupport.WARN_ARGUMENTS_IGNORED;
-import static org.xvm.util.Severity.ERROR;
-import static org.xvm.util.Severity.WARNING;
-
/**
* Interpreter-backed management and monitoring for one runner task.
*/
@@ -67,12 +66,14 @@ static Connector createConnector(ModuleRepository repository) {
* @param entry the entry method to invoke and its arguments
* @param consoleIn the console's input, or {@link EmbeddingSupport.ConsoleInput#NONE};
* used only with a console
+ * @param debugger the run's debugger, or null; it is set before the task starts and
+ * replaced by {@link Debugger#NONE} when the run ends or is closed
*/
static EmbeddingSupport.Control create(Connector connector, ModuleStructure module,
ModuleRepository repository,
EmbeddingSupport.EntryPoint entry, PrintWriter console,
EmbeddingSupport.ConsoleInput consoleIn, File rootDir,
- ErrorListener errs) {
+ Debugger debugger, ErrorListener errs) {
if (!(connector instanceof InterpreterConnector interpreter)) {
throw new IllegalArgumentException("An InterpreterConnector is required");
}
@@ -89,14 +90,12 @@ static EmbeddingSupport.Control create(Connector connector, ModuleStructure modu
} catch (IllegalArgumentException e) {
// the method is why the run does not start, as the command-line runner reports it;
// its container was never the problem
- if (errs != null) {
- errs.log(ERROR, ERR_ENTRY_METHOD, new Object[] {e.getMessage()}, module);
- }
+ errs.error(ERR_ENTRY_METHOD, at(module), e.getMessage());
return null;
}
- if (errs != null && !entry.args().isEmpty() && method.getParamCount() == 0) {
+ if (!entry.args().isEmpty() && method.getParamCount() == 0) {
// the arguments are dropped, with the command-line runner's warning
- errs.log(WARNING, WARN_ARGUMENTS_IGNORED, new Object[] {entry.name()}, module);
+ errs.warn(WARN_ARGUMENTS_IGNORED, at(module), entry.name());
}
if (console != null) {
@@ -124,7 +123,13 @@ static EmbeddingSupport.Control create(Connector connector, ModuleStructure modu
CompletableFuture completion = new CompletableFuture<>();
InterpreterControl control = new InterpreterControl(interpreter, module, rootDir,
- errs, taskId, consoleId, started, completion);
+ errs, taskId, consoleId, debugger, started, completion);
+ if (debugger != null) {
+ // the runner's own code is not the program; breakpoints are in place before the
+ // program's first op
+ debugger.setHost(main);
+ interpreter.setDebugger(debugger);
+ }
postRequest(interpreter, "startTask", hTaskId).whenComplete((r, e) -> {
if (e == null) {
@@ -176,31 +181,42 @@ private static CompletableFuture postRequest(
private InterpreterControl(InterpreterConnector connector, ModuleStructure module,
File rootDir, ErrorListener errs, long taskId, Long consoleId,
- Instant started, CompletableFuture completion) {
+ Debugger debugger, Instant started,
+ CompletableFuture completion) {
this.connector = connector;
this.module = module;
this.rootDir = rootDir;
this.errs = errs;
this.taskId = taskId;
this.consoleId = consoleId;
+ this.debugger = debugger;
this.started = started;
this.completion = completion;
}
+ /**
+ * Replace this run's debugger, if it still is the runtime's, by {@link Debugger#NONE}, which
+ * releases a program it holds stopped.
+ */
+ private void releaseDebugger() {
+ if (debugger != null) {
+ connector.replaceDebugger(debugger, Debugger.NONE);
+ }
+ }
+
private void finish(long result, String failure) {
try {
if (failure.isEmpty()) {
this.result = result;
} else {
this.result = null;
- if (errs != null) {
- errs.log(ERROR, ERR_UNHANDLED_EXCEPTION, new Object[] {failure}, module);
- }
+ errs.error(ERR_UNHANDLED_EXCEPTION, at(module), failure);
}
} finally {
this.stopped = Instant.now();
this.running = false;
unregisterConsole(connector, consoleId);
+ releaseDebugger();
}
}
@@ -245,6 +261,11 @@ public void close() {
}
try {
+ if (debugger != null) {
+ // a stopped program ends where it is, without running on until the runner kills it
+ debugger.terminate();
+ }
+ releaseDebugger();
if (running) {
try {
postRequest(connector, "killTask", xInt64.makeHandle(taskId)).join();
@@ -283,6 +304,7 @@ private String taskStatus() {
private final ErrorListener errs;
private final long taskId;
private final Long consoleId;
+ private final Debugger debugger;
private final Instant started;
private final CompletableFuture completion;
private final AtomicBoolean closed = new AtomicBoolean();
diff --git a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java
index a70c520b812..1ecd5624321 100644
--- a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java
+++ b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java
@@ -6,6 +6,8 @@
import java.util.List;
import java.util.Objects;
+import org.jetbrains.annotations.NotNull;
+
import org.xvm.asm.ErrorList;
import org.xvm.asm.ErrorListener;
import org.xvm.asm.FileStructure;
@@ -31,6 +33,9 @@
import static org.xvm.api.EmbeddingSupport.ERR_INTERNAL;
+import static org.xvm.asm.ErrorListener.NOWHERE;
+import static org.xvm.asm.ErrorListener.at;
+
import static org.xvm.util.Handy.readFileChars;
import static org.xvm.util.Severity.ERROR;
@@ -40,7 +45,9 @@
* Unlike {@link EmbeddingSupport}, a ModuleCompiler is an ordinary object that needs no
* configuration: compiling does not start the runtime, so it does not depend on the runtime's
* JVM-wide state (see {@link EmbeddingSupport}), and any number of ModuleCompiler instances, each
- * with its own core repository, can exist in one JVM. Each call to compile is independent.
+ * with its own core repository, can exist in one JVM. Each call to compile has its own attempt.
+ * Hosts must serialize calls that share repositories; separate instances do not isolate mutable
+ * compiler state held by a repository.
*
*
The compile methods report the same errors as the command-line compiler: every error found in
* a compiler stage, with the compilation stopping at the end of the first stage that found any.
@@ -74,11 +81,12 @@ public ModuleCompiler(ModuleRepository coreRepo) {
*
* @param source the source code for an entire module to compile
* @param input (optional) the module repository to read any required modules from
- * @param errs (optional) the ErrorListener to log any compiler messages to
+ * @param errs the ErrorListener to log any compiler messages to
*
* @return the resulting ModuleStructure, or null if a compiler error occurred
*/
- public ModuleStructure compile(String source, ModuleRepository input, ErrorListener errs) {
+ public ModuleStructure compile(String source, ModuleRepository input, @NotNull ErrorListener errs) {
+ Objects.requireNonNull(errs, "errs");
try {
EmbeddingCompiler compiler = new EmbeddingCompiler(source, input, coreRepo, errs);
return compiler.process() == 0
@@ -88,18 +96,15 @@ public ModuleStructure compile(String source, ModuleRepository input, ErrorListe
// the compiler stops at the end of a stage that logged errors; those errors are already
// in "errs", so this is an ordinary failed compile, as it is for the command-line
// compiler. A tool-level failure is logged only to the silent console, so report it
- if (errs != null && !errs.hasSeriousErrors()) {
- errs.log(ERROR, ERR_INTERNAL, new Object[] {e, "Compilation failed"}, null);
+ if (!errs.hasSeriousErrors()) {
+ errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed");
}
return null;
} catch (RuntimeException | AssertionError e) {
// as in EmbeddingSupport.run(): the compiler runs over caller-supplied source, so a
// failure in it is reported here rather than thrown at the caller, who was promised a
// null instead
- if (errs != null) {
- errs.log(ERROR, ERR_INTERNAL,
- new Object[] {e, "Compilation failed"}, null);
- }
+ errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed");
return null;
}
}
@@ -110,25 +115,35 @@ public ModuleStructure compile(String source, ModuleRepository input, ErrorListe
* @param file the module source file
* @param input (optional) the module repository to read any required modules from
* @param output (optional) the module repository to write any compiled modules to
- * @param errs (optional) the ErrorListener to log any compiler messages to
+ * @param errs the ErrorListener to log any compiler messages to
*
* @return true if the compilation succeeded and the result was placed into the output
*/
public boolean compile(File file, ModuleRepository input, ModuleRepository output,
- ErrorListener errs) {
+ @NotNull ErrorListener errs) {
+ Objects.requireNonNull(errs, "errs");
ModuleStructure module;
try {
module = compile(new String(readFileChars(file)), input, errs);
} catch (IOException e) {
- if (errs != null) {
- errs.log(ERROR, ERR_INTERNAL,
- new Object[] {e, "Unable to read module " + file}, null);
- }
+ errs.error(ERR_INTERNAL, NOWHERE, e, "Unable to read module " + file);
return false;
}
+ return storeCompiled(module, output, errs);
+ }
+ /**
+ * Finish a file compilation by placing its module into the optional output repository.
+ *
+ * @param module the compiled module, or null if the compilation failed or was abandoned
+ * @param output (optional) the module repository to write the compiled module to
+ * @param errs the ErrorListener that received the compilation's messages
+ *
+ * @return true if there is a module and it was stored, or no output was requested
+ */
+ static boolean storeCompiled(ModuleStructure module, ModuleRepository output, ErrorListener errs) {
if (module == null) {
- assert errs == null || errs.hasSeriousErrors();
+ assert errs.hasSeriousErrors() || errs.isAbortDesired();
return false;
}
@@ -136,10 +151,7 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu
try {
output.storeModule(module);
} catch (IOException e) {
- if (errs != null) {
- errs.log(ERROR, ERR_INTERNAL,
- new Object[] {e, "Unable to store module " + module.getName()}, module);
- }
+ errs.error(ERR_INTERNAL, at(module), e, "Unable to store module " + module.getName());
return false;
}
}
diff --git a/javatools/src/main/java/org/xvm/asm/Annotation.java b/javatools/src/main/java/org/xvm/asm/Annotation.java
index f32d829e8ea..b14f8dc0b96 100644
--- a/javatools/src/main/java/org/xvm/asm/Annotation.java
+++ b/javatools/src/main/java/org/xvm/asm/Annotation.java
@@ -321,8 +321,8 @@ public boolean validate(ErrorListener errs) {
// it must be an annotation type
if (getAnnotationType().getExplicitClassFormat() != Component.Format.ANNOTATION) {
- fHalt |= log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION,
- getAnnotationClass().getValueString());
+ log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION, getAnnotationClass().getValueString());
+ fHalt |= errs.isAbortDesired();
}
return fHalt;
diff --git a/javatools/src/main/java/org/xvm/asm/ClassStructure.java b/javatools/src/main/java/org/xvm/asm/ClassStructure.java
index 82eb7fa18f0..d8114bcb444 100644
--- a/javatools/src/main/java/org/xvm/asm/ClassStructure.java
+++ b/javatools/src/main/java/org/xvm/asm/ClassStructure.java
@@ -37,6 +37,9 @@
import org.xvm.util.ListMap;
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
import static org.xvm.util.Handy.readIndex;
import static org.xvm.util.Handy.readMagnitude;
import static org.xvm.util.Handy.writeMagnitude;
@@ -987,7 +990,7 @@ public ResolutionResult resolveName(String sName, Access access, ResolutionColle
if (result == ResolutionResult.UNKNOWN && getFormat() == Format.SERVICE) {
// look into the Service interface itself
ClassStructure clzSvc = (ClassStructure) getConstantPool().clzService().getComponent();
- SimpleCollector collectorSvc = new SimpleCollector(ErrorListener.BLACKHOLE);
+ SimpleCollector collectorSvc = new SimpleCollector(silent(PROBE));
ResolutionResult resultSvc = clzSvc.resolveName(sName, Access.PROTECTED, collectorSvc);
if (resultSvc == ResolutionResult.RESOLVED) {
// only allow child classes; properties and methods are resolved by the TypeInfo
diff --git a/javatools/src/main/java/org/xvm/asm/Component.java b/javatools/src/main/java/org/xvm/asm/Component.java
index 53f60fd41bc..92cec3ad290 100644
--- a/javatools/src/main/java/org/xvm/asm/Component.java
+++ b/javatools/src/main/java/org/xvm/asm/Component.java
@@ -24,6 +24,8 @@
import java.util.stream.Stream;
+import org.jetbrains.annotations.NotNull;
+
import org.xvm.asm.constants.ClassConstant;
import org.xvm.asm.constants.ConditionalConstant;
import org.xvm.asm.constants.IdentityConstant;
@@ -46,7 +48,10 @@
import org.xvm.util.Handy;
import org.xvm.util.Hash;
import org.xvm.util.ListMap;
-import org.xvm.util.Severity;
+
+import static java.util.Objects.requireNonNull;
+
+import static org.xvm.asm.ErrorListener.at;
import static org.xvm.util.Handy.readIndex;
import static org.xvm.util.Handy.readMagnitude;
@@ -2012,8 +2017,8 @@ protected ResolutionResult resolveContributedName(
}
if (m_FVisited != null && m_FVisited.booleanValue() == fAllowInto) {
// recursive contribution
- collector.getErrorListener().log(Severity.FATAL, Constants.VE_CYCLICAL_CONTRIBUTION,
- new Object[] {getName(), contrib.getComposition().toString().toLowerCase()}, this);
+ collector.getErrorListener().fatal(Constants.VE_CYCLICAL_CONTRIBUTION, at(this),
+ getName(), contrib.getComposition().toString().toLowerCase());
return ResolutionResult.ERROR;
}
@@ -3528,8 +3533,8 @@ public String toString() {
*/
public static class SimpleCollector
implements ResolutionCollector {
- public SimpleCollector(ErrorListener errs) {
- m_errs = errs;
+ public SimpleCollector(@NotNull ErrorListener errs) {
+ m_errs = requireNonNull(errs, "errs");
}
@Override
diff --git a/javatools/src/main/java/org/xvm/asm/ComponentResolver.java b/javatools/src/main/java/org/xvm/asm/ComponentResolver.java
index 930c81bd884..3f69ba5d8a1 100644
--- a/javatools/src/main/java/org/xvm/asm/ComponentResolver.java
+++ b/javatools/src/main/java/org/xvm/asm/ComponentResolver.java
@@ -63,9 +63,12 @@ default AstNode getNode() {
/**
* Provide an ErrorListener to report resolution issues to.
+ *
+ * Deliberately not defaulted. A default of "discard" would mean a collector that had not
+ * thought about diagnostics and one that had decided against them looked identical at the
+ * point of resolution, which is the distinction the listener exists to make. A collector
+ * that genuinely wants none says so by naming a silent listener.
*/
- default ErrorListener getErrorListener() {
- return ErrorListener.BLACKHOLE;
- }
+ ErrorListener getErrorListener();
}
}
diff --git a/javatools/src/main/java/org/xvm/asm/ConstantPool.java b/javatools/src/main/java/org/xvm/asm/ConstantPool.java
index bc125aba077..8f18fa229a6 100755
--- a/javatools/src/main/java/org/xvm/asm/ConstantPool.java
+++ b/javatools/src/main/java/org/xvm/asm/ConstantPool.java
@@ -1277,6 +1277,15 @@ public static String getImplicitImportName(String sPath) {
return IMPLICITS.byPath().get(sPath);
}
+ /**
+ * The language's implicit import names, without resolving or loading their components.
+ *
+ * @return the names available through implicit imports
+ */
+ public static Set getImplicitImportNames() {
+ return IMPLICITS.byName().keySet();
+ }
+
/**
* Given the specified typedef name and the context (module, package, class, method) within
* which it exists, obtain a TypedefConstant that represents it.
diff --git a/javatools/src/main/java/org/xvm/asm/ErrorList.java b/javatools/src/main/java/org/xvm/asm/ErrorList.java
index 7c6133e7a71..1c69f7e8444 100644
--- a/javatools/src/main/java/org/xvm/asm/ErrorList.java
+++ b/javatools/src/main/java/org/xvm/asm/ErrorList.java
@@ -9,26 +9,79 @@
import org.xvm.util.Severity;
/**
- * Represents a list of errors collected from a process such as compilation, assembly, or the
- * verifier, with an option to abort the process should a maximum number of errors be exceeded.
+ * An operation's diagnostic buffer with deduplication, severity state, and an optional count budget.
+ *
+ * The first report for each {@link ErrorInfo#genUID()} is retained in insertion order. Each
+ * retained ERROR or FATAL counts toward the budget; warnings and informational reports do not.
+ * {@link #isAbortDesired()} becomes true when a positive budget is reached, or after any FATAL.
+ * Logging still records subsequent reports until the caller honors that stop request.
+ *
+ *
Use this for compilation results that must retain diagnostics, and
+ * {@link ErrorListener#collecting(java.util.function.Consumer)} when a host callback should receive
+ * every report without deduplication. Instances are mutable and are not thread-safe.
*/
public class ErrorList
implements ErrorListener {
// ----- constructors --------------------------------------------------------------------------
+ /**
+ * Construct a buffer that requests an abort on its {@link ErrorListener#DEFAULT_MAX_ERRORS}-th serious report.
+ *
+ *
Use {@link #ErrorList(int)} with {@link #FIRST_ERROR}, {@link #UNLIMITED}, or a caller-specific
+ * positive budget when that policy is required.
+ */
+ public ErrorList() {
+ this(ErrorListener.DEFAULT_MAX_ERRORS);
+ }
+
+ /**
+ * Construct a buffer with the requested serious-error budget.
+ *
+ * @param cMaxErrors request an abort when this many retained ERROR/FATAL reports have arrived;
+ * non-positive values disable the count limit, conventionally {@link #UNLIMITED}
+ */
public ErrorList(int cMaxErrors) {
f_cMaxErrors = cMaxErrors;
}
+ /**
+ * Disable the count budget. A retained FATAL still requests an abort.
+ */
+ public static final int UNLIMITED = 0;
+
+ /**
+ * Request an abort after the first retained ERROR or FATAL.
+ */
+ public static final int FIRST_ERROR = 1;
+
// ----- ErrorListener methods -----------------------------------------------------------------
+ /**
+ * Create a diagnostic buffer with this list's count budget and parent abort policy.
+ *
+ *
The child counts its own retained reports; it does not subtract the parent's count from its
+ * budget. It requests an abort when either its own policy or the parent's policy says to stop.
+ * Reports reach this list only when the child is merged.
+ *
+ * @param node the optional source context for structure-site diagnostics
+ *
+ * @return a new child buffer
+ */
@Override
public ErrorListener branch(AstNode node) {
return new BranchedErrorListener(this, f_cMaxErrors, node);
}
+ /**
+ * Record the first report for this diagnostic's UID and update severity and budget state.
+ *
+ *
Duplicate UIDs do not add entries or spend the count budget. This method does not stop the
+ * caller or refuse reports after the budget is reached; query {@link #isAbortDesired()} explicitly.
+ *
+ * @param err the diagnostic to record
+ */
@Override
- public boolean log(ErrorInfo err) {
+ public void log(ErrorInfo err) {
String uid = err.genUID();
if (f_setUID.add(uid)) {
// remember the highest severity encountered
@@ -46,10 +99,13 @@ public boolean log(ErrorInfo err) {
++m_cErrors;
}
}
-
- return isAbortDesired();
}
+ /**
+ * Test whether a retained FATAL or the configured serious-error count requests an abort.
+ *
+ * @return true for FATAL, or when a positive budget has been reached
+ */
@Override
public boolean isAbortDesired() {
return m_severity == Severity.FATAL || f_cMaxErrors > 0 &&
@@ -117,7 +173,10 @@ public List getErrors() {
}
/**
- * Clear the list of errors, resetting the error collection state.
+ * Clear the retained reports, serious-error count, and worst severity.
+ *
+ * Previously seen diagnostic UIDs remain recorded, so logging the same diagnostic after
+ * clearing still suppresses it. Use a new ErrorList for a new independent operation.
*/
public void clear() {
f_list.clear();
@@ -126,9 +185,12 @@ public void clear() {
}
/**
- * Log the errors from this ErrorList into another ErrorListener.
+ * Replay all retained diagnostics to another listener in insertion order.
*
- * @param errs the ErrorListener to log all the errors from this ErrorList to
+ *
This neither clears this list nor queries the destination's abort policy. Repeated calls
+ * replay the same reports; deduplication, if desired, is the destination's responsibility.
+ *
+ * @param errs the destination for the retained reports
*/
public void logTo(ErrorListener errs) {
for (ErrorInfo err : getErrors()) {
@@ -150,10 +212,21 @@ public String toString() {
// ----- inner class: BranchedErrorListener ----------------------------------------------------
/**
- * The ErrorListener that can be used to capture errors that may or may not be reported.
+ * A temporary diagnostic buffer whose accepted reports can be merged into a parent listener.
+ *
+ *
It retains its own deduplication and count state while observing the parent's abort request.
+ * Discarding the branch leaves the parent's diagnostics untouched. Merging forwards reports and
+ * returns the parent without clearing the branch, so callers should merge an accepted branch once.
*/
public static class BranchedErrorListener
extends ErrorList {
+ /**
+ * Construct a speculative buffer associated with a parent and optional source node.
+ *
+ * @param listener the parent destination and additional abort policy
+ * @param cMaxErrors this branch's serious-error budget, or {@link ErrorList#UNLIMITED}
+ * @param node the optional source context for structure-site reports
+ */
public BranchedErrorListener(ErrorListener listener, int cMaxErrors, AstNode node) {
super(cMaxErrors);
@@ -168,11 +241,14 @@ public ErrorListener branch(AstNode node) {
}
@Override
- public boolean log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) {
- return f_node == null
- ? super.log(severity, sCode, aoParam, xs)
- : log(severity, sCode, aoParam,
- f_node.getSource(), f_node.getStartPosition(), f_node.getEndPosition());
+ public void log(Severity severity, String sCode, Site site, Object... aoParam) {
+ // a branch taken for a particular node re-anchors at that node: a diagnostic raised
+ // against a structure carries no source location of its own, and the node is the
+ // location the brancher knew about
+ if (f_node != null && site instanceof Site.At) {
+ site = ErrorListener.in(f_node.getSource(), f_node.getStartPosition(), f_node.getEndPosition());
+ }
+ super.log(severity, sCode, site, aoParam);
}
@Override
diff --git a/javatools/src/main/java/org/xvm/asm/ErrorListener.java b/javatools/src/main/java/org/xvm/asm/ErrorListener.java
index a13fd78e50e..00e3944d75a 100644
--- a/javatools/src/main/java/org/xvm/asm/ErrorListener.java
+++ b/javatools/src/main/java/org/xvm/asm/ErrorListener.java
@@ -6,6 +6,15 @@
import java.util.Arrays;
import java.util.ResourceBundle;
+import java.util.Set;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+import java.util.function.BooleanSupplier;
+import java.util.function.Consumer;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import org.xvm.compiler.Source;
@@ -13,25 +22,67 @@
import org.xvm.util.Severity;
+import static java.util.Objects.requireNonNull;
+
import static org.xvm.util.Handy.quotedString;
/**
- * A listener for errors being reported about source code, compilation, assembly, or verification of
- * XVM structures.
+ * Receives diagnostics from compilation, assembly, and verification, and exposes the state that
+ * callers use to decide whether work may continue.
+ *
+ *
Reporting and continuation are separate operations. {@link #log(ErrorInfo)} delivers a
+ * diagnostic; {@link #isAbortDesired()} answers the current stop policy. A compiler caller that
+ * previously consumed the boolean result of {@code log} must query the policy explicitly:
+ *
{@code
+ * errors.error(code, ErrorListener.in(source, start, end), arguments);
+ * if (errors.isAbortDesired()) {
+ * return;
+ * }
+ * }
+ * This gives forwarding listeners one place to preserve a parent's error budget or abort
+ * request. It also lets a host query a stop policy independently of emitting another diagnostic.
+ * Changing {@code log} to {@code void} is an intentional source and binary API change; existing
+ * Java implementations and callers must migrate and recompile.
+ *
+ *
Use {@link #collecting(Consumer)} to deliver diagnostics to a host callback while maintaining
+ * severity and code state, {@link ErrorList} to retain and deduplicate reports with an error
+ * budget, and {@link #tee(ErrorListener, ErrorListener)} to forward to two destinations. A bare
+ * lambda implements only reporting and inherits state queries that always return {@code false}.
+ *
+ *
Use {@link #branch(AstNode)} when speculative diagnostics may need to be kept, or
+ * {@link #silence(Silence)} when they must be discarded while preserving this listener's abort
+ * policy. {@link #silent(Silence)} is a standalone discard sink with no parent policy. The named
+ * reasons replace the ambiguous intent of an unnamed discard sink such as {@code BLACKHOLE};
+ * they do not select different compiler modes.
+ *
+ *
Keep an operation's original listener reference stable. Pass a derived silence or branch to
+ * temporary work instead of replacing the owner with a discard sink. This preserves the destination
+ * and its stop policy for later work; it does not make the listener's accumulated state immutable.
+ *
+ *
Listeners belong to an operation. Hosts must serialize callbacks and compiler access;
+ * these factories do not make compilation or listener state thread-safe. Reporting callbacks
+ * may throw, and the forwarding helpers do not catch or translate those exceptions.
+ *
+ *
Factory and constructor parameters annotated {@link NotNull} are required collaborators,
+ * not optional reporting destinations. Null arguments are rejected when the listener is created.
+ * {@link Nullable} marks intentional absence, such as a standalone silence's parent.
*/
@FunctionalInterface
public interface ErrorListener {
// ----- API -----------------------------------------------------------------------------------
/**
- * Handles the logging of an error that originates in Ecstasy source code.
+ * Deliver one diagnostic to this listener.
*
- * @param err the error info
+ *
Implementations decide whether to retain, forward, deduplicate, or discard the report.
+ * Reporting supplies no continuation result. Call {@link #isAbortDesired()} at the caller's
+ * stop checkpoints, and use {@link #hasSeriousErrors()} or {@link #hasError(String)} when deciding
+ * whether a compiler result is usable. A custom reporting callback must maintain those queries
+ * itself or be wrapped with {@link #collecting(Consumer)}.
*
- * @return true to attempt to abort the process that reported the error, or
- * false to attempt to continue the process
+ * @param err the diagnostic to report
*/
- boolean log(ErrorInfo err);
+ void log(ErrorInfo err);
/**
* Handles the logging of an error that originates in Ecstasy source code.
@@ -45,12 +96,11 @@ public interface ErrorListener {
* @param lPosStart the position in the source where the error was detected
* @param lPosEnd the position in the source at which the error concluded
*
- * @return true to attempt to abort the process that reported the error, or
- * false to attempt to continue the process
+ * @deprecated use {@link #log(Severity, String, Site, Object...)} with {@link #in}
*/
- default boolean log(Severity severity, String sCode, Object[] aoParam,
- Source source, long lPosStart, long lPosEnd) {
- return log(new ErrorInfo(severity, sCode, aoParam, source, lPosStart, lPosEnd));
+ @Deprecated
+ default void log(Severity severity, String sCode, Object[] aoParam, Source source, long lPosStart, long lPosEnd) {
+ log(new ErrorInfo(severity, sCode, aoParam, source, lPosStart, lPosEnd));
}
/**
@@ -64,76 +114,580 @@ default boolean log(Severity severity, String sCode, Object[] aoParam,
* @param xs the XvmStructure that the error is related to; may
* be null
*
- * @return true to attempt to abort the process that reported the error, or
- * false to attempt continue the process
+ * @deprecated use {@link #log(Severity, String, Site, Object...)} with {@link #at}
+ */
+ @Deprecated
+ default void log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) {
+ log(severity, sCode, at(xs), aoParam);
+ }
+
+ // ----- reporting -----------------------------------------------------------------------------
+
+ /**
+ * Construct and report a diagnostic with an explicit location and trailing message arguments.
+ *
+ *
Use {@link #in(Source, long, long)} for source text, {@link #at(XvmStructure)} for an
+ * assembled structure, or {@link #NOWHERE} when no location is available. Packaging the location
+ * as a {@link Site} leaves the final parameter available for varargs, so callers need not build
+ * an {@code Object[]} between the message code and location arguments.
+ *
+ *
The default implementation creates an {@link ErrorInfo} and delegates to {@link #log(ErrorInfo)}.
+ * It does not test the listener's abort policy or stop the caller.
+ *
+ * @param severity the diagnostic severity
+ * @param sCode the message code from the compiler's error resources
+ * @param site the diagnostic location; must not be null
+ * @param aoParam the message-format arguments, or an empty array when none are needed
+ *
+ * @throws NullPointerException if {@code site} is null
+ */
+ default void log(Severity severity, String sCode, Site site, Object... aoParam) {
+ switch (site) {
+ case Site.In in -> log(new ErrorInfo(severity, sCode, aoParam, in.source(), in.lPosStart(), in.lPosEnd()));
+ case Site.At at -> log(new ErrorInfo(severity, sCode, aoParam, at.xs()));
+ case Site.None _ -> log(new ErrorInfo(severity, sCode, aoParam, null, 0, 0));
+ }
+ }
+
+ /**
+ * Report an {@link Severity#ERROR} diagnostic for an operation that failed validation.
+ *
+ *
The listener's error budget determines whether this also requests an abort. Reporting an
+ * error does not itself stop the caller.
+ *
+ * @param sCode the message code
+ * @param site the diagnostic location; must not be null
+ * @param aoParam the message-format arguments
+ *
+ * @see #log(Severity, String, Site, Object...)
+ */
+ default void error(String sCode, Site site, Object... aoParam) {
+ log(Severity.ERROR, sCode, site, aoParam);
+ }
+
+ /**
+ * Report a {@link Severity#WARNING} diagnostic without classifying it as a serious error.
+ *
+ * @param sCode the message code
+ * @param site the diagnostic location; must not be null
+ * @param aoParam the message-format arguments
+ *
+ * @see #log(Severity, String, Site, Object...)
+ */
+ default void warn(String sCode, Site site, Object... aoParam) {
+ log(Severity.WARNING, sCode, site, aoParam);
+ }
+
+ /**
+ * Report an {@link Severity#INFO} diagnostic, for example an informational compiler message.
+ *
+ * @param sCode the message code
+ * @param site the diagnostic location; must not be null
+ * @param aoParam the message-format arguments
+ *
+ * @see #log(Severity, String, Site, Object...)
+ */
+ default void info(String sCode, Site site, Object... aoParam) {
+ log(Severity.INFO, sCode, site, aoParam);
+ }
+
+ /**
+ * Report a {@link Severity#FATAL} diagnostic for work that cannot continue successfully.
+ *
+ *
{@link ErrorList} and {@link #collecting(Consumer)} request an abort after a fatal report,
+ * regardless of their count policy. This method does not itself throw or transfer control;
+ * the caller must return, throw, or honor its next stop checkpoint. Code that cannot safely
+ * continue must stop explicitly even when the chosen listener discards the report.
+ *
+ * @param sCode the message code
+ * @param site the diagnostic location; must not be null
+ * @param aoParam the message-format arguments
+ *
+ * @see #log(Severity, String, Site, Object...)
+ */
+ default void fatal(String sCode, Site site, Object... aoParam) {
+ log(Severity.FATAL, sCode, site, aoParam);
+ }
+
+ /**
+ * Create a stateful listener that forwards every report to a host callback.
+ *
+ *
Use this when an embedding host or editor collects diagnostics through a consumer, but the
+ * compiler still needs accurate state queries. Before invoking the consumer, the listener records
+ * the highest severity and the reported code. {@link #hasSeriousErrors()} becomes true for ERROR
+ * or FATAL, {@link #hasError(String)} recognizes codes from any severity, and
+ * {@link #isAbortDesired()} becomes true only for FATAL. There is no count budget.
+ *
+ *
Every report is forwarded, including duplicates; use {@link ErrorList} for deduplication or
+ * a count budget. If the consumer throws, its exception propagates and the recorded state remains.
+ * Create a new listener for each independent operation; there is no reset operation.
+ *
+ * @param consumer the destination for each diagnostic, invoked synchronously
+ *
+ * @return a new listener that retains severity and code state while forwarding reports
+ */
+ @NotNull
+ static ErrorListener collecting(@NotNull Consumer consumer) {
+ requireNonNull(consumer, "consumer");
+ return new ErrorListener() {
+ @Override
+ public void log(ErrorInfo err) {
+ Severity severity = err.getSeverity();
+ if (severity.isWorseThan(m_severity)) {
+ m_severity = severity;
+ }
+ f_setCodes.add(err.getCode());
+ consumer.accept(err);
+ }
+
+ @Override
+ public boolean isAbortDesired() {
+ return m_severity == Severity.FATAL;
+ }
+
+ @Override
+ public boolean hasSeriousErrors() {
+ return m_severity.isAtLeast(Severity.ERROR);
+ }
+
+ @Override
+ public boolean hasError(String sCode) {
+ return f_setCodes.contains(sCode);
+ }
+
+ @Override
+ public String toString() {
+ return "Collecting(worst=" + m_severity + ")";
+ }
+
+ private final Set f_setCodes = ConcurrentHashMap.newKeySet();
+ private volatile Severity m_severity = Severity.NONE;
+ };
+ }
+
+ /**
+ * Wrap an operation listener with an independent, cooperatively polled cancellation policy.
+ *
+ * For example, an editor can mark a completion request stale after a new edit without
+ * emitting a fake compiler error. Every abort query first polls {@code cancelled}; if it
+ * is false, the wrapped listener's abort policy is queried. Cancellation is not latched:
+ * a host that requires a permanent stop must keep its predicate true.
+ *
+ *
Reports and the other state queries delegate to the wrapped listener. Branching and
+ * merging preserve the cancellation predicate around the resulting child or parent. The
+ * wrapper does not interrupt a thread, discard reports, or stop work until a caller checks
+ * the abort policy. Predicate and listener exceptions propagate.
+ *
+ * @param errs the operation listener
+ * @param cancelled a predicate, cheap and safe to poll on the compiling thread
+ *
+ * @return a listener requesting a stop for cancellation or the wrapped listener's policy
+ */
+ @NotNull
+ static ErrorListener cancellable(@NotNull ErrorListener errs, @NotNull BooleanSupplier cancelled) {
+ requireNonNull(errs, "errs");
+ requireNonNull(cancelled, "cancelled");
+ return new ErrorListener() {
+ @Override
+ public void log(ErrorInfo err) {
+ errs.log(err);
+ }
+
+ @Override
+ public boolean isAbortDesired() {
+ return cancelled.getAsBoolean() || errs.isAbortDesired();
+ }
+
+ @Override
+ public boolean hasSeriousErrors() {
+ return errs.hasSeriousErrors();
+ }
+
+ @Override
+ public boolean hasError(String sCode) {
+ return errs.hasError(sCode);
+ }
+
+ @Override
+ public boolean isSilent() {
+ return errs.isSilent();
+ }
+
+ @Override
+ public ErrorListener branch(AstNode node) {
+ // a branch of a cancellable listener is still cancellable: work inside it is just
+ // as pointless once the answer is not wanted
+ return cancellable(errs.branch(node), cancelled);
+ }
+
+ @Override
+ public ErrorListener merge() {
+ // and it has to be mergeable, or branching it would quietly throw the branch away:
+ // the default merge() answers "this", which for a wrapper means the wrapped
+ // branch is never merged into what it branched from
+ return cancellable(errs.merge(), cancelled);
+ }
+
+ @Override
+ @Nullable
+ public Silence silenceReason() {
+ return errs.silenceReason();
+ }
+
+ @Override
+ public String toString() {
+ return "Cancellable(" + errs + ")";
+ }
+ };
+ }
+
+ /**
+ * Create a listener that forwards reports to two destinations in order.
+ *
+ *
For example, combine a host's budgeted listener with a recorder whose reports may later be
+ * replayed. Each report goes to {@code first}, then {@code second}; an abort request does not
+ * skip delivery to the second listener. If the first listener throws, the exception propagates
+ * and the second is not called.
+ *
+ *
The wrapper keeps no state of its own. Its abort, serious-error, and code queries are true
+ * when either destination answers true; it is silent only when both are silent. It uses this
+ * interface's default buffering {@link #branch(AstNode)} behavior rather than independently
+ * branching each destination.
+ *
+ * @param first the first destination
+ * @param second the second destination
+ *
+ * @return a listener combining delivery and the two destinations' current state
+ */
+ @NotNull
+ static ErrorListener tee(@NotNull ErrorListener first, @NotNull ErrorListener second) {
+ requireNonNull(first, "first");
+ requireNonNull(second, "second");
+ return new ErrorListener() {
+ @Override
+ public void log(ErrorInfo err) {
+ first.log(err);
+ second.log(err);
+ }
+
+ @Override
+ public boolean isAbortDesired() {
+ return first.isAbortDesired() || second.isAbortDesired();
+ }
+
+ @Override
+ public boolean hasSeriousErrors() {
+ return first.hasSeriousErrors() || second.hasSeriousErrors();
+ }
+
+ @Override
+ public boolean hasError(String sCode) {
+ return first.hasError(sCode) || second.hasError(sCode);
+ }
+
+ @Override
+ public boolean isSilent() {
+ return first.isSilent() && second.isSilent();
+ }
+
+ @Override
+ public String toString() {
+ return "Tee(" + first + ", " + second + ")";
+ }
+ };
+ }
+
+ /**
+ * Describe a diagnostic associated with an assembled structure.
+ *
+ *
This does not calculate a source span. A node-associated diagnostic branch can re-anchor
+ * the structure report to its node; otherwise an editor host must interpret the structure site.
+ * Prefer {@link #NOWHERE} when the caller has no location.
+ *
+ * @param xs the related structure; null is accepted for legacy unpositioned reports
+ *
+ * @return a structure site referring to {@code xs}
+ */
+ static Site at(XvmStructure xs) {
+ return new Site.At(xs);
+ }
+
+ /**
+ * Describe a diagnostic over a source span.
+ *
+ *
Positions use the encoding returned by {@link Source#getPosition()}, not byte offsets or
+ * LSP positions. This factory stores the supplied values without validating or copying the source.
+ *
+ * @param source the source being diagnosed; prefer {@link #NOWHERE} if absent
+ * @param lPosStart the encoded start position, inclusive
+ * @param lPosEnd the encoded end position, exclusive
+ *
+ * @return a site referring to the supplied source span
+ */
+ static Site in(Source source, long lPosStart, long lPosEnd) {
+ return new Site.In(source, lPosStart, lPosEnd);
+ }
+
+ /**
+ * The location of a diagnostic, independent of its severity and message arguments.
+ *
+ *
Hosts can switch exhaustively over source spans, structure references, and unpositioned
+ * reports. These values describe locations; they do not resolve a structure to source text or
+ * detach the referenced compiler objects for concurrent access.
*/
- default boolean log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) {
- return log(new ErrorInfo(severity, sCode, aoParam, xs));
+ sealed interface Site {
+ /**
+ * A source span using the encoded positions returned by {@link Source#getPosition()}.
+ *
+ * @param source the referenced source; not copied
+ * @param lPosStart the inclusive encoded start position
+ * @param lPosEnd the exclusive encoded end position
+ */
+ record In(Source source, long lPosStart, long lPosEnd) implements Site {}
+
+ /**
+ * A diagnostic associated with a structure rather than an explicit source span.
+ *
+ * @param xs the related structure; null represents a legacy unpositioned report
+ */
+ record At(XvmStructure xs) implements Site {}
+
+ /**
+ * A diagnostic with no source span or structure, such as an operation-wide setup failure.
+ */
+ record None() implements Site {}
}
/**
- * Branch this ErrorListener by creating a new one that will collect subsequent errors
- * in the same manner as this one until it is {@link #merge() merged} or discarded in the
- * (optional) context of the specified node.
+ * The shared location for a diagnostic with no source span or structure.
+ */
+ Site NOWHERE = new Site.None();
+
+ /**
+ * Create a temporary buffer for diagnostics from work whose reports may later be retained.
+ *
+ *
For speculative validation, pass the branch to the trial operation and call {@link #merge()}
+ * once if its reports should reach this listener. Otherwise discard the branch. Reports are
+ * buffered and deduplicated in an {@link ErrorList}; they are not forwarded during the trial.
+ * Use {@link #silence(Silence)} instead when the reports must never be retained.
*
- * @param node (optional) the context ast node
+ *
The default branch has an unlimited count budget, still aborts on its own FATAL, and observes
+ * this parent's abort request. {@link ErrorList#branch(AstNode)} instead copies its configured
+ * count budget. A supplied node re-anchors structure-site reports to that node's source span;
+ * explicit source-site reports retain their own location.
*
- * @return the branched-out ErrorListener
+ * @param node the optional source context for structure-site diagnostics
+ *
+ * @return a new buffering child listener
*/
default ErrorListener branch(AstNode node) {
- return new ErrorList.BranchedErrorListener(this, 0, node);
+ return new ErrorList.BranchedErrorListener(this, ErrorList.UNLIMITED, node);
}
/**
- * Merge all errors collected by this ErrorListener into the one it was branched out of.
+ * Publish a branch's retained diagnostics to its parent and return the parent.
+ *
+ *
The default implementation is a no-op returning this listener, for a sink that has no parent.
+ * A {@link ErrorList.BranchedErrorListener} forwards its retained reports without clearing them;
+ * merging it again replays them again. A parent {@link ErrorList} can deduplicate that replay,
+ * but an arbitrary callback need not. Merge each accepted branch once.
*
- * @return the ErrorListener this one was {@link #branch branched out} of
+ * @return the parent for a branch, or this listener when there is nothing to merge
*/
default ErrorListener merge() {
- throw new UnsupportedOperationException("nothing to merge");
+ return this;
+ }
+
+ /**
+ * The caller's reason for deliberately discarding diagnostics.
+ *
+ *
The legacy {@code BLACKHOLE} name did not distinguish a type-fit probe, follow-on errors
+ * from incomplete analysis, and a caller that deliberately wanted no reports. Naming the reason
+ * makes that decision reviewable and searchable. These reasons have the same discard behavior;
+ * compiler semantics must not depend on which reason was selected.
+ *
+ *
Suppression does not buffer diagnostics for later publication. Use {@link #branch(AstNode)}
+ * when deciding whether reports should eventually be kept. Use a derived {@link #silence(Silence)}
+ * when the parent listener's stop policy must remain active.
+ */
+ enum Silence {
+ /**
+ * Speculative work whose return value answers the question, such as whether an expression fits
+ * a candidate type. Its failure is not a diagnostic for the user's program. Use a branch instead
+ * if the trial's reports may be needed when all alternatives fail.
+ */
+ PROBE,
+
+ /**
+ * Follow-on diagnostics from information already known to be incomplete. Discard them to avoid
+ * burying the original failure in its consequences. Derive this silence from the operation's
+ * listener when its abort policy must remain active. Discarded reports cannot later be recovered.
+ */
+ CASCADE,
+
+ /**
+ * An explicit decision not to receive diagnostics at this destination, for example a command-line
+ * launcher's external delegate when its console already reports them. This is the general-purpose
+ * replacement for an intentional {@code BLACKHOLE}; choose PROBE or CASCADE when those reasons apply.
+ */
+ DISCARD
+ }
+
+ /**
+ * Obtain a shared discard sink without a parent listener or inherited stop policy.
+ *
+ *
It retains no diagnostics, reports no serious errors or codes, never requests an abort, and
+ * is silent. All reasons behave identically. If the caller already has an operation listener,
+ * prefer {@link #silence(Silence)} to preserve that listener's abort or cancellation request.
+ *
+ * @param why the reason for discarding reports
+ *
+ * @return the shared stateless sink for the requested reason
+ */
+ @NotNull
+ static ErrorListener silent(@NotNull Silence why) {
+ return switch (why) {
+ case PROBE -> SILENT_PROBE;
+ case CASCADE -> SILENT_CASCADE;
+ case DISCARD -> SILENT_DISCARD;
+ };
+ }
+
+ /**
+ * Create a discard wrapper for part of this listener's operation while retaining its stop policy.
+ *
+ *
The wrapper does not forward, retain, or replay reports, and its serious-error and code
+ * queries return false. Its abort query delegates to this listener, so suppressing reports cannot
+ * cancel a parent's stop request. {@link SilentErrorListener#suppressed()} exposes that parent,
+ * not the discarded diagnostics. The parent itself is not modified.
+ *
+ *
Silencing an existing {@link SilentErrorListener} returns the same instance and keeps its
+ * original reason, even when a different reason is requested.
+ *
+ * @param why the reason for discarding reports
+ *
+ * @return a wrapper retaining this listener's abort policy, or the existing silent wrapper
+ */
+ @NotNull
+ default ErrorListener silence(@NotNull Silence why) {
+ return new SilentErrorListener(this, why);
}
/**
- * @return true if the ErrorListener has decided to abort the process that reported the error
+ * Describe why reports are discarded, when the listener exposes a named suppression policy.
+ *
+ *
This is explanatory metadata, not a compiler mode. Wrappers do not necessarily expose a
+ * reason even when {@link #isSilent()} is true; use that predicate to test silence.
+ *
+ * @return the suppression reason, or null when no named reason is exposed
+ */
+ @Nullable
+ default Silence silenceReason() {
+ return null;
+ }
+
+ /**
+ * Ask whether the current operation should stop at the caller's next safe checkpoint.
+ *
+ *
This query is independent of reporting and need not imply that an error was emitted. A
+ * listener may request a stop because of an error budget, a fatal diagnostic, or host policy.
+ * Conversely, serious errors may be collected without exhausting the budget. The caller owns
+ * the control flow; the default implementation never requests an abort.
+ *
+ * @return true if the caller should stop the operation
*/
default boolean isAbortDesired() {
return false;
}
/**
- * @return true iff an error has been logged with at least the Severity of Error
+ * Ask whether this listener has recorded an ERROR or FATAL diagnostic.
+ *
+ *
Use this to decide whether a compiler result is usable, not as a substitute for the abort
+ * policy. The default is false because a reporting-only implementation retains no state.
+ *
+ * @return true if a serious diagnostic has been recorded
*/
default boolean hasSeriousErrors() {
return false;
}
/**
- * @return true iff an error has been logged with the specified code
+ * Ask whether this listener has recorded the specified diagnostic code at any severity.
+ *
+ *
The default returns false. Stateful implementations such as {@link ErrorList} and
+ * {@link #collecting(Consumer)} record codes for this query.
+ *
+ * @param sCode the diagnostic code to look for
+ *
+ * @return true if the code has been recorded
*/
default boolean hasError(String sCode) {
return false;
}
/**
- * Used for debugging only.
+ * Ask whether reports sent to this listener are ultimately discarded.
+ *
+ *
Callers can skip work done solely to construct diagnostics when the answer is true. They
+ * must still perform semantic work and honor {@link #isAbortDesired()}; a silent wrapper may
+ * retain a parent's stop request. The default is false. A buffering branch can be silent when
+ * its eventual parent discards the reports, even though the branch records its local state.
*
- * @return true iff this listener sits on top of the BlackHoleListener
+ * @return true if the diagnostic destination is silent
*/
default boolean isSilent() {
return false;
}
- // ----- inner class: BlackholeErrorListener ---------------------------------------------------
+ // ----- inner class: SilentErrorListener ------------------------------------------------------
/**
- * A simple implementation of the ErrorListener that converts reported errors to ErrorInfo
- * objects and routes them to a single sink method.
+ * A discard listener carrying a reason and, optionally, a parent stop policy.
+ *
+ *
Reports are neither forwarded nor retained. The inherited serious-error and code queries
+ * return false; the abort query delegates to the parent when present. {@link #suppressed()}
+ * returns that parent only, and cannot recover discarded messages. Prefer the
+ * {@link ErrorListener#silent(Silence)} and {@link ErrorListener#silence(Silence)} factories.
*/
- class BlackholeErrorListener
+ class SilentErrorListener
implements ErrorListener {
+ /**
+ * Construct a standalone discard listener with no parent abort policy.
+ *
+ * @param why the reason for discarding reports
+ */
+ public SilentErrorListener(@NotNull Silence why) {
+ this(null, why);
+ }
+
+ /**
+ * Construct a discard listener with an optional parent abort policy.
+ *
+ * @param errs the parent whose abort query is retained, or null for no parent policy
+ * @param why the reason for discarding reports
+ */
+ public SilentErrorListener(@Nullable ErrorListener errs, @NotNull Silence why) {
+ f_errs = errs;
+ f_why = requireNonNull(why, "why");
+ }
+
+ /**
+ * Return the listener whose reports this wrapper suppresses.
+ *
+ *
This is a reference to the original destination, not a buffer of discarded diagnostics.
+ * The wrapper does not mutate or silence that destination for other callers.
+ *
+ * @return the parent listener, or null for a standalone discard sink
+ */
+ @Nullable
+ public ErrorListener suppressed() {
+ return f_errs;
+ }
+
@Override
- public boolean log(ErrorInfo err) {
- return false;
+ public void log(ErrorInfo err) {
+ // discarded on purpose; f_why says which purpose
}
@Override
@@ -141,36 +695,90 @@ public ErrorListener merge() {
return this;
}
+ @Override
+ public boolean isAbortDesired() {
+ // Suppressing reports does not make cancelled work useful again.
+ return f_errs != null && f_errs.isAbortDesired();
+ }
+
@Override
public boolean isSilent() {
return true;
}
+ @Override
+ @NotNull
+ public Silence silenceReason() {
+ return f_why;
+ }
+
+ @Override
+ @NotNull
+ public ErrorListener silence(@NotNull Silence why) {
+ requireNonNull(why, "why");
+ // already silent for a stated reason; a second reason would only add a layer
+ return this;
+ }
+
@Override
public String toString() {
- return "(Blackhole)";
+ return f_errs == null ? "(" + f_why + ")" : "(" + f_why + " of " + f_errs + ")";
}
+
+ @Nullable
+ private final ErrorListener f_errs;
+ @NotNull
+ private final Silence f_why;
}
// ----- inner class: Runtime ErrorListener ----------------------------------------------------
/**
- * A simple implementation of the ErrorListener that can be used at runtime. Errors will throw,
- * and non-errors will go to standard out.
+ * A forwarding sink that remembers an abort request after an ERROR or FATAL report.
+ *
+ *
Reports go to the supplied listener, or to {@link #STDERR} for standalone callers.
+ * Reporting does not deliberately throw to stop the operation; callers must query {@link #isAbortDesired()} and
+ * choose their own failure handling. The abort flag remains set for the life of this instance.
+ *
+ *
This sink does not collect diagnostic codes or override the serious-error query. Use
+ * {@link ErrorList} or {@link ErrorListener#collecting(Consumer)} for a stateful compiler host.
*/
class RuntimeErrorListener
implements ErrorListener {
+ /** Construct a standalone listener that reports to standard error. */
+ public RuntimeErrorListener() {
+ this(STDERR);
+ }
+
+ /**
+ * @param errs the destination for runtime diagnostics
+ */
+ public RuntimeErrorListener(ErrorListener errs) {
+ f_errs = requireNonNull(errs, "errs");
+ }
+
@Override
- public boolean log(ErrorInfo err) {
- String s = err.toString();
- if (err.getSeverity().ordinal() >= Severity.ERROR.ordinal()) {
- throw new IllegalStateException(s);
- } else {
- System.out.println(err.getSeverity() + ": " + s);
- return false;
+ public void log(ErrorInfo err) {
+ if (err.getSeverity().isAtLeast(Severity.ERROR)) {
+ m_fAbort = true;
}
+ f_errs.log(err);
}
+ /**
+ * This used to throw from inside log(), which made the decision to stop - and the type of
+ * the exception that stopped it - a property of who was listening rather than of what went
+ * wrong. A caller that asks gets the same answer; a caller that does not is no longer
+ * interrupted in the middle of reporting.
+ */
+ @Override
+ public boolean isAbortDesired() {
+ return m_fAbort;
+ }
+
+ private final ErrorListener f_errs;
+ private volatile boolean m_fAbort;
+
@Override
public String toString() {
return "(Runtime error listener)";
@@ -213,7 +821,7 @@ public ErrorInfo(Severity severity, String sCode, Object[] aoParam,
* {@link Severity#ERROR}, or {@link Severity#FATAL}
* @param sCode the error code that identifies the error message
* @param aoParam the parameters for the error message; may be null
- * @param xs
+ * @param xs the related structure, or null for an unpositioned diagnostic
*/
public ErrorInfo(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) {
m_severity = severity;
@@ -223,6 +831,21 @@ public ErrorInfo(Severity severity, String sCode, Object[] aoParam, XvmStructure
// TODO need to be able to ask the XVM structure for the source & location
}
+ /**
+ * Describe this diagnostic's location as a source span, structure, or unpositioned report.
+ *
+ *
A source takes precedence when present. Otherwise a non-null structure yields a structure
+ * site; if neither is present, the result is {@link ErrorListener#NOWHERE}. No structure-to-source
+ * lookup or copying of compiler objects occurs here.
+ *
+ * @return the diagnostic location
+ */
+ public Site site() {
+ return m_source != null ? new Site.In(m_source, m_lPosStart, m_lPosEnd)
+ : m_xs != null ? new Site.At(m_xs)
+ : NOWHERE;
+ }
+
/**
* @return the Severity of the error
*/
@@ -314,8 +937,11 @@ public String genUID() {
.append(m_sCode);
if (m_aoParam != null) {
+ // the parameters are part of the identity, so they have to be compared, not
+ // digested: a 32-bit hash collides, and a collision here does not merge two
+ // reports of the same thing, it discards one of two different things
sb.append('#')
- .append(Arrays.hashCode(m_aoParam));
+ .append(Arrays.toString(m_aoParam));
}
if (!m_sCode.startsWith("VERIFY")) {
@@ -423,15 +1049,34 @@ public String toString() {
ResourceBundle RESOURCES = ResourceBundle.getBundle("errors");
/**
- * Stateless ErrorListeners.
+ * Shared standalone sink for speculative probes; prefer {@link #silent(Silence)} at call sites.
+ */
+ ErrorListener SILENT_PROBE = new SilentErrorListener(Silence.PROBE);
+ /**
+ * Shared standalone sink for cascade suppression. Use {@link #silence(Silence)} on an operation
+ * listener when its abort policy must be preserved.
+ */
+ ErrorListener SILENT_CASCADE = new SilentErrorListener(Silence.CASCADE);
+ /**
+ * Shared standalone sink for an explicitly unwanted diagnostic destination.
+ */
+ ErrorListener SILENT_DISCARD = new SilentErrorListener(Silence.DISCARD);
+
+ /**
+ * Stateless standard-error destination for standalone callers. Hosts should supply their own
+ * listener to receive diagnostics without writing to a process stream.
+ */
+ ErrorListener STDERR = err -> System.err.println(err.getSeverity() + ": " + err);
+
+ /**
+ * Shared standard-error fallback. Its abort flag is sticky across uses; it is not a fresh
+ * per-operation collector. Embedding hosts should supply their own listener.
*/
- ErrorListener BLACKHOLE = new BlackholeErrorListener();
- ErrorListener RUNTIME = new RuntimeErrorListener();
+ ErrorListener RUNTIME = new RuntimeErrorListener();
/**
- * The default maximum number of serious errors an {@link ErrorList} collects before it asks
- * the process reporting them to abort; for example, the number of errors the compiler collects
- * for one module in one stage.
+ * Default serious-error budget for compilation, shared by tools and {@link ErrorList#ErrorList()}.
+ * Pass an explicit budget to {@link ErrorList#ErrorList(int)} when a different policy is needed.
*/
int DEFAULT_MAX_ERRORS = 512;
diff --git a/javatools/src/main/java/org/xvm/asm/FileStructure.java b/javatools/src/main/java/org/xvm/asm/FileStructure.java
index fd523673249..0f91f99da36 100644
--- a/javatools/src/main/java/org/xvm/asm/FileStructure.java
+++ b/javatools/src/main/java/org/xvm/asm/FileStructure.java
@@ -205,7 +205,6 @@ public FileStructure(FileStructure that) {
m_kind = that.m_kind;
m_fLinked = that.m_fLinked;
- m_errs = that.m_errs;
resetModified();
}
@@ -1452,29 +1451,6 @@ public boolean validateModuleConstants() {
return true;
}
- @Override
- public ErrorListener getErrorListener() {
- ErrorListener errs = m_errs;
- if (errs == null) {
- // getCurrentPool() is an AMBIENT thread-local: it is null on any thread that has not had
- // a pool pushed onto it, which is every thread driving the compiler or runtime from
- // ordinary Java code. Dereferencing it unconditionally turned this diagnostic accessor
- // into an NPE source. Ownership belongs in a parameter, not a thread-local - see the PR
- // discussion - but the null guard is the minimal, behaviour-preserving fix.
- ConstantPool poolCurrent = ConstantPool.getCurrentPool();
- if (poolCurrent != null && poolCurrent != m_pool) {
- errs = poolCurrent.getErrorListener();
- }
- }
- return errs == null ? ErrorListener.RUNTIME : errs;
- }
-
- @Override
- public void setErrorListener(ErrorListener errs) {
- // this is not considered a "mutation" of the FileStructure
- m_errs = errs;
- }
-
// ----- Object methods ------------------------------------------------------------------------
@Override
@@ -1649,10 +1625,4 @@ public boolean isBundle() {
*/
private transient AssemblerContext m_ctx;
- /**
- * Holds an ErrorListener explicitly provided to this FileStructure. An absence of an
- * ErrorListener implies that either the ErrorListener from the ConstantPool associated with
- * the current thread should be used, or failing that, the runtime ErrorListener should be used.
- */
- private transient ErrorListener m_errs;
}
diff --git a/javatools/src/main/java/org/xvm/asm/MethodStructure.java b/javatools/src/main/java/org/xvm/asm/MethodStructure.java
index 41019bf95a4..3336acea3c9 100644
--- a/javatools/src/main/java/org/xvm/asm/MethodStructure.java
+++ b/javatools/src/main/java/org/xvm/asm/MethodStructure.java
@@ -59,6 +59,10 @@
import org.xvm.util.ListMap;
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.at;
+import static org.xvm.asm.ErrorListener.silent;
+
import static org.xvm.util.Handy.indentLines;
import static org.xvm.util.Handy.parseDelimitedString;
import static org.xvm.util.Handy.readIndex;
@@ -757,8 +761,10 @@ public List collectUnresolvedTypeParameters(Set setResolved) {
private static boolean checkConflict(TypeConstant typeResult, FormalConstant constFormal,
boolean fParam, Map mapTypeParams) {
if (typeResult != null) {
+ ErrorListener probe = silent(PROBE);
+
// downgrade enum value types to their base type (e.g. True -> Boolean)
- TypeInfo info = typeResult.ensureTypeInfo(ErrorListener.BLACKHOLE);
+ TypeInfo info = typeResult.ensureTypeInfo(probe);
if (info.getFormat() == Format.ENUMVALUE) {
typeResult = info.getExtends();
}
@@ -774,7 +780,7 @@ private static boolean checkConflict(TypeConstant typeResult, FormalConstant con
// the new parameter type is wider or the old return type is narrower; use it instead
} else {
// the type are not compatible; use the common type (TODO: consider union?)
- typeResult = Op.selectCommonType(typePrev, typeResult, ErrorListener.BLACKHOLE);
+ typeResult = Op.selectCommonType(typePrev, typeResult, probe);
if (typeResult == null) {
// different arguments cause the formal type to resolve into
// incompatible types
@@ -1681,8 +1687,7 @@ public ResolutionResult resolveName(String sName, Access access, ResolutionColle
// REVIEW need a better error?
AstNode node = collector.getNode();
if (node == null) {
- collector.getErrorListener().log(Severity.ERROR,
- Compiler.UNSUPPORTED_DYNAMIC_TYPE_PARAMS, null, this);
+ collector.getErrorListener().error(Compiler.UNSUPPORTED_DYNAMIC_TYPE_PARAMS, at(this));
} else {
node.log(collector.getErrorListener(), Severity.ERROR,
Compiler.UNSUPPORTED_DYNAMIC_TYPE_PARAMS);
diff --git a/javatools/src/main/java/org/xvm/asm/ModuleRepository.java b/javatools/src/main/java/org/xvm/asm/ModuleRepository.java
index 284fa3ef99b..cc51a8cb624 100644
--- a/javatools/src/main/java/org/xvm/asm/ModuleRepository.java
+++ b/javatools/src/main/java/org/xvm/asm/ModuleRepository.java
@@ -1,14 +1,33 @@
package org.xvm.asm;
+import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
+import static java.util.Objects.requireNonNull;
+
/**
* An interface representing the ability to find Modules by identity.
*/
public interface ModuleRepository {
+ /**
+ * Create a filesystem repository. An existing directory selects a directory repository; every
+ * other path selects a file repository. A file need not exist yet, allowing a writable
+ * repository to create it.
+ *
+ * @param file a module directory or file
+ * @param readOnly true to make the repository read-only
+ *
+ * @return the repository for the given path
+ */
+ static ModuleRepository of(File file, boolean readOnly) {
+ return requireNonNull(file, "file").isDirectory()
+ ? new DirRepository(file, readOnly)
+ : new FileRepository(file, readOnly);
+ }
+
/**
* Obtain a set of domain names that are known by this repository.
*
diff --git a/javatools/src/main/java/org/xvm/asm/OpCallable.java b/javatools/src/main/java/org/xvm/asm/OpCallable.java
index db7b012db32..2ae31b1075b 100644
--- a/javatools/src/main/java/org/xvm/asm/OpCallable.java
+++ b/javatools/src/main/java/org/xvm/asm/OpCallable.java
@@ -103,6 +103,16 @@ public boolean usesSuper() {
return m_nFunctionId == A_SUPER;
}
+ /**
+ * @return the function or constructor that this op calls, as the specified frame has it, or
+ * null if the op calls a function that a register holds, or the super function
+ */
+ public MethodConstant getCalledMethod(Frame frame) {
+ return m_nFunctionId <= CONSTANT_OFFSET
+ ? frame.getConstant(m_nFunctionId, MethodConstant.class)
+ : null;
+ }
+
/**
* A "virtual constant" indicating whether or not this op has multiple return values.
*
diff --git a/javatools/src/main/java/org/xvm/asm/OpInvocable.java b/javatools/src/main/java/org/xvm/asm/OpInvocable.java
index 01f13f0ae6a..972ed614e10 100644
--- a/javatools/src/main/java/org/xvm/asm/OpInvocable.java
+++ b/javatools/src/main/java/org/xvm/asm/OpInvocable.java
@@ -106,6 +106,13 @@ protected boolean isTupleReturn() {
return false;
}
+ /**
+ * @return the method that this op invokes, as the specified frame has it
+ */
+ public MethodConstant getInvokedMethod(Frame frame) {
+ return frame.getConstant(m_nMethodId, MethodConstant.class);
+ }
+
@Override
public void resetSimulation() {
if (isMultiReturn()) {
diff --git a/javatools/src/main/java/org/xvm/asm/Reporting.java b/javatools/src/main/java/org/xvm/asm/Reporting.java
new file mode 100644
index 00000000000..e96ca65868c
--- /dev/null
+++ b/javatools/src/main/java/org/xvm/asm/Reporting.java
@@ -0,0 +1,67 @@
+package org.xvm.asm;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * A diagnostic destination that can be diverted for a lexical scope.
+ *
+ * The parser and name resolver use this when callbacks cannot take a listener parameter.
+ * Scopes must be closed in reverse order, normally by try-with-resources. Each scope restores
+ * its predecessor even when the operation throws. An inactive resolver has a null destination;
+ * an active scope always has a listener.
+ *
+ *
This holder controls lifetime, not ownership or concurrency. Owners remain confined to one
+ * compiler operation; sharing a holder across threads is not supported.
+ */
+public final class Reporting {
+ private ErrorListener destination;
+
+ /**
+ * @param errs where diagnostics go to begin with; null if the owner has no destination
+ * until one is given to it
+ */
+ public Reporting(ErrorListener errs) {
+ destination = errs;
+ }
+
+ /**
+ * @return where diagnostics go right now, or null if the owner has none
+ */
+ public ErrorListener get() {
+ return destination;
+ }
+
+ /**
+ * Send diagnostics to the given listener until the returned scope is closed.
+ *
+ * @param errs the listener to report to for the duration of the scope
+ *
+ * @return the scope, which restores the previous destination when closed
+ */
+ public Scope to(ErrorListener errs) {
+ return new Scope(errs);
+ }
+
+ /**
+ * The scope opened by {@link #to}.
+ */
+ public final class Scope
+ implements AutoCloseable {
+ private final ErrorListener previous;
+
+ private Scope(ErrorListener errs) {
+ previous = destination;
+ destination = requireNonNull(errs, "errs");
+ }
+
+ @Override
+ public void close() {
+ destination = previous;
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "Reporting(" + destination + ")";
+ }
+}
diff --git a/javatools/src/main/java/org/xvm/asm/XvmStructure.java b/javatools/src/main/java/org/xvm/asm/XvmStructure.java
index 14000a384ec..138f9663ff9 100755
--- a/javatools/src/main/java/org/xvm/asm/XvmStructure.java
+++ b/javatools/src/main/java/org/xvm/asm/XvmStructure.java
@@ -463,41 +463,13 @@ public boolean validate(ErrorListener errs) {
/**
* Log an error against this structure.
*
- * @param errs the error list to log to, or null to use the runtime ErrorListener
+ * @param errs the error listener to log to
* @param sev the severity of the error
* @param sCode the error code
* @param aoParam the parameters of the error
*/
- public boolean log(ErrorListener errs, Severity sev, String sCode, Object ... aoParam) {
- return ensureErrorListener(errs).log(sev, sCode, aoParam, this);
- }
-
- /**
- * Make sure that an error listener is returned to use.
- *
- * @param errs an error listener, or null
- *
- * @return the error listener passed in, if it was not null, otherwise the previously specified
- * error listener, otherwise the runtime error listener
- */
- public ErrorListener ensureErrorListener(ErrorListener errs) {
- return errs == null ? getErrorListener() : errs;
- }
-
- /**
- * @return the error listener, if provided, otherwise the runtime error listener
- */
- public ErrorListener getErrorListener() {
- return m_xsParent.getErrorListener();
- }
-
- /**
- * Specify an error listener.
- *
- * @param errs the error listener
- */
- public void setErrorListener(ErrorListener errs) {
- m_xsParent.setErrorListener(errs);
+ public void log(ErrorListener errs, Severity sev, String sCode, Object ... aoParam) {
+ errs.log(sev, sCode, ErrorListener.at(this), aoParam);
}
// ----- debugging support ---------------------------------------------------------------------
diff --git a/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java
index 30310fe6107..6eb5b11e8ac 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java
@@ -238,7 +238,8 @@ public boolean validate(ErrorListener errs) {
// type constant
TypeConstant type = m_constType;
if (type instanceof ImmutableTypeConstant) {
- fHalt |= log(errs, Severity.WARNING, VE_IMMUTABLE_REDUNDANT);
+ log(errs, Severity.WARNING, VE_IMMUTABLE_REDUNDANT);
+ fHalt |= errs.isAbortDesired();
}
// a service type cannot be immutable
diff --git a/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java b/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java
index 3bfc486ed29..b4cf3b8a9e3 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java
@@ -292,9 +292,13 @@ public boolean isInto() {
}
/**
+ * Inspect the existing mixin constraint chain without generating a forwarding body.
+ * The returned compiler metadata belongs to the current compilation; hosts must copy facts
+ * before publishing them to another thread. Only call this for a FromInto implementation.
+ *
* @return the (potentially incomplete) MethodInfo from which the "into" MethodBody was created
*/
- MethodInfo getIntoMethodInfo() {
+ public MethodInfo getIntoMethodInfo() {
assert isInto();
return (MethodInfo) m_target;
}
diff --git a/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java b/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java
index c6436f087b5..3213373d95f 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java
@@ -6,6 +6,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
@@ -1348,6 +1349,33 @@ public boolean hasSuper(TypeInfo infoType) {
return getSuper(infoType) != null;
}
+ /**
+ * The written method body selected by super dispatch, when there is one. Field, native,
+ * delegation and into-constraint bodies have no source method target. Inspection uses the
+ * same chain selection as getSuper(), without generating or optimizing forwarding code.
+ *
+ * @param infoType the enclosing type information used to locate the inherited method
+ *
+ * @return the written super-dispatch target, or empty when no source method represents it
+ */
+ public Optional getSuperMethod(TypeInfo infoType) {
+ MethodStructure method = getTopmostMethodStructure(infoType);
+ if (method.getParent().getParent() instanceof PropertyStructure property &&
+ (method == property.getGetter() || method == property.getSetter()) &&
+ infoType.findProperty(property.getIdentityConstant()).hasField()) {
+ return Optional.empty();
+ }
+ MethodBody[] chain = m_aBodyResolved;
+ MethodBody body = chain == null ? findSuper(infoType, getChain())
+ : chain.length > 1 ? chain[1] : null;
+ return Optional.ofNullable(body)
+ .filter(candidate -> candidate.getImplementation() == Implementation.Explicit ||
+ candidate.getImplementation() == Implementation.Default)
+ .map(MethodBody::getMethodStructure)
+ .filter(target -> !target.isSynthetic())
+ .map(MethodStructure::getIdentityConstant);
+ }
+
/**
* @return the "super" signature
*/
diff --git a/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java b/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java
index 4c0f38fc191..a0f786c1d2a 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java
@@ -304,7 +304,7 @@ else if (!(bodyAdd.isRO() && typeResult.isA(typeAdd))
for (Annotation annotation : aAnnoBase) {
TypeConstant typeAnnoBase = annotation.getAnnotationType();
if (typeAnnoAdd.equals(typeAnnoBase)) {
- idProp.log(errs, Severity.WARNING, VE_DUP_ANNOTATION_IGNORED,
+ that.getIdentity().log(errs, Severity.WARNING, VE_DUP_ANNOTATION_IGNORED,
that.getIdentity().getParentConstant().getValueString(),
getName(),
annoAdd.getAnnotationClass().getValueString());
@@ -312,7 +312,7 @@ else if (!(bodyAdd.isRO() && typeResult.isA(typeAdd))
}
if (typeAnnoAdd.isA(typeAnnoBase)) {
- idProp.log(errs, Severity.WARNING, VE_SUP_ANNOTATION_IGNORED,
+ that.getIdentity().log(errs, Severity.WARNING, VE_SUP_ANNOTATION_IGNORED,
that.getIdentity().getParentConstant().getValueString(),
getName(),
typeAnnoAdd.getValueString(),
diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java b/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java
index ba27cbed5ee..99cb8e54fbb 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java
@@ -10,6 +10,9 @@
import org.xvm.asm.ErrorListener;
import org.xvm.asm.Op;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* A TypeCollector is used to collect a number of types, such as would occur from return statements
* within a lambda, in order to infer a type from that collection of types.
@@ -167,19 +170,21 @@ public TypeConstant inferSingle(TypeConstant typeRequired) {
return null;
}
+ ErrorListener probe = silent(PROBE);
+
TypeConstant typeCommon = inferFrom(listTypes.toArray(new TypeConstant[cTypes]), f_pool);
if (typeRequired != null && typeRequired.containsFormalType(true)) {
typeCommon = typeRequired.resolvePending(f_pool, typeCommon);
}
- typeCommon = Op.selectCommonType(typeCommon, typeRequired, ErrorListener.BLACKHOLE);
+ typeCommon = Op.selectCommonType(typeCommon, typeRequired, probe);
if (typeRequired != null &&
(typeCommon == null || !typeCommon.isAssignableTo(typeRequired))) {
// approach above didn't quite work; try to match with individual types one-by-one
- TypeConstant typeAlt = Op.selectCommonType(typeRequired, listTypes.get(0), ErrorListener.BLACKHOLE);
+ TypeConstant typeAlt = Op.selectCommonType(typeRequired, listTypes.get(0), probe);
for (int i = 1; i < cTypes; i++) {
- typeAlt = Op.selectCommonType(typeAlt, listTypes.get(i), ErrorListener.BLACKHOLE);
+ typeAlt = Op.selectCommonType(typeAlt, listTypes.get(i), probe);
}
if (typeAlt != null) {
@@ -342,8 +347,7 @@ public TypeConstant[] inferMulti(TypeConstant[] atypeRequired) {
for (int iCol = 0; iCol < cWidth; ++iCol) {
TypeConstant typeRequired = iCol < cReqTypes ? atypeRequired[iCol] : null;
if (typeRequired != null) {
- aResult[iCol] = Op.selectCommonType(aResult[iCol], typeRequired,
- ErrorListener.BLACKHOLE);
+ aResult[iCol] = Op.selectCommonType(aResult[iCol], typeRequired, silent(PROBE));
}
}
}
@@ -429,7 +433,7 @@ public static TypeConstant inferFrom(TypeConstant[] aTypes, ConstantPool pool) {
}
}
- typeCommon = Op.selectCommonType(type, typeCommon, ErrorListener.BLACKHOLE);
+ typeCommon = Op.selectCommonType(type, typeCommon, silent(PROBE));
if (typeCommon == null) {
// no obvious common type
return null;
diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java
index fa2a018b02f..b63074d2443 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java
@@ -12,10 +12,10 @@
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
-import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
@@ -39,6 +39,7 @@
import org.xvm.asm.ConstantPool;
import org.xvm.asm.ErrorList;
import org.xvm.asm.ErrorListener;
+import org.xvm.asm.ErrorListener.ErrorInfo;
import org.xvm.asm.GenericTypeResolver;
import org.xvm.asm.MethodStructure;
import org.xvm.asm.ModuleStructure;
@@ -91,6 +92,11 @@
import static java.lang.constant.ConstantDescs.CD_boolean;
import static java.lang.constant.ConstantDescs.CD_int;
+import static org.xvm.asm.ErrorListener.Silence.CASCADE;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+import static org.xvm.asm.ErrorListener.tee;
+
import static org.xvm.javajit.Builder.CD_Class;
import static org.xvm.javajit.Builder.CD_Ctx;
import static org.xvm.javajit.Builder.CD_nType;
@@ -1780,12 +1786,27 @@ public TypeConstant widenEnumValueTypes() {
// ----- TypeInfo support ----------------------------------------------------------------------
/**
- * Obtain the information about this type, resolved from its recursive composition.
+ * Obtain the information about this type, resolved from its recursive composition, without
+ * reporting anything about the attempt.
+ *
+ * A TypeConstant is an interned value shared by everything, so when it is asked to build a
+ * TypeInfo and given no listener it has no caller to ask. It used to walk up to its file
+ * structure and report to whatever that file was last told - an ambient lookup which, for the
+ * two thirds of these call sites that run after compilation is over, ended at a listener that
+ * prints to stdout. Measured across a full XDK build and test run, that listener never
+ * received anything, and during a compilation the file was parked on a silence anyway. So the
+ * silence is said here instead of arranged elsewhere.
+ *
+ *
This convenience read does not report diagnostics to a host. It is used by speculative and
+ * metadata queries, including provisional compositions. Diagnostics recorded by a completed
+ * build remain available for replay; a caller responsible for reporting a selected source use
+ * must pass its listener to {@link #ensureTypeInfo(ErrorListener)}. Silence here is not evidence
+ * that every diagnostic encountered is spurious.
*
* @return the flattened TypeInfo that represents the resolved type of this TypeConstant
*/
public TypeInfo ensureTypeInfo() {
- return ensureTypeInfo(getErrorListener());
+ return ensureTypeInfo(silent(CASCADE));
}
/**
@@ -1814,10 +1835,38 @@ public TypeInfo ensureTypeInfo(ErrorListener errs) {
TypeInfo info = getTypeInfo();
if (isComplete(info) && isUpToDate(info)) {
+ // the cached answer was built for whoever asked first. If that was a speculative
+ // probe, the probe produced the diagnostics and this caller - which may be the one
+ // that actually cared - would otherwise hear nothing. Replay them instead, so what a
+ // caller is told does not depend on the order callers happen to arrive in
+ replayDiagnostics(errs);
return info;
}
- return ensureTypeInfo(info, errs);
+ // record what building it has to say, so a later caller can be told the same
+ ErrorList recorder = new ErrorList(ErrorList.UNLIMITED);
+ info = ensureTypeInfo(info, tee(errs, recorder));
+
+ // freeze it. An ErrorList is a listener, which is what the recording needed to be while
+ // it was being made and not what it should be once it is made: keeping one per
+ // TypeConstant for the life of the pool holds an error budget, a severity, a dedup set
+ // and a mutable list, to say something a List already says. Most builds report nothing -
+ // measured over an XDK build, all of them - and copyOf gives back the shared empty list
+ // for those, so saying "nothing" costs no allocation at all
+ m_diagnostics = List.copyOf(recorder.getErrors());
+ return info;
+ }
+
+ /**
+ * Report the diagnostics that were produced while this type's TypeInfo was built.
+ *
+ *
Deduplication in the receiving listener makes this idempotent: a caller that asks twice, or
+ * two callers sharing a listener, are told once.
+ *
+ * @param errs the listener to replay them to
+ */
+ private void replayDiagnostics(ErrorListener errs) {
+ m_diagnostics.forEach(errs::log);
}
private synchronized TypeInfo ensureTypeInfo(TypeInfo info, ErrorListener errs) {
@@ -2104,6 +2153,16 @@ protected void setTypeInfo(TypeInfo info) {
}
}
+ /**
+ * The diagnostics produced while this type's TypeInfo was built, replayed to later callers
+ * that get the memoized result; see {@link #replayDiagnostics}.
+ *
+ *
Empty until the TypeInfo has been built, and empty afterwards when building it had nothing
+ * to say - which is the ordinary case. Never null: "not built yet" and "built quietly" are
+ * the same answer to everyone who reads this, so there is nothing for a third state to say.
+ */
+ private transient volatile List m_diagnostics = List.of();
+
/**
* @return the invalidation count that this TypeConstant has already processed
*/
@@ -2183,6 +2242,26 @@ public static boolean isComplete(TypeInfo info) {
return rankTypeInfo(info) == 3;
}
+ /**
+ * Choose the listener to report the rest of a TypeInfo build to.
+ *
+ * Once a contribution has turned out to be incomplete, what follows is reported against a
+ * type that is known to be missing pieces, so the diagnostics are consequences of what is
+ * absent rather than faults in the source. They are suppressed for the remainder of the
+ * build; the incompleteness itself is what the caller is told, by the return value.
+ *
+ * The choice is made at each use rather than by rebinding the listener, so that the errs
+ * parameter still means what its signature says all the way down the method.
+ *
+ * @param fIncomplete whether the build is already known to be incomplete
+ * @param errs the caller's listener
+ *
+ * @return the listener to report to
+ */
+ private static ErrorListener cascade(boolean fIncomplete, ErrorListener errs) {
+ return fIncomplete ? errs.silence(CASCADE) : errs;
+ }
+
/**
* Determine if the passed TypeInfo is up-to-date for this type.
*
@@ -2378,7 +2457,7 @@ private TypeInfo buildBaseTypeInfoImpl(
// validate the type parameters against the properties
checkTypeParameterProperties(mapTypeParams, mapVirtProps,
- fComplete && !errs.hasSeriousErrors() ? errs : ErrorListener.BLACKHOLE);
+ fComplete && !errs.hasSeriousErrors() ? errs : errs.silence(CASCADE));
Annotation[] aAnnoMixin = fComplete
? collectMixinAnnotations(listProcess)
@@ -2759,7 +2838,7 @@ private TypeInfo buildStructInfo(ErrorListener errs) {
typeContrib = typeContrib.removeAccess();
typeContrib = pool.ensureAccessTypeConstant(typeContrib, Access.STRUCT);
- TypeInfo infoContrib = typeContrib.ensureTypeInfoInternal(errs);
+ TypeInfo infoContrib = typeContrib.ensureTypeInfoInternal(cascade(fIncomplete, errs));
if (isComplete(infoContrib)) {
for (Map.Entry entry : infoContrib.getProperties().entrySet()) {
PropertyInfo prop = entry.getValue();
@@ -2771,14 +2850,13 @@ private TypeInfo buildStructInfo(ErrorListener errs) {
}
} else {
fIncomplete = true;
- errs = ErrorListener.BLACKHOLE;
}
break;
}}
}
// add Object.toString() method
- MethodInfo infoToString = pool.typeObject().ensureTypeInfo(errs).
+ MethodInfo infoToString = pool.typeObject().ensureTypeInfo(cascade(fIncomplete, errs)).
getMethodBySignature(pool.sigToString());
mapMethods.putIfAbsent(infoToString.getIdentity(), infoToString);
@@ -3567,18 +3645,14 @@ private boolean createCallChains(
case Into: {
// append to the call chain
TypeConstant typeContrib = contrib.getTypeConstant(); // already resolved
- TypeInfo infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(errs);
+ TypeInfo infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(cascade(fIncomplete, errs));
if (!isComplete(infoContrib)) {
fIncomplete |= computeIncomplete(composition, typeContrib, infoContrib, setDepends);
- if (fIncomplete) {
- errs = ErrorListener.BLACKHOLE;
- }
}
if (infoContrib != null) {
- infoContrib.contributeChains(listmapClassChain, listmapDefaultChain,
- listmapRootChain, composition);
- layerOnTypeParams(mapTypeParams, typeContrib, infoContrib.getTypeParams(), errs);
+ infoContrib.contributeChains(listmapClassChain, listmapDefaultChain, listmapRootChain, composition);
+ layerOnTypeParams(mapTypeParams, typeContrib, infoContrib.getTypeParams(), cascade(fIncomplete, errs));
}
break;
}
@@ -3737,18 +3811,16 @@ private boolean collectMemberInfo(
int nBasePropRank = mapProps.size();
int nBaseMethRank = mapMethods.size();
- if (!collectSelfTypeParameters(struct, mapTypeParams, mapContribProps, nBasePropRank, errs)) {
+ if (!collectSelfTypeParameters(struct, mapTypeParams, mapContribProps, nBasePropRank, cascade(fIncomplete, errs))) {
fIncomplete = true;
- errs = ErrorListener.BLACKHOLE;
}
var listExplode = new ArrayList();
boolean fInterface = struct.getFormat() == Component.Format.INTERFACE;
if (!collectChildInfo(constId, fInterface, struct, mapTypeParams,
mapContribProps, mapContribMethods, mapContribChildren, listExplode,
- mapVirtProps, nBasePropRank, nBaseMethRank, errs)) {
+ mapVirtProps, nBasePropRank, nBaseMethRank, cascade(fIncomplete, errs))) {
fIncomplete = true;
- errs = ErrorListener.BLACKHOLE;
}
// the order in which the properties are layered on and exploded is extremely
@@ -3770,21 +3842,19 @@ private boolean collectMemberInfo(
// layer on the property so its information is all correct before we have to
// make any decisions about how to process the property
prop = layerOnProp(constId, ContribSource.Self, null, mapProps, mapVirtProps,
- typeContrib, idProp, prop, errs);
+ typeContrib, idProp, prop, cascade(fIncomplete, errs));
// now that the necessary data is in place, explode the property
- if (!fNative && !explodeProperty(constId, struct, idProp, prop,
- mapProps, mapVirtProps, mapMethods, mapVirtMethods, errs)) {
+ if (!fNative && !explodeProperty(constId, struct, idProp, prop, mapProps,
+ mapVirtProps, mapMethods, mapVirtMethods, cascade(fIncomplete, errs))) {
fIncomplete = true;
- errs = ErrorListener.BLACKHOLE;
}
}
} else {
- infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(errs);
+ infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(cascade(fIncomplete, errs));
if (!isComplete(infoContrib)) {
if (computeIncomplete(composition, typeContrib, infoContrib, setDepends)) {
fIncomplete = true;
- errs = ErrorListener.BLACKHOLE;
}
if (infoContrib == null) {
// even if the contribution has an incomplete info we can still proceed
@@ -3838,7 +3908,7 @@ private boolean collectMemberInfo(
// process properties
layerOnProps(constId, contribSource, idDelegate, mapProps, mapVirtProps,
- typeContrib, mapContribProps, errs);
+ typeContrib, mapContribProps, cascade(fIncomplete, errs));
// if there are any remaining declared-but-not-overridden properties originating from
// an interface on a class once the "self" layer is applied, then those need to be
@@ -3846,7 +3916,7 @@ private boolean collectMemberInfo(
if (fSelf && !isInterface(constId, struct) && !struct.isExplicitlyAbstract()) {
for (Entry entry : mapProps.entrySet()) {
PropertyInfo infoOld = entry.getValue();
- PropertyInfo infoNew = infoOld.finishAdoption(fNative, errs);
+ PropertyInfo infoNew = infoOld.finishAdoption(fNative, cascade(fIncomplete, errs));
if (infoNew != infoOld) {
entry.setValue(infoNew);
if (infoNew.isVirtual()) {
@@ -3863,7 +3933,7 @@ private boolean collectMemberInfo(
if (!mapContribMethods.isEmpty()) {
assert contrib.getComposition() != Composition.Annotation;
layerOnMethods(constId, contribSource, idDelegate, mapMethods, mapVirtMethods,
- typeContrib, mapContribMethods, errs);
+ typeContrib, mapContribMethods, cascade(fIncomplete, errs));
}
// process children
@@ -3875,7 +3945,7 @@ private boolean collectMemberInfo(
if (infoPrev != null) {
ChildInfo infoNew = infoPrev.layerOn(infoChild);
if (infoNew == null) {
- log(errs, Severity.ERROR, VE_CHILD_COLLISION,
+ log(cascade(fIncomplete, errs), Severity.ERROR, VE_CHILD_COLLISION,
constId,
sName,
contrib.getTypeConstant(),
@@ -3893,7 +3963,7 @@ private boolean collectMemberInfo(
// to be processed by "finishAdoption"
for (Entry entry : mapMethods.entrySet()) {
MethodInfo infoOld = entry.getValue();
- MethodInfo infoNew = infoOld.finishAdoption(fNative, errs);
+ MethodInfo infoNew = infoOld.finishAdoption(fNative, cascade(fIncomplete, errs));
if (infoNew != infoOld) {
entry.setValue(infoNew);
if (infoNew.isVirtual()) {
@@ -3975,7 +4045,6 @@ protected boolean explodeProperty(
infoProp.getHead().markExploded();
} else {
fComplete = false;
- errs = ErrorListener.BLACKHOLE;
}
// layer on any annotations, if any
@@ -3993,13 +4062,12 @@ protected boolean explodeProperty(
}
typeAnno = pool.ensureAccessTypeConstant(typeAnno, Access.PROTECTED);
- TypeInfo infoAnno = typeAnno.ensureTypeInfoInternal(errs);
+ TypeInfo infoAnno = typeAnno.ensureTypeInfoInternal(cascade(!fComplete, errs));
if (infoAnno == null) {
fComplete = false;
- errs = ErrorListener.BLACKHOLE;
} else {
nestAndLayerOn(constId, idProp, mapProps, mapVirtProps, mapMethods, mapVirtMethods,
- typeAnno, infoAnno, ContribSource.Annotation, errs);
+ typeAnno, infoAnno, ContribSource.Annotation, cascade(!fComplete, errs));
}
}
@@ -4054,11 +4122,11 @@ protected boolean explodeProperty(
idGet.getValueString() + " at " + this.getValueString());
}
infoGet = infoGet.layerOn(new MethodInfo(new MethodBody(idGet,
- idGet.getSignature(), Implementation.Implicit), nRank), false, errs);
+ idGet.getSignature(), Implementation.Implicit), nRank), false, cascade(!fComplete, errs));
if (infoSet != null) {
infoSet = infoSet.layerOn(new MethodInfo(new MethodBody(idSet,
- idSet.getSignature(), Implementation.Implicit), nRank + 1), false, errs);
+ idSet.getSignature(), Implementation.Implicit), nRank + 1), false, cascade(!fComplete, errs));
}
}
@@ -7661,7 +7729,7 @@ public TypeConstant getJitCCType() {
public TypeConstant getJitICType() {
// TODO CP: plug in the new logic
TypeConstant type = getJitCCType();
- assert type.isNativeRef() || type.ensureTypeInfo().isNewable(false, ErrorListener.BLACKHOLE);
+ assert type.isNativeRef() || type.ensureTypeInfo().isNewable(false, silent(PROBE));
return type;
}
diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java b/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java
index 7d84ae258be..c506f4069cf 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java
@@ -37,6 +37,9 @@
import org.xvm.util.ListMap;
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* The fully realized "flattened" information about a type.
*/
@@ -803,7 +806,7 @@ public boolean isNewable(boolean fSingleton, ErrorListener errs) {
return false;
}
TypeConstant typeParent = f_type.getParentType();
- if (!typeParent.ensureTypeInfo(errs).isNewable(false, ErrorListener.BLACKHOLE)) {
+ if (!typeParent.ensureTypeInfo(errs).isNewable(false, silent(PROBE))) {
// the parent is abstract, so the virtual child "new-ability" will be checked
// by concrete parent's subclasses
return true;
diff --git a/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java
index 1ff5142b810..5cdcf2c5ae0 100644
--- a/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java
+++ b/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java
@@ -18,7 +18,8 @@
import org.xvm.compiler.CompilerException;
import org.xvm.util.Hash;
-import org.xvm.util.Severity;
+
+import static org.xvm.asm.ErrorListener.at;
/**
* Represent a type constant that will eventually be replaced with a real type constant.
@@ -517,7 +518,7 @@ public boolean validate(ErrorListener errs) {
return getResolvedType().validate(errs);
}
- errs.log(Severity.ERROR, Compiler.NAME_UNRESOLVABLE, new Object[]{getValueString()}, this);
+ errs.error(Compiler.NAME_UNRESOLVABLE, at(this), getValueString());
return true;
}
diff --git a/javatools/src/main/java/org/xvm/asm/op/MoveThis.java b/javatools/src/main/java/org/xvm/asm/op/MoveThis.java
index 7a4e15d9708..187b22943d7 100644
--- a/javatools/src/main/java/org/xvm/asm/op/MoveThis.java
+++ b/javatools/src/main/java/org/xvm/asm/op/MoveThis.java
@@ -139,14 +139,13 @@ public int process(Frame frame, int iPC) {
}
if (m_nAccess != 0) {
- Access access = switch (m_nAccess) {
+ hOuter = hOuter.ensureAccess(switch (m_nAccess) {
case A_PUBLIC -> Access.PUBLIC;
case A_PROTECTED -> Access.PROTECTED;
case A_PRIVATE -> Access.PRIVATE;
case A_STRUCT -> Access.STRUCT;
default -> throw new IllegalStateException();
- };
- hOuter = hOuter.ensureAccess(access);
+ });
}
int nToValue = m_nToValue;
diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java
index cbeb25b45c0..6aebf5b71e8 100644
--- a/javatools/src/main/java/org/xvm/compiler/Compiler.java
+++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java
@@ -1,5 +1,7 @@
package org.xvm.compiler;
+import static java.util.Objects.requireNonNull;
+
import org.xvm.asm.ConstantPool;
import org.xvm.asm.ErrorListener;
import org.xvm.asm.FileStructure;
@@ -9,6 +11,8 @@
import org.xvm.compiler.ast.StageMgr;
import org.xvm.compiler.ast.TypeCompositionStatement;
+import org.xvm.compiler.ast.bindings.CursorBinding;
+import org.xvm.compiler.ast.bindings.InvocationBinding;
/**
* A module compiler for Ecstasy code.
@@ -27,18 +31,39 @@ public class Compiler {
* @param errs the listener to log any errors to during the various phases of compilation
*/
public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs) {
+ this(stmtModule, errs, InvocationBinding.Collector.NONE);
+ }
+
+ /**
+ * Construct a compiler that records call facts in the supplied attempt-owned collector.
+ *
+ * @param stmtModule the assembled module declaration to compile
+ * @param errs the diagnostic listener and abort policy for this operation
+ * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection
+ */
+ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, InvocationBinding.Collector bindings) {
+ this(stmtModule, errs, bindings, CursorBinding.Collector.NONE);
+ }
+
+ /**
+ * Construct a module compiler using the supplied attempt-owned fact collectors.
+ *
+ * @param stmtModule the assembled module declaration to compile
+ * @param errs the diagnostic listener and abort policy for this operation
+ * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection
+ * @param cursors the attempt-owned cursor-fact collector, or its NONE instance to disable collection
+ */
+ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, InvocationBinding.Collector bindings, CursorBinding.Collector cursors) {
if (stmtModule == null) {
throw new IllegalArgumentException("AST node for module required");
}
if (stmtModule.getCategory().getId() != Token.Id.MODULE) {
throw new IllegalArgumentException("AST node for module is not a module statement");
}
- if (errs == null) {
- throw new IllegalArgumentException("ErrorListener required");
- }
-
m_stmtModule = stmtModule;
- m_errs = errs;
+ f_errs = requireNonNull(errs, "errs");
+ f_bindings = requireNonNull(bindings, "bindings");
+ f_cursors = requireNonNull(cursors, "cursors");
}
// ----- accessors -----------------------------------------------------------------------------
@@ -56,7 +81,7 @@ public TypeCompositionStatement getModuleStatement() {
*/
public ErrorListener getErrorListener() {
validateCompiler();
- return m_errs;
+ return f_errs;
}
/**
@@ -91,7 +116,7 @@ public boolean alreadyReached(Stage stage) {
* @return true if the compiler has decided to abort the process
*/
public boolean isAbortDesired() {
- return m_errs.isAbortDesired();
+ return f_errs.isAbortDesired();
}
// ----- public API ----------------------------------------------------------------------------
@@ -114,15 +139,14 @@ public FileStructure generateInitialFileStructure() {
if (getStage() == Stage.Initial) {
setStage(Stage.Registering);
- StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, m_errs);
+ StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, f_errs, f_bindings, f_cursors);
if (!mgr.processComplete()) {
- if (m_errs.hasSeriousErrors()) {
+ if (f_errs.hasSeriousErrors() || f_errs.isAbortDesired()) {
return null;
}
throw new CompilerException("failed to create module");
}
m_structFile = m_stmtModule.getComponent().getFileStructure();
- m_structFile.setErrorListener(ErrorListener.BLACKHOLE);
setStage(Stage.Registered);
}
@@ -188,7 +212,7 @@ public boolean resolveNames(boolean fLastAttempt) {
if (!alreadyReached(Stage.Resolving)) {
// first time through: resolve starting from the module, and recurse down
setStage(Stage.Resolving);
- m_mgr = new StageMgr(m_stmtModule, Stage.Resolved, m_errs);
+ m_mgr = new StageMgr(m_stmtModule, Stage.Resolved, f_errs, f_bindings, f_cursors);
}
if (fLastAttempt) {
@@ -232,7 +256,7 @@ public boolean validateExpressions(boolean fLastAttempt) {
if (!alreadyReached(Stage.Validating)) {
// first time through: resolve starting from the module, and recurse down
setStage(Stage.Validating);
- m_mgr = new StageMgr(m_stmtModule, Stage.Validated, m_errs);
+ m_mgr = new StageMgr(m_stmtModule, Stage.Validated, f_errs, f_bindings, f_cursors);
}
if (fLastAttempt) {
@@ -276,7 +300,7 @@ public boolean generateCode(boolean fLastAttempt) {
if (!alreadyReached(Stage.Emitting)) {
// first time through: resolve starting from the module, and recurse down
setStage(Stage.Emitting);
- m_mgr = new StageMgr(m_stmtModule, Stage.Emitted, m_errs);
+ m_mgr = new StageMgr(m_stmtModule, Stage.Emitted, f_errs, f_bindings, f_cursors);
}
if (fLastAttempt) {
@@ -286,11 +310,11 @@ public boolean generateCode(boolean fLastAttempt) {
if (m_mgr.processComplete()) {
setStage(Stage.Emitted);
- if (!m_errs.hasSeriousErrors()) {
- // "purge" the constant pool and do a final validation on the entire module structure
+ if (!f_errs.hasSeriousErrors()) {
+ // "purge" the constant pool and do a final validation on the entire module
+ // structure
m_structFile.reregisterConstants(true);
- m_structFile.validate(m_errs);
- m_structFile.setErrorListener(null);
+ m_structFile.validate(f_errs);
}
}
}
@@ -303,8 +327,8 @@ public boolean generateCode(boolean fLastAttempt) {
* method will report any unresolved names as fatal errors.
*/
public void logRemainingDeferredAsErrors() {
- if (!m_errs.hasSeriousErrors()) {
- m_mgr.logDeferredAsErrors(m_errs);
+ if (!f_errs.hasSeriousErrors()) {
+ m_mgr.logDeferredAsErrors(f_errs);
}
}
@@ -378,7 +402,11 @@ private void setStage(Stage stage) {
/**
* The ErrorListener to report errors to.
*/
- private final ErrorListener m_errs;
+ private final ErrorListener f_errs;
+
+ /** Optional tooling output, confined to this compiler attempt. */
+ private final InvocationBinding.Collector f_bindings;
+ private final CursorBinding.Collector f_cursors;
/**
* The FileStructure that this compiler is putting together in a series of passes.
diff --git a/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java b/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java
index dc4afbdf977..c482747acdf 100644
--- a/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java
+++ b/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java
@@ -39,7 +39,6 @@
import org.xvm.runtime.Utils;
import org.xvm.util.ListMap;
-import org.xvm.util.Severity;
/**
* The compiler of the "eval" script used by the debugger.
@@ -65,7 +64,7 @@ public EvalCompiler(Frame frame, String sMethod) {
*/
public MethodStructure createLambda(TypeConstant typeReturn) {
ConstantPool pool = f_frame.poolContext();
- ErrorList errs = m_errs = new ErrorList(1);
+ ErrorList errs = f_errs;
MethodStructure method = f_frame.f_function;
ClassStructure clz = method.getContainingClass();
@@ -134,8 +133,8 @@ public MethodStructure createLambda(TypeConstant typeReturn) {
return lambda;
} catch (Exception e) {
if (!errs.hasSeriousErrors()) {
- errs.log(Severity.FATAL, Parser.FATAL_ERROR, null,
- f_source, f_source.getPosition(), f_source.getPosition());
+ errs.fatal(Parser.FATAL_ERROR, ErrorListener.in(
+ f_source, f_source.getPosition(), f_source.getPosition()));
}
return discard(lambda);
}
@@ -155,7 +154,7 @@ private static MethodStructure discard(MethodStructure lambda) {
* @return a list of errors
*/
public List getErrors() {
- return m_errs.getErrors();
+ return f_errs.getErrors();
}
/**
@@ -319,9 +318,9 @@ protected Argument getLocalVar(String sName, Branch branch) {
private final Source f_source;
/**
- * The errors.
+ * Diagnostics owned by this evaluation, available before lambda compilation starts.
*/
- private ErrorList m_errs;
+ private final ErrorList f_errs = new ErrorList(ErrorList.FIRST_ERROR);
/**
* A synthetic MethodDeclarationStatement that contains the eval body.
diff --git a/javatools/src/main/java/org/xvm/compiler/Lexer.java b/javatools/src/main/java/org/xvm/compiler/Lexer.java
index 89cbfb0ca36..fd3d89fb33b 100644
--- a/javatools/src/main/java/org/xvm/compiler/Lexer.java
+++ b/javatools/src/main/java/org/xvm/compiler/Lexer.java
@@ -19,6 +19,10 @@
import org.xvm.util.PackedInteger;
import org.xvm.util.Severity;
+import static java.util.Objects.requireNonNull;
+
+import static org.xvm.asm.ErrorListener.in;
+
import static org.xvm.util.Handy.hexitValue;
import static org.xvm.util.Handy.isAsciiLetter;
import static org.xvm.util.Handy.isDigit;
@@ -39,16 +43,14 @@ public class Lexer
*
* @param source the source to parse
*/
- public Lexer(Source source, ErrorListener errorListener) {
+ public Lexer(Source source, ErrorListener errs) {
if (source == null) {
throw new IllegalArgumentException("Source required");
}
- if (errorListener == null) {
- throw new IllegalArgumentException("ErrorListener required");
- }
+ requireNonNull(errs, "errs");
m_source = source;
- m_errorListener = errorListener;
+ m_errorListener = errs;
eatWhitespace();
}
@@ -131,6 +133,10 @@ public boolean hasNext() {
@Override
public Token next() {
+ // Valid input must also observe cancellation; no diagnostic may be emitted.
+ if (m_errorListener.isAbortDesired()) {
+ throw new CompilerException("Tokenization aborted");
+ }
if (hasNext()) {
return atoken[iNext++];
}
@@ -231,6 +237,10 @@ public static boolean isValidQualifiedModule(String sName) {
@Override
public Token next() {
+ // Valid input must also observe cancellation; no diagnostic may be emitted.
+ if (m_errorListener.isAbortDesired()) {
+ throw new CompilerException("Tokenization aborted");
+ }
boolean fWhitespaceBefore = m_fWhitespace;
Token token = eatToken();
boolean fWhitespaceAfter = eatWhitespace();
@@ -407,7 +417,7 @@ protected Token eatBinaryLiteral(long lInitPos, boolean fMultiline) {
} else if (ch == '_') {
if (fFirst) {
// it's an error to start with an underscore
- log(Severity.ERROR, ILLEGAL_HEX, new Object[] {String.valueOf(ch)}, lInitPos);
+ log(Severity.ERROR, ILLEGAL_HEX, span(lInitPos), String.valueOf(ch));
}
// ignore the _ (it's used for spacing within the literal)
} else if (!fMultiline) {
@@ -422,7 +432,7 @@ protected Token eatBinaryLiteral(long lInitPos, boolean fMultiline) {
}
} else {
// error
- log(Severity.ERROR, ILLEGAL_HEX, new Object[] {String.valueOf(ch)}, lInitPos);
+ log(Severity.ERROR, ILLEGAL_HEX, span(lInitPos), String.valueOf(ch));
source.rewind();
break;
}
@@ -851,7 +861,7 @@ protected Token eatToken() {
default:
if (!isIdentifierStart(chInit)) {
- log(Severity.ERROR, ILLEGAL_CHAR, new Object[]{quotedChar(chInit)}, lInitPos);
+ log(Severity.ERROR, ILLEGAL_CHAR, span(lInitPos), quotedChar(chInit));
}
// fall through
case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':
@@ -1045,9 +1055,8 @@ protected Token eatToken() {
case LIT_FLOAT:
if (!fFloat) {
// we were expecting an integer
- log(Severity.ERROR, ILLEGAL_NUMBER, new Object[] {
- extractSource(lInitPos, tokNum.getEndPosition())},
- lInitPos);
+ log(Severity.ERROR, ILLEGAL_NUMBER, span(lInitPos),
+ extractSource(lInitPos, tokNum.getEndPosition()));
return tokNum;
}
// fall through
@@ -1836,8 +1845,8 @@ protected PackedInteger eatDigits(boolean fNeg, int radix, int[] digitCount) {
protected Token eatTime(long lInitPos) {
Token tokDate = eatDate(lInitPos, true);
if (!(match('t') || expect('T'))) {
- log(Severity.ERROR, BAD_TIME, new Object[] {tokDate.getValue()},
- tokDate.getStartPosition(), tokDate.getEndPosition());
+ log(Severity.ERROR, BAD_TIME,
+ span(tokDate.getStartPosition(), tokDate.getEndPosition()), tokDate.getValue());
return tokDate;
}
@@ -1887,7 +1896,7 @@ protected Token eatTimeZone(long lInitPos) {
if (nHour >= 0 && nMin >= 0) {
if (nHour > 16 || nMin > 59) {
- log(Severity.ERROR, BAD_TIMEZONE, new Object[] {sZone}, lStart, lEnd);
+ log(Severity.ERROR, BAD_TIMEZONE, span(lStart, lEnd), sZone);
}
}
@@ -1907,7 +1916,7 @@ protected int eatDigits(int digitCount) {
int n = 0;
for (int i = 0; i < digitCount; ++i) {
if (!isNextCharDigit(10)) {
- log(Severity.ERROR, EXPECTED_DIGITS, new Object[] {digitCount, i}, lPosStart);
+ log(Severity.ERROR, EXPECTED_DIGITS, span(lPosStart), digitCount, i);
return -n;
}
@@ -2136,7 +2145,7 @@ protected Token eatDate(long lInitPos, boolean fContinued) {
String sDate = extractSource(lLitPos, lEndPos);
if (nYear >= 0 && nMonth >= 0 && nDay >= 0) {
if (nYear < 1582 || nMonth < 1 || nMonth > 12 || nDay < 1 || nDay > 31) {
- log(Severity.ERROR, BAD_DATE, new Object[] {sDate}, lLitPos, lEndPos);
+ log(Severity.ERROR, BAD_DATE, span(lLitPos, lEndPos), sDate);
}
}
@@ -2196,7 +2205,7 @@ protected Token eatTimeOfDay(long lInitPos, boolean fContinued) {
String sTime = extractSource(lStart, lEnd);
if (nHour >= 0 && nMin >= 0 && nSec >= 0) {
if (nHour > 23 || nMin > 59 || nSec > 59 && !(nHour == 23 && nMin == 59 && nSec == 60)) {
- log(Severity.ERROR, BAD_TIME_OF_DAY, new Object[] {sTime}, lStart, lEnd);
+ log(Severity.ERROR, BAD_TIME_OF_DAY, span(lStart, lEnd), sTime);
}
}
@@ -2343,7 +2352,7 @@ protected Token eatDuration(long lInitPos) {
String sDuration = extractSource(lStart, lEnd).toUpperCase();
if (fErr) {
- log(Severity.ERROR, BAD_DURATION, new Object[] {sDuration}, lStart, lEnd);
+ log(Severity.ERROR, BAD_DURATION, span(lStart, lEnd), sDuration);
} else {
peekNotIdentifierOrNumber();
}
@@ -2447,9 +2456,8 @@ protected boolean expect(char ch) {
}
if (chActual != ch) {
- log(Severity.ERROR, EXPECTED_CHAR,
- new Object[] {String.valueOf(ch), String.valueOf(chActual)},
- m_source.getPosition());
+ log(Severity.ERROR, EXPECTED_CHAR, span(m_source.getPosition()),
+ String.valueOf(ch), String.valueOf(chActual));
return false;
}
@@ -2477,7 +2485,7 @@ protected void peekNotIdentifierOrNumber() {
long lEnd = m_source.getPosition();
m_source.rewind();
if (ch >= '0' && ch <= '9' || isIdentifierPart(ch)) {
- log(Severity.ERROR, UNEXPECTED_CHAR, new Object[] {ch}, m_source.getPosition(), lEnd);
+ log(Severity.ERROR, UNEXPECTED_CHAR, span(m_source.getPosition(), lEnd), ch);
}
}
@@ -2507,7 +2515,7 @@ protected void log(Severity severity, String sCode, long lPosPrev, long lPosStar
m_source.next();
}
- log(severity, sCode, new Object[]{extractSource(lPosPrev, lPosEnd)}, lPosStart, lPosEnd);
+ log(severity, sCode, span(lPosStart, lPosEnd), extractSource(lPosPrev, lPosEnd));
}
/**
@@ -2521,11 +2529,42 @@ protected void log(Severity severity, String sCode, Object[] aoParam, long lPosS
* Log an error.
*/
protected void log(Severity severity, String sCode, Object[] aoParam, long lPosStart, long lPosEnd) {
- if (m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd)) {
+ log(severity, sCode, span(lPosStart, lPosEnd), aoParam);
+ }
+
+ /**
+ * Log an error, with the message parameters as a trailing varargs.
+ *
+ * The location is a {@link ErrorListener.Site} rather than a pair of positions so that the
+ * parameters can be the tail; taking them as an array in the middle is what forces a call site
+ * to build one by hand.
+ */
+ protected void log(Severity severity, String sCode, ErrorListener.Site site, Object... aoParam) {
+ m_errorListener.log(severity, sCode, site, aoParam);
+ if (m_errorListener.isAbortDesired()) {
throw new CompilerException("error list is full: " + m_errorListener);
}
}
+ /**
+ * @param lPosStart where the diagnostic starts
+ *
+ * @return the span from there to the current position in the script
+ */
+ protected ErrorListener.Site span(long lPosStart) {
+ return in(m_source, lPosStart, m_source.getPosition());
+ }
+
+ /**
+ * @param lPosStart where the diagnostic starts
+ * @param lPosEnd where it ends
+ *
+ * @return that span of the script
+ */
+ protected ErrorListener.Site span(long lPosStart, long lPosEnd) {
+ return in(m_source, lPosStart, lPosEnd);
+ }
+
// ----- helper methods ------------------------------------------------------------------------
/**
diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java
index 20fdd3b0e89..b9fe2e06d16 100644
--- a/javatools/src/main/java/org/xvm/compiler/Parser.java
+++ b/javatools/src/main/java/org/xvm/compiler/Parser.java
@@ -1,5 +1,7 @@
package org.xvm.compiler;
+import static java.util.Objects.requireNonNull;
+
import java.io.File;
import java.io.IOException;
@@ -9,15 +11,27 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
import org.xvm.asm.ErrorList;
import org.xvm.asm.ErrorListener;
+import org.xvm.asm.Reporting;
import org.xvm.asm.Version;
import org.xvm.compiler.Token.Id;
import org.xvm.compiler.ast.*;
+import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement;
+import org.xvm.compiler.ast.partial.IncompleteExpression;
+import org.xvm.compiler.ast.partial.IncompleteLocalDeclaration;
+import org.xvm.compiler.ast.partial.IncompleteStatement;
+import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement;
+import org.xvm.compiler.ast.partial.PartialSyntax;
import org.xvm.tool.ResourceDir;
@@ -25,6 +39,11 @@
import org.xvm.util.ListMap;
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.in;
+
+import static org.xvm.asm.ErrorListener.Silence.DISCARD;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* A recursive descent parser for Ecstasy source code.
*/
@@ -38,7 +57,42 @@ public class Parser {
* @param listener the error listener
*/
public Parser(Source source, ErrorListener listener) {
- this(source, listener, null);
+ this(source, listener, null, false, NO_CURSOR);
+ }
+
+ /**
+ * Create a parser that can retain a trailing incomplete expression statement for explicit
+ * partial analysis. Errors are still reported; this does not make the source compilable.
+ *
+ * @param source the source snapshot to parse
+ * @param listener the diagnostic listener and abort policy for this parse
+ *
+ * @return a parser that retains only the explicitly supported incomplete syntax boundaries
+ */
+ public static Parser forPartialAnalysis(Source source, ErrorListener listener) {
+ return new Parser(source, listener, null, true, NO_CURSOR);
+ }
+
+ /**
+ * Retain a supported incomplete statement at a cursor, without truncating the source.
+ * The cursor uses a position token obtained from {@link Source#getPosition()} for this text.
+ * Supports missing member/call suffixes, typed member prefixes and calls before an existing
+ * closing parenthesis at statement and final call-argument boundaries. Cursor-selected syntax
+ * need not be malformed; it is inspected as a prefix, without validating the selected operation.
+ * Missing call/group/index closers around that operation retain their enclosing syntax at a
+ * statement or outer-delimiter boundary; a cursor at EOF also retains unfinished blocks.
+ *
+ * @param source the source snapshot to parse
+ * @param cursor the selected position in the owning Source snapshot
+ * @param listener the diagnostic listener and abort policy for this parse
+ *
+ * @return a parser that retains only the explicitly supported incomplete syntax boundaries
+ */
+ public static Parser forPartialAnalysis(Source source, long cursor, ErrorListener listener) {
+ if (cursor == NO_CURSOR) {
+ throw new IllegalArgumentException("A source cursor position is required");
+ }
+ return new Parser(source, listener, null, true, cursor);
}
/**
@@ -48,30 +102,34 @@ public Parser(Source source, ErrorListener listener) {
* @param atoken the tokens to parse
*/
protected Parser(Parser parent, Token[] atoken) {
- this(parent.m_source, parent.m_errorListener, parent.m_lexer.createLexer(atoken));
+ this(parent.m_source, parent.f_errs.get(), parent.m_lexer.createLexer(atoken), false,
+ NO_CURSOR);
}
- private Parser(Source source, ErrorListener errs, Lexer lexer) {
+ private Parser(Source source, ErrorListener errs, Lexer lexer, boolean fPartialAnalysis,
+ long cursor) {
if (source == null) {
throw new IllegalArgumentException("Source required");
}
- if (errs == null) {
- throw new IllegalArgumentException("ErrorListener required");
- }
-
- m_source = source;
- m_errorListener = errs;
+ m_source = source;
+ f_errs = new Reporting(requireNonNull(errs, "errs"));
+ // Lexical reports and cancellation follow the active speculative attempt.
+ ErrorListener lexicalReports = new ErrorListener() {
+ @Override
+ public void log(ErrorInfo error) {
+ f_errs.get().log(error);
+ }
- // we need to route lexical reports through the active lookahead attempt, if any
- ErrorListener listenerWrapper = err -> {
- if (m_lookAhead == null) {
- return m_errorListener.log(err);
+ @Override
+ public boolean isAbortDesired() {
+ return f_errs.get().isAbortDesired();
}
- m_lookAhead.log(err);
- return false;
};
- m_lexer = lexer == null ? new Lexer(source, listenerWrapper) : lexer;
+ m_lexer = lexer == null ? new Lexer(source, lexicalReports) : lexer;
+
+ f_partialAnalysis = fPartialAnalysis;
+ f_cursor = cursor;
// prime the token stream
next();
@@ -87,7 +145,10 @@ private Parser(Source source, ErrorListener errs, Lexer lexer) {
* AliasStatements-opt TypeDeclaration
* }
*
- * @return the top level type declaration
+ * Recover at statement/declaration boundaries when possible. Recovered syntax is incomplete;
+ * callers must check their listener for errors before entering semantic compilation.
+ *
+ * @return the top level syntax, possibly omitting malformed statements or declarations
*
* @throws CompilerException if a parsing error occurs while parsing the
* source code that forces the parser to abandon its progress before
@@ -100,9 +161,13 @@ public StatementBlock parseSource() {
// during parsing
m_fDone = true;
+ long lStart = peek().getStartPosition();
List stmts = parseTypeCompositionComponents(null, new ArrayList<>(), true);
- m_root = new StatementBlock(stmts, m_source, stmts.getFirst().getStartPosition(),
- stmts.getLast().getEndPosition());
+ if (stmts.isEmpty() && !f_errs.get().hasSeriousErrors()) {
+ log(Severity.ERROR, NO_TYPE_FOUND, lStart, m_source.getPosition());
+ }
+ m_root = new StatementBlock(stmts, m_source, lStart,
+ stmts.isEmpty() ? m_source.getPosition() : stmts.getLast().getEndPosition());
// there shouldn't be more in the file; (note that a zero-length token doesn't count,
// since it is probably the synthetic closing '}')
@@ -139,47 +204,58 @@ public Map parseImplicits() {
}
/**
- * Quick-scan the file for the module name.
+ * Quick-scan the source for the name of the module it declares, ignoring everything else.
*
- * @return the module name
+ * Parser diagnostics from the scan use a {@link ErrorListener.Silence#DISCARD} silence.
+ * The module name itself is parsed against a separate listener so a malformed name is not
+ * accepted. Lexical diagnostics
+ * still use the lexer's original listener; callers performing only this scan should supply
+ * a discard listener when constructing the parser.
+ *
+ * @return the module's dotted name, or null if the source declares no module - or declares
+ * one whose name does not parse
*/
public String parseModuleNameIgnoreEverythingElse() {
- ErrorListener errsPrev = m_errorListener;
- try {
- m_errorListener = ErrorListener.BLACKHOLE;
-
- Loop: while (!eof()) {
- if (match(Id.MODULE) != null) {
- if (!eof()) {
- m_errorListener = new ErrorList(1);
- List tokens = parseQualifiedName();
- if (!m_errorListener.hasSeriousErrors()) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0, c = tokens.size(); i < c; ++i) {
- if (i > 0) {
- sb.append('.');
- }
- sb.append(tokens.get(i).getValueText());
- }
- return sb.toString();
- }
+ try (Reporting.Scope quiet = reportingTo(silent(DISCARD))) {
+ while (!eof()) {
+ if (match(Id.MODULE) == null) {
+ // not a module declaration; skip it, and give up at the first body we meet,
+ // because a module declaration cannot follow one
+ Id id = current().getId();
+ if (id == Id.L_CURLY || id == Id.R_CURLY) {
+ return null;
}
- } else {
- switch (current().getId()) {
- case L_CURLY:
- case R_CURLY:
- break Loop;
+ } else if (!eof()) {
+ String sName = parseModuleName();
+ if (sName != null) {
+ return sName;
}
}
}
- } catch (RuntimeException ignore) {
- } finally {
- m_errorListener = errsPrev;
+ } catch (RuntimeException _) {
+ // a quick scan answers or gives up without propagating parsing failures
}
return null;
}
+ /**
+ * Parse the qualified name of a module that has just been announced by its keyword.
+ *
+ * @return the dotted name, or null if it did not parse cleanly
+ */
+ private String parseModuleName() {
+ ErrorList errs = new ErrorList(ErrorList.FIRST_ERROR);
+ List tokens;
+ try (Reporting.Scope reporting = reportingTo(errs)) {
+ tokens = parseQualifiedName();
+ }
+
+ return errs.hasSeriousErrors()
+ ? null
+ : tokens.stream().map(Token::getValueText).collect(Collectors.joining("."));
+ }
+
/**
* As part of the runtime (NOT compile-time), parse the name of the class that is in the source.
*
@@ -351,14 +427,20 @@ TypeCompositionStatement parseTypeDeclarationStatementAfterModifiers(long lStart
name = qualified.get(0);
}
- // optional type parameters
- List typeParams = parseTypeParameterList(false);
-
- // optional constructor definition
- List constructorParams = parseParameterList(false);
-
- // sequence of compositions
- List compositions = parseCompositions();
+ Mark header = mark();
+ List typeParams = null;
+ List constructorParams;
+ List compositions;
+ try {
+ typeParams = parseTypeParameterList(false, true);
+ constructorParams = parseParameterList(false);
+ compositions = parseCompositions(true);
+ } catch (IncompleteHeader error) {
+ return retainTypeDeclaration(header, lStartPos, category, name, qualified,
+ List.of(error.site), error.withFormals(typeParams));
+ } catch (CompilerException error) {
+ return retainTypeDeclaration(header, lStartPos, category, name, qualified, List.of(), error);
+ }
// TypeCompositionBody
StatementBlock body = null;
@@ -381,7 +463,7 @@ TypeCompositionStatement parseTypeDeclarationStatementAfterModifiers(long lStart
*
* @return a list of compositions
*/
- List parseCompositions() {
+ List parseCompositions(boolean header) {
List compositions = new ArrayList<>();
while (true) {
// the keywords below require "match()" to extract them, because they are context
@@ -389,18 +471,19 @@ List parseCompositions() {
Token keyword;
if ((keyword = match(Id.EXTENDS)) != null) {
do {
- TypeExpression type = parseExtendedTypeExpression();
+ TypeExpression type = header ? parseDeclarationType(true) : parseExtendedTypeExpression();
List args = parseArgumentList(false, false, false);
compositions.add(new CompositionNode.Extends(null, keyword, type, args,
prev().getEndPosition()));
} while (match(Id.COMMA) != null);
} else if ((keyword = match(Id.IMPLEMENTS)) != null) {
do {
- compositions.add(new CompositionNode.Implements(null, keyword, parseExtendedTypeExpression()));
+ compositions.add(new CompositionNode.Implements(null, keyword,
+ header ? parseDeclarationType(true) : parseExtendedTypeExpression()));
} while (match(Id.COMMA) != null);
} else if ((keyword = match(Id.DELEGATES)) != null) {
do {
- TypeExpression type = parseExtendedTypeExpression();
+ TypeExpression type = header ? parseDeclarationType(true) : parseExtendedTypeExpression();
expect(Id.L_PAREN);
Expression expr = parseExpression();
Token tokEnd = expect(Id.R_PAREN);
@@ -416,7 +499,7 @@ List parseCompositions() {
TypeExpression type = null;
List constraints = null;
if (match(Id.CONDITIONAL) == null) {
- type = parseTypeExpression();
+ type = header ? parseDeclarationType() : parseTypeExpression();
} else {
do {
// parse the type parameter list e.g. "",
@@ -450,7 +533,8 @@ List parseCompositions() {
compositions.add(new CompositionNode.Incorporates(null, keyword, type, args, constraints));
} while (match(Id.COMMA) != null);
} else if ((keyword = match(Id.INTO)) != null) {
- compositions.add(new CompositionNode.Into(null, keyword, parseExtendedTypeExpression()));
+ compositions.add(new CompositionNode.Into(null, keyword,
+ header ? parseDeclarationType(true) : parseExtendedTypeExpression()));
} else { // not context-sensitive keywords
switch (peek().getId()) {
case IMPORT: {
@@ -582,58 +666,62 @@ StatementBlock parseTypeCompositionBody(Token category) {
List parseTypeCompositionComponents(Expression exprCondition, List stmts,
boolean fFileLevel) {
boolean fFoundType = false;
- while (match(Id.R_CURLY) == null) {
- Statement stmt;
- switch (peek().getId()) {
- case IMPORT:
- stmt = parseImportStatement(exprCondition);
- break;
+ while (!atBlockEnd(fFileLevel)) {
+ Mark statementStart = mark();
+ try {
+ Statement stmt;
+ switch (peek().getId()) {
+ case IMPORT:
+ stmt = parseImportStatement(exprCondition);
+ break;
- case TYPEDEF:
- stmt = parseTypeDefStatement(exprCondition, null);
- break;
+ case TYPEDEF:
+ stmt = parseTypeDefStatement(exprCondition, null);
+ break;
- default: {
- Token start = peek();
-
- stmt = parseTypeCompositionComponent(exprCondition, false);
- fFoundType = true;
-
- if (fFileLevel) {
- // module cannot have any statements before it in the source
- if (stmt instanceof TypeCompositionStatement stmtType) {
- if (stmtType.getCategory().getId() == Id.MODULE && !stmts.isEmpty()) {
- if (stmts.stream().allMatch(ImportStatement.class::isInstance)) {
- // transfer the imports from outside the module body to inside
- // the module body
- stmtType.ensureBody().getStatements().addAll(0, stmts);
- stmts.clear();
- } else {
- log(Severity.ERROR, MODULE_NOT_ROOT, start.getStartPosition(),
- start.getEndPosition());
+ default: {
+ Token start = peek();
+
+ stmt = parseTypeCompositionComponent(exprCondition, false);
+ fFoundType = true;
+
+ if (fFileLevel) {
+ // module cannot have any statements before it in the source
+ if (stmt instanceof TypeCompositionStatement stmtType) {
+ if (stmtType.getCategory().getId() == Id.MODULE && !stmts.isEmpty()) {
+ if (stmts.stream().allMatch(ImportStatement.class::isInstance)) {
+ // transfer the imports from outside the module body to inside
+ // the module body
+ stmtType.ensureBody().getStatements().addAll(0, stmts);
+ stmts.clear();
+ } else {
+ log(Severity.ERROR, MODULE_NOT_ROOT, start.getStartPosition(),
+ start.getEndPosition());
+ }
}
+ } else {
+ log(Severity.ERROR, NO_TYPE_FOUND, start.getStartPosition(),
+ start.getEndPosition());
}
- } else {
- log(Severity.ERROR, NO_TYPE_FOUND, start.getStartPosition(),
- start.getEndPosition());
}
+ break;
+ }
}
- break;
- }
- }
- stmts.add(stmt);
- if (stmt instanceof MethodDeclarationStatement) {
- MethodDeclarationStatement stmtFinally =
- ((MethodDeclarationStatement) stmt).getConstructorFinally();
- if (stmtFinally != null) {
- stmts.add(stmtFinally);
+ stmts.add(stmt);
+ if (stmt instanceof MethodDeclarationStatement method) {
+ MethodDeclarationStatement stmtFinally = method.getConstructorFinally();
+ if (stmtFinally != null) {
+ stmts.add(stmtFinally);
+ }
}
- }
- if (fFileLevel && fFoundType) {
- // at the file level, there is nothing after the outermost type's conclusion
- break;
+ if (fFileLevel && fFoundType) {
+ // at the file level, there is nothing after the outermost type's conclusion
+ break;
+ }
+ } catch (CompilerException e) {
+ recoverStatement(statementStart, e);
}
}
@@ -719,7 +807,7 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
// evaluate annotations
if (annotations != null) {
for (AnnotationExpression annotation : annotations) {
- annotation.log(m_errorListener, Severity.ERROR, Compiler.ANNOTATION_UNEXPECTED);
+ annotation.log(f_errs.get(), Severity.ERROR, Compiler.ANNOTATION_UNEXPECTED);
}
}
@@ -737,7 +825,8 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
}
// fall through
default:
- modifier.log(m_errorListener, m_source, Severity.ERROR, Compiler.KEYWORD_UNEXPECTED, modifier.getValueText());
+ modifier.log(f_errs.get(), m_source, Severity.ERROR, Compiler.KEYWORD_UNEXPECTED,
+ modifier.getValueText());
break;
}
}
@@ -749,12 +838,19 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
case CONDITIONAL:
case VOID: {
// it's definitely a method or a function
- List typeVars = peek(Id.COMP_LT) ? parseTypeParameterList(true) : null;
- Token conditional = match(Id.CONDITIONAL);
- List returns = parseReturnList();
- Token name = expect(Id.IDENTIFIER);
- return parseMethodDeclarationAfterName(lStartPos, exprCondition, doc,
- modifiers, annotations, typeVars, conditional, returns, name);
+ Mark header = mark();
+ List typeVars = null;
+ try {
+ typeVars = peek(Id.COMP_LT) ? parseTypeParameterList(true, true) : null;
+ Token conditional = match(Id.CONDITIONAL);
+ List returns = parseDeclarationReturns();
+ Token name = expect(Id.IDENTIFIER);
+ return parseMethodDeclarationAfterName(lStartPos, exprCondition, doc,
+ modifiers, annotations, typeVars, conditional, returns, name);
+ } catch (IncompleteHeader error) {
+ return retainDeclaration(header, lStartPos, null, IncompleteDeclarationStatement.Kind.METHOD,
+ List.of(error.site), error.withFormals(typeVars));
+ }
}
case CONSTRUCT: {
@@ -779,6 +875,22 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
}
case L_PAREN: {
+ if (!fInMethod && f_cursor != NO_CURSOR) {
+ // Outside a method this is declaration syntax, not a tuple value. Probe the
+ // type grammar without letting a discarded interpretation publish diagnostics.
+ Mark header = mark();
+ var branch = f_errs.get().branch(null);
+ try (var ignored = reportingTo(branch)) {
+ parseDeclarationReturns();
+ } catch (IncompleteHeader error) {
+ branch.merge();
+ return retainDeclaration(header, lStartPos, null, IncompleteDeclarationStatement.Kind.METHOD,
+ List.of(error.site), error);
+ } catch (CompilerException ignored) {
+ // The ordinary declaration grammar below owns non-cursor failures.
+ }
+ restore(header);
+ }
// it's a property or a method, but the property type is parenthesized (odd!) or
// the method return types (with optional names) are parenthesized (multi-return),
// which means that to know it's a method, we have to find one of:
@@ -847,7 +959,7 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
if (expr.isLValueSyntax()) {
listLVals.add(expr);
} else {
- expr.log(m_errorListener, Severity.ERROR, NOT_ASSIGNABLE);
+ expr.log(f_errs.get(), Severity.ERROR, NOT_ASSIGNABLE);
}
} else {
listLVals.add(new VariableDeclarationStatement(
@@ -911,6 +1023,14 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
throw new CompilerException("var or val keyword outside of method");
}
+ var missingName = declarationNameSlot(new VariableTypeExpression(tokType));
+ if (missingName.isPresent() && peek(Id.ASN)) {
+ current();
+ Expression initializer = parseExpression();
+ expect(Id.SEMICOLON);
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ return new IncompleteLocalDeclaration(missingName.orElseThrow(), initializer, prev().getEndPosition());
+ }
Token tokName = matchNameOrAny();
if (tokName == null) {
// var and val are not reserved keywords; they are context sensitive types
@@ -918,8 +1038,10 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
} else {
VariableTypeExpression typeDecl = new VariableTypeExpression(tokType);
VariableDeclarationStatement stmtDecl = new VariableDeclarationStatement(typeDecl, tokName, false);
- AssignmentStatement stmtAsn = new AssignmentStatement(stmtDecl, match(Id.ASN), parseExpression());
- expect(Id.SEMICOLON);
+ Token op = match(Id.ASN);
+ Expression value = parsePartialValue();
+ AssignmentStatement stmtAsn = new AssignmentStatement(stmtDecl, op, value);
+ finishValueStatement(value);
return stmtAsn;
}
}
@@ -934,7 +1056,15 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
type = expr.toTypeExpression();
} else {
- type = parseTypeExpression();
+ Mark header = mark();
+ try {
+ type = parseDeclarationType();
+ } catch (IncompleteHeader e) {
+ Token name = matchNameOrAny();
+ var kind = peek(Id.L_PAREN) ? IncompleteDeclarationStatement.Kind.METHOD
+ : IncompleteDeclarationStatement.Kind.PROPERTY;
+ return retainDeclaration(header, lStartPos, name, kind, List.of(e.site), e);
+ }
}
if (type instanceof BadTypeExpression) {
@@ -942,6 +1072,14 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet
}
// it's a constant, property, or method
+ if (!fInMethod) {
+ var missingName = declarationNameSlot(type);
+ if (missingName.isPresent()) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ return retainDeclaration(mark(), lStartPos, null, IncompleteDeclarationStatement.Kind.PROPERTY,
+ List.of(missingName.orElseThrow()), new CompilerException("Missing declaration name"));
+ }
+ }
Token name = expectNameOrAny();
if (peek(Id.COMP_LT) || peek(Id.L_PAREN)) {
// '<' indicates redundant return type list
@@ -992,15 +1130,25 @@ Statement parsePossibleExpressionOrAssignmentStatement(Expression expr) {
case COND_OR_ASN:
case COND_NN_ASN:
case COND_ELSE_ASN: {
- AssignmentStatement stmt = new AssignmentStatement(expr, current(), parseExpression());
- expect(Id.SEMICOLON);
+ Token op = current();
+ Expression value = op.getId() == Id.ASN ? parsePartialValue() : parseExpression();
+ AssignmentStatement stmt = new AssignmentStatement(expr, op, value);
+ finishValueStatement(value);
return stmt;
}
}
if (isMissingStatementSemicolon()) {
long lPos = expr.getEndPosition();
- log(Severity.ERROR, MISSING_SEMICOLON, lPos, lPos);
+ if (!containsCursorHole(expr)) {
+ log(Severity.ERROR, MISSING_SEMICOLON, lPos, lPos);
+ }
+ return new ExpressionStatement(expr);
+ }
+
+ // what is being typed at the end of a line ends its statement there; the later line is the next one
+ if (containsCursorHole(expr) && !eof()
+ && Source.calculateLine(peek().getStartPosition()) > Source.calculateLine(expr.getEndPosition())) {
return new ExpressionStatement(expr);
}
@@ -1054,9 +1202,9 @@ Statement parseVariableDeclarationAfterName(List annotatio
Expression value = null;
Token op = match(Id.ASN);
if (op != null) {
- value = parseExpression();
+ value = parsePartialValue();
}
- expect(Id.SEMICOLON);
+ finishValueStatement(value);
// apply any annotations to the variable type; "@A @B @C T" is "@A of (@B of (@C of T))"
if (annotations != null) {
@@ -1082,15 +1230,27 @@ Statement parseVariableDeclarationAfterName(List annotatio
* StatementBlock
* }
*
- * @return a MethodDeclarationStatement
+ * @return a method declaration, or retained incomplete header syntax after a parse error
*/
- MethodDeclarationStatement parseMethodDeclarationAfterName(long lStartPos, Expression exprCondition,
+ Statement parseMethodDeclarationAfterName(long lStartPos, Expression exprCondition,
Token doc, List modifiers, List annotations,
List typeVars, Token conditional, List returns, Token name) {
- List redundantReturns = parseTypeParameterTypeList(false, true);
- List params = parseParameterList(true);
- long lEndPos = prev().getEndPosition();
- StatementBlock body;
+ Mark header = mark();
+ List redundantReturns;
+ List params;
+ try {
+ redundantReturns = parseTypeParameterTypeList(false, true);
+ params = parseParameterList(true);
+ } catch (IncompleteHeader e) {
+ // This incomplete method has no registered type parameters or own scope yet.
+ return retainDeclaration(header, lStartPos, name, IncompleteDeclarationStatement.Kind.METHOD,
+ List.of(e.site), e.withFormals(typeVars));
+ } catch (CompilerException e) {
+ return retainDeclaration(header, lStartPos, name, IncompleteDeclarationStatement.Kind.METHOD,
+ List.of(), e);
+ }
+ long lEndPos = prev().getEndPosition();
+ StatementBlock body;
switch (peek().getId()) {
case SEMICOLON:
body = null;
@@ -1099,10 +1259,10 @@ MethodDeclarationStatement parseMethodDeclarationAfterName(long lStartPos, Expre
case ASN:
Token eq = expect(Id.ASN);
Expression expr = parseExpression();
- Token semi = expect(Id.SEMICOLON);
+ finishValueStatement(expr);
ReturnStatement stmt = new ReturnStatement(eq, expr);
body = new StatementBlock(Arrays.asList(stmt), stmt.getStartPosition(),
- semi.getEndPosition());
+ prev().getEndPosition());
break;
default:
body = parseStatementBlock();
@@ -1179,7 +1339,7 @@ PropertyDeclarationStatement parsePropertyDeclarationFinish(long lStartPos,
// "=" Expression ";"
value = parseExpression();
lEndPos = value.getEndPosition();
- expect(Id.SEMICOLON);
+ finishValueStatement(value);
} else if (body == null || fNeedsSemi) {
expect(Id.SEMICOLON);
}
@@ -1196,13 +1356,47 @@ PropertyDeclarationStatement parsePropertyDeclarationFinish(long lStartPos,
StatementBlock parseStatementBlock() {
Token tokStart = expect(Id.L_CURLY);
List stmts = new ArrayList<>();
- while (match(Id.R_CURLY) == null) {
- stmts.add(parseStatement());
+ retainEmptyCursor(stmts);
+ while (!atBlockEnd(false)) {
+ Mark statementStart = mark();
+ long lStart = peek().getStartPosition();
+ try {
+ stmts.add(parseStatement());
+ retainEmptyCursor(stmts);
+ } catch (IncompleteSyntax e) {
+ // Do not reinterpret a return, assignment, condition or nested call as a
+ // standalone expression. Only the intact statement prefix is supported.
+ if (e.statement.getStartPosition() == lStart) {
+ stmts.add(e.statement);
+ // The cursor boundary has already consumed the intact prefix. Keep the
+ // enclosing brace and following declarations instead of scanning the open
+ // call again as an unmatched parenthesis.
+ if (f_cursor != NO_CURSOR) {
+ match(Id.SEMICOLON);
+ continue;
+ }
+ }
+ recoverStatement(statementStart, e);
+ } catch (CompilerException e) {
+ recoverStatement(statementStart, e);
+ }
}
return new StatementBlock(stmts, tokStart.getStartPosition(), prev().getEndPosition());
}
+ /** A zero-width insertion anchor after an intact statement or opening brace. */
+ private void retainEmptyCursor(List statements) {
+ if (f_cursor != NO_CURSOR && (prev().getId() == Id.L_CURLY
+ || prev().getId() == Id.R_CURLY || prev().getId() == Id.SEMICOLON)
+ && endsAt(f_cursor)) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ statements.add(new IncompleteStatement(
+ new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""), f_cursor, INCOMPLETE_EXPRESSION));
+ match(Id.SEMICOLON);
+ }
+ }
+
/**
* Parse an Ecstasy statement.
*
@@ -1706,16 +1900,16 @@ AstNode parseCondition(boolean fNegated) {
// test for a negated conditional assignment
Token tokNot = null;
AssignmentStatement stmtAsn = null;
- try (SafeLookAhead attempt = new SafeLookAhead()) {
+ try (Attempt attempt = attempt()) {
tokNot = expect(Id.NOT);
if (match(Id.L_PAREN) != null) {
AstNode stmtPeek = parseCondition(true);
if (attempt.isClean() && stmtPeek instanceof AssignmentStatement) {
stmtAsn = (AssignmentStatement) stmtPeek;
- attempt.keepResults();
+ attempt.keep();
}
}
- } catch (CompilerException ignore) {}
+ } catch (CompilerException _) {}
if (stmtAsn != null) {
stmtAsn.negate(tokNot, expect(Id.R_PAREN));
@@ -1839,8 +2033,18 @@ ReturnStatement parseReturnStatement() {
// note: it is possible that the expression list is parenthesized, in which case it will be
// parsed as a single expression (a tuple literal), and the compiler will have to
// it out later
- List exprs = parseExpressionList();
- expect(Id.SEMICOLON);
+ long start = peek().getStartPosition();
+ List exprs;
+ try {
+ exprs = parseExpressionList();
+ } catch (IncompleteSyntax e) {
+ exprs = List.of(retainPartialValue(start, e));
+ }
+ if (exprs.size() == 1) {
+ finishValueStatement(exprs.getFirst());
+ } else {
+ expect(Id.SEMICOLON);
+ }
return new ReturnStatement(keyword, exprs);
}
@@ -2452,7 +2656,7 @@ AssignmentStatement parseVariableInitializer(boolean fAllowExpr) {
*/
Expression parseLinkerCondition() {
Expression expr = parseExpression();
- expr.validateCondition(m_errorListener);
+ expr.validateCondition(f_errs.get());
return expr;
}
@@ -3039,6 +3243,18 @@ private Expression parsePrefixExpressionBody(boolean fExtended) {
* @return an expression
*/
Expression parsePostfixExpression(boolean fExtended) {
+ try {
+ return parsePostfixValue(fExtended);
+ } catch (IncompleteSyntax e) {
+ if (f_cursor == NO_CURSOR) {
+ throw e;
+ }
+ return new IncompleteExpression(e.statement);
+ }
+ }
+
+ /** Retain a cursor hole as syntax so enclosing operators establish their real contexts. */
+ private Expression parsePostfixValue(boolean fExtended) {
Expression expr = parsePrimaryExpression(fExtended);
while (true) {
switch (peek().getId()) {
@@ -3060,7 +3276,10 @@ Expression parsePostfixExpression(boolean fExtended) {
break;
case DOT: {
- expect(Id.DOT);
+ Token dot = expect(Id.DOT);
+ if (endsAt(f_cursor)) {
+ throw incomplete(expr, dot, List.of(), List.of());
+ }
switch (peek().getId()) {
case NEW: {
expr = parseNewExpression(expr);
@@ -3090,21 +3309,33 @@ Expression parsePostfixExpression(boolean fExtended) {
name = expect(Id.IDENTIFIER);
}
}
+ // a cursor at the name's start, right after the dot, completes the member too, with
+ // nothing of it typed
+ if (noDeRef == null && name.getId() == Id.IDENTIFIER
+ && name.getStartPosition() <= f_cursor && f_cursor <= name.getEndPosition()
+ && endsAt(name.getEndPosition(), true)) {
+ var hole = incomplete(expr, dot, name);
+ if (peek(Id.L_PAREN) || peek(Id.ASYNC_PAREN)) {
+ expr = new IncompleteExpression(hole.statement);
+ break;
+ }
+ throw hole;
+ }
long lEndPos = name.getEndPosition();
List params = null;
Token tokPeekLT = match(Id.COMP_LT);
if (tokPeekLT != null) {
putBack(tokPeekLT);
- try (SafeLookAhead attempt = new SafeLookAhead()) {
+ try (Attempt attempt = attempt()) {
params = parseTypeParameterTypeList(true, true);
if (attempt.isClean()) {
- attempt.keepResults();
+ attempt.keep();
lEndPos = prev().getEndPosition();
} else {
params = null;
}
- } catch (CompilerException ignore) {}
+ } catch (CompilerException _) {}
}
if (expr instanceof NamedTypeExpression) {
@@ -3128,8 +3359,7 @@ Expression parsePostfixExpression(boolean fExtended) {
case L_PAREN:
case ASYNC_PAREN:
// ArgumentList
- expr = new InvocationExpression(expr, peek(Id.ASYNC_PAREN),
- parseArgumentList(true, true, false), prev().getEndPosition());
+ expr = parseInvocation(expr);
break;
case L_SQUARE: {
@@ -3150,7 +3380,8 @@ Expression parsePostfixExpression(boolean fExtended) {
} else if (match(Id.COND) == null) {
// "someArray[3]"
List indexes = parseExpressionList();
- expr = new ArrayAccessExpression(expr, indexes, expect(Id.R_SQUARE));
+ expr = new ArrayAccessExpression(expr, indexes,
+ expectPartialClose(Id.R_SQUARE, indexes));
} else {
// "SomeClass[?,?]"
int cExplicitDims = 1;
@@ -3191,7 +3422,10 @@ NewExpression parseNewExpression(Expression left) {
// argument list for a "virtual new"; assume it's a virtual new, and we'll back
// up if we were wrong
Mark mark = mark();
- args = parseArgumentList(true, false, false);
+ args = f_partialAnalysis
+ ? parsePartialArguments(new NewExpression(left, keyword, null, List.of(),
+ -1, null, keyword.getEndPosition()), false)
+ : parseArgumentList(true, false, false);
lEndPos = prev().getEndPosition();
Token.Id idNext = peek().getId();
@@ -3212,7 +3446,19 @@ NewExpression parseNewExpression(Expression left) {
boolean fArray = type instanceof ArrayTypeExpression;
if (fArray) {
if (peek(Id.L_SQUARE)) {
- args = parseArgumentList(true, false, true);
+ try {
+ args = f_partialAnalysis
+ ? parsePartialArguments(new NewExpression(left, keyword, type, List.of(),
+ ((ArrayTypeExpression) type).getDimensions(), null, lEndPos), false)
+ : parseArgumentList(true, false, true);
+ } catch (IncompleteSyntax failure) {
+ // Finish the construction's suffix so a written supplier does not hide
+ // the dimension cursor. This prefix query does not validate the supplier.
+ if (prev().getId() == Id.R_SQUARE) {
+ parseArgumentList(false, false, false);
+ }
+ throw failure;
+ }
dims = args.size();
lEndPos = prev().getEndPosition();
} else {
@@ -3220,7 +3466,10 @@ NewExpression parseNewExpression(Expression left) {
}
// parenthesized arguments after the dims
- List argsTrailing = parseArgumentList(false, false, false);
+ List argsTrailing = f_partialAnalysis && peek(Id.L_PAREN)
+ ? parsePartialArguments(new NewExpression(left, keyword, type, args,
+ dims, null, lEndPos), false)
+ : parseArgumentList(false, false, false);
if (argsTrailing != null) {
if (!argsTrailing.isEmpty()) {
if (args.isEmpty()) {
@@ -3232,7 +3481,22 @@ NewExpression parseNewExpression(Expression left) {
lEndPos = prev().getEndPosition();
}
} else {
- args = parseArgumentList(true, false, false);
+ try {
+ args = f_partialAnalysis
+ ? parsePartialArguments(new NewExpression(left, keyword, type, List.of(),
+ -1, null, type.getEndPosition()), false)
+ : parseArgumentList(true, false, false);
+ } catch (IncompleteSyntax failure) {
+ if (left == null && peek(Id.L_CURLY)) {
+ // Preserve the anonymous body as construction syntax. A cursor probe
+ // must not mistake it for ordinary construction followed by a block.
+ body = parseTypeCompositionBody(keyword);
+ var site = failure.statement;
+ site.replaceChild(site.getTarget(), new NewExpression(left, keyword, type,
+ List.of(), -1, body, prev().getEndPosition()));
+ }
+ throw failure;
+ }
lEndPos = prev().getEndPosition();
}
@@ -3285,6 +3549,11 @@ NewExpression parseNewExpression(Expression left) {
* @return an expression
*/
Expression parsePrimaryExpression(boolean fExtended) {
+ if (f_cursor != NO_CURSOR && endsAt(f_cursor)) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ return new IncompleteExpression(new IncompleteStatement(
+ new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""), f_cursor, INCOMPLETE_EXPRESSION));
+ }
switch (peek().getId()) {
case ANY: {
IgnoredNameExpression exprIgnore = new IgnoredNameExpression(current());
@@ -3406,6 +3675,10 @@ Expression parsePrimaryExpression(boolean fExtended) {
boolean fQuit = false;
Token dot;
while (nameNDR == null && (dot = match(Id.DOT)) != null) {
+ if (fNormal && atLineEnd(f_cursor)) {
+ // the name on the next line starts the next statement, not this access
+ throw incomplete(new NameExpression(left, nameNDR, name, null, lEndPos), dot, List.of(), List.of());
+ }
Token nameNext = match(Id.IDENTIFIER);
if (nameNext == null) {
// it's not a ".name" construct, so the name expression ended BEFORE the dot
@@ -3414,7 +3687,15 @@ Expression parsePrimaryExpression(boolean fExtended) {
break;
}
- left = new NameExpression(left, nameNDR, name, null, lEndPos);
+ left = new NameExpression(left, nameNDR, name, null, lEndPos);
+ if (fNormal && nameNext.getStartPosition() <= f_cursor && f_cursor <= nameNext.getEndPosition()
+ && endsAt(nameNext.getEndPosition(), true)) {
+ var hole = incomplete(left, dot, nameNext);
+ if (peek(Id.L_PAREN) || peek(Id.ASYNC_PAREN)) {
+ return new IncompleteExpression(hole.statement);
+ }
+ throw hole;
+ }
nameNDR = null; // only gets applied once
name = nameNext;
lEndPos = name.getEndPosition();
@@ -3433,6 +3714,12 @@ Expression parsePrimaryExpression(boolean fExtended) {
return expr;
}
+ if (left == null && name.getStartPosition() < f_cursor && f_cursor <= name.getEndPosition()
+ && endsAt(name.getEndPosition())) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ throw new IncompleteSyntax(new IncompleteStatement(name, f_cursor, INCOMPLETE_EXPRESSION));
+ }
+
// test for a non-auto-narrowing modifier ("!")
Token tokNoNarrow = !peek().hasLeadingWhitespace()
? match(Id.NOT)
@@ -3444,17 +3731,17 @@ Expression parsePrimaryExpression(boolean fExtended) {
Token tokPeekLT = match(Id.COMP_LT);
if (tokPeekLT != null) {
putBack(tokPeekLT);
- try (SafeLookAhead attempt = new SafeLookAhead()) {
+ try (Attempt attempt = attempt()) {
params = parseTypeParameterTypeList(true, true);
if (attempt.isClean()
// "index>1" is a comparison and a shift, not a type
&& (peek().getId() != Id.COMP_GT || peek().hasLeadingWhitespace())) {
- attempt.keepResults();
+ attempt.keep();
lEndPos = prev().getEndPosition();
} else {
params = null;
}
- } catch (CompilerException ignore) {}
+ } catch (CompilerException _) {}
}
// test to see if this is a tuple literal of the form "Tuple:(", or some other
@@ -3530,7 +3817,10 @@ Expression parsePrimaryExpression(boolean fExtended) {
// parsed to see if it's followed by a lambda operator
List exprs = new ArrayList<>();
exprs.add(expr);
- while (match(Id.R_PAREN, match(Id.COMMA) == null) == null) {
+ Token tupleClose;
+ while ((tupleClose = match(Id.COMMA) == null
+ ? expectPartialClose(Id.R_PAREN, exprs)
+ : matchPartialClose(Id.R_PAREN, exprs)) == null) {
exprs.add(parseExpression());
}
@@ -3540,7 +3830,7 @@ Expression parsePrimaryExpression(boolean fExtended) {
}
// it's a Tuple literal
- return new TupleExpression(null, exprs, tokLParen.getStartPosition(), prev().getEndPosition());
+ return new TupleExpression(null, exprs, tokLParen.getStartPosition(), tupleClose.getEndPosition());
case R_PAREN:
// this is either a parenthesized expression or a single parameter for a
@@ -3572,9 +3862,8 @@ Expression parsePrimaryExpression(boolean fExtended) {
}
default:
- expect(Id.R_PAREN);
- skipToNextStatement();
- return expr;
+ return new ParenthesizedExpression(expr, tokLParen.getStartPosition(),
+ expectPartialClose(Id.R_PAREN, List.of(expr)).getEndPosition());
}
}
@@ -3678,7 +3967,7 @@ Expression parsePrimaryExpression(boolean fExtended) {
byte[] abData = null;
try {
abData = m_source.includeBinary(sFile);
- } catch (IOException ignore) {}
+ } catch (IOException _) {}
if (abData == null) {
abData = new byte[0];
fErr = true;
@@ -3689,7 +3978,7 @@ Expression parsePrimaryExpression(boolean fExtended) {
try {
Source source = m_source.includeString(sFile);
sData = source == null ? null : source.toRawString();
- } catch (IOException ignore) {}
+ } catch (IOException _) {}
if (sData == null) {
sData = "";
fErr = true;
@@ -4058,12 +4347,13 @@ Expression parseComplexLiteral(TypeExpression type) {
if (peek().getId() == Id.ASN) {
List keys = new ArrayList<>();
List values = new ArrayList<>();
- while (match(Id.R_SQUARE) == null) {
+ Token close;
+ while ((close = match(Id.R_SQUARE)) == null) {
keys.add(keys.isEmpty() ? expr : parseExpression());
expect(Id.ASN);
values.add(parseExpression());
if (match(Id.COMMA) == null) {
- expect(Id.R_SQUARE);
+ close = expectPartialClose(Id.R_SQUARE, values);
break;
}
}
@@ -4072,12 +4362,12 @@ Expression parseComplexLiteral(TypeExpression type) {
Token tokName = new Token(ofMap, ofMap, Id.IDENTIFIER, "Map");
TypeExpression exprType = new NamedTypeExpression(null,
Collections.singletonList(tokName), null, null, null, ofMap);
- return new MapExpression(exprType, keys, values, prev().getEndPosition());
+ return new MapExpression(exprType, keys, values, close.getEndPosition());
}
}
- expect(Id.R_SQUARE);
- break;
+ return new ListExpression(type, exprs, lStartPos,
+ expectPartialClose(Id.R_SQUARE, exprs).getEndPosition());
}
}
return new ListExpression(type, exprs, lStartPos, prev().getEndPosition());
@@ -4106,8 +4396,8 @@ Expression parseComplexLiteral(TypeExpression type) {
expect(Id.ASN);
values.add(parseExpression());
if (match(Id.COMMA) == null) {
- expect(Id.R_SQUARE);
- break;
+ return new MapExpression(type, keys, values,
+ expectPartialClose(Id.R_SQUARE, values).getEndPosition());
}
}
return new MapExpression(type, keys, values, prev().getEndPosition());
@@ -4115,17 +4405,16 @@ Expression parseComplexLiteral(TypeExpression type) {
case "Tuple": {
expect(Id.L_PAREN);
- List exprs = null;
- while (match(Id.R_PAREN) == null) {
- if (exprs == null) {
- exprs = new ArrayList<>();
- } else {
+ List exprs = new ArrayList<>();
+ Token close;
+ while ((close = matchPartialClose(Id.R_PAREN, exprs)) == null) {
+ if (!exprs.isEmpty()) {
expect(Id.COMMA);
}
exprs.add(parseExpression());
}
return new TupleExpression(type, exprs, type.getStartPosition(),
- prev().getEndPosition());
+ close.getEndPosition());
}
case "Path":
@@ -4210,11 +4499,16 @@ public TypeExpression parseExtendedTypeExpression() {
* @return a type expression
*/
TypeExpression parseIntersectingTypeExpression(boolean fExtended) {
- TypeExpression expr = parseUnionedTypeExpression(fExtended);
+ return parseIntersectingTypeExpression(fExtended, false);
+ }
+
+ /** Pass header recovery explicitly through the type grammar; other type contexts stay strict. */
+ private TypeExpression parseIntersectingTypeExpression(boolean fExtended, boolean fHeader) {
+ TypeExpression expr = parseUnionedTypeExpression(fExtended, fHeader);
Token tokOp;
do {
if ((tokOp = match(Id.ADD)) != null || (tokOp = match(Id.SUB)) != null) {
- expr = new BiTypeExpression(expr, tokOp, parseUnionedTypeExpression(fExtended));
+ expr = new BiTypeExpression(expr, tokOp, parseUnionedTypeExpression(fExtended, fHeader));
}
} while (tokOp != null);
return expr;
@@ -4237,10 +4531,10 @@ TypeExpression parseIntersectingTypeExpression(boolean fExtended) {
*
* @return a type expression
*/
- TypeExpression parseUnionedTypeExpression(boolean fExtended) {
- TypeExpression expr = parseNonBiTypeExpression(fExtended);
+ TypeExpression parseUnionedTypeExpression(boolean fExtended, boolean fHeader) {
+ TypeExpression expr = parseNonBiTypeExpression(fExtended, fHeader);
while (peek(Id.BIT_OR)) {
- expr = new BiTypeExpression(expr, expect(Id.BIT_OR), parseNonBiTypeExpression(fExtended));
+ expr = new BiTypeExpression(expr, expect(Id.BIT_OR), parseNonBiTypeExpression(fExtended, fHeader));
}
return expr;
}
@@ -4300,9 +4594,9 @@ TypeExpression parseUnionedTypeExpression(boolean fExtended) {
*
* @return a type expression
*/
- TypeExpression parseNonBiTypeExpression(boolean fExtended) {
+ TypeExpression parseNonBiTypeExpression(boolean fExtended, boolean fHeader) {
try (var ignore = nest()) {
- return parseNonBiTypeExpressionBody(fExtended);
+ return parseNonBiTypeExpressionBody(fExtended, fHeader);
}
}
@@ -4313,22 +4607,23 @@ TypeExpression parseNonBiTypeExpression(boolean fExtended) {
*
* @return a type expression
*/
- private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) {
+ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended, boolean fHeader) {
+ retainEmptyTypeArgument(fHeader);
TypeExpression type;
Token tokAccess = null;
switch (peek().getId()) {
case L_PAREN:
expect(Id.L_PAREN);
- type = parseExtendedTypeExpression();
- expect(Id.R_PAREN);
+ type = parseIntersectingTypeExpression(true, fHeader);
+ expectHeaderTypeClose(Id.R_PAREN, List.of(type), fHeader);
break;
case AT:
- type = parseAnnotatedTypeExpression(fExtended);
+ type = parseAnnotatedTypeExpression(fExtended, fHeader);
break;
case FUNCTION:
- type = parseFunctionTypeExpression();
+ type = parseFunctionTypeExpression(fHeader);
break;
case IMMUTABLE: {
@@ -4348,7 +4643,7 @@ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) {
}
}
- type = new DecoratedTypeExpression(tokImmut, parseNonBiTypeExpression(fExtended));
+ type = new DecoratedTypeExpression(tokImmut, parseNonBiTypeExpression(fExtended, fHeader));
break;
}
@@ -4377,54 +4672,67 @@ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) {
}
tokAccess = match(Id.STRUCT);
}
- type = parseNamedTypeExpression(tokAccess);
+ type = parseNamedTypeExpression(tokAccess, fHeader);
break;
}
while (true) {
switch (peek().getId()) {
- case L_SQUARE:
- // this could be either:
- // -> NonBiTypeExpression ArrayDims
- // -> NonBiTypeExpression ArrayIndexes
- // in the case of the ArrayIndexes, we do NOT consume that portion of the
- // expression; we use it to give us a dimension count, as if it were ArrayDims
+ case L_SQUARE: {
+ // Sizes belong to the surrounding construction, not to its type. Look ahead
+ // with a branch so a cursor hole is reported only when the sizes are reparsed
+ // with their owning NewExpression. No diagnostic escapes a discarded lookahead.
Mark mark = mark();
-
- expect(Id.L_SQUARE);
- int cDims = 0;
- int cIndexes = 0;
- while (match(Id.R_SQUARE) == null) {
- if (cDims + cIndexes > 0) {
- expect(Id.COMMA);
- }
-
- Token dim = peek(); // just for error reporting
- if (match(Id.COND) == null) {
- parseExpression();
- if (cIndexes == 0 && cDims > 0) {
- // just log the first one that deviates
- log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition());
+ var branch = f_errs.get().branch(null);
+ try (var ignored = reportingTo(branch)) {
+ expect(Id.L_SQUARE);
+ int cDims = 0;
+ int cIndexes = 0;
+ while (true) {
+ if (f_cursor != NO_CURSOR && canRetainIncomplete()) {
+ int dimensions = cDims + cIndexes + 1 + (peek(Id.COMMA) ? 1 : 0);
+ restore(mark);
+ return new ArrayTypeExpression(type, dimensions, f_cursor);
}
- ++cIndexes;
- } else { // we ate the "?"
- if (cDims == 0 && cIndexes > 0) {
- // just log the first one that deviates
- log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition());
+ if (match(Id.R_SQUARE) != null) {
+ break;
+ }
+ if (cDims + cIndexes > 0) {
+ expect(Id.COMMA);
}
- ++cDims;
- }
- }
- long lEndPos = prev().getEndPosition();
- type = new ArrayTypeExpression(type, cDims + cIndexes, lEndPos);
- // if there were only indexes, then we need to leave them in place because the
- // type expression does not consume them
- if (cDims == 0 && cIndexes > 0) {
- restore(mark);
- return type;
+ Token dim = peek();
+ if (match(Id.COND) == null) {
+ Expression index = f_partialAnalysis ? parsePartialValue() : parseExpression();
+ ++cIndexes;
+ if (f_cursor != NO_CURSOR && (containsCursorHole(index) || canRetainIncomplete())) {
+ int dimensions = cDims + cIndexes + (peek(Id.COMMA) ? 1 : 0);
+ long end = Math.max(f_cursor, prev().getEndPosition());
+ restore(mark);
+ return new ArrayTypeExpression(type, dimensions, end);
+ }
+ if (cIndexes == 1 && cDims > 0) {
+ log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition());
+ }
+ } else {
+ if (cDims == 0 && cIndexes > 0) {
+ log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition());
+ }
+ ++cDims;
+ }
+ }
+ type = new ArrayTypeExpression(type, cDims + cIndexes, prev().getEndPosition());
+ if (cDims == 0 && cIndexes > 0) {
+ restore(mark);
+ return type;
+ }
+ } catch (CompilerException error) {
+ branch.merge();
+ throw error;
}
+ branch.merge();
break;
+ }
case COND:
if (!peek().hasLeadingWhitespace()) {
@@ -4455,9 +4763,9 @@ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) {
*
* @return an AnnotatedTypeExpression
*/
- AnnotatedTypeExpression parseAnnotatedTypeExpression(boolean fExtended) {
+ AnnotatedTypeExpression parseAnnotatedTypeExpression(boolean fExtended, boolean fHeader) {
AnnotationExpression annotation = parseAnnotation(true);
- TypeExpression type = parseNonBiTypeExpression(fExtended);
+ TypeExpression type = parseNonBiTypeExpression(fExtended, fHeader);
return new AnnotatedTypeExpression(annotation, type);
}
@@ -4477,19 +4785,23 @@ AnnotatedTypeExpression parseAnnotatedTypeExpression(boolean fExtended) {
* @return a FunctionTypeExpression
*/
FunctionTypeExpression parseFunctionTypeExpression() {
+ return parseFunctionTypeExpression(false);
+ }
+
+ private FunctionTypeExpression parseFunctionTypeExpression(boolean header) {
Token function = expect(Id.FUNCTION);
// return values
Token conditional = match(Id.CONDITIONAL);
- List listReturn = parseReturnList();
+ List listReturn = parseReturnList(header);
// see if the parameters precede the name
- List listParam = parseParameterTypeList(false);
+ List listParam = parseParameterTypeList(false, header);
if (listParam == null) {
// name optionally comes before or after the parameters
Token name = expect(Id.IDENTIFIER);
- listParam = parseParameterTypeList(true);
+ listParam = parseParameterTypeList(true, header);
// pretend the name is the next token (as if we didn't eat it already)
putBack(name);
@@ -4520,6 +4832,10 @@ FunctionTypeExpression parseFunctionTypeExpression() {
* @return a NamedTypeExpression
*/
NamedTypeExpression parseNamedTypeExpression(Token tokAccess) {
+ return parseNamedTypeExpression(tokAccess, false);
+ }
+
+ private NamedTypeExpression parseNamedTypeExpression(Token tokAccess, boolean fHeader) {
NamedTypeExpression expr = null;
do {
if (expr != null) {
@@ -4527,7 +4843,22 @@ NamedTypeExpression parseNamedTypeExpression(Token tokAccess) {
}
// QualifiedName
- List names = parseQualifiedName();
+ List names = new ArrayList<>();
+ do {
+ boolean hole = fHeader && f_cursor != NO_CURSOR && canRetainTypeSlot();
+ Token name = hole ? new Token(f_cursor, f_cursor, Id.IDENTIFIER, "") : expect(Id.IDENTIFIER);
+ names.add(name);
+ // A cursor in a qualifier owns this token only; the remaining dotted suffix
+ // stays in source. A trailing dot owns a zero-width insertion slot.
+ if (fHeader && (hole || peek(Id.DOT) && name.getStartPosition() < f_cursor
+ && f_cursor <= name.getEndPosition())) {
+ var selected = expr == null
+ ? new NamedTypeExpression(null, names, tokAccess, null, null, name.getEndPosition())
+ : new NamedTypeExpression(expr, names, null, name.getEndPosition());
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ throw new IncompleteHeader(IncompleteStatement.forDeclarationType(selected, f_cursor));
+ }
+ } while (match(Id.DOT) != null);
if (tokAccess != null && expr != null) {
log(Severity.ERROR, NO_CHILD_ACCESS, tokAccess.getStartPosition(),
@@ -4545,7 +4876,7 @@ NamedTypeExpression parseNamedTypeExpression(Token tokAccess) {
}
// TypeParameterTypeList
- List params = parseTypeParameterTypeList(false, true);
+ List params = parseTypeParameterTypeList(false, true, fHeader);
if (expr == null) {
expr = new NamedTypeExpression(null, names, tokAccess, tokNarrow, params,
@@ -4763,6 +5094,10 @@ AnnotationExpression parseAnnotation(boolean required) {
* @return a list of zero or more type parameters, or null if there were no angle brackets
*/
List parseTypeParameterList(boolean required) {
+ return parseTypeParameterList(required, false);
+ }
+
+ private List parseTypeParameterList(boolean required, boolean header) {
List typeParams = null;
if (match(Id.COMP_LT, required) != null) {
typeParams = new ArrayList<>();
@@ -4777,7 +5112,12 @@ List parseTypeParameterList(boolean required) {
Token param = expect(Id.IDENTIFIER);
TypeExpression type = null;
if (match(Id.EXTENDS) != null) {
- type = parseExtendedTypeExpression();
+ try {
+ type = header ? parseDeclarationType(true) : parseExtendedTypeExpression();
+ } catch (IncompleteHeader error) {
+ throw error.withFormals(Stream.concat(typeParams.stream(),
+ Stream.of(new Parameter(new BadTypeExpression((Expression) error.writtenType.clone()), param))).toList());
+ }
}
typeParams.add(new Parameter(type, param));
}
@@ -4807,13 +5147,19 @@ List parseTypeParameterList(boolean required) {
* @return a list of zero or more types, or null if there were no angle brackets
*/
List parseTypeParameterTypeList(boolean required, boolean fAllowTypeSequence) {
+ return parseTypeParameterTypeList(required, fAllowTypeSequence, false);
+ }
+
+ private List parseTypeParameterTypeList(boolean required, boolean fAllowTypeSequence,
+ boolean fHeader) {
List types = null;
if (match(Id.COMP_LT, required) != null) {
+ retainEmptyTypeArgument(fHeader);
if (match(Id.COMP_GT) != null) {
types = Collections.emptyList();
} else {
- types = parseTypeExpressionList(fAllowTypeSequence);
- expect(Id.COMP_GT);
+ types = parseTypeExpressionList(fAllowTypeSequence, fHeader);
+ expectHeaderTypeClose(Id.COMP_GT, types, fHeader);
}
}
return types;
@@ -4831,23 +5177,48 @@ List parseTypeParameterTypeList(boolean required, boolean fAllow
* @return a list of type expressions
*/
List parseTypeExpressionList(boolean fAllowTypeSequence) {
+ return parseTypeExpressionList(fAllowTypeSequence, false);
+ }
+
+ private List parseTypeExpressionList(boolean fAllowTypeSequence, boolean fHeader) {
List types = new ArrayList<>();
while (true) {
if (!types.isEmpty() && match(Id.COMMA) == null) {
return types;
}
+ retainEmptyTypeArgument(fHeader);
+
if (fAllowTypeSequence && peek(Id.COMP_LT)) {
Token tokStart = peek();
- List listSeq = parseTypeParameterTypeList(true, false);
+ List listSeq = parseTypeParameterTypeList(true, false, fHeader);
Token tokEnd = prev();
types.add(new TupleTypeExpression(listSeq, tokStart.getStartPosition(), tokEnd.getEndPosition()));
} else {
- types.add(parseExtendedTypeExpression());
+ types.add(parseIntersectingTypeExpression(true, fHeader));
}
}
}
+ /** An explicit empty generic slot has no invented type expression or generic owner. */
+ private void retainEmptyTypeArgument(boolean header) {
+ if (header && f_cursor != NO_CURSOR && canRetainTypeSlot()) {
+ throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""));
+ }
+ }
+
+ /** Only explicit cursor probes can select an empty type slot at a grammar boundary. */
+ private boolean canRetainTypeSlot() {
+ return m_cSpeculating == 0 && !m_fAvoidRecovery && !f_errs.get().isAbortDesired()
+ && prev().getEndPosition() <= f_cursor
+ && f_cursor <= (eof() ? m_source.getPosition() : peek().getStartPosition())
+ && (switch (peek().getId()) {
+ case COMP_GT, COMP_GTEQ, SHR, SHR_ASN, USHR, USHR_ASN, BIT_OR, ADD, SUB -> true;
+ case IDENTIFIER -> f_cursor < peek().getStartPosition();
+ default -> canRetainIncomplete();
+ });
+ }
+
/**
* Parse a list of package injections, starting with the opening parenthesis.
*
@@ -4919,21 +5290,227 @@ List parseParameterList(boolean required) {
List params = null;
if (match(Id.L_PAREN, required) != null) {
params = new ArrayList<>();
+ if (f_cursor != NO_CURSOR && canRetainIncomplete()) {
+ throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""));
+ }
if (match(Id.R_PAREN) == null) {
- do {
- TypeExpression type = parseTypeExpression();
- Token name = expect(Id.IDENTIFIER);
- Expression value = null;
+ while (true) {
+ TypeExpression type = parseDeclarationType();
+ var missingName = declarationNameSlot(type);
+ if (missingName.isPresent()) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ throw new IncompleteHeader(missingName.orElseThrow());
+ }
+ Token name = expect(Id.IDENTIFIER);
+ Expression value = null;
if (match(Id.ASN) != null) {
value = parseExpression();
}
params.add(new Parameter(type, name, value));
- } while (match(Id.R_PAREN, (match(Id.COMMA) == null)) == null);
+ Token comma = match(Id.COMMA);
+ if (comma != null && f_cursor != NO_CURSOR && canRetainIncomplete()) {
+ throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""));
+ }
+ if ((comma == null ? expectPartialClose(Id.R_PAREN, params) : match(Id.R_PAREN)) != null) {
+ break;
+ }
+ }
}
}
return params;
}
+ /** A header type prefix is queried in its enclosing declaration, never as a value. */
+ private TypeExpression parseDeclarationType() {
+ return parseDeclarationType(false);
+ }
+
+ /**
+ * Only a grammar position that requires a name may retain an empty name slot. In a method
+ * body, a bare expression followed by whitespace is not evidence of a variable declaration.
+ * Keep the complete written type as syntax; no parameter or property is registered.
+ */
+ private Optional declarationNameSlot(TypeExpression type) {
+ return f_cursor != NO_CURSOR && m_cSpeculating == 0 && !m_fAvoidRecovery
+ && !f_errs.get().isAbortDesired() && !(type instanceof BadTypeExpression)
+ && prev().getEndPosition() < f_cursor
+ && f_cursor <= (eof() ? m_source.getPosition() : peek().getStartPosition())
+ && m_source.toString(prev().getEndPosition(), f_cursor).isBlank()
+ && (eof() || peek(Id.ASN) || peek(Id.SEMICOLON) || peek(Id.R_CURLY)
+ || peek(Id.R_PAREN) || peek(Id.COMMA))
+ ? Optional.of(IncompleteStatement.forDeclarationName(type, prev(), f_cursor))
+ : Optional.empty();
+ }
+
+ private TypeExpression parseDeclarationType(boolean extended) {
+ if (f_cursor != NO_CURSOR && canRetainIncomplete()) {
+ throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""));
+ }
+ boolean header = f_cursor != NO_CURSOR && m_cSpeculating == 0 && !m_fAvoidRecovery
+ && !f_errs.get().isAbortDesired();
+ TypeExpression type = parseIntersectingTypeExpression(extended, header);
+ if (header) {
+ var prefix = declarationTypePrefix(type);
+ if (prefix.isPresent()) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ throw new IncompleteHeader(IncompleteStatement.forDeclarationType(prefix.orElseThrow(), f_cursor), type, List.of());
+ }
+ }
+ return type;
+ }
+
+ /**
+ * Select a written type name, including a generic base whose arguments follow the cursor. The
+ * caller transfers this original node into the cursor site; ordinary adoption and cloning
+ * then own it. This is syntax selection, not validation of the enclosing type's constraints.
+ */
+ private Optional declarationTypePrefix(TypeExpression type) {
+ if (type instanceof NamedTypeExpression named && named.getModule() == null
+ && named.getNameToken() != null && named.getNameToken().getStartPosition() < f_cursor
+ && f_cursor <= named.getNameToken().getEndPosition()) {
+ return Optional.of(named);
+ }
+ var types = type instanceof FunctionTypeExpression function
+ ? Stream.concat(function.getReturnValues().stream().map(Parameter::getType),
+ function.getParamTypes().stream())
+ : StreamSupport.stream(type.children().spliterator(), false)
+ .filter(TypeExpression.class::isInstance).map(TypeExpression.class::cast);
+ return types.flatMap(child -> declarationTypePrefix(child).stream()).findFirst();
+ }
+
+ /** Missing type closers are tolerated only around a selected header prefix, never in normal parsing. */
+ private void expectHeaderTypeClose(Id close, List types, boolean header) {
+ if (match(close) != null) {
+ return;
+ }
+ if (header && m_cSpeculating == 0 && !m_fAvoidRecovery && !f_errs.get().isAbortDesired()
+ && (atMissingClose() || peek(Id.IDENTIFIER) || peek(Id.COMP_GT))
+ && types.stream().anyMatch(type -> declarationTypePrefix(type).isPresent())) {
+ return;
+ }
+ expect(close);
+ }
+
+ private IncompleteHeader incompleteHeader(Token prefix) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ return new IncompleteHeader(new IncompleteStatement(prefix, f_cursor, INCOMPLETE_EXPRESSION));
+ }
+
+ /** Unwinds header parsing while carrying the incomplete site, written type and formal parameters. */
+ private static class IncompleteHeader extends CompilerException {
+ private IncompleteHeader(IncompleteStatement site) {
+ this(site, site.getTarget(), List.of());
+ }
+
+ private IncompleteHeader(IncompleteStatement site, Expression writtenType, List formals) {
+ super("Incomplete declaration header");
+ this.site = site;
+ this.formals = formals;
+ this.writtenType = writtenType;
+ }
+
+ private IncompleteHeader withFormals(List parameters) {
+ return parameters == null ? this : new IncompleteHeader(site, writtenType,
+ Stream.concat(formals.stream(), parameters.stream()).distinct().toList());
+ }
+
+ private final IncompleteStatement site;
+ private final List formals;
+ private final Expression writtenType;
+ }
+
+ /** Keep a type's real body for structure, but never register an unfinished inheritance header. */
+ private TypeCompositionStatement retainTypeDeclaration(Mark header, long start, Token category,
+ Token name, List qualified, List sites, CompilerException error) {
+ if (m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) {
+ throw error;
+ }
+ restore(header);
+ while (!eof() && !peek(Id.R_CURLY)) {
+ if (f_errs.get().isAbortDesired()) {
+ throw error;
+ }
+ if (peek(Id.L_CURLY)) {
+ StatementBlock body = parseTypeCompositionBody(category);
+ return new IncompleteTypeCompositionStatement(m_source, category, name, qualified, body,
+ start, body.getEndPosition(), sites,
+ error instanceof IncompleteHeader incomplete ? incomplete.formals : List.of());
+ }
+ if (match(Id.SEMICOLON) != null) {
+ break;
+ }
+ current();
+ }
+ return new IncompleteTypeCompositionStatement(m_source, category, name, qualified, null,
+ start, retainedDeclarationEnd(sites), sites,
+ error instanceof IncompleteHeader incomplete ? incomplete.formals : List.of());
+ }
+
+ /**
+ * Retain only the known declaration name and source extent. A brace or semicolon bounds
+ * this recovery; the body is skipped as one unit so its names cannot leak into the owner.
+ * Unfinished parameter syntax never registers a partial method signature.
+ */
+ private IncompleteDeclarationStatement retainDeclaration(Mark header, long start, Token name,
+ IncompleteDeclarationStatement.Kind kind, List sites, CompilerException error) {
+ if (m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) {
+ throw error;
+ }
+ restore(header);
+ while (!eof() && !peek(Id.R_CURLY)) {
+ if (f_errs.get().isAbortDesired()) {
+ throw error;
+ }
+ if (peek(Id.L_CURLY)) {
+ current();
+ // Consume the body by brace depth so missing parameter delimiters cannot eat
+ // its siblings. current() also preserves the actual closing token's range.
+ int depth = 1;
+ while (depth > 0 && !eof()) {
+ if (f_errs.get().isAbortDesired()) {
+ throw error;
+ }
+ switch (current().getId()) {
+ case L_CURLY -> ++depth;
+ case R_CURLY -> --depth;
+ default -> {}
+ }
+ }
+ break;
+ }
+ if (match(Id.SEMICOLON) != null) {
+ break;
+ }
+ current();
+ }
+ return new IncompleteDeclarationStatement(kind, name, start, retainedDeclarationEnd(sites), sites,
+ error instanceof IncompleteHeader incomplete ? incomplete.formals : List.of());
+ }
+
+ /** An empty name slot at EOF includes the whitespace after its last written type token. */
+ private long retainedDeclarationEnd(List sites) {
+ return Math.max(prev().getEndPosition(), sites.stream().mapToLong(IncompleteStatement::getEndPosition)
+ .max().orElse(prev().getEndPosition()));
+ }
+
+ /** Declaration return slots select types without publishing an unfinished callable. */
+ private List parseDeclarationReturns() {
+ if (f_cursor == NO_CURSOR) {
+ return parseReturnList();
+ }
+ if (match(Id.VOID) != null) {
+ return List.of();
+ }
+ if (match(Id.L_PAREN) == null) {
+ return List.of(new Parameter(parseDeclarationType()));
+ }
+ List returns = new ArrayList<>();
+ do {
+ returns.add(new Parameter(parseDeclarationType(), matchNameOrAny()));
+ } while (match(Id.R_PAREN, match(Id.COMMA) == null) == null);
+ return returns;
+ }
+
/**
* Parse a list of parameter types (without parameter names).
*
@@ -4946,12 +5523,16 @@ List parseParameterList(boolean required) {
* @return
*/
List parseParameterTypeList(boolean required) {
+ return parseParameterTypeList(required, false);
+ }
+
+ private List parseParameterTypeList(boolean required, boolean header) {
List types = null;
if (match(Id.L_PAREN, required) != null) {
types = peek(Id.R_PAREN)
? Collections.emptyList()
- : parseTypeExpressionList(false);
- expect(Id.R_PAREN);
+ : parseTypeExpressionList(false, header);
+ expectHeaderTypeClose(Id.R_PAREN, types, header);
}
return types;
}
@@ -5022,62 +5603,324 @@ List parseArgumentList(boolean required, boolean allowCurrying, bool
List args = new ArrayList<>();
if (match(idClose) == null) {
do {
- Token label = null;
- if (!fArray) {
- // special case where the parameter names are being specified with the arguments
- if (peek(Id.IDENTIFIER)) {
- Token name = expect(Id.IDENTIFIER);
- if (match(Id.ASN) == null) {
- // oops, it wasn't a "name=value" argument
- putBack(name);
- } else {
- label = name;
- }
- }
- }
+ args.add(parseArgument(allowCurrying, fArray));
+ } while (match(idClose, (match(Id.COMMA) == null)) == null);
+ }
- Expression expr;
- if (allowCurrying && !fArray) {
- switch (peek().getId()) {
- case ANY: {
- // one of two cases: an unbound argument, or a lambda whose one parameter is
- // ignored
- Token tokUnbound = expect(Id.ANY);
- if (peek(Id.LAMBDA)) {
- putBack(tokUnbound);
- expr = parseExpression();
- } else {
- expr = new NonBindingExpression(tokUnbound.getStartPosition(),
- tokUnbound.getEndPosition(), null);
- }
- break;
- }
+ return args;
+ }
- case COMP_LT: {
- Token tokOpen = expect(Id.COMP_LT);
- TypeExpression type = parseTypeExpression();
- Token tokClose = expect(Id.COMP_GT);
- Token tokUnbound = expect(Id.ANY);
- expr = new NonBindingExpression(tokOpen.getStartPosition(),
- tokUnbound.getEndPosition(), type);
- break;
- }
+ /** Parse one argument, shared by calls, constructors and annotation argument lists. */
+ private Expression parseArgument(boolean allowCurrying, boolean fArray) {
+ return parseArgument(allowCurrying, fArray, false);
+ }
- default:
- expr = parseExpression();
- break;
- }
+ private Expression parseArgument(boolean allowCurrying, boolean fArray, boolean partial) {
+ Token label = null;
+ if (!fArray) {
+ // special case where the parameter names are being specified with the arguments
+ if (peek(Id.IDENTIFIER)) {
+ Token name = expect(Id.IDENTIFIER);
+ if (match(Id.ASN) == null) {
+ // oops, it wasn't a "name=value" argument
+ putBack(name);
} else {
+ label = name;
+ }
+ }
+ }
+
+ if (partial && canRetainIncomplete() && peek(Id.COMMA)) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ Expression hole = new IncompleteExpression(new IncompleteStatement(
+ new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""), f_cursor, INCOMPLETE_EXPRESSION));
+ return label == null ? hole : new LabeledExpression(label, hole);
+ }
+ if (partial && label != null && endsAt(f_cursor)) {
+ throw new IncompleteArgument(label);
+ }
+ Expression expr;
+ if (allowCurrying && !fArray) {
+ switch (peek().getId()) {
+ case ANY: {
+ // one of two cases: an unbound argument, or a lambda whose one parameter is
+ // ignored
+ Token tokUnbound = expect(Id.ANY);
+ if (peek(Id.LAMBDA)) {
+ putBack(tokUnbound);
expr = parseExpression();
+ } else {
+ expr = new NonBindingExpression(tokUnbound.getStartPosition(),
+ tokUnbound.getEndPosition(), null);
}
+ break;
+ }
- args.add(label == null ? expr : new LabeledExpression(label, expr));
- } while (match(idClose, (match(Id.COMMA) == null)) == null);
+ case COMP_LT: {
+ Token tokOpen = expect(Id.COMP_LT);
+ TypeExpression type = parseTypeExpression();
+ Token tokClose = expect(Id.COMP_GT);
+ Token tokUnbound = expect(Id.ANY);
+ expr = new NonBindingExpression(tokOpen.getStartPosition(),
+ tokUnbound.getEndPosition(), type);
+ break;
+ }
+
+ default:
+ expr = partial ? parsePartialValue() : parseExpression();
+ break;
+ }
+ } else {
+ expr = partial ? parsePartialValue() : parseExpression();
+ }
+
+ return label == null ? expr : new LabeledExpression(label, expr);
+ }
+
+ /** Retain the opening token and separators only for the explicit partial-analysis parser. */
+ private Expression parseInvocation(Expression callee) {
+ if (!f_partialAnalysis) {
+ return new InvocationExpression(callee, peek(Id.ASYNC_PAREN),
+ parseArgumentList(true, true, false), prev().getEndPosition());
}
+ boolean async = peek(Id.ASYNC_PAREN);
+ List args = parsePartialArguments(callee, true);
+ return new InvocationExpression(callee, async, args, prev().getEndPosition());
+ }
+
+ /** Shared cursor syntax for ordinary calls and constructor arguments. */
+ private List parsePartialArguments(Expression callee, boolean allowBindings) {
+ boolean array = callee instanceof NewExpression && peek(Id.L_SQUARE);
+ Id close = array ? Id.R_SQUARE : Id.R_PAREN;
+ Token open = array || allowBindings && peek(Id.ASYNC_PAREN) ? current() : expect(Id.L_PAREN);
+ List args = new ArrayList<>();
+ List separators = new ArrayList<>();
+ boolean incomplete = containsCursorHole(callee);
+ if (endsAt(f_cursor) && !peek(Id.COMMA)) {
+ match(close);
+ throw incomplete(callee, open, args, separators);
+ }
+ if (match(close) == null) {
+ while (true) {
+ Expression argument;
+ try {
+ argument = parseArgument(allowBindings, array, f_cursor != NO_CURSOR);
+ } catch (IncompleteArgument e) {
+ match(close);
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ throw new IncompleteSyntax(IncompleteStatement.forNamedArgument(
+ callee, open, args, separators, f_cursor, e.name));
+ }
+ Expression value = argument instanceof LabeledExpression labeled
+ ? labeled.getUnderlyingExpression() : argument;
+ // A direct final argument prefix belongs to this call's fitting context. Keep
+ // its token rather than validating it as a value or retaining a second site.
+ // Compound/member expressions and slots before later arguments keep their sites.
+ if (value instanceof IncompleteExpression partial && partial.getSite().isNameCompletion()
+ && endsAt(f_cursor) && !peek(Id.COMMA)) {
+ match(close);
+ throw new IncompleteSyntax(IncompleteStatement.forArgumentPrefix(callee, open,
+ args, separators, f_cursor,
+ argument instanceof LabeledExpression labeled ? labeled.getNameToken() : null,
+ partial.getSite().getMemberName().orElseThrow()));
+ }
+ args.add(argument);
+ if (containsCursorHole(value)) {
+ incomplete = true;
+ }
+ Token comma = match(Id.COMMA);
+ if (comma != null) {
+ separators.add(comma);
+ }
+ if (endsAt(f_cursor)) {
+ match(close);
+ throw incomplete(callee, open, args, separators);
+ }
+ if (comma == null && incomplete && !peek(close) && atMissingClose()) {
+ break;
+ }
+ if (match(close, comma == null) != null) {
+ break;
+ }
+ }
+ }
+ if (incomplete) {
+ throw incomplete(callee, open, args, separators);
+ }
return args;
}
+ private boolean canRetainIncomplete() {
+ return canRetainIncompleteAt(f_cursor);
+ }
+
+ /** A mid-token value cursor uses the same recovery boundary after its complete written token. */
+ private boolean canRetainIncompleteAt(long cursor) {
+ return canRetainIncompleteAt(cursor, false);
+ }
+
+ /** A selected member name can precede a call whose arguments must still be parsed and owned. */
+ private boolean canRetainIncompleteAt(long cursor, boolean allowCall) {
+ if (!f_partialAnalysis || m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) {
+ return false;
+ }
+ if (cursor == NO_CURSOR) {
+ return eof();
+ }
+ return prev().getEndPosition() <= cursor
+ && cursor <= (eof() ? m_source.getPosition() : peek().getStartPosition())
+ && (eof() || peek(Id.R_CURLY) || peek(Id.SEMICOLON) || peek(Id.R_PAREN)
+ || peek(Id.R_SQUARE) || peek(Id.COMMA) || peek(Id.COLON) || peek(Id.L_CURLY)
+ || prev().getId() == Id.IDENTIFIER && switch (peek().getId()) {
+ case ADD, SUB, MUL, DIV, MOD, DIVREM, SHL, SHR, USHR,
+ BIT_AND, BIT_OR, BIT_XOR, COND_AND, COND_OR, COND_XOR,
+ COMP_EQ, COMP_NEQ, COMP_LT, COMP_LTEQ, COMP_GT, COMP_GTEQ,
+ COMP_ORD, COND_ELSE -> true;
+ case COND -> peek().hasLeadingWhitespace();
+ default -> false;
+ }
+ || allowCall && (peek(Id.L_PAREN) || peek(Id.ASYNC_PAREN)));
+ }
+
+ /**
+ * Whether the operation being typed at the cursor ends there: the next token ends it, or the
+ * cursor ends a line that the next token cannot continue.
+ *
+ * @param cursor the position after the operation's last written token, or the cursor itself
+ *
+ * @return whether partial analysis retains the operation as the cursor's site
+ */
+ private boolean endsAt(long cursor) {
+ return endsAt(cursor, false);
+ }
+
+ /** A selected member name can also end before a call whose arguments must still be parsed. */
+ private boolean endsAt(long cursor, boolean allowCall) {
+ return canRetainIncompleteAt(cursor, allowCall) || atLineEnd(cursor);
+ }
+
+ /**
+ * What is being typed at the end of its line, before code on a later line that cannot continue
+ * it, ends there: the later line is the next statement, not the rest of a name, a member access,
+ * a value or a call's arguments. Without this, what is typed and that statement parse as one,
+ * which is an error, and the cursor has no site. A cursor after a comment at the line's end is
+ * in the comment, not at the end of the code.
+ */
+ private boolean atLineEnd(long cursor) {
+ if (!f_partialAnalysis || m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()
+ || cursor == NO_CURSOR || eof()) {
+ return false;
+ }
+ Token next = peek();
+ return prev().getEndPosition() <= cursor && cursor <= next.getStartPosition()
+ && Source.calculateLine(next.getStartPosition()) > Source.calculateLine(cursor)
+ && m_source.toString(prev().getEndPosition(), cursor).isBlank()
+ && switch (next.getId()) {
+ case DOT, L_PAREN, ASYNC_PAREN, L_SQUARE, COND,
+ ADD, SUB, MUL, DIV, MOD, DIVREM, SHL, SHR, USHR,
+ BIT_AND, BIT_OR, BIT_XOR, COND_AND, COND_OR, COND_XOR,
+ COMP_EQ, COMP_NEQ, COMP_LT, COMP_LTEQ, COMP_GT, COMP_GTEQ,
+ COMP_ORD, COND_ELSE, ASN, AS, IS -> false;
+ default -> true;
+ };
+ }
+
+ /**
+ * Retain enclosing syntax around a cursor hole at a statement/outer-delimiter boundary.
+ * The existing incomplete-expression diagnostic covers the hole and its missing suffix;
+ * this zero-width end marker neither edits the source nor consumes an enclosing delimiter.
+ * Without that hole, ordinary parsing (including speculation and error budgets) still fails.
+ */
+ private Token expectPartialClose(Id close, List extends AstNode> expressions) {
+ Token token = matchPartialClose(close, expressions);
+ return token == null ? expect(close) : token;
+ }
+
+ private Token matchPartialClose(Id close, List extends AstNode> expressions) {
+ Token token = match(close);
+ if (token != null) {
+ return token;
+ }
+ if (atMissingClose() && expressions.stream().anyMatch(this::containsCursorHole)) {
+ long end = Math.max(f_cursor, prev().getEndPosition());
+ return new Token(end, end, close);
+ }
+ return null;
+ }
+
+ private boolean atMissingClose() {
+ return f_cursor != NO_CURSOR && m_cSpeculating == 0 && !m_fAvoidRecovery
+ && !f_errs.get().isAbortDesired()
+ && (eof() || peek(Id.SEMICOLON) || peek(Id.R_CURLY)
+ || peek(Id.R_PAREN) || peek(Id.R_SQUARE) || peek(Id.L_CURLY));
+ }
+
+ /** Query syntax ownership rather than caching cursor state on ordinary AST nodes. */
+ private boolean containsCursorHole(AstNode node) {
+ return f_cursor != NO_CURSOR && PartialSyntax.containsAt(node, f_cursor);
+ }
+
+ private IncompleteSyntax incomplete(Expression target, Token operator,
+ List arguments, List separators) {
+ long position = f_cursor == NO_CURSOR ? m_source.getPosition() : f_cursor;
+ String code = f_cursor == NO_CURSOR ? UNEXPECTED_EOF : INCOMPLETE_EXPRESSION;
+ log(Severity.ERROR, code, position, position);
+ return new IncompleteSyntax(
+ new IncompleteStatement(target, operator, arguments, separators, position, code));
+ }
+
+ private IncompleteSyntax incomplete(Expression receiver, Token dot, Token memberName) {
+ log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor);
+ return new IncompleteSyntax(new IncompleteStatement(
+ receiver, dot, memberName, f_cursor, INCOMPLETE_EXPRESSION));
+ }
+
+ /** Preserve the enclosing statement only when its whole value is the incomplete operation. */
+ private Expression parsePartialValue() {
+ long start = peek().getStartPosition();
+ try {
+ return parseExpression();
+ } catch (IncompleteSyntax e) {
+ return retainPartialValue(start, e);
+ }
+ }
+
+ private Expression retainPartialValue(long start, IncompleteSyntax error) {
+ if (f_cursor == NO_CURSOR || error.statement.getStartPosition() != start) {
+ throw error;
+ }
+ return new IncompleteExpression(error.statement);
+ }
+
+ private void finishValueStatement(Expression value) {
+ if (value instanceof IncompleteExpression || atMissingClose() && containsCursorHole(value)) {
+ match(Id.SEMICOLON);
+ } else {
+ expect(Id.SEMICOLON);
+ }
+ }
+
+ /** Unwind to the enclosing statement without pretending the unfinished expression has a value. */
+ private static class IncompleteSyntax extends CompilerException {
+ private IncompleteSyntax(IncompleteStatement statement) {
+ super("Incomplete expression");
+ this.statement = statement;
+ }
+
+ private final IncompleteStatement statement;
+ }
+
+ /** Unwind only to the owning call, retaining a written label without inventing a value. */
+ private static class IncompleteArgument extends CompilerException {
+ private IncompleteArgument(Token name) {
+ super("Incomplete named argument");
+ this.name = name;
+ }
+
+ private final Token name;
+ }
+
/**
* Parse a declared list of return types.
*
@@ -5099,15 +5942,19 @@ List parseArgumentList(boolean required, boolean allowCurrying, bool
* }
*/
List parseReturnList() {
+ return parseReturnList(false);
+ }
+
+ private List parseReturnList(boolean header) {
List listReturn;
if (match(Id.VOID) != null) {
listReturn = Collections.emptyList();
} else if (match(Id.L_PAREN) == null) {
- listReturn = Collections.singletonList(new Parameter(parseTypeExpression()));
+ listReturn = List.of(new Parameter(parseIntersectingTypeExpression(false, header)));
} else {
listReturn = new ArrayList<>();
do {
- listReturn.add(new Parameter(parseTypeExpression(), matchNameOrAny()));
+ listReturn.add(new Parameter(parseIntersectingTypeExpression(false, header), matchNameOrAny()));
} while (match(Id.R_PAREN, (match(Id.COMMA) == null)) == null);
}
return listReturn;
@@ -5218,19 +6065,70 @@ LiteralExpression parseVersionLiteral(boolean fRequired) {
return tokVer == null ? null : new LiteralExpression(tokVer);
}
+ /**
+ * Finish a block without consuming the synthetic EOF token. Missing braces are errors, but
+ * completed declarations and statements remain useful to source tools. A speculative parse
+ * still fails immediately, and cancellation/error budgets always stop recovery.
+ */
+ private boolean atBlockEnd(boolean fFileLevel) {
+ if (f_errs.get().isAbortDesired()) {
+ throw new CompilerException("Parsing aborted");
+ }
+ if (eof()) {
+ // An explicit EOF cursor already reports its unfinished suffix, including containers.
+ // Other cursors and ordinary parsing must still diagnose missing block braces.
+ if (!fFileLevel && !f_errs.get().hasError(UNEXPECTED_EOF)
+ && !(f_cursor == m_source.getPosition()
+ && f_errs.get().hasError(INCOMPLETE_EXPRESSION))) {
+ log(Severity.ERROR, UNEXPECTED_EOF, m_source.getPosition(), m_source.getPosition());
+ }
+ // Retain an end marker for recovered containers without advancing past EOF.
+ m_tokenPrev = new Token(m_source.getPosition(), m_source.getPosition(), Id.R_CURLY);
+ return true;
+ }
+ return match(Id.R_CURLY) != null;
+ }
+
+ /**
+ * Drop one malformed statement and resume at a lexical statement boundary. Rewind to its
+ * beginning so delimiters consumed by the failed parse cannot change the enclosing scope.
+ * Always consume its first token before scanning, even if that token is a statement keyword.
+ */
+ private void recoverStatement(Mark start, CompilerException error) {
+ if (m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) {
+ throw error;
+ }
+ if (eof()) {
+ return;
+ }
+ restore(start);
+ Token token = current();
+ switch (token.getId()) {
+ case L_CURLY, L_PAREN, L_SQUARE:
+ skipEnclosed(token.getId());
+ break;
+ default:
+ skipToNextStatement();
+ break;
+ }
+ }
+
/**
* Attempt to get out of whatever parsing mess we got ourselves into by
* figuring out where the current statement ends, and starting anew from
* there.
*/
void skipToNextStatement() {
- while (true) {
+ while (!eof()) {
switch (peek().getId()) {
case SEMICOLON:
next();
return;
case L_CURLY:
+ skipEnclosed(current().getId());
+ return;
+
case L_PAREN:
case L_SQUARE:
skipEnclosed(current().getId());
@@ -5291,7 +6189,7 @@ void skipToNextStatement() {
* @param idOpen the opening parenthesis / curlies / brackets
*/
void skipEnclosed(Id idOpen) {
- while (true) {
+ while (!eof()) {
switch (peek().getId()) {
case L_CURLY:
case L_PAREN:
@@ -5441,12 +6339,11 @@ protected void putBack(Token token) {
}
protected static class Mark {
- long pos;
- Token token;
- Token putBack;
- Token lastMatch;
- Token doc;
- boolean noRec;
+ long pos;
+ Token token;
+ Token putBack;
+ Token lastMatch;
+ Token doc;
}
protected Mark mark() {
@@ -5456,17 +6353,17 @@ protected Mark mark() {
mark.putBack = m_tokenPutBack == null ? null : m_tokenPutBack.clone();
mark.lastMatch = m_tokenPrev == null ? null : m_tokenPrev .clone();
mark.doc = m_doc;
- mark.noRec = m_fAvoidRecovery;
return mark;
}
protected void restore(Mark mark) {
+ // A nesting-limit or listener abort ends this parse, including after speculative rewind.
+ // Token backtracking must not re-enable recovery from an exhausted parser budget.
m_lexer.restore(mark.pos);
- m_token = mark.token;
- m_tokenPutBack = mark.putBack;
- m_tokenPrev = mark.lastMatch;
- m_doc = mark.doc;
- m_fAvoidRecovery = mark.noRec;
+ m_token = mark.token;
+ m_tokenPutBack = mark.putBack;
+ m_tokenPrev = mark.lastMatch;
+ m_doc = mark.doc;
}
/**
@@ -5622,30 +6519,29 @@ protected Token takeDoc() {
* @param aoParam
*/
protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd, Object... aoParam) {
- if (m_lookAhead != null) {
- m_lookAhead.log(severity, sCode, aoParam, lPosStart, lPosEnd);
- } else if (m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd)) {
+ f_errs.get().log(severity, sCode, in(m_source, lPosStart, lPosEnd), aoParam);
+ if (f_errs.get().isAbortDesired()) {
m_fAvoidRecovery = true;
- throw new CompilerException("error list is full: " + m_errorListener);
+ throw new CompilerException("error list is full: " + f_errs.get());
}
}
/**
* Count one level of nested parsing, to be released when the returned handle is closed:
*
- *
+ *
* try (var ignore = nest()) {
* ...
* }
- *
+ *
*
- * The parser is a recursive descent, and a level of nesting in the source costs a dozen or so
+ * The parser is a recursive descent, and a level of nesting in the source costs a dozen or so
* Java frames. Without a limit, sufficiently nested source exhausts the thread's stack, and a
* StackOverflowError is not something a caller can be asked to handle: it escapes the compiler
* and reaches whatever embeds it. Counting the levels turns that into an ordinary error with a
* source position.
*
- * The count is shared by every construct that nests, because they nest through each other -
+ *
The count is shared by every construct that nests, because they nest through each other -
* a type inside an expression inside a statement - so one budget bounds the descent no matter
* how the source alternates between them. It counts levels of parsing rather than levels of
* source: one construct can pass more than one guard on its way down, so the limit is a bound
@@ -5657,6 +6553,7 @@ protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd
*/
private Nesting nest() {
if (m_cNesting >= MAX_NESTING_DEPTH) {
+ m_fAvoidRecovery = true;
Token token = peek();
log(Severity.ERROR, NESTING_TOO_DEEP, token.getStartPosition(), token.getEndPosition(),
MAX_NESTING_DEPTH);
@@ -5678,57 +6575,113 @@ public void close() {
}
}
- public class SafeLookAhead
- implements AutoCloseable {
- public SafeLookAhead() {
- m_oldLookAhead = m_lookAhead;
- m_lookAhead = this;
- m_mark = mark();
- }
+ /**
+ * Report to the given listener until the returned scope is closed.
+ *
+ * @param errs the listener to report to for the duration of the scope
+ */
+ private Reporting.Scope reportingTo(ErrorListener errs) {
+ return f_errs.to(errs);
+ }
+
+ /**
+ * Begin a speculative parse: an attempt whose tokens and whose diagnostics both count only if
+ * it is kept.
+ *
+ *
The parser used to hand-roll this, buffering nothing and throwing away every diagnostic
+ * below ERROR even when the attempt was kept - which lost those diagnostics for good, because
+ * a kept attempt is the real parse and nothing re-reads those tokens. An attempt is a
+ * {@link ErrorListener#branch} instead, so keeping it merges what it had to say and dropping
+ * it drops the lot, which is the behaviour the rest of the compiler already gets.
+ *
+ * @return a scope that restores tokens and discards diagnostics unless the attempt is kept
+ */
+ public Attempt attempt() {
+ return new Attempt();
+ }
- public void log(Severity severity, String sCode, Object[] aoParam, long lPosStart, long lPosEnd) {
- log(new ErrorList.ErrorInfo(severity, sCode, aoParam, m_source, lPosStart, lPosEnd));
+ /**
+ * The scope opened by {@link #attempt()}.
+ */
+ public class Attempt
+ implements AutoCloseable, ErrorListener {
+ /** Save the token position and install a diagnostic branch until this attempt is closed. */
+ public Attempt() {
+ // read the destination before moving it: the branch buffers into whatever this parser
+ // was reporting to when the attempt began
+ f_branch = f_errs.get().branch(null);
+ f_scope = f_errs.to(this);
+ f_mark = mark();
+ ++m_cSpeculating;
}
- private void log(ErrorListener.ErrorInfo err) {
- if (err.getSeverity().ordinal() >= Severity.ERROR.ordinal()) {
- m_err = err;
- m_fKeepResults = false;
- throw new CompilerException("err=" + m_err);
+ @Override
+ public void log(ErrorInfo err) {
+ f_branch.log(err);
+
+ // an attempt that has gone wrong has answered; abandoning it here is what stops the
+ // parser from trying to recover inside a guess
+ if (err.getSeverity().isAtLeast(Severity.ERROR)) {
+ m_fKeep = false;
+ throw new CompilerException("err=" + err);
}
}
+ @Override
+ public boolean isAbortDesired() {
+ return f_branch.isAbortDesired();
+ }
+
+ @Override
+ public boolean hasSeriousErrors() {
+ return f_branch.hasSeriousErrors();
+ }
+
+ @Override
+ public boolean hasError(String sCode) {
+ return f_branch.hasError(sCode);
+ }
+
+ /**
+ * @return true iff nothing serious has been reported against this attempt
+ */
public boolean isClean() {
- return m_err == null;
+ return !f_branch.hasSeriousErrors();
}
- public void keepResults() {
- m_fKeepResults = true;
+ /**
+ * Keep what this attempt parsed, and with it whatever it had to say.
+ */
+ public void keep() {
+ m_fKeep = true;
}
@Override
public void close() {
- assert m_lookAhead == this;
- m_lookAhead = m_oldLookAhead;
+ assert f_errs.get() == this;
+ f_scope.close();
+ --m_cSpeculating;
- if (m_fKeepResults) {
- assert m_err == null;
+ if (m_fKeep) {
+ assert isClean();
+ f_branch.merge();
} else {
- restore(m_mark);
+ // the attempt did not happen: its tokens are put back and its branch is dropped
+ restore(f_mark);
}
}
- SafeLookAhead m_oldLookAhead;
- Mark m_mark;
- ErrorList.ErrorInfo m_err;
- boolean m_fKeepResults;
+ private final ErrorListener f_branch;
+ private final Reporting.Scope f_scope;
+ private final Mark f_mark;
+ private boolean m_fKeep;
}
/**
* @return true iff it's ok to try to recover from a parsing error
*/
protected boolean recoverable() {
- return !eof() && !m_fAvoidRecovery && m_lookAhead == null;
+ return !eof() && !m_fAvoidRecovery && m_cSpeculating == 0;
}
// ----- Object methods ------------------------------------------------------------------------
@@ -5871,6 +6824,10 @@ public String toString() {
* Semicolon is missing.
*/
public static final String MISSING_SEMICOLON = "PARSER-29";
+ /**
+ * An explicit partial-analysis cursor marks an unfinished expression.
+ */
+ public static final String INCOMPLETE_EXPRESSION = "PARSER-31";
/**
* Nesting is too deep.
@@ -5886,6 +6843,9 @@ public String toString() {
// ----- data members --------------------------------------------------------------------------
+ /** Reserved value that cannot be produced by Source.getPosition(). */
+ private static final long NO_CURSOR = -1;
+
/**
* The Source to parse.
*/
@@ -5904,13 +6864,23 @@ public String toString() {
/**
* The ErrorListener to report errors to.
*/
- private ErrorListener m_errorListener;
+ /**
+ * Where this parser's diagnostics go: the listener it was handed, or - for as long as a
+ * scope or an {@link Attempt} is open - somewhere else.
+ */
+ private final Reporting f_errs;
/**
* The lexical analyzer.
*/
private final Lexer m_lexer;
+ /** Whether trailing incomplete expression statements may be retained for partial analysis. */
+ private final boolean f_partialAnalysis;
+
+ /** Explicit cursor, or NO_CURSOR for ordinary parsing and trailing-EOF partial analysis. */
+ private final long f_cursor;
+
/**
* The "put back" token.
*/
@@ -5950,5 +6920,9 @@ public String toString() {
/**
* Object supporting unpredictable amount of look-ahead.
*/
- private SafeLookAhead m_lookAhead;
+ /**
+ * How many speculative attempts are open. Recovery is for the real parse: inside an attempt a
+ * syntax error is an answer, so there is nothing to recover from.
+ */
+ private int m_cSpeculating;
}
diff --git a/javatools/src/main/java/org/xvm/compiler/Token.java b/javatools/src/main/java/org/xvm/compiler/Token.java
index 54faab17062..8d9c18e152c 100644
--- a/javatools/src/main/java/org/xvm/compiler/Token.java
+++ b/javatools/src/main/java/org/xvm/compiler/Token.java
@@ -7,6 +7,9 @@
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.NOWHERE;
+import static org.xvm.asm.ErrorListener.in;
+
import static org.xvm.util.Handy.appendChar;
import static org.xvm.util.Handy.appendString;
@@ -316,17 +319,15 @@ public String getString(Source source) {
* {@link Severity#WARNING}, {@link Severity#ERROR}, or {@link Severity#FATAL}
* @param sCode the error code that identifies the error message
* @param aoParam the parameters for the error message; may be null
- *
- * @return true to attempt to abort the process that reported the error, or false to attempt to
- * continue the process
*/
- public boolean log(ErrorListener errs, Source source, Severity severity, String sCode, Object... aoParam) {
+ public void log(ErrorListener errs, Source source, Severity severity, String sCode, Object... aoParam) {
if (aoParam == null || aoParam.length == 0) {
aoParam = new Object[] {source == null ? toString() : getString(source)};
}
- return errs.log(severity, sCode, aoParam, source,
- source == null ? 0L : getStartPosition(), source == null ? 0L : getEndPosition());
+ errs.log(severity, sCode, source == null
+ ? NOWHERE
+ : in(source, getStartPosition(), getEndPosition()), aoParam);
}
// ----- Object methods ------------------------------------------------------------------------
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java
index 43ef4064b15..a1af49acbf3 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java
@@ -40,6 +40,9 @@
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* A type annotation is used for type annotations with an optional argument list.
*/
@@ -61,6 +64,12 @@ public AnnotationExpression(Annotation anno, AstNode node) {
this.lEndPos = node.getEndPosition();
}
+ /** Return an independent syntax tree with the same node type. */
+ @Override
+ public AnnotationExpression copyTree() {
+ return (AnnotationExpression) super.copyTree();
+ }
+
// ----- accessors -----------------------------------------------------------------------------
@Override
@@ -300,7 +309,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) {
if (exprNew instanceof NameExpression exprName &&
exprName.getMeaning() == NameExpression.Meaning.Method) {
MethodConstant idMethod = (MethodConstant)
- exprName.resolveRawArgument(ctx, false, ErrorListener.BLACKHOLE);
+ exprName.resolveRawArgument(ctx, false, silent(PROBE));
method = (MethodStructure) idMethod.getComponent();
} else if (exprNew instanceof LambdaExpression exprLambda) {
method = exprLambda.getLambda();
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java b/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java
index 8fb3762c434..f8eaabcbf6e 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java
@@ -21,6 +21,8 @@
import org.xvm.util.Severity;
+import static java.util.Objects.requireNonNull;
+
/**
* The AnonInnerClass represents the suggested shape for an anonymous inner class.
*/
@@ -33,7 +35,7 @@ public class AnonInnerClass {
*/
public AnonInnerClass(TypeExpression expr, ErrorListener errs) {
assert expr != null;
- assert errs != null;
+ requireNonNull(errs, "errs");
m_exprType = expr;
f_errs = errs;
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java
index 97a9d77a931..faf549c4dc3 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java
@@ -19,10 +19,10 @@
import org.xvm.asm.constants.ArrayConstant;
import org.xvm.asm.constants.IntConstant;
-import org.xvm.asm.constants.RangeConstant;
import org.xvm.asm.constants.MapConstant;
import org.xvm.asm.constants.MethodConstant;
import org.xvm.asm.constants.MethodInfo;
+import org.xvm.asm.constants.RangeConstant;
import org.xvm.asm.constants.SignatureConstant;
import org.xvm.asm.constants.TypeConstant;
import org.xvm.asm.constants.TypeInfo;
@@ -38,6 +38,9 @@
import org.xvm.util.PackedInteger;
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* An array access expression is an expression followed by an array index expression.
*
@@ -191,6 +194,8 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti
}
}
+ ErrorListener probe = silent(PROBE);
+
// at this point, either the "array" expression has no idea what type it will result in, or
// its best guess doesn't have an array operator that results in the required type; "probe"
// for some likely types that the array access operators are known to exist on
@@ -206,7 +211,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti
// (which must be determinable at compile time in order to know which tuple elements
// are being accessed)
if (typeTarget != null && typeTarget.isTuple()
- || expr.testFit(ctx, pool.typeTuple(), fExhaustive, null) == TypeFit.Fit) {
+ || expr.testFit(ctx, pool.typeTuple(), fExhaustive, probe) == TypeFit.Fit) {
TypeConstant typeTest = determineTupleTestType(typeRequired);
return typeTest == null
// the only thing that we can say for sure at this point is that the return
@@ -227,7 +232,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti
// this:type) and index could be a Range
// array[index]
- if (!isSliceOp() && exprIndex.testFit(ctx, pool.typeInt64(), fExhaustive, null).isFit()) {
+ if (!isSliceOp() && exprIndex.testFit(ctx, pool.typeInt64(), fExhaustive, probe).isFit()) {
return exprTarget.testFit(ctx, pool.ensureParameterizedTypeConstant(
pool.typeList(), typeRequired), fExhaustive, errs);
}
@@ -235,7 +240,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti
// array[index..index] or array[index..index)
// REVIEW what if it is a Range or Range ???
if (exprTarget.testFit(ctx, typeRequired, fExhaustive, errs) == TypeFit.Fit
- && exprIndex.testFit(ctx, pool.ensureRangeType(pool.typeInt64()), fExhaustive, null).isFit()) {
+ && exprIndex.testFit(ctx, pool.ensureRangeType(pool.typeInt64()), fExhaustive, probe).isFit()) {
return TypeFit.Fit;
}
} else { // not a List, but might still be UniformIndexed and/or Sliceable
@@ -261,7 +266,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti
// Index -> Element - probably does not matter what indexes are, since
// Sliceable ops return this:type
if (testType(ctx, exprTarget, typeTarget, pool.typeSliceable())
- && exprIndex.testFit(ctx, pool.typeRange(), fExhaustive, null).isFit()) {
+ && exprIndex.testFit(ctx, pool.typeRange(), fExhaustive, probe).isFit()) {
fit = exprTarget.testFit(ctx, typeRequired, fExhaustive, errs);
if (fit.isFit()) {
return fit;
@@ -291,16 +296,17 @@ && testType(ctx, exprTarget, typeTarget, pool.typeMap())) {
Expression exprRow = indexes.get(1);
// matrix[index,index]
- if (exprCol.testFit(ctx, pool.typeInt64(), fExhaustive, null).isFit() &&
- exprRow.testFit(ctx, pool.typeInt64(), fExhaustive, null).isFit()) {
+ if (exprCol.testFit(ctx, pool.typeInt64(), fExhaustive, probe).isFit() &&
+ exprRow.testFit(ctx, pool.typeInt64(), fExhaustive, probe).isFit()) {
return expr.testFit(ctx, pool.ensureParameterizedTypeConstant(
pool.typeMatrix(), typeRequired), fExhaustive, errs);
}
// matrix[index..index,index..index]
TypeConstant typeInterval = pool.ensureRangeType(pool.typeInt64());
- if (typeRequired.isA(pool.typeMatrix()) && exprCol.testFit(ctx, typeInterval, fExhaustive, null).isFit()
- && exprRow.testFit(ctx, typeInterval, fExhaustive, null).isFit()) {
+ if (typeRequired.isA(pool.typeMatrix())
+ && exprCol.testFit(ctx, typeInterval, fExhaustive, probe).isFit()
+ && exprRow.testFit(ctx, typeInterval, fExhaustive, probe).isFit()) {
// REVIEW same issue as array above
TypeConstant typeElement = typeRequired.resolveGenericType("Element");
return typeElement == null
@@ -327,14 +333,16 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
int cIndexes = indexes.size();
Expression[] aexprIndexes = indexes.toArray(new Expression[cIndexes]);
+ ErrorListener probe = silent(PROBE);
+
// test if the access is against a tuple expression
if (typeArray != null && typeArray.isTuple()
- || typeArray == null && exprArray.testFit(ctx, pool.typeTuple(), false, null) == TypeFit.Fit) {
+ || typeArray == null && exprArray.testFit(ctx, pool.typeTuple(), false, probe) == TypeFit.Fit) {
if (typeArray == null) {
typeArrayReq = pool.typeTuple();
} else {
TypeConstant typeTupleTest = determineTupleTestType(typeRequired);
- if (exprArray.testFit(ctx, typeTupleTest, false, null).isFit()) {
+ if (exprArray.testFit(ctx, typeTupleTest, false, probe).isFit()) {
typeArrayReq = typeTupleTest;
}
}
@@ -344,11 +352,11 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
if (typeRequired != null) {
// array[index]
TypeConstant typeElement = null;
- if (!isSliceOp() && aexprIndexes[0].testFit(ctx, pool.typeInt64(), false, null).isFit()) {
+ if (!isSliceOp() && aexprIndexes[0].testFit(ctx, pool.typeInt64(), false, probe).isFit()) {
typeElement = typeRequired;
// array[index..index] or array[index..index)
} else if (typeRequired.isA(pool.typeList()) && aexprIndexes[0].testFit(ctx,
- pool.ensureRangeType(pool.typeInt64()), false, null).isFit()) {
+ pool.ensureRangeType(pool.typeInt64()), false, probe).isFit()) {
// REVIEW keep this in sync with testFit()
typeElement = typeRequired.resolveGenericType("Element");
}
@@ -356,7 +364,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
if (typeElement != null) {
TypeConstant typeArrayTest =
pool.ensureParameterizedTypeConstant(typeArrayReq, typeElement);
- if (exprArray.testFit(ctx, typeArrayTest, false, null).isFit()) {
+ if (exprArray.testFit(ctx, typeArrayTest, false, probe).isFit()) {
typeArrayReq = typeArrayTest;
}
}
@@ -369,7 +377,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
TypeConstant typeIndex = typeArray == null
? null
: typeArray.resolveGenericType("Index");
- if (typeIndex == null || !aexprIndexes[0].testFit(ctx, typeIndex, false, null).isFit()) {
+ if (typeIndex == null || !aexprIndexes[0].testFit(ctx, typeIndex, false, probe).isFit()) {
typeIndex = aexprIndexes[0].getImplicitType(ctx);
typeIndex = determineIndexType(ctx, exprArray, typeArray, aexprIndexes, typeIndex);
}
@@ -379,7 +387,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
if (typeRequired != null) {
TypeConstant typeTest = pool.ensureParameterizedTypeConstant(
pool.typeIndexed(), typeIndex, typeRequired);
- if (exprArray.testFit(ctx, typeTest, false, null).isFit()) {
+ if (exprArray.testFit(ctx, typeTest, false, probe).isFit()) {
// we figured out what to ask for, including the index and element types
typeElement = typeRequired;
typeArrayReq = typeTest;
@@ -390,7 +398,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
// we can only figure out the index type
TypeConstant typeArrayTest =
pool.ensureParameterizedTypeConstant(typeArrayReq, typeIndex);
- if (exprArray.testFit(ctx, typeArrayTest, false, null).isFit()) {
+ if (exprArray.testFit(ctx, typeArrayTest, false, probe).isFit()) {
typeArrayReq = typeArrayTest;
}
}
@@ -404,14 +412,14 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
// matrix[index,index]
TypeConstant typeElement = null;
- if (exprCol.testFit(ctx, pool.typeInt64(), false, null).isFit() &&
- exprRow.testFit(ctx, pool.typeInt64(), false, null).isFit()) {
+ if (exprCol.testFit(ctx, pool.typeInt64(), false, probe).isFit() &&
+ exprRow.testFit(ctx, pool.typeInt64(), false, probe).isFit()) {
typeElement = typeRequired;
// array[index..index]
} else if (typeRequired.isA(pool.typeInterval())) {
TypeConstant typeIntInterval = pool.ensureRangeType(pool.typeInt64());
- if (exprCol.testFit(ctx, typeIntInterval, false, null).isFit() &&
- exprRow.testFit(ctx, typeIntInterval, false, null).isFit()) {
+ if (exprCol.testFit(ctx, typeIntInterval, false, probe).isFit() &&
+ exprRow.testFit(ctx, typeIntInterval, false, probe).isFit()) {
// REVIEW keep this in sync with testFit()
typeElement = typeRequired.resolveGenericType("Element");
}
@@ -420,7 +428,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
if (typeElement != null) {
TypeConstant typeArrayTest =
pool.ensureParameterizedTypeConstant(typeArrayReq, typeRequired);
- if (exprArray.testFit(ctx, typeArrayTest, false, null).isFit()) {
+ if (exprArray.testFit(ctx, typeArrayTest, false, probe).isFit()) {
typeArrayReq = typeArrayTest;
}
}
@@ -493,8 +501,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
System.arraycopy(aexprIndexes, 0, aExprSet, 0, cIndexes);
// note: cannot fill in the expression that represents the value being set because it
// hasn't yet been validated (for example, if our parent is an AssignmentStatement)
- m_idSet = findOpMethod(ctx, typeArray, "setElement", "[]=", aExprSet, null,
- ErrorListener.BLACKHOLE);
+ m_idSet = findOpMethod(ctx, typeArray, "setElement", "[]=", aExprSet, null, probe);
}
// tuple is different from other container types (like array) in that every field in the
@@ -669,6 +676,8 @@ private MethodConstant findArrayAccessor(
Set setMatch = new HashSet<>();
Map mapMethods = new HashMap<>();
+ ErrorListener probe = silent(PROBE);
+
Set setAll = findPotentialOps(infoTarget, cArgs);
NextOp: for (MethodConstant idMethod : setAll) {
SignatureConstant sig = idMethod.getSignature();
@@ -701,7 +710,7 @@ private MethodConstant findArrayAccessor(
TypeConstant typeArg = atypeArgs[i];
if (typeArg == null || !isAssignable(ctx, typeArg, typeParam)) {
Expression exprArg = aexprArgs[i];
- if (!exprArg.testFit(ctx, typeParam, false, null).isFit()) {
+ if (!exprArg.testFit(ctx, typeParam, false, probe).isFit()) {
continue NextOp;
}
}
@@ -722,7 +731,7 @@ private MethodConstant findArrayAccessor(
return switch (setMatch.size()) {
case 0 -> null;
case 1 -> setMatch.iterator().next();
- default -> chooseBest(setMatch, typeTarget, mapMethods, ErrorListener.BLACKHOLE);
+ default -> chooseBest(setMatch, typeTarget, mapMethods, probe);
};
}
@@ -860,7 +869,7 @@ private boolean indexesFit(Context ctx, MethodConstant method) {
int cTypes = atypes.length;
if (cTypes == indexes.size()) {
for (int i = 0; i < cTypes; ++i) {
- if (!indexes.get(i).testFit(ctx, atypes[i], false, null).isFit()) {
+ if (!indexes.get(i).testFit(ctx, atypes[i], false, silent(PROBE)).isFit()) {
return false;
}
}
@@ -1111,7 +1120,7 @@ TypeConstant estimateIndexType(Context ctx, TypeConstant typeTarget, String sInd
TypeConstant typeIndex = typeTarget == null
? null
: typeTarget.resolveGenericType(sIndexParam);
- if (typeIndex == null || !exprIndex.testFit(ctx, typeIndex, false, null).isFit()) {
+ if (typeIndex == null || !exprIndex.testFit(ctx, typeIndex, false, silent(PROBE)).isFit()) {
typeIndex = exprIndex.getImplicitType(ctx);
}
return typeIndex;
@@ -1131,11 +1140,12 @@ TypeConstant estimateIndexType(Context ctx, TypeConstant typeTarget, String sInd
*/
private TypeConstant determineIndexType(Context ctx, Expression exprArray,
TypeConstant typeArray, Expression[] aexprIndexes, TypeConstant typeIndex) {
+ ErrorListener probe = silent(PROBE);
+
ConstantPool pool = pool();
if (typeIndex == null) {
if (typeArray != null) {
- MethodConstant id = findOpMethod(ctx, typeArray, "getElement", "[]", aexprIndexes,
- null, ErrorListener.BLACKHOLE);
+ MethodConstant id = findOpMethod(ctx, typeArray, "getElement", "[]", aexprIndexes, null, probe);
if (id != null && id.getRawReturns().length >= 1) {
typeIndex = id.getRawReturns()[0];
if (!pool.typeObject().isA(typeIndex)) {
@@ -1148,7 +1158,7 @@ private TypeConstant determineIndexType(Context ctx, Expression exprArray,
}
if (exprArray.testFit(ctx,
- pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, null).isFit()) {
+ pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, probe).isFit()) {
return typeIndex;
}
@@ -1156,7 +1166,7 @@ private TypeConstant determineIndexType(Context ctx, Expression exprArray,
for (MethodInfo info : setInfos) {
typeIndex = info.getSignature().getRawReturns()[0];
if (exprArray.testFit(ctx,
- pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, null).isFit()) {
+ pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, probe).isFit()) {
return typeIndex;
}
}
@@ -1179,7 +1189,7 @@ private boolean testType(Context ctx, Expression expr, TypeConstant typeExpr, Ty
return true;
}
- return expr.testFit(ctx, typeTest, false, null) == TypeFit.Fit;
+ return expr.testFit(ctx, typeTest, false, silent(PROBE)) == TypeFit.Fit;
}
/**
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java
index 1c0afec533a..50eb62a2c90 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java
@@ -13,6 +13,9 @@
import org.xvm.compiler.Token;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* Expression for "expression as type".
*/
@@ -35,7 +38,7 @@ public long getEndPosition() {
@Override
public TypeConstant getImplicitType(Context ctx) {
- return ((TypeExpression) expr2).ensureTypeConstant(ctx, null);
+ return ((TypeExpression) expr2).ensureTypeConstant(ctx, silent(PROBE));
}
@Override
@@ -59,7 +62,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe
TypeConstant type = exprType.ensureTypeConstant(ctx, errs).resolveAutoNarrowingBase();
- if (!exprType.isDynamic() && expr1.testFit(ctx, type, false, null).isFit()) {
+ if (!exprType.isDynamic() && expr1.testFit(ctx, type, false, silent(PROBE)).isFit()) {
typeRequest = type;
m_fCastRequired = false;
}
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java
index 0d46b4e1c4b..e3256469227 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java
@@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import org.xvm.asm.Argument;
import org.xvm.asm.Assignment;
@@ -591,11 +592,7 @@ public String toString() {
if (!conds.isEmpty()) {
sb.append(' ')
- .append(conds.get(0));
- for (int i = 1, c = conds.size(); i < c; ++i) {
- sb.append(", ")
- .append(conds.get(i));
- }
+ .append(conds.stream().map(Object::toString).collect(Collectors.joining(", ")));
}
sb.append(';');
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java
index 8ebc52bc5b8..a87a50065b5 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java
@@ -34,6 +34,9 @@
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* An assignment statement specifies an l-value, an assignment operator, and an r-value.
*
@@ -386,6 +389,8 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) {
return null;
}
+ ErrorListener probe = silent(PROBE);
+
// regardless of whether the LValue is a statement or expression, all L-Values must be able
// to provide an expression as a representative form
Expression exprLeft = nodeLeft.getLValueExpression();
@@ -407,12 +412,12 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) {
// contributions
Context ctxInfer = ctxRValue.enterInferring(atypeLeft[0]);
- TypeFit fit = rvalue.testFitMulti(ctxInfer, atypeTest, false, null);
+ TypeFit fit = rvalue.testFitMulti(ctxInfer, atypeTest, false, probe);
if (!fit.isFit() && cLeft > 1) {
Expression exprUnpack = new UnpackExpression(rvalue, null);
- fit = exprUnpack.testFitMulti(ctxInfer, atypeTest, false, null);
+ fit = exprUnpack.testFitMulti(ctxInfer, atypeTest, false, probe);
if (fit.isFit()) {
rvalue = exprUnpack;
}
@@ -456,7 +461,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) {
if (exprLeft instanceof NameExpression exprName && exprName.isDynamicVar()) {
// test for a future assignment first
TypeConstant typeFuture = pool.ensureFuture(typeLeft);
- if (rvalue.testFit(ctxRValue, typeFuture, false, null).isFit()) {
+ if (rvalue.testFit(ctxRValue, typeFuture, false, probe).isFit()) {
typeLeft = typeFuture;
}
}
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java
index d6d613d4ea7..88756897bcf 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java
@@ -18,8 +18,12 @@
import java.util.NoSuchElementException;
import java.util.Set;
+import java.util.function.BiConsumer;
+
import java.util.stream.Collectors;
+import org.jetbrains.annotations.NotNull;
+
import org.xvm.asm.Argument;
import org.xvm.asm.Component;
import org.xvm.asm.ComponentResolver;
@@ -51,12 +55,20 @@
import org.xvm.compiler.Token;
import org.xvm.compiler.ast.Expression.TypeFit;
import org.xvm.compiler.ast.NameExpression.Meaning;
+import org.xvm.compiler.ast.bindings.CursorBinding;
+import org.xvm.compiler.ast.bindings.InvocationBinding;
import org.xvm.util.ListMap;
import org.xvm.util.Severity;
import static org.xvm.util.Handy.indentLines;
+import static org.xvm.asm.ErrorListener.NOWHERE;
+import static org.xvm.asm.ErrorListener.in;
+
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* Common base class for all statements and expressions.
*/
@@ -124,15 +136,29 @@ protected T adopt(T child) {
}
/**
- * Return an Iterable/Iterator that represents all the child nodes of this node.
+ * Return a one-shot cursor over the child nodes of this node. The cursor supports replacement
+ * and removal during compiler traversal. Its Iterable view uses the same cursor, not a new
+ * traversal; use {@link #childNodes()} when an independently iterable view is needed.
*
- * @return an Iterable of child nodes (from whence an Iterator can be obtained)
+ * @return a mutable traversal cursor
*/
public ChildIterator children() {
Field[] fields = getChildFields();
return fields.length == 0 ? ChildIterator.EMPTY : new ChildIteratorImpl(fields);
}
+ /**
+ * Return a view of this node's children that starts an independent traversal for each iterator.
+ * This is a live view, not a snapshot: subsequent traversals see subsequent AST changes.
+ * Callers that need a stable list must copy it while they own the tree; iteration does not
+ * provide synchronization with compiler mutation.
+ *
+ * @return an iterable view of the child nodes
+ */
+ public Iterable childNodes() {
+ return this::children;
+ }
+
/**
* Replace the specified child of this AstNode with a new child.
*
@@ -201,8 +227,39 @@ protected Field[] getChildFields() {
return NO_FIELDS;
}
+ /**
+ * Compatibility entry point for {@link #copyTree()}.
+ *
+ * @return a tree copy of this node
+ */
@Override
+ @SuppressWarnings("MethodDoesntCallSuperMethod") // copyTree owns allocation and copying.
public AstNode clone() {
+ return copyTree();
+ }
+
+ /**
+ * Copy this node and its registered syntax children, preserving the concrete node type, source
+ * positions and compilation stage. Copied children belong to their copied owners. The root
+ * retains its original parent until a caller adopts it into another tree; copying does not
+ * replace or reparent any original node.
+ *
+ * This is not a fresh parse or an isolated copy of all compiler state. The default
+ * implementation shallow-copies non-child fields, including resolved bindings and transient
+ * fields. Overrides must handle owned state that cannot be shared, such as a lambda's
+ * generated method. Recovery nodes construct fresh shells and copy their written syntax
+ * without carrying inherited validation or emission state.
+ *
+ *
The default implementation still uses shallow cloning followed by reflective copying of
+ * registered children; copied child lists are mutable. Constructor-based overrides must copy
+ * and adopt their children and preserve root metadata with {@link #copyTreeMetadataTo(AstNode)}.
+ * Specialize this method rather than {@link #clone()} so both entry points behave alike.
+ * Declare a covariant return type when specializing, so callers can copy that node type without
+ * a cast. A caller-selected method type parameter would not enforce that relationship.
+ *
+ * @return a tree copy of this node
+ */
+ public AstNode copyTree() {
AstNode that;
try {
that = (AstNode) super.clone();
@@ -222,21 +279,19 @@ public AstNode clone() {
if (oVal != null) {
if (oVal instanceof AstNode node) {
- AstNode nodeNew = node.clone();
+ var nodeNew = node.copyTree();
that.adopt(nodeNew);
oVal = nodeNew;
- } else if (oVal instanceof List list) {
- ArrayList listNew = new ArrayList<>();
- for (AstNode node : (List) list) {
- listNew.add(node.clone());
- }
-
+ } else if (oVal instanceof List> list) {
+ var listNew = list.stream()
+ .map(AstNode.class::cast)
+ .map(AstNode::copyTree)
+ .collect(Collectors.toCollection(ArrayList::new));
that.adopt(listNew);
oVal = listNew;
} else {
- throw new IllegalStateException(
- "unsupported container type: " + oVal.getClass().getSimpleName());
+ throw new IllegalStateException("unsupported container type: " + oVal.getClass().getSimpleName());
}
try {
@@ -250,6 +305,21 @@ public AstNode clone() {
return that;
}
+ /**
+ * Preserve the root metadata of a constructor-based tree copy. The caller is responsible for
+ * copying and adopting children; this method does not transfer validation or emission state.
+ *
+ * @param copy the newly constructed node
+ * @param the concrete node type
+ *
+ * @return the supplied copy with this node's parent and compilation stage
+ */
+ protected final T copyTreeMetadataTo(T copy) {
+ copy.setParent(getParent());
+ copy.setStage(getStage());
+ return copy;
+ }
+
/**
* @return the current compilation stage for this node
*/
@@ -371,12 +441,15 @@ protected AstNode getCodeContainer() {
/**
* @return a unique counter in the context of the code container
+ *
+ * @throws IllegalStateException if this node has no enclosing code container
*/
protected int getCodeContainerCounter() {
AstNode parent = getParent();
- return parent == null
- ? null
- : parent.getCodeContainerCounter();
+ if (parent == null) {
+ throw new IllegalStateException("No enclosing code container for " + getClass().getSimpleName());
+ }
+ return parent.getCodeContainerCounter();
}
/**
@@ -638,16 +711,12 @@ public Access getDefaultAccess() {
* @param sCode the error code that identifies the error message
* @param aoParam the parameters for the error message; may be null
*
- * @return true to attempt to abort the process that reported the error, or
- * false to attempt to continue the process
*/
- public boolean log(ErrorListener errs, Severity severity, String sCode, Object... aoParam) {
+ public void log(ErrorListener errs, Severity severity, String sCode, Object... aoParam) {
Source source = getSource();
- return errs == null
- ? severity.ordinal() >= Severity.ERROR.ordinal()
- : errs.log(severity, sCode, aoParam, source,
- source == null ? 0L : getStartPosition(),
- source == null ? 0L : getEndPosition());
+ errs.log(severity, sCode, source == null
+ ? NOWHERE
+ : in(source, getStartPosition(), getEndPosition()), aoParam);
}
// ----- compile phases ------------------------------------------------------------------------
@@ -660,9 +729,9 @@ public boolean log(ErrorListener errs, Severity severity, String sCode, Object..
* checking for errors that are obvious from "this point down" (no lateral evaluation of
* structures, because we can't count on them even existing yet.)
*
- * The general idea is that this method recurses through the structure, allowing each node
- * to introduce itself as the parent of each node under it, and that the nodes which will be
- * structures in the resulting FileStructure will register themselves.
+ * This method recurses through the tree, establishing each node as the parent of its
+ * children. Nodes that represent structures in the resulting FileStructure also register
+ * themselves.
*
* Type parameters for the types must also be registered, because they are also types, and
* they will be required to already be present when the second pass begins.
@@ -683,9 +752,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) {
* compilation is not only unpredictable, but the potential exists for dependencies in either
* direction (first to last and/or vice versa).
*
- * As a result, some questions may come to an AstNode to resolve that it is not yet prepared to
- * resolve, in which case the caller (another AstNode) has to add itself to the list of nodes
- * that require another pass.
+ *
A node may be asked to resolve a name before its dependencies are ready. In that case,
+ * the calling node must defer its own resolution to another pass.
*
*
* - Packages that import modules are able to verify that those modules are available to
@@ -733,19 +801,20 @@ protected void updateLineNumber(Code code) {
/**
* If any of the children of this node have been previously deferred, catch them up now.
*
- * @param errs the error list to log to
+ * @param errs the error list to log to
+ * @param bindings the enclosing compilation attempt's invocation collector
+ * @param cursors the enclosing compilation attempt's cursor collector
*
* @return true if the children got caught up; false if the catch-up aborted
*/
- protected boolean catchUpChildren(ErrorListener errs) {
+ protected boolean catchUpChildren(ErrorListener errs, InvocationBinding.Collector bindings,
+ CursorBinding.Collector cursors) {
// determine what stage we're trying to catch the children up to
Stage stageTarget = getStage();
if (!stageTarget.isTargetable()) {
- // we want to catch up to the last target that this component completed, and not
- // actually pass the stage that this component is at, which can be accomplished using
- // StageMgr processChildrenExcept() or processChildren() if the children need to
- // complete the stage that this node is currently working on (if this node is currently
- // in a transition stage)
+ // catch up only to the last completed target stage
+ // use StageMgr.processChildrenExcept() or processChildren() when children must
+ // complete the stage this node is currently processing
stageTarget = stageTarget.prevTarget();
if (!stageTarget.isTargetable()) {
assert stageTarget == Stage.Initial;
@@ -777,7 +846,7 @@ protected boolean catchUpChildren(ErrorListener errs) {
ErrorListener errsTemp = errs.branch(this);
while (stageOldest.compareTo(stageTarget) < 0) {
Stage stageNext = stageOldest.nextTarget();
- StageMgr mgrKids = new StageMgr(listChildren, stageNext, errsTemp);
+ StageMgr mgrKids = new StageMgr(listChildren, stageNext, errsTemp, bindings, cursors);
for (int cTries = 0; !mgrKids.processComplete(); cTries++) {
if (errsTemp.isAbortDesired() || cTries > 20) {
mgrKids.logDeferredAsErrors(errsTemp);
@@ -824,9 +893,8 @@ protected boolean canResolveNames() {
return !resolver.getNameResolver().isFirstTime();
}
- // for all other components (that don't override this method because they know more about
- // whether or not they can resolve names), we'll assume that if they haven't been resolved,
- // then they don't know how to resolve names
+ // other components must have reached name resolution before they can resolve names
+ // subclasses can override this method when they have more specific readiness checks
return alreadyReached(Stage.Resolving);
}
@@ -902,10 +970,10 @@ protected TypeConstant[] validateExpressions(Context ctx, List listE
* that are not explicitly specified are considered to be assigned to their default values. In
* the case of a non-call, the default method parameters are treated in the same manner as
* others, and any parameter that is not explicitly specified remains un-bound. For example,
- * having a function void foo(Int a, Int b = 0, Boolean c = False, Int d = 1) a [call]
- * expression "foo(1, c=True)" will result into a function call "foo(1, 0, True, 1)", while the
- * equivalent [bind] expression {@code &foo(1, c=True)} will result into a function of type
- * "function void (Int, Int)", where parameters "b" and "d" remain unbound.
+ * given {@code void foo(Int a, Int b = 0, Boolean c = False, Int d = 1)}, the call expression
+ * {@code foo(1, c=True)} invokes {@code foo(1, 0, True, 1)}, while the equivalent binding
+ * expression {@code &foo(1, c=True)} produces a function of type {@code function void (Int, Int)},
+ * where parameters {@code b} and {@code d} remain unbound.
*
* @param ctx the compilation context
* @param typeTarget the type to search the method or function for
@@ -973,7 +1041,7 @@ protected MethodConstant findMethod(Context ctx, TypeConstant typeTarget, TypeIn
mapNamedExpr, atypeReturn, setIs, setConvert, mapMethods, errsTemp);
// now choose the best match
- boolean fArgsComplete = cArgs == 0 || listExprArgs.stream().allMatch(Expression::isCompletable);
+ boolean fArgsComplete = listExprArgs == null || listExprArgs.stream().allMatch(Expression::isCompletable);
if (!setIs.isEmpty()) {
return fArgsComplete
? chooseBest(setIs, typeTarget, mapMethods, errs)
@@ -1074,6 +1142,29 @@ private void collectMatchingMethods(
Set setConvert,
Map mapMethods,
ErrorListener errs) {
+ collectMatchingMethods(ctx, typeTarget, infoTarget, setMethods, listExprArgs, fCall,
+ mapNamedExpr, atypeReturn, setIs, setConvert, mapMethods, errs, (_, _) -> {});
+ }
+
+ /**
+ * Reuse ordinary argument fitting. The package-private observer lets partial-call queries copy
+ * tentative signatures and argument order without adding cursor-result policy to the fitter.
+ * The callback runs synchronously after a match is recorded in the supplied collections.
+ */
+ final void collectMatchingMethods(
+ Context ctx,
+ TypeConstant typeTarget,
+ TypeInfo infoTarget,
+ Set setMethods,
+ List listExprArgs,
+ boolean fCall,
+ Map mapNamedExpr,
+ TypeConstant[] atypeReturn,
+ Set setIs,
+ Set setConvert,
+ Map mapMethods,
+ ErrorListener errs,
+ BiConsumer> matching) {
ConstantPool pool = pool();
int cExprs = listExprArgs == null ? 0 : listExprArgs.size();
int cReturns = atypeReturn == null ? 0 : atypeReturn.length;
@@ -1217,7 +1308,7 @@ private void collectMatchingMethods(
lit.getLiteral().getValueText());
} else {
if (exprArg instanceof NameExpression exprName) {
- typeExpr = exprName.getImplicitType(ctx, typeParam, ErrorListener.BLACKHOLE);
+ typeExpr = exprName.getImplicitType(ctx, typeParam, silent(PROBE));
}
log(errsTemp, Severity.ERROR, Compiler.INCOMPATIBLE_PARAMETER_TYPE,
@@ -1289,6 +1380,7 @@ private void collectMatchingMethods(
setIs.add(idMethod);
}
mapMethods.put(idMethod, method);
+ matching.accept(sigMethod, listArgs);
}
// if there is any ambiguity, don't report anything; the caller will log a generic
@@ -1327,7 +1419,7 @@ protected TypeConstant[] transformTypeArguments(Context ctx,
protected TypeConstant transformType(Context ctx, NameExpression exprName) {
ConstantPool pool = pool();
TypeConstant type = pool.typeType();
- Argument arg = exprName.resolveRawArgument(ctx, false, ErrorListener.BLACKHOLE);
+ Argument arg = exprName.resolveRawArgument(ctx, false, silent(PROBE));
if (arg instanceof Register reg) {
PropertyConstant idProp = type.ensureTypeInfo().findProperty("DataType").getIdentity();
FormalConstant idFormal = pool.ensureDynamicFormal(
@@ -1648,12 +1740,12 @@ protected void dump(PrintWriter out, String sIndentFirst, String sIndent) {
Object value = entry.getValue();
switch (value) {
case null -> iter.remove();
- case Map map -> {
+ case Map, ?> map -> {
if (map.isEmpty()) {
iter.remove();
}
}
- case Collection coll -> {
+ case Collection> coll -> {
if (coll.isEmpty()) {
iter.remove();
}
@@ -1700,14 +1792,14 @@ protected void dump(PrintWriter out, String sIndentFirst, String sIndent) {
out.print(sIndentCat + sCat);
// find the kids
- int cKids;
- Iterator iterK;
+ int cKids;
+ Iterator> iterK;
switch (value) {
- case Map kids -> {
+ case Map, ?> kids -> {
cKids = kids.size();
iterK = kids.entrySet().iterator();
}
- case Collection kids -> {
+ case Collection> kids -> {
cKids = kids.size();
iterK = kids.iterator();
}
@@ -1733,7 +1825,7 @@ protected void dump(PrintWriter out, String sIndentFirst, String sIndent) {
out.println();
}
node.dump(out, sIndent1, sIndent2);
- } else if (kid instanceof Map.Entry) {
+ } else if (kid instanceof Map.Entry, ?>) {
if (fFirstK) {
out.println();
}
@@ -1772,11 +1864,9 @@ public String getDumpDesc() {
/**
* Build and return a map that allows the caller to navigate the children of this node.
*
- *
Assume some type T which represents either an AstNode instance, or an object that implements
- * toString(). The keys of the map should be strings that describe the categories of the
- * children, while the values should provide the info about the children of this AstNode,
- * either as an object of type T, a Collection of type T, an array of type T, or a Map whose
- * keys and values are of type T.
+ *
Keys describe the categories of children. Each value is a child, a collection or array
+ * of children, or a map whose keys and values describe children. A child can be an AstNode
+ * or another object with a useful {@code toString()} representation.
*
* @return a map containing all the child information to dump
*/
@@ -1817,13 +1907,14 @@ protected UnsupportedOperationException notImplemented() {
* @return an ArrayList
*/
protected static ArrayList ensureArrayList(List list) {
- return list instanceof ArrayList alist
+ return list instanceof ArrayList alist
? alist
: new ArrayList<>(list);
}
/**
- * Collect fields by name.
+ * Collect fields by name. Nodes in another package of this compiler module grant access only
+ * to these explicitly registered child fields; their representation need not become public.
*
* @param clz the class on which the fields exist
* @param names the field names
@@ -1842,7 +1933,6 @@ protected static Field[] fieldsForNames(Class> clz, String... names) {
while (clzTry != null) {
try {
Field field = clzTry.getDeclaredField(names[i]);
- assert field != null;
Class> clzField = field.getType();
if (!AstNode.class.isAssignableFrom(clzField)
&& !List.class.isAssignableFrom(clzField)) {
@@ -1850,6 +1940,14 @@ protected static Field[] fieldsForNames(Class> clz, String... names) {
+ clzField.getSimpleName() + " on field "
+ clzTry.getSimpleName() + '.' + names[i]);
}
+ if (!clzTry.getPackageName().equals(AstNode.class.getPackageName())
+ && (!AstNode.class.isAssignableFrom(clz)
+ || clz.getModule() != AstNode.class.getModule()
+ || clzTry.getModule() != AstNode.class.getModule()
+ || !field.trySetAccessible())) {
+ throw new IllegalStateException("cannot access registered child field "
+ + clzTry.getName() + '.' + names[i]);
+ }
fields[i] = field;
continue NextField;
} catch (NoSuchFieldException e) {
@@ -1873,11 +1971,15 @@ protected static Field[] fieldsForNames(Class> clz, String... names) {
// ----- inner class: ChildIterator ------------------------------------------------------------
/**
- * Represents an Iterator that can also replace the most recently iterated element.
+ * Represents an Iterator that can also replace the most recently iterated element. Iterable
+ * is retained for compatibility with enhanced-for loops that mutate through this same cursor.
+ * Calling {@link #iterator()} does not restart traversal; readers can use
+ * {@link AstNode#childNodes()} for independent iterations.
*/
public interface ChildIterator
extends Iterable, Iterator {
@Override
+ @NotNull
default Iterator iterator() {
return this;
}
@@ -1905,6 +2007,7 @@ public AstNode next() {
};
}
+ /** Traverses registered child fields, retaining their mutation cursor for replacement/removal. */
protected final class ChildIteratorImpl
implements ChildIterator {
/**
@@ -1917,17 +2020,19 @@ private ChildIteratorImpl(Field[] fields) {
this.fields = fields;
}
+ @Override
public boolean hasNext() {
return state == HAS_NEXT || prepareNextElement();
}
+ @Override
public AstNode next() {
if (state == HAS_NEXT || prepareNextElement()) {
state = HAS_PREV;
if (value instanceof AstNode node) {
return node;
} else {
- return (AstNode) ((Iterator) value).next();
+ return (AstNode) ((Iterator>) value).next();
}
}
@@ -1935,7 +2040,7 @@ public AstNode next() {
}
private boolean prepareNextElement() {
- if (value instanceof Iterator iter && iter.hasNext()) {
+ if (value instanceof Iterator> iter && iter.hasNext()) {
state = HAS_NEXT;
return true;
}
@@ -1959,12 +2064,12 @@ private boolean prepareNextField() {
}
if (next != null) {
- if (next instanceof List list) {
+ if (next instanceof List> list) {
if (!list.isEmpty()) {
value = list.listIterator();
return true;
}
- } else if (next instanceof Collection coll) {
+ } else if (next instanceof Collection> coll) {
if (!coll.isEmpty()) {
value = coll.iterator();
return true;
@@ -1981,6 +2086,7 @@ private boolean prepareNextField() {
return false;
}
+ @Override
public void remove() {
if (state == HAS_PREV) {
if (value instanceof AstNode) {
@@ -1994,7 +2100,7 @@ public void remove() {
return;
}
- if (value instanceof Iterator iter) {
+ if (value instanceof Iterator> iter) {
// tell the underlying iterator to remove the value
iter.remove();
state = NOT_PREP;
@@ -2005,6 +2111,7 @@ public void remove() {
throw new IllegalStateException();
}
+ @Override
public void replaceWith(AstNode newChild) {
if (state == HAS_PREV) {
if (value instanceof AstNode) {
@@ -2017,8 +2124,11 @@ public void replaceWith(AstNode newChild) {
return;
}
- if (value instanceof ListIterator iter) {
- iter.set(newChild);
+ if (value instanceof ListIterator> iter) {
+ // Registered child lists contain AstNodes; reflection erases their element type.
+ @SuppressWarnings("unchecked")
+ var children = (ListIterator) iter;
+ children.set(newChild);
return;
}
}
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/BindingAst.java b/javatools/src/main/java/org/xvm/compiler/ast/BindingAst.java
new file mode 100644
index 00000000000..f70373ea958
--- /dev/null
+++ b/javatools/src/main/java/org/xvm/compiler/ast/BindingAst.java
@@ -0,0 +1,36 @@
+package org.xvm.compiler.ast;
+
+import org.xvm.asm.Register;
+
+import org.xvm.compiler.Token;
+
+/**
+ * Internal bridge for source bindings that need access to ordinary AST parameter slots.
+ * Java subpackages do not share package access; this preserves the existing visibility of
+ * written parameter lists and their binding setter while fact collection lives in ast.bindings.
+ * Hosts read the published bindings instead of calling this compiler-worker operation.
+ */
+public final class BindingAst {
+ private BindingAst() {}
+
+ /**
+ * Associate an existing lambda parameter register with its written declaration.
+ * This neither allocates a register nor changes capture discovery or parameter ordering.
+ *
+ * @param lambda the source lambda being validated
+ * @param sourceIndex the written parameter index, excluding generated capture parameters
+ * @param register the register already allocated by ordinary validation
+ *
+ * @return the written name token, or null when the parameter has no name expression
+ */
+ public static Token bindLambdaParameter(LambdaExpression lambda, int sourceIndex, Register register) {
+ if (lambda.hasOnlyParamNames()) {
+ return lambda.paramNames.get(sourceIndex) instanceof NameExpression expression
+ ? expression.getNameToken() : null;
+ }
+
+ Parameter parameter = lambda.params.get(sourceIndex);
+ parameter.setResolvedTarget(register);
+ return parameter.getNameToken();
+ }
+}
diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java b/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java
index 451e1cde608..65246f7e2dd 100644
--- a/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java
+++ b/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java
@@ -15,8 +15,8 @@
import org.xvm.asm.constants.ArrayConstant;
import org.xvm.asm.constants.IntConstant;
-import org.xvm.asm.constants.RangeConstant;
import org.xvm.asm.constants.MatchAnyConstant;
+import org.xvm.asm.constants.RangeConstant;
import org.xvm.asm.constants.TypeConstant;
import org.xvm.asm.constants.ValueConstant;
@@ -39,6 +39,9 @@
import org.xvm.util.PackedInteger;
import org.xvm.util.Severity;
+import static org.xvm.asm.ErrorListener.Silence.PROBE;
+import static org.xvm.asm.ErrorListener.silent;
+
/**
* The CaseManager is shared compilation logic used by both the "switch" statement and expression.
*/
@@ -170,13 +173,6 @@ public Label getSwitchConstantLabel() {
return m_labelConstant;
}
- /**
- * @return the number of case constants (including default)
- */
- public int getCaseCount() {
- return m_aconstCase.length + (hasDefaultCase() ? 1 : 0);
- }
-
/**
* @return True iff there is an explicit default label
*/
@@ -336,7 +332,7 @@ protected boolean validateCondition(Context ctx, List