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

Side by Side Diff: lib/src/codegen/ast_builder.dart

Issue 1879373004: Implement modular compilation (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 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
« no previous file with comments | « lib/src/closure/closure_annotator.dart ('k') | lib/src/codegen/html_codegen.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'package:analyzer/dart/ast/ast.dart';
6 import 'package:analyzer/dart/ast/token.dart';
7 import 'package:analyzer/src/dart/ast/token.dart';
8 import 'package:analyzer/src/generated/utilities_dart.dart';
9 import 'package:logging/logging.dart' as logger;
10
11 final _log = new logger.Logger('dev_compiler.ast_builder');
12
13 // Wrappers around constructors for the dart ast. The AstBuilder class
14 // provides a higher-level interface, abstracting both from the lexical
15 // details and some of helper classes. The RawAstBuilder class provides
16 // a low-level wrapper class (below) abstracts from the lexical details
17 // but otherwise faithfully mirrors the construction API.
18 class AstBuilder {
19 static SimpleIdentifier identifierFromString(String name) {
20 return RawAstBuilder.identifierFromString(name);
21 }
22
23 static PrefixedIdentifier prefixedIdentifier(
24 SimpleIdentifier pre, SimpleIdentifier id) {
25 return RawAstBuilder.prefixedIdentifier(pre, id);
26 }
27
28 static TypeParameter typeParameter(SimpleIdentifier name,
29 [TypeName bound = null]) {
30 return RawAstBuilder.typeParameter(name, bound);
31 }
32
33 static TypeParameterList typeParameterList(List<TypeParameter> params) {
34 return RawAstBuilder.typeParameterList(params);
35 }
36
37 static TypeArgumentList typeArgumentList(List<TypeName> args) {
38 return RawAstBuilder.typeArgumentList(args);
39 }
40
41 static ArgumentList argumentList(List<Expression> args) {
42 return RawAstBuilder.argumentList(args);
43 }
44
45 static TypeName typeName(Identifier id, List<TypeName> args) {
46 TypeArgumentList argList = null;
47 if (args != null && args.length > 0) argList = typeArgumentList(args);
48 return RawAstBuilder.typeName(id, argList);
49 }
50
51 static FunctionTypeAlias functionTypeAlias(
52 TypeName ret,
53 SimpleIdentifier name,
54 List<TypeParameter> tParams,
55 List<FormalParameter> params) {
56 TypeParameterList tps =
57 (tParams.length == 0) ? null : typeParameterList(tParams);
58 FormalParameterList fps = formalParameterList(params);
59 return RawAstBuilder.functionTypeAlias(ret, name, tps, fps);
60 }
61
62 static BooleanLiteral booleanLiteral(bool b) {
63 return RawAstBuilder.booleanLiteral(b);
64 }
65
66 static NullLiteral nullLiteral() {
67 return RawAstBuilder.nullLiteral();
68 }
69
70 static IntegerLiteral integerLiteral(int i) {
71 return RawAstBuilder.integerLiteral(i);
72 }
73
74 static StringLiteral stringLiteral(String s) {
75 return RawAstBuilder.simpleStringLiteral(s);
76 }
77
78 static StringLiteral multiLineStringLiteral(String s) {
79 return RawAstBuilder.tripleQuotedStringLiteral(s);
80 }
81
82 static AsExpression asExpression(Expression exp, TypeName type) {
83 return RawAstBuilder.asExpression(exp, type);
84 }
85
86 static IsExpression isExpression(Expression exp, TypeName type) {
87 return RawAstBuilder.isExpression(exp, type);
88 }
89
90 static ParenthesizedExpression parenthesizedExpression(Expression exp) {
91 return RawAstBuilder.parenthesizedExpression(exp);
92 }
93
94 static Expression parenthesize(Expression exp) {
95 if (exp is Identifier ||
96 exp is ParenthesizedExpression ||
97 exp is FunctionExpressionInvocation ||
98 exp is MethodInvocation) return exp;
99 return parenthesizedExpression(exp);
100 }
101
102 static PropertyAccess propertyAccess(
103 Expression target, SimpleIdentifier name) {
104 var p = new Token(TokenType.PERIOD, 0);
105 return new PropertyAccess(target, p, name);
106 }
107
108 static MethodInvocation methodInvoke(
109 Expression target, SimpleIdentifier name, NodeList<Expression> args) {
110 var p = new Token(TokenType.PERIOD, 0);
111 return new MethodInvocation(target, p, name, null, argumentList(args));
112 }
113
114 static TokenType getTokenType(String lexeme) {
115 switch (lexeme) {
116 case "&":
117 return TokenType.AMPERSAND;
118 case "&&":
119 return TokenType.AMPERSAND_AMPERSAND;
120 case "&=":
121 return TokenType.AMPERSAND_EQ;
122 case "@":
123 return TokenType.AT;
124 case "!":
125 return TokenType.BANG;
126 case "!=":
127 return TokenType.BANG_EQ;
128 case "|":
129 return TokenType.BAR;
130 case "||":
131 return TokenType.BAR_BAR;
132 case "|=":
133 return TokenType.BAR_EQ;
134 case ":":
135 return TokenType.COLON;
136 case ",":
137 return TokenType.COMMA;
138 case "^":
139 return TokenType.CARET;
140 case "^=":
141 return TokenType.CARET_EQ;
142 case "}":
143 return TokenType.CLOSE_CURLY_BRACKET;
144 case ")":
145 return TokenType.CLOSE_PAREN;
146 case "]":
147 return TokenType.CLOSE_SQUARE_BRACKET;
148 case "=":
149 return TokenType.EQ;
150 case "==":
151 return TokenType.EQ_EQ;
152 case "=>":
153 return TokenType.FUNCTION;
154 case ">":
155 return TokenType.GT;
156 case ">=":
157 return TokenType.GT_EQ;
158 case ">>":
159 return TokenType.GT_GT;
160 case ">>=":
161 return TokenType.GT_GT_EQ;
162 case "#":
163 return TokenType.HASH;
164 case "[]":
165 return TokenType.INDEX;
166 case "[]=":
167 return TokenType.INDEX_EQ;
168 case "is":
169 return TokenType.IS;
170 case "<":
171 return TokenType.LT;
172 case "<=":
173 return TokenType.LT_EQ;
174 case "<<":
175 return TokenType.LT_LT;
176 case "<<=":
177 return TokenType.LT_LT_EQ;
178 case "-":
179 return TokenType.MINUS;
180 case "-=":
181 return TokenType.MINUS_EQ;
182 case "--":
183 return TokenType.MINUS_MINUS;
184 case "{":
185 return TokenType.OPEN_CURLY_BRACKET;
186 case "(":
187 return TokenType.OPEN_PAREN;
188 case "[":
189 return TokenType.OPEN_SQUARE_BRACKET;
190 case "%":
191 return TokenType.PERCENT;
192 case "%=":
193 return TokenType.PERCENT_EQ;
194 case ".":
195 return TokenType.PERIOD;
196 case "..":
197 return TokenType.PERIOD_PERIOD;
198 case "+":
199 return TokenType.PLUS;
200 case "+=":
201 return TokenType.PLUS_EQ;
202 case "++":
203 return TokenType.PLUS_PLUS;
204 case "?":
205 return TokenType.QUESTION;
206 case ";":
207 return TokenType.SEMICOLON;
208 case "/":
209 return TokenType.SLASH;
210 case "/=":
211 return TokenType.SLASH_EQ;
212 case "*":
213 return TokenType.STAR;
214 case "*=":
215 return TokenType.STAR_EQ;
216 case "\${":
217 return TokenType.STRING_INTERPOLATION_EXPRESSION;
218 case "\$":
219 return TokenType.STRING_INTERPOLATION_IDENTIFIER;
220 case "~":
221 return TokenType.TILDE;
222 case "~/":
223 return TokenType.TILDE_SLASH;
224 case "~/=":
225 return TokenType.TILDE_SLASH_EQ;
226 case "`":
227 return TokenType.BACKPING;
228 case "\\":
229 return TokenType.BACKSLASH;
230 case "...":
231 return TokenType.PERIOD_PERIOD_PERIOD;
232 case "??":
233 return TokenType.QUESTION_QUESTION;
234 case "??=":
235 return TokenType.QUESTION_QUESTION_EQ;
236 default:
237 return null;
238 }
239 }
240
241 static Token _binaryOperation(String oper) {
242 var type = getTokenType(oper);
243 assert(type != null);
244 return new Token(type, 0);
245 }
246
247 static BinaryExpression binaryExpression(
248 Expression l, String oper, Expression r) {
249 Token token = _binaryOperation(oper);
250 return RawAstBuilder.binaryExpression(l, token, r);
251 }
252
253 static ConditionalExpression conditionalExpression(
254 Expression cond, Expression tExp, Expression fExp) {
255 return RawAstBuilder.conditionalExpression(cond, tExp, fExp);
256 }
257
258 static Expression application(Expression function, List<Expression> es) {
259 ArgumentList args = argumentList(es);
260 return RawAstBuilder.functionExpressionInvocation(function, args);
261 }
262
263 static FormalParameterList formalParameterList(List<FormalParameter> params) {
264 return RawAstBuilder.formalParameterList(params);
265 }
266
267 static Block block(List<Statement> statements) {
268 return RawAstBuilder.block(statements);
269 }
270
271 static MethodDeclaration blockMethodDeclaration(
272 TypeName rt,
273 SimpleIdentifier m,
274 List<FormalParameter> params,
275 List<Statement> statements,
276 {bool isStatic: false}) {
277 FormalParameterList fl = formalParameterList(params);
278 Block b = block(statements);
279 BlockFunctionBody body = RawAstBuilder.blockFunctionBody(b);
280 return RawAstBuilder.methodDeclaration(rt, m, fl, body, isStatic: isStatic);
281 }
282
283 static FunctionDeclaration blockFunctionDeclaration(
284 TypeName rt,
285 SimpleIdentifier f,
286 List<FormalParameter> params,
287 List<Statement> statements) {
288 FunctionExpression fexp = blockFunction(params, statements);
289 return RawAstBuilder.functionDeclaration(rt, f, fexp);
290 }
291
292 static FunctionExpression blockFunction(
293 List<FormalParameter> params, List<Statement> statements) {
294 FormalParameterList fl = formalParameterList(params);
295 Block b = block(statements);
296 BlockFunctionBody body = RawAstBuilder.blockFunctionBody(b);
297 return RawAstBuilder.functionExpression(fl, body);
298 }
299
300 static FunctionExpression expressionFunction(
301 List<FormalParameter> params, Expression body,
302 [bool decl = false]) {
303 FormalParameterList fl = formalParameterList(params);
304 ExpressionFunctionBody b = RawAstBuilder.expressionFunctionBody(body, decl);
305 return RawAstBuilder.functionExpression(fl, b);
306 }
307
308 static FunctionDeclarationStatement functionDeclarationStatement(
309 TypeName rType, SimpleIdentifier name, FunctionExpression fe) {
310 var fd = RawAstBuilder.functionDeclaration(rType, name, fe);
311 return RawAstBuilder.functionDeclarationStatement(fd);
312 }
313
314 static Statement returnExpression([Expression e]) {
315 return RawAstBuilder.returnExpression(e);
316 }
317
318 // let b = e1 in e2 == (\b.e2)(e1)
319 static Expression letExpression(
320 FormalParameter b, Expression e1, Expression e2) {
321 FunctionExpression l = expressionFunction(<FormalParameter>[b], e2);
322 return application(parenthesize(l), <Expression>[e1]);
323 }
324
325 static SimpleFormalParameter simpleFormal(SimpleIdentifier v, TypeName t) {
326 return RawAstBuilder.simpleFormalParameter(v, t);
327 }
328
329 static FunctionTypedFormalParameter functionTypedFormal(
330 TypeName ret, SimpleIdentifier v, List<FormalParameter> params) {
331 FormalParameterList ps = formalParameterList(params);
332 return RawAstBuilder.functionTypedFormalParameter(ret, v, ps);
333 }
334
335 static FormalParameter requiredFormal(NormalFormalParameter fp) {
336 return RawAstBuilder.requiredFormalParameter(fp);
337 }
338
339 static FormalParameter optionalFormal(NormalFormalParameter fp) {
340 return RawAstBuilder.optionalFormalParameter(fp);
341 }
342
343 static FormalParameter namedFormal(NormalFormalParameter fp) {
344 return RawAstBuilder.namedFormalParameter(fp);
345 }
346
347 static NamedExpression namedParameter(String s, Expression e) {
348 return namedExpression(s, e);
349 }
350
351 static NamedExpression namedExpression(String s, Expression e) {
352 return RawAstBuilder.namedExpression(identifierFromString(s), e);
353 }
354
355 /// Declares a single variable `var <name> = <init>` with the type and name
356 /// specified by the VariableElement. See also [variableStatement].
357 static VariableDeclarationList declareVariable(SimpleIdentifier name,
358 [Expression init]) {
359 var eqToken = init != null ? new Token(TokenType.EQ, 0) : null;
360 var varToken = new KeywordToken(Keyword.VAR, 0);
361 return new VariableDeclarationList(null, null, varToken, null,
362 [new VariableDeclaration(name, eqToken, init)]);
363 }
364
365 static VariableDeclarationStatement variableStatement(SimpleIdentifier name,
366 [Expression init]) {
367 return RawAstBuilder
368 .variableDeclarationStatement(declareVariable(name, init));
369 }
370
371 static InstanceCreationExpression instanceCreation(
372 ConstructorName ctor, List<Expression> args) {
373 var newToken = new KeywordToken(Keyword.NEW, 0);
374 return new InstanceCreationExpression(
375 newToken, ctor, RawAstBuilder.argumentList(args));
376 }
377 }
378
379 // This class provides a low-level wrapper around the constructors for
380 // the AST. It mostly simply abstracts from the lexical tokens.
381 class RawAstBuilder {
382 static ConstructorName constructorName(TypeName type,
383 [SimpleIdentifier name]) {
384 Token period = name != null ? new Token(TokenType.PERIOD, 0) : null;
385 return new ConstructorName(type, period, name);
386 }
387
388 static SimpleIdentifier identifierFromString(String name) {
389 StringToken token = new SyntheticStringToken(TokenType.IDENTIFIER, name, 0);
390 return new SimpleIdentifier(token);
391 }
392
393 static PrefixedIdentifier prefixedIdentifier(
394 SimpleIdentifier pre, SimpleIdentifier id) {
395 Token period = new Token(TokenType.PERIOD, 0);
396 return new PrefixedIdentifier(pre, period, id);
397 }
398
399 static TypeParameter typeParameter(SimpleIdentifier name,
400 [TypeName bound = null]) {
401 Token keyword =
402 (bound == null) ? null : new KeywordToken(Keyword.EXTENDS, 0);
403 return new TypeParameter(null, null, name, keyword, bound);
404 }
405
406 static TypeParameterList typeParameterList(List<TypeParameter> params) {
407 Token lb = new Token(TokenType.LT, 0);
408 Token rb = new Token(TokenType.GT, 0);
409 return new TypeParameterList(lb, params, rb);
410 }
411
412 static TypeArgumentList typeArgumentList(List<TypeName> args) {
413 Token lb = new Token(TokenType.LT, 0);
414 Token rb = new Token(TokenType.GT, 0);
415 return new TypeArgumentList(lb, args, rb);
416 }
417
418 static ArgumentList argumentList(List<Expression> args) {
419 Token lp = new BeginToken(TokenType.OPEN_PAREN, 0);
420 Token rp = new Token(TokenType.CLOSE_PAREN, 0);
421 return new ArgumentList(lp, args, rp);
422 }
423
424 static TypeName typeName(Identifier id, TypeArgumentList l) {
425 return new TypeName(id, l);
426 }
427
428 static FunctionTypeAlias functionTypeAlias(TypeName ret,
429 SimpleIdentifier name, TypeParameterList tps, FormalParameterList fps) {
430 Token semi = new Token(TokenType.SEMICOLON, 0);
431 Token td = new KeywordToken(Keyword.TYPEDEF, 0);
432 return new FunctionTypeAlias(null, null, td, ret, name, tps, fps, semi);
433 }
434
435 static BooleanLiteral booleanLiteral(bool b) {
436 var k = new KeywordToken(b ? Keyword.TRUE : Keyword.FALSE, 0);
437 return new BooleanLiteral(k, b);
438 }
439
440 static NullLiteral nullLiteral() {
441 var n = new KeywordToken(Keyword.NULL, 0);
442 return new NullLiteral(n);
443 }
444
445 static IntegerLiteral integerLiteral(int i) {
446 StringToken token = new StringToken(TokenType.INT, '$i', 0);
447 return new IntegerLiteral(token, i);
448 }
449
450 static SimpleStringLiteral simpleStringLiteral(String s) {
451 StringToken token = new StringToken(TokenType.STRING, "\"" + s + "\"", 0);
452 return new SimpleStringLiteral(token, s);
453 }
454
455 static SimpleStringLiteral tripleQuotedStringLiteral(String s) {
456 StringToken token = new StringToken(TokenType.STRING, '"""' + s + '"""', 0);
457 return new SimpleStringLiteral(token, s);
458 }
459
460 static AsExpression asExpression(Expression exp, TypeName type) {
461 Token token = new KeywordToken(Keyword.AS, 0);
462 return new AsExpression(exp, token, type);
463 }
464
465 static IsExpression isExpression(Expression exp, TypeName type) {
466 Token token = new KeywordToken(Keyword.IS, 0);
467 return new IsExpression(exp, token, null, type);
468 }
469
470 static ParenthesizedExpression parenthesizedExpression(Expression exp) {
471 Token lp = new BeginToken(TokenType.OPEN_PAREN, exp.offset);
472 Token rp = new Token(TokenType.CLOSE_PAREN, exp.end);
473 return new ParenthesizedExpression(lp, exp, rp);
474 }
475
476 static BinaryExpression binaryExpression(
477 Expression l, Token op, Expression r) {
478 return new BinaryExpression(l, op, r);
479 }
480
481 static ConditionalExpression conditionalExpression(
482 Expression cond, Expression tExp, Expression fExp) {
483 var q = new Token(TokenType.QUESTION, 0);
484 var c = new Token(TokenType.COLON, 0);
485 return new ConditionalExpression(cond, q, tExp, c, fExp);
486 }
487
488 static Expression functionExpressionInvocation(
489 Expression function, ArgumentList es) {
490 return new FunctionExpressionInvocation(function, null, es);
491 }
492
493 static FormalParameterList formalParameterList(List<FormalParameter> params) {
494 Token lp = new BeginToken(TokenType.OPEN_PAREN, 0);
495 Token rp = new Token(TokenType.CLOSE_PAREN, 0);
496 bool hasOptional = params.any((p) => p.kind == ParameterKind.POSITIONAL);
497 bool hasNamed = params.any((p) => p.kind == ParameterKind.NAMED);
498 assert(!(hasOptional && hasNamed));
499 Token ld = null;
500 Token rd = null;
501 if (hasOptional) {
502 ld = new BeginToken(TokenType.OPEN_SQUARE_BRACKET, 0);
503 rd = new Token(TokenType.CLOSE_SQUARE_BRACKET, 0);
504 }
505 if (hasNamed) {
506 ld = new BeginToken(TokenType.OPEN_CURLY_BRACKET, 0);
507 rd = new Token(TokenType.CLOSE_CURLY_BRACKET, 0);
508 }
509 return new FormalParameterList(lp, params, ld, rd, rp);
510 }
511
512 static Block block(List<Statement> statements) {
513 Token ld = new BeginToken(TokenType.OPEN_CURLY_BRACKET, 0);
514 Token rd = new Token(TokenType.CLOSE_CURLY_BRACKET, 0);
515 return new Block(ld, statements, rd);
516 }
517
518 static BlockFunctionBody blockFunctionBody(Block b) {
519 return new BlockFunctionBody(null, null, b);
520 }
521
522 static ExpressionFunctionBody expressionFunctionBody(Expression body,
523 [bool decl = false]) {
524 Token semi = (decl) ? new Token(TokenType.SEMICOLON, 0) : null;
525 return new ExpressionFunctionBody(null, null, body, semi);
526 }
527
528 static ExpressionStatement expressionStatement(Expression expression) {
529 Token semi = new Token(TokenType.SEMICOLON, 0);
530 return new ExpressionStatement(expression, semi);
531 }
532
533 static FunctionDeclaration functionDeclaration(
534 TypeName rt, SimpleIdentifier f, FunctionExpression fexp) {
535 return new FunctionDeclaration(null, null, null, rt, null, f, fexp);
536 }
537
538 static MethodDeclaration methodDeclaration(TypeName rt, SimpleIdentifier m,
539 FormalParameterList fl, FunctionBody body,
540 {bool isStatic: false}) {
541 Token st = isStatic ? new KeywordToken(Keyword.STATIC, 0) : null;
542 return new MethodDeclaration(
543 null, null, null, st, rt, null, null, m, null, fl, body);
544 }
545
546 static FunctionExpression functionExpression(
547 FormalParameterList fl, FunctionBody body) {
548 return new FunctionExpression(null, fl, body);
549 }
550
551 static FunctionDeclarationStatement functionDeclarationStatement(
552 FunctionDeclaration fd) {
553 return new FunctionDeclarationStatement(fd);
554 }
555
556 static Statement returnExpression([Expression e]) {
557 Token ret = new KeywordToken(Keyword.RETURN, 0);
558 Token semi = new Token(TokenType.SEMICOLON, 0);
559 return new ReturnStatement(ret, e, semi);
560 }
561
562 static SimpleFormalParameter simpleFormalParameter(
563 SimpleIdentifier v, TypeName t) {
564 return new SimpleFormalParameter(null, <Annotation>[], null, t, v);
565 }
566
567 static FunctionTypedFormalParameter functionTypedFormalParameter(
568 TypeName ret, SimpleIdentifier v, FormalParameterList ps) {
569 return new FunctionTypedFormalParameter(
570 null, <Annotation>[], ret, v, null, ps);
571 }
572
573 static FormalParameter requiredFormalParameter(NormalFormalParameter fp) {
574 return fp;
575 }
576
577 static FormalParameter optionalFormalParameter(NormalFormalParameter fp) {
578 return new DefaultFormalParameter(fp, ParameterKind.POSITIONAL, null, null);
579 }
580
581 static FormalParameter namedFormalParameter(NormalFormalParameter fp) {
582 return new DefaultFormalParameter(fp, ParameterKind.NAMED, null, null);
583 }
584
585 static NamedExpression namedParameter(SimpleIdentifier s, Expression e) {
586 return namedExpression(s, e);
587 }
588
589 static NamedExpression namedExpression(SimpleIdentifier s, Expression e) {
590 Label l = new Label(s, new Token(TokenType.COLON, 0));
591 return new NamedExpression(l, e);
592 }
593
594 static VariableDeclarationStatement variableDeclarationStatement(
595 VariableDeclarationList varDecl) {
596 var semi = new Token(TokenType.SEMICOLON, 0);
597 return new VariableDeclarationStatement(varDecl, semi);
598 }
599 }
OLDNEW
« no previous file with comments | « lib/src/closure/closure_annotator.dart ('k') | lib/src/codegen/html_codegen.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698