Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Unified Diff: pkg/kernel/lib/analyzer/loader.dart

Issue 2641523002: Fix some crashes in dartk. (Closed)
Patch Set: Mark main as static, update status files again Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/kernel/lib/analyzer/ast_from_analyzer.dart ('k') | tests/co19/co19-kernel.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/kernel/lib/analyzer/loader.dart
diff --git a/pkg/kernel/lib/analyzer/loader.dart b/pkg/kernel/lib/analyzer/loader.dart
index cb5c9c6d3ec1f82d393179d6c9ee9fa68c016178..d3743af2cf6ff6a73f5252b2571e89953c42d8eb 100644
--- a/pkg/kernel/lib/analyzer/loader.dart
+++ b/pkg/kernel/lib/analyzer/loader.dart
@@ -109,7 +109,8 @@ class DartLoader implements ReferenceLevelLoader {
DartLoader(this.repository, DartOptions options, Packages packages,
{DartSdk dartSdk, AnalysisContext context})
- : this.context = context ?? createContext(options, packages, dartSdk: dartSdk),
+ : this.context =
+ context ?? createContext(options, packages, dartSdk: dartSdk),
this.applicationRoot = options.applicationRoot;
String getLibraryName(LibraryElement element) {
@@ -401,11 +402,13 @@ class DartLoader implements ReferenceLevelLoader {
}
ast.Member getMemberReference(Element element) {
+ assert(element != null);
assert(element is! Member); // Use the "base element".
return _members[element] ??= _buildMemberReference(element);
}
ast.Member _buildMemberReference(Element element) {
+ assert(element != null);
var node = _buildOrphanedMemberReference(element);
// Set the parent pointer and store it in the enclosing class or library.
// If the enclosing library is being built from the AST, do not add the
@@ -428,6 +431,7 @@ class DartLoader implements ReferenceLevelLoader {
}
ast.Member _buildOrphanedMemberReference(Element element) {
+ assert(element != null);
ClassElement classElement = element.enclosingElement is ClassElement
? element.enclosingElement
: null;
@@ -685,15 +689,42 @@ class DartLoader implements ReferenceLevelLoader {
}
}
+ ast.Procedure _getMainMethod(Uri uri) {
+ Source source = context.sourceFactory.forUri2(uri);
+ LibraryElement library = context.computeLibraryElement(source);
+ var mainElement = library.entryPoint;
+ if (mainElement == null) return null;
+ var mainMember = getMemberReference(mainElement);
+ if (mainMember is ast.Procedure && !mainMember.isAccessor) {
+ return mainMember;
+ }
+ // Top-level 'main' getters are not supported at the moment.
+ return null;
+ }
+
+ ast.Procedure _makeMissingMainMethod(ast.Library library) {
+ var main = new ast.Procedure(
+ new ast.Name('main'),
+ ast.ProcedureKind.Method,
+ new ast.FunctionNode(new ast.ExpressionStatement(new ast.Throw(
+ new ast.StringLiteral('Program has no main method')))),
+ isStatic: true)
+ ..fileUri = library.fileUri;
+ library.addMember(main);
+ return main;
+ }
+
ast.Program loadProgram(Uri mainLibrary, {Target target, bool compileSdk}) {
- ast.Library library = repository
- .getLibraryReference(applicationRoot.relativeUri(mainLibrary));
+ Uri uri = applicationRoot.relativeUri(mainLibrary);
+ ast.Library library = repository.getLibraryReference(uri);
ensureLibraryIsLoaded(library);
+ var mainMethod = _getMainMethod(mainLibrary);
loadEverything(target: target, compileSdk: compileSdk);
var program = new ast.Program(repository.libraries);
- program.mainMethod = library.procedures.firstWhere(
- (member) => member.name?.name == 'main',
- orElse: () => null);
+ if (mainMethod == null) {
+ mainMethod = _makeMissingMainMethod(library);
+ }
+ program.mainMethod = mainMethod;
for (LibraryElement libraryElement in libraryElements) {
for (CompilationUnitElement compilationUnitElement
in libraryElement.units) {
« no previous file with comments | « pkg/kernel/lib/analyzer/ast_from_analyzer.dart ('k') | tests/co19/co19-kernel.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698