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

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

Issue 9245002: Support getters and setters. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 10 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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');
(...skipping 12 matching lines...) Expand all
23 const ElementKind('generative_constructor'); 23 const ElementKind('generative_constructor');
24 static final ElementKind FIELD = const ElementKind('field'); 24 static final ElementKind FIELD = const ElementKind('field');
25 static final ElementKind VARIABLE_LIST = const ElementKind('variable_list'); 25 static final ElementKind VARIABLE_LIST = const ElementKind('variable_list');
26 static final ElementKind FIELD_LIST = const ElementKind('field_list'); 26 static final ElementKind FIELD_LIST = const ElementKind('field_list');
27 static final ElementKind GENERATIVE_CONSTRUCTOR_BODY = 27 static final ElementKind GENERATIVE_CONSTRUCTOR_BODY =
28 const ElementKind('generative_constructor_body'); 28 const ElementKind('generative_constructor_body');
29 static final ElementKind COMPILATION_UNIT = 29 static final ElementKind COMPILATION_UNIT =
30 const ElementKind('compilation_unit'); 30 const ElementKind('compilation_unit');
31 static final ElementKind GETTER = const ElementKind('getter'); 31 static final ElementKind GETTER = const ElementKind('getter');
32 static final ElementKind SETTER = const ElementKind('setter'); 32 static final ElementKind SETTER = const ElementKind('setter');
33 static final ElementKind ABSTRACT_FIELD = const ElementKind('abstract_field');
33 34
34 toString() => id; 35 toString() => id;
35 } 36 }
36 37
37 class Element implements Hashable { 38 class Element implements Hashable {
38 final SourceString name; 39 final SourceString name;
39 final ElementKind kind; 40 final ElementKind kind;
40 final Element enclosingElement; 41 final Element enclosingElement;
41 Modifiers get modifiers() => null; 42 Modifiers get modifiers() => null;
42 43
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 201
201 Type computeType(Compiler compiler) { 202 Type computeType(Compiler compiler) {
202 return compiler.types.dynamicType; 203 return compiler.types.dynamicType;
203 } 204 }
204 205
205 parseNode(DiagnosticListener listener) { 206 parseNode(DiagnosticListener listener) {
206 throw "internal error: ForeignElement has no node"; 207 throw "internal error: ForeignElement has no node";
207 } 208 }
208 } 209 }
209 210
211 class AbstractFieldElement extends Element {
212 FunctionElement getter;
213 FunctionElement setter;
214 AbstractFieldElement(SourceString name, Element enclosing)
215 : super(name, ElementKind.ABSTRACT_FIELD, enclosing);
216
217 Type computeType(Compiler compiler) {
218 throw "internal error: AbstractFieldElement has no type";
219 }
220
221 Node parseNode(DiagnosticListener listener) {
222 throw "internal error: AbstractFieldElement has no node";
223 }
224 }
225
210 /** 226 /**
211 * TODO(ngeoffray): Remove this method in favor of using the universe. 227 * TODO(ngeoffray): Remove this method in favor of using the universe.
212 * 228 *
213 * Return the type referred to by the type annotation. This method 229 * Return the type referred to by the type annotation. This method
214 * accepts annotations with 'typeName == null' to indicate a missing 230 * accepts annotations with 'typeName == null' to indicate a missing
215 * annotation. 231 * annotation.
216 */ 232 */
217 Type getType(TypeAnnotation typeAnnotation, compiler, types) { 233 Type getType(TypeAnnotation typeAnnotation, compiler, types) {
218 if (typeAnnotation == null || typeAnnotation.typeName == null) { 234 if (typeAnnotation == null || typeAnnotation.typeName == null) {
219 return types.dynamicType; 235 return types.dynamicType;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 ClassElement(SourceString name, CompilationUnitElement enclosing) 420 ClassElement(SourceString name, CompilationUnitElement enclosing)
405 : localMembers = new Map<SourceString, Element>(), 421 : localMembers = new Map<SourceString, Element>(),
406 constructors = new Map<SourceString, Element>(), 422 constructors = new Map<SourceString, Element>(),
407 super(name, ElementKind.CLASS, enclosing); 423 super(name, ElementKind.CLASS, enclosing);
408 424
409 void addMember(Element element, DiagnosticListener listener) { 425 void addMember(Element element, DiagnosticListener listener) {
410 members = members.prepend(element); 426 members = members.prepend(element);
411 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR || 427 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR ||
412 element.modifiers.isFactory()) { 428 element.modifiers.isFactory()) {
413 constructors[element.name] = element; 429 constructors[element.name] = element;
430 } else if (element.kind == ElementKind.GETTER
431 || element.kind == ElementKind.SETTER) {
432 Element existing = localMembers[element.name];
433 if (existing != null) {
434 if (existing.kind !== ElementKind.ABSTRACT_FIELD) {
435 listener.cancel('duplicate definition of $name', element: element);
436 listener.cancel('existing definition', element: existing);
437 } else {
438 AbstractFieldElement field = existing;
439 if (element.kind == ElementKind.GETTER) {
440 if (field.getter != null) {
441 listener.cancel('duplicate definition of getter ${element.name}',
442 element: element);
443 listener.cancel('existing definition', element: field.getter);
444 } else {
445 field.getter = element;
446 }
447 } else {
448 if (field.setter != null) {
449 listener.cancel('duplicate definition of setter ${element.name}',
450 element: element);
451 listener.cancel('existing definition', element: field.setter);
452 } else {
453 field.setter = element;
454 }
455 }
456 }
457 } else {
458 AbstractFieldElement field =
459 new AbstractFieldElement(element.name, this);
460 localMembers[element.name] = field;
461 if (element.kind == ElementKind.GETTER) {
462 field.getter = element;
463 } else {
464 field.setter = element;
465 }
466 }
414 } else { 467 } else {
415 localMembers[element.name] = element; 468 localMembers[element.name] = element;
416 } 469 }
417 } 470 }
418 471
419 Type computeType(compiler) { 472 Type computeType(compiler) {
420 if (type === null) { 473 if (type === null) {
421 type = new SimpleType(name, this); 474 type = new SimpleType(name, this);
422 } 475 }
423 return type; 476 return type;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 ClassElement get superclass() { 524 ClassElement get superclass() {
472 assert(isResolved); 525 assert(isResolved);
473 return supertype === null ? null : supertype.element; 526 return supertype === null ? null : supertype.element;
474 } 527 }
475 } 528 }
476 529
477 class Elements { 530 class Elements {
478 static bool isInstanceField(Element element) { 531 static bool isInstanceField(Element element) {
479 return (element !== null) 532 return (element !== null)
480 && element.isInstanceMember() 533 && element.isInstanceMember()
481 && (element.kind === ElementKind.FIELD); 534 && (element.kind === ElementKind.FIELD
535 || element.kind === ElementKind.GETTER
536 || element.kind === ElementKind.SETTER);
482 } 537 }
483 538
484 static bool isStaticOrTopLevelField(Element element) { 539 static bool isStaticOrTopLevelField(Element element) {
485 return (element != null) 540 return (element != null)
486 && !element.isInstanceMember() 541 && !element.isInstanceMember()
487 && (element.kind === ElementKind.FIELD); 542 && (element.kind === ElementKind.FIELD
543 || element.kind === ElementKind.GETTER
544 || element.kind === ElementKind.SETTER);
488 } 545 }
489 546
490 static bool isInstanceMethod(Element element) { 547 static bool isInstanceMethod(Element element) {
491 return (element != null) 548 return (element != null)
492 && element.isInstanceMember() 549 && element.isInstanceMember()
493 && (element.kind === ElementKind.FUNCTION); 550 && (element.kind === ElementKind.FUNCTION);
494 } 551 }
495 552
496 static bool isClosureSend(Send send, TreeElements elements) { 553 static bool isClosureSend(Send send, TreeElements elements) {
497 if (send.isPropertyAccess) return false; 554 if (send.isPropertyAccess) return false;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 else if (str === '>=') str = 'ge'; 587 else if (str === '>=') str = 'ge';
531 else if (str === '>') str = 'gt'; 588 else if (str === '>') str = 'gt';
532 else if (str === '<=') str = 'le'; 589 else if (str === '<=') str = 'le';
533 else if (str === '<') str = 'lt'; 590 else if (str === '<') str = 'lt';
534 else if (str === '&' || str === '&=') str = 'and'; 591 else if (str === '&' || str === '&=') str = 'and';
535 else if (str === '^' || str === '^=') str = 'xor'; 592 else if (str === '^' || str === '^=') str = 'xor';
536 else if (str === '|' || str === '|=') str = 'or'; 593 else if (str === '|' || str === '|=') str = 'or';
537 return new SourceString('$receiver\$$str'); 594 return new SourceString('$receiver\$$str');
538 } 595 }
539 } 596 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698