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

Side by Side Diff: pkg/compiler/lib/src/resolution/semantic_visitor.dart

Issue 1047673002: Add SemanticDeclVisitor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Correct long lines Created 5 years, 8 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 dart2js.semantics_visitor; 5 library dart2js.semantics_visitor;
6 6
7 import '../constants/expressions.dart'; 7 import '../constants/expressions.dart';
8 import '../dart2jslib.dart' show invariant; 8 import '../dart2jslib.dart' show invariant;
9 import '../dart_types.dart'; 9 import '../dart_types.dart';
10 import '../elements/elements.dart'; 10 import '../elements/elements.dart';
11 import '../helpers/helpers.dart'; 11 import '../helpers/helpers.dart';
12 import '../tree/tree.dart'; 12 import '../tree/tree.dart';
13 import '../universe/universe.dart'; 13 import '../universe/universe.dart';
14 import '../util/util.dart' show Spannable, SpannableAssertionFailure; 14 import '../util/util.dart' show Spannable, SpannableAssertionFailure;
15 import 'access_semantics.dart'; 15 import 'access_semantics.dart';
16 import 'operators.dart'; 16 import 'operators.dart';
17 import 'resolution.dart'; 17 import 'resolution.dart';
18 import 'send_structure.dart'; 18 import 'send_structure.dart';
19 19
20 part 'semantic_visitor_mixins.dart'; 20 part 'semantic_visitor_mixins.dart';
21 part 'send_resolver.dart'; 21 part 'send_resolver.dart';
22 22
23 abstract class SemanticVisitor<R, A> extends Visitor<R> 23 /// Mixin that couples a [SendResolverMixin] to a [SemanticSendVisitor] in a
24 with SendResolverMixin { 24 /// [Visitor].
25 TreeElements elements; 25 abstract class SemanticSendResolvedMixin<R, A>
26 26 implements Visitor<R>, SendResolverMixin {
27 SemanticVisitor(this.elements);
28 27
29 SemanticSendVisitor<R, A> get sendVisitor; 28 SemanticSendVisitor<R, A> get sendVisitor;
30 29
31 @override 30 @override
32 R visitIdentifier(Identifier node) { 31 R visitIdentifier(Identifier node) {
33 // TODO(johnniwinther): Support argument. 32 // TODO(johnniwinther): Support argument.
34 A arg = null; 33 A arg = null;
35 if (node.isThis()) { 34 if (node.isThis()) {
36 // TODO(johnniwinther): Parse `this` as a [Send] whose selector is `this` 35 // TODO(johnniwinther): Parse `this` as a [Send] whose selector is `this`
37 // to normalize with `this(...)`. 36 // to normalize with `this(...)`.
(...skipping 27 matching lines...) Expand all
65 64
66 NewStructure structure = computeNewStructure(node); 65 NewStructure structure = computeNewStructure(node);
67 if (structure == null) { 66 if (structure == null) {
68 return internalError(node, 'No structure for $node'); 67 return internalError(node, 'No structure for $node');
69 } else { 68 } else {
70 return structure.dispatch(sendVisitor, node, arg); 69 return structure.dispatch(sendVisitor, node, arg);
71 } 70 }
72 } 71 }
73 } 72 }
74 73
74 /// Mixin that couples a [DeclResolverMixin] to a [SemanticDeclVisitor] in a
75 /// [Visitor].
76 abstract class SemanticDeclResolvedMixin<R, A>
77 implements Visitor<R>, DeclResolverMixin {
78
79 SemanticDeclVisitor<R, A> get declVisitor;
80
81 @override
82 R visitFunctionExpression(FunctionExpression node) {
83 // TODO(johnniwinther): Support argument.
84 A arg = null;
85
86 DeclStructure structure = computeFunctionStructure(node);
87 if (structure == null) {
88 return internalError(node, 'No structure for $node');
89 } else {
90 return structure.dispatch(declVisitor, node, arg);
91 }
92 }
93
94 visitInitializers(NodeList initializers, A arg) {
95 if (initializers != null) {
96 for (Node node in initializers) {
97 InitializerStructure structure = computeInitializerStructure(node);
98 if (structure == null) {
99 return internalError(node, 'No structure for $node');
100 } else {
101 return structure.dispatch(declVisitor, node, arg);
102 }
103 }
104 }
105 }
106
107 visitParameters(NodeList parameters, A arg) {
108 computeParameterStructures(
109 parameters,
110 (VariableDefinitions definitions, ParameterStructure structure) {
111 if (structure == null) {
112 return internalError(definitions, 'No structure for $definitions');
113 } else {
114 return structure.dispatch(declVisitor, definitions, arg);
115 }
116 });
117 }
118
119 @override
120 R visitVariableDefinitions(VariableDefinitions definitions) {
121 // TODO(johnniwinther): Support argument.
122 A arg = null;
123
124 computeVariableStructures(
125 definitions,
126 (Node node, VariableStructure structure) {
127 if (structure == null) {
128 return internalError(node, 'No structure for $node');
129 } else {
130 return structure.dispatch(declVisitor, node, arg);
131 }
132 });
133 return null;
134 }
135 }
136
137 abstract class SemanticVisitor<R, A> extends Visitor<R>
138 with SemanticSendResolvedMixin<R, A>,
139 SendResolverMixin,
140 SemanticDeclResolvedMixin<R, A>,
141 DeclResolverMixin {
142 TreeElements elements;
143
144 SemanticVisitor(this.elements);
145 }
146
75 // TODO(johnniwinther): Add visits for [visitLocalConstantGet], 147 // TODO(johnniwinther): Add visits for [visitLocalConstantGet],
76 // [visitLocalConstantInvoke], [visitStaticConstantGet], etc. 148 // [visitLocalConstantInvoke], [visitStaticConstantGet], etc.
77 abstract class SemanticSendVisitor<R, A> { 149 abstract class SemanticSendVisitor<R, A> {
78 R apply(Node node, A arg); 150 R apply(Node node, A arg);
79 151
80 /// Read of the [parameter]. 152 /// Read of the [parameter].
81 /// 153 ///
82 /// For instance: 154 /// For instance:
83 /// m(parameter) => parameter; 155 /// m(parameter) => parameter;
84 /// 156 ///
(...skipping 2853 matching lines...) Expand 10 before | Expand all | Expand 10 after
2938 /// m2() => new C.a(true, 42); 3010 /// m2() => new C.a(true, 42);
2939 /// 3011 ///
2940 R errorUnresolvedRedirectingFactoryConstructorInvoke( 3012 R errorUnresolvedRedirectingFactoryConstructorInvoke(
2941 NewExpression node, 3013 NewExpression node,
2942 ConstructorElement constructor, 3014 ConstructorElement constructor,
2943 InterfaceType type, 3015 InterfaceType type,
2944 NodeList arguments, 3016 NodeList arguments,
2945 Selector selector, 3017 Selector selector,
2946 A arg); 3018 A arg);
2947 } 3019 }
3020
3021 abstract class SemanticDeclVisitor<R, A> {
3022 R apply(Node node, A arg);
3023
3024 /// Apply this visitor to the [parameters].
3025 applyParameters(NodeList parameters, A arg);
3026
3027 /// Apply this visitor to the constructor [initializers].
3028 applyInitializers(NodeList initializers, A arg);
3029
3030 /// A declaration of a top level [getter].
3031 ///
3032 /// For instance
3033 /// get m => 42;
3034 ///
3035 R visitTopLevelGetterDecl(
3036 FunctionExpression node,
3037 MethodElement getter,
3038 Node body,
3039 A arg);
3040
3041 /// A declaration of a top level [setter].
3042 ///
3043 /// For instance
3044 /// set m(a) {}
3045 ///
3046 R visitTopLevelSetterDecl(
3047 FunctionExpression node,
3048 MethodElement setter,
3049 NodeList parameters,
3050 Node body,
3051 A arg);
3052
3053 /// A declaration of a top level [function].
3054 ///
3055 /// For instance
3056 /// m(a) {}
3057 ///
3058 R visitTopLevelFunctionDecl(
3059 FunctionExpression node,
3060 MethodElement function,
3061 NodeList parameters,
3062 Node body,
3063 A arg);
3064
3065 /// A declaration of a static [getter].
3066 ///
3067 /// For instance
3068 /// class C {
3069 /// static get m => 42;
3070 /// }
3071 ///
3072 R visitStaticGetterDecl(
3073 FunctionExpression node,
3074 MethodElement getter,
3075 Node body,
3076 A arg);
3077
3078 /// A declaration of a static [setter].
3079 ///
3080 /// For instance
3081 /// class C {
3082 /// static set m(a) {}
3083 /// }
3084 ///
3085 R visitStaticSetterDecl(
3086 FunctionExpression node,
3087 MethodElement setter,
3088 NodeList parameters,
3089 Node body,
3090 A arg);
3091
3092 /// A declaration of a static [function].
3093 ///
3094 /// For instance
3095 /// class C {
3096 /// static m(a) {}
3097 /// }
3098 ///
3099 R visitStaticFunctionDecl(
3100 FunctionExpression node,
3101 MethodElement function,
3102 NodeList parameters,
3103 Node body,
3104 A arg);
3105
3106 /// A declaration of an abstract instance [getter].
3107 ///
3108 /// For instance
3109 /// abstract class C {
3110 /// get m;
3111 /// }
3112 ///
3113 R visitAbstractGetterDecl(
3114 FunctionExpression node,
3115 MethodElement getter,
3116 A arg);
3117
3118 /// A declaration of an abstract instance [setter].
3119 ///
3120 /// For instance
3121 /// abstract class C {
3122 /// set m(a);
3123 /// }
3124 ///
3125 R visitAbstractSetterDecl(
3126 FunctionExpression node,
3127 MethodElement setter,
3128 NodeList parameters,
3129 A arg);
3130
3131 /// A declaration of an abstract instance [method].
3132 ///
3133 /// For instance
3134 /// abstract class C {
3135 /// m(a);
3136 /// }
3137 ///
3138 R visitAbstractMethodDecl(
3139 FunctionExpression node,
3140 MethodElement method,
3141 NodeList parameters,
3142 A arg);
3143
3144 /// A declaration of an instance [getter].
3145 ///
3146 /// For instance
3147 /// class C {
3148 /// get m => 42;
3149 /// }
3150 ///
3151 R visitInstanceGetterDecl(
3152 FunctionExpression node,
3153 MethodElement getter,
3154 Node body,
3155 A arg);
3156
3157 /// A declaration of an instance [setter].
3158 ///
3159 /// For instance
3160 /// class C {
3161 /// set m(a) {}
3162 /// }
3163 ///
3164 R visitInstanceSetterDecl(
3165 FunctionExpression node,
3166 MethodElement setter,
3167 NodeList parameters,
3168 Node body,
3169 A arg);
3170
3171 /// A declaration of an instance [method].
3172 ///
3173 /// For instance
3174 /// class C {
3175 /// m(a) {}
3176 /// }
3177 ///
3178 R visitInstanceMethodDecl(
3179 FunctionExpression node,
3180 MethodElement method,
3181 NodeList parameters,
3182 Node body,
3183 A arg);
3184
3185 /// A declaration of a local [function].
3186 ///
3187 /// For instance `local` in
3188 /// m() {
3189 /// local(a) {}
3190 /// }
3191 ///
3192 R visitLocalFunctionDecl(
3193 FunctionExpression node,
3194 LocalFunctionElement function,
3195 NodeList parameters,
3196 Node body,
3197 A arg);
3198
3199 /// A declaration of a [closure].
3200 ///
3201 /// For instance `(a) {}` in
3202 /// m() {
3203 /// var closure = (a) {};
3204 /// }
3205 ///
3206 R visitClosureDecl(
3207 FunctionExpression node,
3208 LocalFunctionElement closure,
3209 NodeList parameters,
3210 Node body,
3211 A arg);
3212
3213 /// A declaration of the [index]th [parameter] in a constructor, setter,
3214 /// method or function.
3215 ///
3216 /// For instance `a` in
3217 /// m(a) {}
3218 ///
3219 R visitParameterDecl(
3220 VariableDefinitions node,
3221 Node definition,
3222 ParameterElement parameter,
3223 int index,
3224 A arg);
3225
3226 /// A declaration of the [index]th optional [parameter] in a constructor,
3227 /// method or function with the explicit [defaultValue]. If no default value
3228 /// is declared, [defaultValue] is `null`.
3229 ///
3230 /// For instance `a` in
3231 /// m([a = 42]) {}
3232 ///
3233 R visitOptionalParameterDecl(
3234 VariableDefinitions node,
3235 Node definition,
3236 ParameterElement parameter,
3237 ConstantExpression defaultValue,
3238 int index,
3239 A arg);
3240
3241 /// A declaration of a named [parameter] in a constructor, method or function
3242 /// with the explicit [defaultValue]. If no default value is declared,
3243 /// [defaultValue] is `null`.
3244 ///
3245 /// For instance `a` in
3246 /// m({a: 42}) {}
3247 ///
3248 R visitNamedParameterDecl(
3249 VariableDefinitions node,
3250 Node definition,
3251 ParameterElement parameter,
3252 ConstantExpression defaultValue,
3253 A arg);
3254
3255 /// A declaration of the [index]th [parameter] as an initializing formal in a
3256 /// constructor.
3257 ///
3258 /// For instance `a` in
3259 /// class C {
3260 /// var a;
3261 /// C(this.a);
3262 /// }
3263 ///
3264 R visitInitializingFormalDecl(
3265 VariableDefinitions node,
3266 Node definition,
3267 InitializingFormalElement parameter,
3268 int index,
3269 A arg);
3270
3271 /// A declaration of the [index]th optional [parameter] as an initializing
3272 /// formal in a constructor with the explicit [defaultValue]. If no default
3273 /// value is declared, [defaultValue] is `null`.
3274 ///
3275 /// For instance `a` in
3276 /// class C {
3277 /// var a;
3278 /// C([this.a = 42]);
3279 /// }
3280 ///
3281 R visitOptionalInitializingFormalDecl(
3282 VariableDefinitions node,
3283 Node definition,
3284 InitializingFormalElement parameter,
3285 ConstantExpression defaultValue,
3286 int index,
3287 A arg);
3288
3289 /// A declaration of a named [parameter] as an initializing formal in a
3290 /// constructor with the explicit [defaultValue]. If no default value is
3291 /// declared, [defaultValue] is `null`.
3292 ///
3293 /// For instance `a` in
3294 /// class C {
3295 /// var a;
3296 /// C({this.a: 42});
3297 /// }
3298 ///
3299 R visitNamedInitializingFormalDecl(
3300 VariableDefinitions node,
3301 Node definition,
3302 InitializingFormalElement parameter,
3303 ConstantExpression defaultValue,
3304 A arg);
3305
3306 /// A declaration of a local [variable] with the explicit [initializer]. If
3307 /// no initializer is declared, [initializer] is `null`.
3308 ///
3309 /// For instance `a` in
3310 /// m() {
3311 /// var a = 42;
3312 /// }
3313 ///
3314 R visitLocalVariableDecl(
3315 VariableDefinitions node,
3316 Node definition,
3317 LocalVariableElement variable,
3318 Node initializer,
3319 A arg);
3320
3321 /// A declaration of a local constant [variable] initialized to [constant].
3322 ///
3323 /// For instance `a` in
3324 /// m() {
3325 /// const a = 42;
3326 /// }
3327 ///
3328 R visitLocalConstantDecl(
3329 VariableDefinitions node,
3330 Node definition,
3331 LocalVariableElement variable,
3332 ConstantExpression constant,
3333 A arg);
3334
3335 /// A declaration of a top level [field] with the explicit [initializer].
3336 /// If no initializer is declared, [initializer] is `null`.
3337 ///
3338 /// For instance `a` in
3339 /// var a = 42;
3340 ///
3341 R visitTopLevelFieldDecl(
3342 VariableDefinitions node,
3343 Node definition,
3344 FieldElement field,
3345 Node initializer,
3346 A arg);
3347
3348 /// A declaration of a top level constant [field] initialized to [constant].
3349 ///
3350 /// For instance `a` in
3351 /// const a = 42;
3352 ///
3353 R visitTopLevelConstantDecl(
3354 VariableDefinitions node,
3355 Node definition,
3356 FieldElement field,
3357 ConstantExpression constant,
3358 A arg);
3359
3360 /// A declaration of a static [field] with the explicit [initializer].
3361 /// If no initializer is declared, [initializer] is `null`.
3362 ///
3363 /// For instance `a` in
3364 /// class C {
3365 /// static var a = 42;
3366 /// }
3367 ///
3368 R visitStaticFieldDecl(
3369 VariableDefinitions node,
3370 Node definition,
3371 FieldElement field,
3372 Node initializer,
3373 A arg);
3374
3375 /// A declaration of a static constant [field] initialized to [constant].
3376 ///
3377 /// For instance `a` in
3378 /// class C {
3379 /// static const a = 42;
3380 /// }
3381 ///
3382 R visitStaticConstantDecl(
3383 VariableDefinitions node,
3384 Node definition,
3385 FieldElement field,
3386 ConstantExpression constant,
3387 A arg);
3388
3389 /// A declaration of an instance [field] with the explicit [initializer].
3390 /// If no initializer is declared, [initializer] is `null`.
3391 ///
3392 /// For instance `a` in
3393 /// class C {
3394 /// var a = 42;
3395 /// }
3396 ///
3397 R visitInstanceFieldDecl(
3398 VariableDefinitions node,
3399 Node definition,
3400 FieldElement field,
3401 Node initializer,
3402 A arg);
3403
3404 /// A declaration of a generative [constructor] with the explicit constructor
3405 /// [initializers].
3406 ///
3407 /// For instance `C` in
3408 /// class C {
3409 /// var a;
3410 /// C(a) : this.a = a, super();
3411 /// }
3412 ///
3413 R visitGenerativeConstructorDecl(
3414 FunctionExpression node,
3415 ConstructorElement constructor,
3416 NodeList parameters,
3417 NodeList initializers,
3418 Node body,
3419 A arg);
3420
3421 /// A declaration of a redirecting generative [constructor] with
3422 /// [initializers] containing the redirecting constructor invocation.
3423 ///
3424 /// For instance `C` in
3425 /// class C {
3426 /// C() : this._();
3427 /// C._();
3428 /// }
3429 ///
3430 R visitRedirectingGenerativeConstructorDecl(
3431 FunctionExpression node,
3432 ConstructorElement constructor,
3433 NodeList parameters,
3434 NodeList initializers,
3435 A arg);
3436
3437 /// A declaration of a factory [constructor].
3438 ///
3439 /// For instance `C` in
3440 /// class C {
3441 /// factory C(a) => null;
3442 /// }
3443 ///
3444 R visitFactoryConstructorDecl(
3445 FunctionExpression node,
3446 ConstructorElement constructor,
3447 NodeList parameters,
3448 Node body,
3449 A arg);
3450
3451 /// A declaration of a redirecting factory [constructor]. The immediate
3452 /// redirection target and its type is provided in [redirectionTarget] and
3453 /// [redirectionType], respectively.
3454 ///
3455 /// For instance
3456 /// class C<T> {
3457 /// factory C() = C<int>.a;
3458 /// factory C.a() = C<C<T>>.b;
3459 /// C.b();
3460 /// }
3461 /// where `C` has the redirection target `C.a` of type `C<int>` and `C.a` has
3462 /// the redirection target `C.b` of type `C<C<T>>`.
3463 ///
3464 R visitRedirectingFactoryConstructorDecl(
3465 FunctionExpression node,
3466 ConstructorElement constructor,
3467 NodeList parameters,
3468 InterfaceType redirectionType,
3469 ConstructorElement redirectionTarget,
3470 A arg);
3471
3472 /// An initializer of [field] with [initializer] as found in constructor
3473 /// initializers.
3474 ///
3475 /// For instance `this.a = 42` in
3476 /// class C {
3477 /// var a;
3478 /// C() : this.a = 42;
3479 /// }
3480 ///
3481 R visitFieldInitializer(
3482 SendSet node,
3483 FieldElement field,
3484 Node initializer,
3485 A arg);
3486
3487 /// An initializer of an unresolved field with [initializer] as found in
3488 /// generative constructor initializers.
3489 ///
3490 /// For instance `this.a = 42` in
3491 /// class C {
3492 /// C() : this.a = 42;
3493 /// }
3494 ///
3495 R errorUnresolvedFieldInitializer(
3496 SendSet node,
3497 Element element,
3498 Node initializer,
3499 A arg);
3500
3501 /// An super constructor invocation of [superConstructor] with [arguments] as
3502 /// found in generative constructor initializers.
3503 ///
3504 /// For instance `super(42)` in
3505 /// class B {
3506 /// B(a);
3507 /// }
3508 /// class C extends B {
3509 /// C() : super(42);
3510 /// }
3511 ///
3512 R visitSuperConstructorInvoke(
3513 Send node,
3514 ConstructorElement superConstructor,
3515 InterfaceType type,
3516 NodeList arguments,
3517 Selector selector,
3518 A arg);
3519
3520 /// An super constructor invocation of an unresolved with [arguments] as
3521 /// found in generative constructor initializers.
3522 ///
3523 /// For instance `super(42)` in
3524 /// class B {
3525 /// B(a);
3526 /// }
3527 /// class C extends B {
3528 /// C() : super.unresolved(42);
3529 /// }
3530 ///
3531 R errorUnresolvedSuperConstructorInvoke(
3532 Send node,
3533 Element element,
3534 NodeList arguments,
3535 Selector selector,
3536 A arg);
3537
3538 /// An this constructor invocation of [thisConstructor] with [arguments] as
3539 /// found in a redirecting generative constructors initializer.
3540 ///
3541 /// For instance `this._(42)` in
3542 /// class C {
3543 /// C() : this._(42);
3544 /// C._(a);
3545 /// }
3546 ///
3547 R visitThisConstructorInvoke(
3548 Send node,
3549 ConstructorElement thisConstructor,
3550 NodeList arguments,
3551 Selector selector,
3552 A arg);
3553
3554 /// An this constructor invocation of an unresolved constructor with
3555 /// [arguments] as found in a redirecting generative constructors initializer.
3556 ///
3557 /// For instance `this._(42)` in
3558 /// class C {
3559 /// C() : this._(42);
3560 /// }
3561 ///
3562 R errorUnresolvedThisConstructorInvoke(
3563 Send node,
3564 Element element,
3565 NodeList arguments,
3566 Selector selector,
3567 A arg);
3568 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698