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

Side by Side Diff: dart/frog/leg/elements/elements.dart

Issue 9634008: Introduce element categories and move resolver towards being more compositional. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | dart/frog/leg/resolver.dart » ('j') | dart/frog/leg/resolver.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library('elements'); 5 #library('elements');
6 6
7 #import('../tree/tree.dart'); 7 #import('../tree/tree.dart');
8 #import('../scanner/scannerlib.dart'); 8 #import('../scanner/scannerlib.dart');
9 #import('../leg.dart'); // TODO(karlklose): we only need type. 9 #import('../leg.dart'); // TODO(karlklose): we only need type.
10 #import('../util/util.dart'); 10 #import('../util/util.dart');
11 11
12 class ElementCategory {
13 static final int NONE = 0;
ngeoffray 2012/03/08 14:00:45 I would add a comment on what has NONE category (g
ahe 2012/03/08 18:13:40 Done.
14
15 /** Field, parameter, or variable. */
16 static final int SLOT = 1;
17
18 /** Function, method, or foreign function. */
19 static final int FUNCTION = 2;
20
21 static final int CLASS = 4;
22
23 static final int PREFIX = 8;
24
25 /** Constructor or factory. */
26 static final int FACTORY = 16;
27
28 static final int ALIAS = 32;
29
30 static final int SUPER = 64;
31
32 static final int IMPLIES_TYPE = CLASS | ALIAS;
33 }
34
12 class ElementKind { 35 class ElementKind {
13 final String id; 36 final String id;
37 final int category;
14 38
15 const ElementKind(String this.id); 39 const ElementKind(String this.id, this.category);
16 40
17 static final ElementKind VARIABLE = const ElementKind('variable'); 41 static final ElementKind VARIABLE =
18 static final ElementKind PARAMETER = const ElementKind('parameter'); 42 const ElementKind('variable', ElementCategory.SLOT);
19 static final ElementKind FUNCTION = const ElementKind('function'); 43 static final ElementKind PARAMETER =
20 static final ElementKind CLASS = const ElementKind('class'); 44 const ElementKind('parameter', ElementCategory.SLOT);
21 static final ElementKind FOREIGN = const ElementKind('foreign'); 45 static final ElementKind FUNCTION =
46 const ElementKind('function', ElementCategory.FUNCTION);
47 static final ElementKind CLASS =
48 const ElementKind('class', ElementCategory.CLASS);
49 static final ElementKind FOREIGN =
50 const ElementKind('foreign', ElementCategory.FUNCTION);
22 static final ElementKind GENERATIVE_CONSTRUCTOR = 51 static final ElementKind GENERATIVE_CONSTRUCTOR =
23 const ElementKind('generative_constructor'); 52 const ElementKind('generative_constructor', ElementCategory.FACTORY);
24 static final ElementKind FIELD = const ElementKind('field'); 53 static final ElementKind FIELD =
25 static final ElementKind VARIABLE_LIST = const ElementKind('variable_list'); 54 const ElementKind('field', ElementCategory.SLOT);
26 static final ElementKind FIELD_LIST = const ElementKind('field_list'); 55 static final ElementKind VARIABLE_LIST =
56 const ElementKind('variable_list', ElementCategory.NONE);
57 static final ElementKind FIELD_LIST =
58 const ElementKind('field_list', ElementCategory.NONE);
27 static final ElementKind GENERATIVE_CONSTRUCTOR_BODY = 59 static final ElementKind GENERATIVE_CONSTRUCTOR_BODY =
28 const ElementKind('generative_constructor_body'); 60 const ElementKind('generative_constructor_body', ElementCategory.NONE);
29 static final ElementKind COMPILATION_UNIT = 61 static final ElementKind COMPILATION_UNIT =
30 const ElementKind('compilation_unit'); 62 const ElementKind('compilation_unit', ElementCategory.NONE);
31 static final ElementKind GETTER = const ElementKind('getter'); 63 static final ElementKind GETTER =
32 static final ElementKind SETTER = const ElementKind('setter'); 64 const ElementKind('getter', ElementCategory.SLOT); // TODO(ahe): Sb. NONE.
33 static final ElementKind ABSTRACT_FIELD = const ElementKind('abstract_field'); 65 static final ElementKind SETTER =
34 static final ElementKind LIBRARY = const ElementKind('library'); 66 const ElementKind('setter', ElementCategory.SLOT); // TODO(ahe): Sb. NONE.
35 static final ElementKind PREFIX = const ElementKind('prefix'); 67 static final ElementKind ABSTRACT_FIELD =
36 static final ElementKind TYPEDEF = const ElementKind('typedef'); 68 const ElementKind('abstract_field', ElementCategory.SLOT);
69 static final ElementKind LIBRARY =
70 const ElementKind('library', ElementCategory.NONE);
71 static final ElementKind PREFIX =
72 const ElementKind('prefix', ElementCategory.PREFIX);
73 static final ElementKind TYPEDEF =
74 const ElementKind('typedef', ElementCategory.ALIAS);
37 75
38 static final ElementKind STATEMENT = const ElementKind('statement'); 76 static final ElementKind STATEMENT =
39 static final ElementKind LABEL = const ElementKind('label'); 77 const ElementKind('statement', ElementCategory.NONE);
78 static final ElementKind LABEL =
79 const ElementKind('label', ElementCategory.NONE);
40 80
41 toString() => id; 81 toString() => id;
42 } 82 }
43 83
44 class Element implements Hashable { 84 class Element implements Hashable {
45 final SourceString name; 85 final SourceString name;
46 final ElementKind kind; 86 final ElementKind kind;
47 final Element enclosingElement; 87 final Element enclosingElement;
48 Modifiers get modifiers() => null; 88 Modifiers get modifiers() => null;
49 89
(...skipping 13 matching lines...) Expand all
63 bool isGenerativeConstructor() => kind === ElementKind.GENERATIVE_CONSTRUCTOR; 103 bool isGenerativeConstructor() => kind === ElementKind.GENERATIVE_CONSTRUCTOR;
64 bool isCompilationUnit() { 104 bool isCompilationUnit() {
65 return kind === ElementKind.COMPILATION_UNIT || 105 return kind === ElementKind.COMPILATION_UNIT ||
66 kind === ElementKind.LIBRARY; 106 kind === ElementKind.LIBRARY;
67 } 107 }
68 bool isClass() => kind === ElementKind.CLASS; 108 bool isClass() => kind === ElementKind.CLASS;
69 bool isVariable() => kind === ElementKind.VARIABLE; 109 bool isVariable() => kind === ElementKind.VARIABLE;
70 bool isParameter() => kind === ElementKind.PARAMETER; 110 bool isParameter() => kind === ElementKind.PARAMETER;
71 bool isStatement() => kind === ElementKind.STATEMENT; 111 bool isStatement() => kind === ElementKind.STATEMENT;
72 bool isTypedef() => kind === ElementKind.TYPEDEF; 112 bool isTypedef() => kind === ElementKind.TYPEDEF;
73 // TODO(ahe): Remove this method. 113 bool impliesType() => (kind.category & ElementCategory.IMPLIES_TYPE) != 0;
74 bool isClassOrInterfaceOrTypedef() {
75 return kind == ElementKind.CLASS || kind == ElementKind.TYPEDEF;
76 }
77 114
78 bool isAssignable() { 115 bool isAssignable() {
79 if (modifiers != null && modifiers.isFinal()) return false; 116 if (modifiers != null && modifiers.isFinal()) return false;
80 if (isFunction() || isGenerativeConstructor()) return false; 117 if (isFunction() || isGenerativeConstructor()) return false;
81 return true; 118 return true;
82 } 119 }
83 120
84 Token position() => null; 121 Token position() => null;
85 122
86 const Element(this.name, this.kind, this.enclosingElement); 123 const Element(this.name, this.kind, this.enclosingElement);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 } 231 }
195 232
196 class PrefixElement extends Element { 233 class PrefixElement extends Element {
197 final LiteralString prefix; 234 final LiteralString prefix;
198 final LibraryElement library; 235 final LibraryElement library;
199 236
200 PrefixElement(LiteralString prefix, 237 PrefixElement(LiteralString prefix,
201 LibraryElement this.library, 238 LibraryElement this.library,
202 Element enclosing) 239 Element enclosing)
203 : this.prefix = prefix, 240 : this.prefix = prefix,
204 super(prefix.dartString.source, ElementKind.PREFIX, enclosing) { 241 super(prefix.dartString.source, ElementKind.PREFIX, enclosing);
205 } 242
243 lookupLocalMember(SourceString name) => library.lookupLocalMember(name);
206 } 244 }
207 245
208 class TypedefElement extends Element { 246 class TypedefElement extends Element {
209 TypedefElement(SourceString name, Element enclosing) 247 TypedefElement(SourceString name, Element enclosing)
210 : super(name, ElementKind.TYPEDEF, enclosing); 248 : super(name, ElementKind.TYPEDEF, enclosing);
211 } 249 }
212 250
213 class VariableElement extends Element { 251 class VariableElement extends Element {
214 final VariableListElement variables; 252 final VariableListElement variables;
215 Expression cachedNode; // The send or the identifier in the variables list. 253 Expression cachedNode; // The send or the identifier in the variables list.
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 FunctionType computeType(Compiler compiler) { 551 FunctionType computeType(Compiler compiler) {
514 if (type != null) return type; 552 if (type != null) return type;
515 Type returnType = compiler.types.voidType; 553 Type returnType = compiler.types.voidType;
516 type = new FunctionType(returnType, const EmptyLink<Type>(), this); 554 type = new FunctionType(returnType, const EmptyLink<Type>(), this);
517 return type; 555 return type;
518 } 556 }
519 557
520 Node parseNode(DiagnosticListener listener) { 558 Node parseNode(DiagnosticListener listener) {
521 if (cachedNode != null) return cachedNode; 559 if (cachedNode != null) return cachedNode;
522 cachedNode = new FunctionExpression( 560 cachedNode = new FunctionExpression(
523 new Identifier.synthetic(''), 561 new Identifier(enclosingElement.position()),
524 new NodeList.empty(), 562 new NodeList.empty(),
525 new Block(new NodeList.empty()), 563 new Block(new NodeList.empty()),
526 null, null, null, null); 564 null, null, null, null);
527 return cachedNode; 565 return cachedNode;
528 } 566 }
529 567
530 Token position() => null; 568 Token position() => null;
531 } 569 }
532 570
533 class ClassElement extends ContainerElement { 571 class ClassElement extends ContainerElement {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 compiler.resolveType(this); 649 compiler.resolveType(this);
612 isResolved = true; 650 isResolved = true;
613 } 651 }
614 return this; 652 return this;
615 } 653 }
616 654
617 Element lookupLocalMember(SourceString memberName) { 655 Element lookupLocalMember(SourceString memberName) {
618 return localMembers[memberName]; 656 return localMembers[memberName];
619 } 657 }
620 658
659 Element lookupSuperMember(SourceString name) {
660 for (ClassElement s = superclass; s != null; s = s.superclass) {
661 Element e = s.lookupLocalMember(name);
662 if (e !== null) return e;
663 }
664 return null;
665 }
666
621 Element lookupConstructor(SourceString className, 667 Element lookupConstructor(SourceString className,
622 [SourceString constructorName = 668 [SourceString constructorName =
623 const SourceString(''), 669 const SourceString(''),
624 Element noMatch(Element)]) { 670 Element noMatch(Element)]) {
625 // TODO(karlklose): have a map from class names to a map of constructors 671 // TODO(karlklose): have a map from class names to a map of constructors
626 // instead of creating the name here? 672 // instead of creating the name here?
627 SourceString normalizedName; 673 SourceString normalizedName;
628 if (constructorName !== const SourceString('')) { 674 if (constructorName !== const SourceString('')) {
629 normalizedName = Elements.constructConstructorName(className, 675 normalizedName = Elements.constructConstructorName(className,
630 constructorName); 676 constructorName);
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 852
807 LabelElement addLabel(Identifier label, String labelName) { 853 LabelElement addLabel(Identifier label, String labelName) {
808 LabelElement result = new LabelElement(label, labelName, this, 854 LabelElement result = new LabelElement(label, labelName, this,
809 enclosingElement); 855 enclosingElement);
810 labels = labels.prepend(result); 856 labels = labels.prepend(result);
811 return result; 857 return result;
812 } 858 }
813 859
814 Node parseNode(DiagnosticListener l) => statement; 860 Node parseNode(DiagnosticListener l) => statement;
815 } 861 }
OLDNEW
« no previous file with comments | « no previous file | dart/frog/leg/resolver.dart » ('j') | dart/frog/leg/resolver.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698