OLD | NEW |
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 #ifndef VM_PARSER_H_ | 5 #ifndef VM_PARSER_H_ |
6 #define VM_PARSER_H_ | 6 #define VM_PARSER_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 | 9 |
10 #include "vm/ast.h" | 10 #include "vm/ast.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 ASSERT(has_expression_temp_var()); | 72 ASSERT(has_expression_temp_var()); |
73 return expression_temp_var_; | 73 return expression_temp_var_; |
74 } | 74 } |
75 void set_expression_temp_var(LocalVariable* value) { | 75 void set_expression_temp_var(LocalVariable* value) { |
76 ASSERT(!has_expression_temp_var()); | 76 ASSERT(!has_expression_temp_var()); |
77 expression_temp_var_ = value; | 77 expression_temp_var_ = value; |
78 } | 78 } |
79 bool has_expression_temp_var() const { | 79 bool has_expression_temp_var() const { |
80 return expression_temp_var_ != NULL; | 80 return expression_temp_var_ != NULL; |
81 } | 81 } |
82 static LocalVariable* CreateExpressionTempVar(intptr_t token_index); | 82 static LocalVariable* CreateExpressionTempVar(intptr_t token_pos); |
83 | 83 |
84 int first_parameter_index() const { return first_parameter_index_; } | 84 int first_parameter_index() const { return first_parameter_index_; } |
85 int first_stack_local_index() const { return first_stack_local_index_; } | 85 int first_stack_local_index() const { return first_stack_local_index_; } |
86 int copied_parameter_count() const { return copied_parameter_count_; } | 86 int copied_parameter_count() const { return copied_parameter_count_; } |
87 int stack_local_count() const { return stack_local_count_; } | 87 int stack_local_count() const { return stack_local_count_; } |
88 | 88 |
89 void AllocateVariables(); | 89 void AllocateVariables(); |
90 | 90 |
91 private: | 91 private: |
92 const Function& function_; | 92 const Function& function_; |
93 SequenceNode* node_sequence_; | 93 SequenceNode* node_sequence_; |
94 AstNode* instantiator_; | 94 AstNode* instantiator_; |
95 Array& default_parameter_values_; | 95 Array& default_parameter_values_; |
96 LocalVariable* saved_context_var_; | 96 LocalVariable* saved_context_var_; |
97 LocalVariable* expression_temp_var_; | 97 LocalVariable* expression_temp_var_; |
98 | 98 |
99 int first_parameter_index_; | 99 int first_parameter_index_; |
100 int first_stack_local_index_; | 100 int first_stack_local_index_; |
101 int copied_parameter_count_; | 101 int copied_parameter_count_; |
102 int stack_local_count_; | 102 int stack_local_count_; |
103 | 103 |
104 DISALLOW_COPY_AND_ASSIGN(ParsedFunction); | 104 DISALLOW_COPY_AND_ASSIGN(ParsedFunction); |
105 }; | 105 }; |
106 | 106 |
107 | 107 |
| 108 // The class TokenStreamIterator encapsulates iteration over the TokenStream |
| 109 // object. The parser uses this to iterate over tokens while parsing a script |
| 110 // or a function. |
| 111 class TokenStreamIterator : ValueObject { |
| 112 public: |
| 113 TokenStreamIterator(const TokenStream& tokens, intptr_t token_pos) |
| 114 : tokens_(tokens), token_position_(token_pos) { } |
| 115 |
| 116 intptr_t NumberOfTokens() const { return tokens_.Length(); } |
| 117 bool IsValid() const; |
| 118 |
| 119 inline Token::Kind CurrentTokenKind() const; |
| 120 Token::Kind LookaheadTokenKind(intptr_t num_tokens) const; |
| 121 |
| 122 intptr_t CurrentPosition() const { return token_position_; } |
| 123 void SetCurrentPosition(intptr_t value) { token_position_ = value; } |
| 124 |
| 125 void Advance() { token_position_ += 1; } |
| 126 |
| 127 RawObject* CurrentToken() const; |
| 128 RawString* CurrentLiteral() const; |
| 129 |
| 130 private: |
| 131 const TokenStream& tokens_; |
| 132 intptr_t token_position_; |
| 133 }; |
| 134 |
| 135 |
108 class Parser : ValueObject { | 136 class Parser : ValueObject { |
109 public: | 137 public: |
110 Parser(const Script& script, const Library& library); | 138 Parser(const Script& script, const Library& library); |
111 Parser(const Script& script, | 139 Parser(const Script& script, const Function& function, intptr_t token_pos); |
112 const Function& function, | |
113 intptr_t token_index); | |
114 | 140 |
115 // Parse the top level of a whole script file and register declared classes | 141 // Parse the top level of a whole script file and register declared classes |
116 // and interfaces in the given library. | 142 // and interfaces in the given library. |
117 static void ParseCompilationUnit(const Library& library, | 143 static void ParseCompilationUnit(const Library& library, |
118 const Script& script); | 144 const Script& script); |
119 | 145 |
120 static void ParseFunction(ParsedFunction* parsed_function); | 146 static void ParseFunction(ParsedFunction* parsed_function); |
121 | 147 |
122 // Build an error object containing a formatted error or warning message. | 148 // Build an error object containing a formatted error or warning message. |
123 // A null script means no source and a negative token_index means no position. | 149 // A null script means no source and a negative token_pos means no position. |
124 static RawError* FormatError(const Script& script, | 150 static RawError* FormatError(const Script& script, |
125 intptr_t token_index, | 151 intptr_t token_pos, |
126 const char* message_header, | 152 const char* message_header, |
127 const char* format, | 153 const char* format, |
128 va_list args); | 154 va_list args); |
129 | 155 |
130 // Same as FormatError, but appends the new error to the 'prev_error'. | 156 // Same as FormatError, but appends the new error to the 'prev_error'. |
131 static RawError* FormatErrorWithAppend(const Error& prev_error, | 157 static RawError* FormatErrorWithAppend(const Error& prev_error, |
132 const Script& script, | 158 const Script& script, |
133 intptr_t token_index, | 159 intptr_t token_pos, |
134 const char* message_header, | 160 const char* message_header, |
135 const char* format, | 161 const char* format, |
136 va_list args); | 162 va_list args); |
137 | 163 |
138 private: | 164 private: |
139 struct Block; | 165 struct Block; |
140 class TryBlocks; | 166 class TryBlocks; |
141 | 167 |
142 // The function being parsed. | 168 // The function being parsed. |
143 const Function& current_function() const; | 169 const Function& current_function() const; |
(...skipping 19 matching lines...) Expand all Loading... |
163 // The class or interface being parsed. | 189 // The class or interface being parsed. |
164 const Class& current_class() const; | 190 const Class& current_class() const; |
165 void set_current_class(const Class& value); | 191 void set_current_class(const Class& value); |
166 | 192 |
167 // Parsing a library or a regular source script. | 193 // Parsing a library or a regular source script. |
168 bool is_library_source() const { | 194 bool is_library_source() const { |
169 return (script_.kind() == RawScript::kScript) || | 195 return (script_.kind() == RawScript::kScript) || |
170 (script_.kind() == RawScript::kLibrary); | 196 (script_.kind() == RawScript::kLibrary); |
171 } | 197 } |
172 | 198 |
| 199 intptr_t TokenPos() const { return tokens_iterator_.CurrentPosition(); } |
173 inline Token::Kind CurrentToken(); | 200 inline Token::Kind CurrentToken(); |
174 Token::Kind LookaheadToken(int num_tokens); | 201 Token::Kind LookaheadToken(int num_tokens); |
175 String* CurrentLiteral() const; | 202 String* CurrentLiteral() const; |
176 RawDouble* CurrentDoubleLiteral() const; | 203 RawDouble* CurrentDoubleLiteral() const; |
177 RawInteger* CurrentIntegerLiteral() const; | 204 RawInteger* CurrentIntegerLiteral() const; |
178 | 205 |
179 // Sets parser to given token position in the stream. | 206 // Sets parser to given token position in the stream. |
180 void SetPosition(intptr_t position); | 207 void SetPosition(intptr_t position); |
181 | 208 |
182 void ConsumeToken() { | 209 void ConsumeToken() { |
183 // Reset cache and advance the token. | 210 // Reset cache and advance the token. |
184 token_kind_ = Token::kILLEGAL; | 211 token_kind_ = Token::kILLEGAL; |
185 token_index_++; | 212 tokens_iterator_.Advance(); |
186 CompilerStats::num_tokens_consumed++; | 213 CompilerStats::num_tokens_consumed++; |
187 } | 214 } |
188 void ConsumeRightAngleBracket(); | 215 void ConsumeRightAngleBracket(); |
189 void ExpectToken(Token::Kind token_expected); | 216 void ExpectToken(Token::Kind token_expected); |
190 void ExpectSemicolon(); | 217 void ExpectSemicolon(); |
191 void UnexpectedToken(); | 218 void UnexpectedToken(); |
192 String* ExpectTypeIdentifier(const char* msg); | 219 String* ExpectTypeIdentifier(const char* msg); |
193 String* ExpectIdentifier(const char* msg); | 220 String* ExpectIdentifier(const char* msg); |
194 bool IsLiteral(const char* literal); | 221 bool IsLiteral(const char* literal); |
195 | 222 |
(...skipping 18 matching lines...) Expand all Loading... |
214 void SkipFunctionLiteral(); | 241 void SkipFunctionLiteral(); |
215 void SkipStringLiteral(); | 242 void SkipStringLiteral(); |
216 void SkipQualIdent(); | 243 void SkipQualIdent(); |
217 | 244 |
218 void CheckConstructorCallTypeArguments( | 245 void CheckConstructorCallTypeArguments( |
219 intptr_t pos, | 246 intptr_t pos, |
220 Function& constructor, | 247 Function& constructor, |
221 const AbstractTypeArguments& type_arguments); | 248 const AbstractTypeArguments& type_arguments); |
222 | 249 |
223 // Format an error or warning message into the message_buffer. | 250 // Format an error or warning message into the message_buffer. |
224 // A null script means no source and a negative token_index means no position. | 251 // A null script means no source and a negative token_pos means no position. |
225 static void FormatMessage(const Script& script, | 252 static void FormatMessage(const Script& script, |
226 intptr_t token_index, | 253 intptr_t token_pos, |
227 const char* message_header, | 254 const char* message_header, |
228 char* message_buffer, | 255 char* message_buffer, |
229 intptr_t message_buffer_size, | 256 intptr_t message_buffer_size, |
230 const char* format, | 257 const char* format, |
231 va_list args); | 258 va_list args); |
232 | 259 |
233 // Reports error/warning message at location of current token. | 260 // Reports error/warning message at location of current token. |
234 void ErrorMsg(const char* msg, ...); | 261 void ErrorMsg(const char* msg, ...); |
235 void Warning(const char* msg, ...); | 262 void Warning(const char* msg, ...); |
236 void Unimplemented(const char* msg); | 263 void Unimplemented(const char* msg); |
237 | 264 |
238 // Reports error message at given location. | 265 // Reports error message at given location. |
239 void ErrorMsg(intptr_t token_index, const char* msg, ...); | 266 void ErrorMsg(intptr_t token_pos, const char* msg, ...); |
240 void Warning(intptr_t token_index, const char* msg, ...); | 267 void Warning(intptr_t token_pos, const char* msg, ...); |
241 | 268 |
242 // Reports an already formatted error message. | 269 // Reports an already formatted error message. |
243 void ErrorMsg(const Error& error); | 270 void ErrorMsg(const Error& error); |
244 | 271 |
245 // Concatenates two error messages, the previous and the current one. | 272 // Concatenates two error messages, the previous and the current one. |
246 void AppendErrorMsg( | 273 void AppendErrorMsg( |
247 const Error& prev_error, intptr_t token_index, const char* format, ...); | 274 const Error& prev_error, intptr_t token_pos, const char* format, ...); |
248 | 275 |
249 const Instance& EvaluateConstExpr(AstNode* expr); | 276 const Instance& EvaluateConstExpr(AstNode* expr); |
250 AstNode* RunStaticFieldInitializer(const Field& field); | 277 AstNode* RunStaticFieldInitializer(const Field& field); |
251 RawObject* EvaluateConstConstructorCall( | 278 RawObject* EvaluateConstConstructorCall( |
252 const Class& type_class, | 279 const Class& type_class, |
253 const AbstractTypeArguments& type_arguments, | 280 const AbstractTypeArguments& type_arguments, |
254 const Function& constructor, | 281 const Function& constructor, |
255 ArgumentListNode* arguments); | 282 ArgumentListNode* arguments); |
256 AstNode* FoldConstExpr(intptr_t expr_pos, AstNode* expr); | 283 AstNode* FoldConstExpr(intptr_t expr_pos, AstNode* expr); |
257 | 284 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 | 365 |
339 void ChainNewBlock(LocalScope* outer_scope); | 366 void ChainNewBlock(LocalScope* outer_scope); |
340 void OpenBlock(); | 367 void OpenBlock(); |
341 void OpenLoopBlock(); | 368 void OpenLoopBlock(); |
342 void OpenFunctionBlock(const Function& func); | 369 void OpenFunctionBlock(const Function& func); |
343 SequenceNode* CloseBlock(); | 370 SequenceNode* CloseBlock(); |
344 | 371 |
345 LocalVariable* LookupPhaseParameter(); | 372 LocalVariable* LookupPhaseParameter(); |
346 LocalVariable* LookupReceiver(LocalScope* from_scope, bool test_only); | 373 LocalVariable* LookupReceiver(LocalScope* from_scope, bool test_only); |
347 void CaptureReceiver(); | 374 void CaptureReceiver(); |
348 AstNode* LoadReceiver(intptr_t token_index); | 375 AstNode* LoadReceiver(intptr_t token_pos); |
349 AstNode* LoadFieldIfUnresolved(AstNode* node); | 376 AstNode* LoadFieldIfUnresolved(AstNode* node); |
350 AstNode* LoadClosure(PrimaryNode* primary); | 377 AstNode* LoadClosure(PrimaryNode* primary); |
351 AstNode* CallGetter(intptr_t token_index, | 378 AstNode* CallGetter(intptr_t token_pos, AstNode* object, const String& name); |
352 AstNode* object, | |
353 const String& name); | |
354 | 379 |
355 AstNode* ParseAssertStatement(); | 380 AstNode* ParseAssertStatement(); |
356 AstNode* ParseJump(String* label_name); | 381 AstNode* ParseJump(String* label_name); |
357 AstNode* ParseIfStatement(String* label_name); | 382 AstNode* ParseIfStatement(String* label_name); |
358 AstNode* ParseWhileStatement(String* label_name); | 383 AstNode* ParseWhileStatement(String* label_name); |
359 AstNode* ParseDoWhileStatement(String* label_name); | 384 AstNode* ParseDoWhileStatement(String* label_name); |
360 AstNode* ParseForStatement(String* label_name); | 385 AstNode* ParseForStatement(String* label_name); |
361 AstNode* ParseForInStatement(intptr_t forin_pos, SourceLabel* label); | 386 AstNode* ParseForInStatement(intptr_t forin_pos, SourceLabel* label); |
362 CaseNode* ParseCaseClause(LocalVariable* switch_expr_value, | 387 CaseNode* ParseCaseClause(LocalVariable* switch_expr_value, |
363 SourceLabel* case_label); | 388 SourceLabel* case_label); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 RawString* ResolveImportVar(intptr_t ident_pos, const String& ident); | 483 RawString* ResolveImportVar(intptr_t ident_pos, const String& ident); |
459 AstNode* OptimizeBinaryOpNode(intptr_t op_pos, | 484 AstNode* OptimizeBinaryOpNode(intptr_t op_pos, |
460 Token::Kind binary_op, | 485 Token::Kind binary_op, |
461 AstNode* lhs, | 486 AstNode* lhs, |
462 AstNode* rhs); | 487 AstNode* rhs); |
463 AstNode* ExpandAssignableOp(intptr_t op_pos, | 488 AstNode* ExpandAssignableOp(intptr_t op_pos, |
464 Token::Kind assignment_op, | 489 Token::Kind assignment_op, |
465 AstNode* lhs, | 490 AstNode* lhs, |
466 AstNode* rhs); | 491 AstNode* rhs); |
467 AstNode* PrepareCompoundAssignmentNodes(AstNode** expr); | 492 AstNode* PrepareCompoundAssignmentNodes(AstNode** expr); |
468 LocalVariable* CreateTempConstVariable(intptr_t token_index, | 493 LocalVariable* CreateTempConstVariable(intptr_t token_pos, |
469 intptr_t token_id, | 494 intptr_t token_id, |
470 const char* s); | 495 const char* s); |
471 | 496 |
472 static bool IsAssignableExpr(AstNode* expr); | 497 static bool IsAssignableExpr(AstNode* expr); |
473 | 498 |
474 static bool IsPrefixOperator(Token::Kind token); | 499 static bool IsPrefixOperator(Token::Kind token); |
475 static bool IsIncrementOperator(Token::Kind token); | 500 static bool IsIncrementOperator(Token::Kind token); |
476 | 501 |
477 static SequenceNode* NodeAsSequenceNode(intptr_t sequence_pos, | 502 static SequenceNode* NodeAsSequenceNode(intptr_t sequence_pos, |
478 AstNode* node, | 503 AstNode* node, |
479 LocalScope* scope); | 504 LocalScope* scope); |
480 | 505 |
481 SequenceNode* MakeImplicitConstructor(const Function& func); | 506 SequenceNode* MakeImplicitConstructor(const Function& func); |
482 AstNode* MakeStaticCall(const char* class_name, | 507 AstNode* MakeStaticCall(const char* class_name, |
483 const char* function_name, | 508 const char* function_name, |
484 ArgumentListNode* arguments); | 509 ArgumentListNode* arguments); |
485 String& Interpolate(ArrayNode* values); | 510 String& Interpolate(ArrayNode* values); |
486 AstNode* MakeAssertCall(intptr_t begin, intptr_t end); | 511 AstNode* MakeAssertCall(intptr_t begin, intptr_t end); |
487 AstNode* ThrowTypeError(intptr_t type_pos, const AbstractType& type); | 512 AstNode* ThrowTypeError(intptr_t type_pos, const AbstractType& type); |
488 | 513 |
489 void CheckFunctionIsCallable(intptr_t token_index, const Function& function); | 514 void CheckFunctionIsCallable(intptr_t token_pos, const Function& function); |
490 void CheckOperatorArity(const MemberDesc& member, Token::Kind operator_token); | 515 void CheckOperatorArity(const MemberDesc& member, Token::Kind operator_token); |
491 | 516 |
492 const LocalVariable& GetIncrementTempLocal(); | 517 const LocalVariable& GetIncrementTempLocal(); |
493 void EnsureExpressionTemp(); | 518 void EnsureExpressionTemp(); |
494 AstNode* CreateAssignmentNode(AstNode* original, AstNode* rhs); | 519 AstNode* CreateAssignmentNode(AstNode* original, AstNode* rhs); |
495 AstNode* InsertClosureCallNodes(AstNode* condition); | 520 AstNode* InsertClosureCallNodes(AstNode* condition); |
496 | 521 |
497 ConstructorCallNode* CreateConstructorCallNode( | 522 ConstructorCallNode* CreateConstructorCallNode( |
498 intptr_t token_index, | 523 intptr_t token_pos, |
499 const AbstractTypeArguments& type_arguments, | 524 const AbstractTypeArguments& type_arguments, |
500 const Function& constructor, | 525 const Function& constructor, |
501 ArgumentListNode* arguments); | 526 ArgumentListNode* arguments); |
502 | 527 |
503 | 528 |
504 const Script& script_; | 529 const Script& script_; |
505 const TokenStream& tokens_; | 530 TokenStreamIterator tokens_iterator_; |
506 intptr_t token_index_; | 531 Token::Kind token_kind_; // Cached token kind for current token. |
507 Token::Kind token_kind_; // Cached token kind for the token_index_. | |
508 Block* current_block_; | 532 Block* current_block_; |
509 | 533 |
510 // is_top_level_ is true if parsing the "top level" of a compilation unit, | 534 // is_top_level_ is true if parsing the "top level" of a compilation unit, |
511 // that is interface and class definitions. | 535 // that is interface and class definitions. |
512 bool is_top_level_; | 536 bool is_top_level_; |
513 | 537 |
514 // The member currently being parsed during "top level" parsing. | 538 // The member currently being parsed during "top level" parsing. |
515 MemberDesc* current_member_; | 539 MemberDesc* current_member_; |
516 | 540 |
517 // Parser mode to allow/disallow function literals. This is used in | 541 // Parser mode to allow/disallow function literals. This is used in |
(...skipping 18 matching lines...) Expand all Loading... |
536 | 560 |
537 // Allocate temporary only once per function. | 561 // Allocate temporary only once per function. |
538 LocalVariable* expression_temp_; | 562 LocalVariable* expression_temp_; |
539 | 563 |
540 DISALLOW_COPY_AND_ASSIGN(Parser); | 564 DISALLOW_COPY_AND_ASSIGN(Parser); |
541 }; | 565 }; |
542 | 566 |
543 } // namespace dart | 567 } // namespace dart |
544 | 568 |
545 #endif // VM_PARSER_H_ | 569 #endif // VM_PARSER_H_ |
OLD | NEW |