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

Side by Side Diff: runtime/vm/parser.cc

Issue 63983005: Simplify the desugaring of catch clauses. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Parse catch clauses with a loop instead of recursion. Created 7 years, 1 month 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 | « runtime/vm/parser.h ('k') | runtime/vm/symbols.h » ('j') | no next file with comments »
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 #include "vm/parser.h" 5 #include "vm/parser.h"
6 6
7 #include "lib/invocation_mirror.h" 7 #include "lib/invocation_mirror.h"
8 #include "vm/bigint_operations.h" 8 #include "platform/utils.h"
9 #include "vm/bootstrap.h" 9 #include "vm/bootstrap.h"
10 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
11 #include "vm/compiler.h" 11 #include "vm/compiler.h"
12 #include "vm/compiler_stats.h" 12 #include "vm/compiler_stats.h"
13 #include "vm/dart_api_impl.h" 13 #include "vm/dart_api_impl.h"
14 #include "vm/dart_entry.h" 14 #include "vm/dart_entry.h"
15 #include "vm/flags.h" 15 #include "vm/flags.h"
16 #include "vm/growable_array.h" 16 #include "vm/growable_array.h"
17 #include "vm/handles.h"
18 #include "vm/heap.h"
19 #include "vm/isolate.h"
17 #include "vm/longjump.h" 20 #include "vm/longjump.h"
21 #include "vm/native_arguments.h"
18 #include "vm/native_entry.h" 22 #include "vm/native_entry.h"
19 #include "vm/object.h" 23 #include "vm/object.h"
20 #include "vm/object_store.h" 24 #include "vm/object_store.h"
25 #include "vm/os.h"
21 #include "vm/resolver.h" 26 #include "vm/resolver.h"
27 #include "vm/scanner.h"
22 #include "vm/scopes.h" 28 #include "vm/scopes.h"
23 #include "vm/stack_frame.h" 29 #include "vm/stack_frame.h"
24 #include "vm/symbols.h" 30 #include "vm/symbols.h"
31 #include "vm/timer.h"
32 #include "vm/zone.h"
25 33
26 namespace dart { 34 namespace dart {
27 35
28 DEFINE_FLAG(bool, enable_asserts, false, "Enable assert statements."); 36 DEFINE_FLAG(bool, enable_asserts, false, "Enable assert statements.");
29 DEFINE_FLAG(bool, enable_type_checks, false, "Enable type checks."); 37 DEFINE_FLAG(bool, enable_type_checks, false, "Enable type checks.");
30 DEFINE_FLAG(bool, trace_parser, false, "Trace parser operations."); 38 DEFINE_FLAG(bool, trace_parser, false, "Trace parser operations.");
31 DEFINE_FLAG(bool, warning_as_error, false, "Treat warnings as errors."); 39 DEFINE_FLAG(bool, warning_as_error, false, "Treat warnings as errors.");
32 DEFINE_FLAG(bool, silent_warnings, false, "Silence warnings."); 40 DEFINE_FLAG(bool, silent_warnings, false, "Silence warnings.");
33 DEFINE_FLAG(bool, warn_mixin_typedef, true, "Warning on legacy mixin typedef"); 41 DEFINE_FLAG(bool, warn_mixin_typedef, true, "Warning on legacy mixin typedef");
34 DECLARE_FLAG(bool, error_on_bad_type); 42 DECLARE_FLAG(bool, error_on_bad_type);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 195
188 // Class which describes an inlined finally block which is used to generate 196 // Class which describes an inlined finally block which is used to generate
189 // inlined code for the finally blocks when there is an exit from a try 197 // inlined code for the finally blocks when there is an exit from a try
190 // block using 'return', 'break' or 'continue'. 198 // block using 'return', 'break' or 'continue'.
191 class Parser::TryBlocks : public ZoneAllocated { 199 class Parser::TryBlocks : public ZoneAllocated {
192 public: 200 public:
193 TryBlocks(Block* try_block, TryBlocks* outer_try_block, intptr_t try_index) 201 TryBlocks(Block* try_block, TryBlocks* outer_try_block, intptr_t try_index)
194 : try_block_(try_block), 202 : try_block_(try_block),
195 inlined_finally_nodes_(), 203 inlined_finally_nodes_(),
196 outer_try_block_(outer_try_block), 204 outer_try_block_(outer_try_block),
197 try_index_(try_index) { } 205 try_index_(try_index),
206 inside_catch_(false) { }
198 207
199 TryBlocks* outer_try_block() const { return outer_try_block_; } 208 TryBlocks* outer_try_block() const { return outer_try_block_; }
200 Block* try_block() const { return try_block_; } 209 Block* try_block() const { return try_block_; }
201 intptr_t try_index() const { return try_index_; } 210 intptr_t try_index() const { return try_index_; }
211 bool inside_catch() const { return inside_catch_; }
212 void enter_catch() { inside_catch_ = true; }
202 213
203 void AddNodeForFinallyInlining(AstNode* node); 214 void AddNodeForFinallyInlining(AstNode* node);
204 AstNode* GetNodeToInlineFinally(int index) { 215 AstNode* GetNodeToInlineFinally(int index) {
205 if (0 <= index && index < inlined_finally_nodes_.length()) { 216 if (0 <= index && index < inlined_finally_nodes_.length()) {
206 return inlined_finally_nodes_[index]; 217 return inlined_finally_nodes_[index];
207 } 218 }
208 return NULL; 219 return NULL;
209 } 220 }
210 221
211 private: 222 private:
212 Block* try_block_; 223 Block* try_block_;
213 GrowableArray<AstNode*> inlined_finally_nodes_; 224 GrowableArray<AstNode*> inlined_finally_nodes_;
214 TryBlocks* outer_try_block_; 225 TryBlocks* outer_try_block_;
215 const intptr_t try_index_; 226 const intptr_t try_index_;
227 bool inside_catch_;
216 228
217 DISALLOW_COPY_AND_ASSIGN(TryBlocks); 229 DISALLOW_COPY_AND_ASSIGN(TryBlocks);
218 }; 230 };
219 231
220 232
221 void Parser::TryBlocks::AddNodeForFinallyInlining(AstNode* node) { 233 void Parser::TryBlocks::AddNodeForFinallyInlining(AstNode* node) {
222 inlined_finally_nodes_.Add(node); 234 inlined_finally_nodes_.Add(node);
223 } 235 }
224 236
225 237
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 } 1077 }
1066 LocalVariable* catch_excp_var = 1078 LocalVariable* catch_excp_var =
1067 current_block_->scope->LocalLookupVariable(Symbols::ExceptionVar()); 1079 current_block_->scope->LocalLookupVariable(Symbols::ExceptionVar());
1068 if (catch_excp_var == NULL) { 1080 if (catch_excp_var == NULL) {
1069 catch_excp_var = new LocalVariable(token_pos, 1081 catch_excp_var = new LocalVariable(token_pos,
1070 Symbols::ExceptionVar(), 1082 Symbols::ExceptionVar(),
1071 Type::ZoneHandle(Type::DynamicType())); 1083 Type::ZoneHandle(Type::DynamicType()));
1072 current_block_->scope->AddVariable(catch_excp_var); 1084 current_block_->scope->AddVariable(catch_excp_var);
1073 } 1085 }
1074 LocalVariable* catch_trace_var = 1086 LocalVariable* catch_trace_var =
1075 current_block_->scope->LocalLookupVariable(Symbols::StacktraceVar()); 1087 current_block_->scope->LocalLookupVariable(Symbols::StackTraceVar());
1076 if (catch_trace_var == NULL) { 1088 if (catch_trace_var == NULL) {
1077 catch_trace_var = new LocalVariable(token_pos, 1089 catch_trace_var = new LocalVariable(token_pos,
1078 Symbols::StacktraceVar(), 1090 Symbols::StackTraceVar(),
1079 Type::ZoneHandle(Type::DynamicType())); 1091 Type::ZoneHandle(Type::DynamicType()));
1080 current_block_->scope->AddVariable(catch_trace_var); 1092 current_block_->scope->AddVariable(catch_trace_var);
1081 } 1093 }
1082 1094
1083 OpenBlock(); // Start try block. 1095 OpenBlock(); // Start try block.
1084 AstNode* expr = ParseExpr(kAllowConst, kConsumeCascades); 1096 AstNode* expr = ParseExpr(kAllowConst, kConsumeCascades);
1085 const Field& field = Field::ZoneHandle(func.saved_static_field()); 1097 const Field& field = Field::ZoneHandle(func.saved_static_field());
1086 ASSERT(!field.is_const()); 1098 ASSERT(!field.is_const());
1087 StoreStaticFieldNode* store = new StoreStaticFieldNode(field.token_pos(), 1099 StoreStaticFieldNode* store = new StoreStaticFieldNode(field.token_pos(),
1088 field, 1100 field,
1089 expr); 1101 expr);
1090 current_block_->statements->Add(store); 1102 current_block_->statements->Add(store);
1091 SequenceNode* try_block = CloseBlock(); // End try block. 1103 SequenceNode* try_block = CloseBlock(); // End try block.
1092 1104
1093 OpenBlock(); // Start catch handler list. 1105 OpenBlock(); // Start catch handler list.
1094 SourceLabel* end_catch_label =
1095 SourceLabel::New(token_pos, NULL, SourceLabel::kCatch);
1096 current_block_->scope->AddLabel(end_catch_label);
1097
1098 OpenBlock(); // Start catch clause. 1106 OpenBlock(); // Start catch clause.
1099 AstNode* compare_transition_sentinel = new ComparisonNode( 1107 AstNode* compare_transition_sentinel = new ComparisonNode(
1100 token_pos, 1108 token_pos,
1101 Token::kEQ_STRICT, 1109 Token::kEQ_STRICT,
1102 new LoadStaticFieldNode(token_pos, field), 1110 new LoadStaticFieldNode(token_pos, field),
1103 new LiteralNode(field.token_pos(), Object::transition_sentinel())); 1111 new LiteralNode(field.token_pos(), Object::transition_sentinel()));
1104 1112
1105 SequenceNode* store_null = new SequenceNode(token_pos, NULL); 1113 SequenceNode* store_null = new SequenceNode(token_pos, NULL);
1106 store_null->Add(new StoreStaticFieldNode( 1114 store_null->Add(new StoreStaticFieldNode(
1107 field.token_pos(), 1115 field.token_pos(),
1108 field, 1116 field,
1109 new LiteralNode(token_pos, Instance::ZoneHandle()))); 1117 new LiteralNode(token_pos, Instance::ZoneHandle())));
1110 AstNode* transition_sentinel_check = 1118 AstNode* transition_sentinel_check =
1111 new IfNode(token_pos, compare_transition_sentinel, store_null, NULL); 1119 new IfNode(token_pos, compare_transition_sentinel, store_null, NULL);
1112 current_block_->statements->Add(transition_sentinel_check); 1120 current_block_->statements->Add(transition_sentinel_check);
1113 1121
1114 current_block_->statements->Add( 1122 current_block_->statements->Add(
1115 new ThrowNode(token_pos, 1123 new ThrowNode(token_pos,
1116 new LoadLocalNode(token_pos, catch_excp_var), 1124 new LoadLocalNode(token_pos, catch_excp_var),
1117 new LoadLocalNode(token_pos, catch_trace_var))); 1125 new LoadLocalNode(token_pos, catch_trace_var)));
1118 current_block_->statements->Add(
1119 new JumpNode(token_pos, Token::kCONTINUE, end_catch_label));
1120 SequenceNode* catch_clause = CloseBlock(); // End catch clause. 1126 SequenceNode* catch_clause = CloseBlock(); // End catch clause.
1121 1127
1122 current_block_->statements->Add(catch_clause); 1128 current_block_->statements->Add(catch_clause);
1123 SequenceNode* catch_handler_list = CloseBlock(); // End catch handler list. 1129 SequenceNode* catch_handler_list = CloseBlock(); // End catch handler list.
1124 CatchClauseNode* catch_block = 1130 CatchClauseNode* catch_block =
1125 new CatchClauseNode(token_pos, 1131 new CatchClauseNode(token_pos,
1126 catch_handler_list, 1132 catch_handler_list,
1127 Array::ZoneHandle(Object::empty_array().raw()), 1133 Array::ZoneHandle(Object::empty_array().raw()),
1128 context_var, 1134 context_var,
1129 catch_excp_var, 1135 catch_excp_var,
1130 catch_trace_var, 1136 catch_trace_var,
1131 CatchClauseNode::kInvalidTryIndex, 1137 CatchClauseNode::kInvalidTryIndex,
1132 false); // No stack trace needed. 1138 false); // No stack trace needed.
1133 1139
1134 AstNode* try_catch_node = new TryCatchNode(token_pos, 1140 AstNode* try_catch_node = new TryCatchNode(token_pos,
1135 try_block, 1141 try_block,
1136 end_catch_label,
1137 context_var, 1142 context_var,
1138 catch_block, 1143 catch_block,
1139 NULL, // No finally block. 1144 NULL, // No finally block.
1140 AllocateTryIndex()); 1145 AllocateTryIndex());
1141 current_block_->statements->Add(try_catch_node); 1146 current_block_->statements->Add(try_catch_node);
1142 return CloseBlock(); 1147 return CloseBlock();
1143 } 1148 }
1144 1149
1145 1150
1146 // Create AstNodes for an implicit instance getter method: 1151 // Create AstNodes for an implicit instance getter method:
(...skipping 5491 matching lines...) Expand 10 before | Expand all | Expand 10 after
6638 condition = InsertClosureCallNodes(condition); 6643 condition = InsertClosureCallNodes(condition);
6639 condition = new UnaryOpNode(condition_pos, Token::kNOT, condition); 6644 condition = new UnaryOpNode(condition_pos, Token::kNOT, condition);
6640 AstNode* assert_throw = MakeAssertCall(condition_pos, condition_end); 6645 AstNode* assert_throw = MakeAssertCall(condition_pos, condition_end);
6641 return new IfNode(condition_pos, 6646 return new IfNode(condition_pos,
6642 condition, 6647 condition,
6643 NodeAsSequenceNode(condition_pos, assert_throw, NULL), 6648 NodeAsSequenceNode(condition_pos, assert_throw, NULL),
6644 NULL); 6649 NULL);
6645 } 6650 }
6646 6651
6647 6652
6648 struct CatchParamDesc { 6653 struct CatchParameter {
hausner 2013/11/08 21:52:30 As I've aked in previous rounds of this review, pl
Kevin Millikin (Google) 2013/11/11 11:39:57 CatchParamDesc is not a very good name. First, wh
6649 CatchParamDesc() 6654 CatchParameter()
6650 : token_pos(0), type(NULL), var(NULL) { } 6655 : token_pos(0), type(NULL), name(NULL), var(NULL) { }
6651 intptr_t token_pos; 6656 intptr_t token_pos;
6652 const AbstractType* type; 6657 const AbstractType* type;
6653 const String* var; 6658 const String* name;
6659 LocalVariable* var;
6654 }; 6660 };
6655 6661
6656 6662
6657 // Populate local scope of the catch block with the catch parameters. 6663 // Populate local scope of the catch block with the catch parameters.
6658 void Parser::AddCatchParamsToScope(const CatchParamDesc& exception_param, 6664 // Return false if the stack trace parameter shadows the exception
6659 const CatchParamDesc& stack_trace_param, 6665 // parameter.
6660 LocalScope* scope) { 6666 static bool AddCatchParamsToScope(CatchParameter* exception_param,
hausner 2013/11/08 21:52:30 Please leave this as a method of the parser and ke
Kevin Millikin (Google) 2013/11/11 11:39:57 Why? The reason I moved it is so we could see the
6661 if (exception_param.var != NULL) { 6667 CatchParameter* stack_trace_param,
6662 LocalVariable* var = new LocalVariable(exception_param.token_pos, 6668 LocalScope* scope) {
6663 *exception_param.var, 6669 if (exception_param->name != NULL) {
6664 *exception_param.type); 6670 LocalVariable* var = new LocalVariable(exception_param->token_pos,
6671 *exception_param->name,
6672 *exception_param->type);
6665 var->set_is_final(); 6673 var->set_is_final();
6666 bool added_to_scope = scope->AddVariable(var); 6674 bool added_to_scope = scope->AddVariable(var);
6667 ASSERT(added_to_scope); 6675 ASSERT(added_to_scope);
6676 exception_param->var = var;
6668 } 6677 }
6669 if (stack_trace_param.var != NULL) { 6678 if (stack_trace_param->name != NULL) {
6670 LocalVariable* var = new LocalVariable(TokenPos(), 6679 LocalVariable* var = new LocalVariable(stack_trace_param->token_pos,
6671 *stack_trace_param.var, 6680 *stack_trace_param->name,
6672 *stack_trace_param.type); 6681 *stack_trace_param->type);
6673 var->set_is_final(); 6682 var->set_is_final();
6674 bool added_to_scope = scope->AddVariable(var); 6683 bool added_to_scope = scope->AddVariable(var);
6675 if (!added_to_scope) { 6684 stack_trace_param->var = var;
6676 ErrorMsg(stack_trace_param.token_pos, 6685 return added_to_scope;
6677 "name '%s' already exists in scope",
6678 stack_trace_param.var->ToCString());
6679 }
6680 } 6686 }
6687 return true;
6681 } 6688 }
6682 6689
6683 6690
6684 SequenceNode* Parser::ParseFinallyBlock() { 6691 SequenceNode* Parser::ParseFinallyBlock() {
6685 TRACE_PARSER("ParseFinallyBlock"); 6692 TRACE_PARSER("ParseFinallyBlock");
6686 OpenBlock(); 6693 OpenBlock();
6687 ExpectToken(Token::kLBRACE); 6694 ExpectToken(Token::kLBRACE);
6688 ParseStatementSequence(); 6695 ParseStatementSequence();
6689 ExpectToken(Token::kRBRACE); 6696 ExpectToken(Token::kRBRACE);
6690 SequenceNode* finally_block = CloseBlock(); 6697 SequenceNode* finally_block = CloseBlock();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
6738 InlinedFinallyNode* finally_node) { 6745 InlinedFinallyNode* finally_node) {
6739 if (node->IsReturnNode()) { 6746 if (node->IsReturnNode()) {
6740 node->AsReturnNode()->AddInlinedFinallyNode(finally_node); 6747 node->AsReturnNode()->AddInlinedFinallyNode(finally_node);
6741 } else { 6748 } else {
6742 ASSERT(node->IsJumpNode()); 6749 ASSERT(node->IsJumpNode());
6743 node->AsJumpNode()->AddInlinedFinallyNode(finally_node); 6750 node->AsJumpNode()->AddInlinedFinallyNode(finally_node);
6744 } 6751 }
6745 } 6752 }
6746 6753
6747 6754
6748 AstNode* Parser::ParseTryStatement(String* label_name) { 6755 SequenceNode* Parser::ParseCatchClauses(
6749 TRACE_PARSER("ParseTryStatement"); 6756 intptr_t handler_pos,
6750 6757 LocalVariable* exception_var,
6751 // We create three stack slots for exceptions here: 6758 LocalVariable* stack_trace_var,
6752 // ':saved_try_context_var' - Used to save the context before start of the try 6759 const GrowableObjectArray& handler_types,
6753 // block. The context register is restored from 6760 bool* needs_stack_trace) {
6754 // this slot before processing the catch block 6761 // All catch blocks are merged into an if-then-else sequence of the
6755 // handler. 6762 // different types specified using the 'is' operator. While parsing
6756 // ':exception_var' - Used to save the current exception object that was 6763 // record the type tests (or NULL for an generic catch) and the catch
6757 // thrown. 6764 // bodies in a pair of parallel lists. Afterward, construct the nested
6758 // ':stacktrace_var' - Used to save the current stack trace object into which 6765 // if-then-else.
6759 // the stack trace was copied into when an exception was 6766 GrowableArray<ComparisonNode*> type_tests;
6760 // thrown. 6767 GrowableArray<SequenceNode*> catch_blocks;
6761 // :exception_var and :stacktrace_var get set with the exception object 6768 while ((CurrentToken() == Token::kCATCH) || IsLiteral("on")) {
6762 // and the stacktrace object when an exception is thrown. 6769 // Open a block that contains the if or an unconditional body. It's
6763 // These three implicit variables can never be captured variables. 6770 // closed in the loop that builds the if-then-else nest.
6764 LocalVariable* context_var =
6765 current_block_->scope->LocalLookupVariable(Symbols::SavedTryContextVar());
6766 if (context_var == NULL) {
6767 context_var = new LocalVariable(TokenPos(),
6768 Symbols::SavedTryContextVar(),
6769 Type::ZoneHandle(Type::DynamicType()));
6770 current_block_->scope->AddVariable(context_var);
6771 }
6772 LocalVariable* catch_excp_var =
6773 current_block_->scope->LocalLookupVariable(Symbols::ExceptionVar());
6774 if (catch_excp_var == NULL) {
6775 catch_excp_var = new LocalVariable(TokenPos(),
6776 Symbols::ExceptionVar(),
6777 Type::ZoneHandle(Type::DynamicType()));
6778 current_block_->scope->AddVariable(catch_excp_var);
6779 }
6780 LocalVariable* catch_trace_var =
6781 current_block_->scope->LocalLookupVariable(Symbols::StacktraceVar());
6782 if (catch_trace_var == NULL) {
6783 catch_trace_var = new LocalVariable(TokenPos(),
6784 Symbols::StacktraceVar(),
6785 Type::ZoneHandle(Type::DynamicType()));
6786 current_block_->scope->AddVariable(catch_trace_var);
6787 }
6788
6789 const intptr_t try_pos = TokenPos();
6790 ConsumeToken(); // Consume the 'try'.
6791
6792 SourceLabel* try_label = NULL;
6793 if (label_name != NULL) {
6794 try_label = SourceLabel::New(try_pos, label_name, SourceLabel::kStatement);
6795 OpenBlock(); 6771 OpenBlock();
6796 current_block_->scope->AddLabel(try_label);
6797 }
6798
6799 // Now parse the 'try' block.
6800 OpenBlock();
6801 PushTryBlock(current_block_);
6802 ExpectToken(Token::kLBRACE);
6803 ParseStatementSequence();
6804 ExpectToken(Token::kRBRACE);
6805 SequenceNode* try_block = CloseBlock();
6806
6807 if ((CurrentToken() != Token::kCATCH) && !IsLiteral("on") &&
6808 (CurrentToken() != Token::kFINALLY)) {
6809 ErrorMsg("catch or finally clause expected");
6810 }
6811
6812 // Now create a label for the end of catch block processing so that we can
6813 // jump over the catch block code after executing the try block.
6814 SourceLabel* end_catch_label =
6815 SourceLabel::New(TokenPos(), NULL, SourceLabel::kCatch);
6816
6817 // Now parse the 'catch' blocks if any and merge all of them into
6818 // an if-then sequence of the different types specified using the 'is'
6819 // operator.
6820 bool generic_catch_seen = false;
6821 const intptr_t handler_pos = TokenPos();
6822 OpenBlock(); // Start the catch block sequence.
6823 current_block_->scope->AddLabel(end_catch_label);
6824 const GrowableObjectArray& handler_types =
6825 GrowableObjectArray::Handle(GrowableObjectArray::New());
6826 bool needs_stacktrace = false;
6827 while ((CurrentToken() == Token::kCATCH) || IsLiteral("on")) {
6828 const intptr_t catch_pos = TokenPos(); 6772 const intptr_t catch_pos = TokenPos();
6829 CatchParamDesc exception_param; 6773 CatchParameter exception_param;
6830 CatchParamDesc stack_trace_param; 6774 CatchParameter stack_trace_param;
6831 if (IsLiteral("on")) { 6775 if (IsLiteral("on")) {
6832 ConsumeToken(); 6776 ConsumeToken();
6833 exception_param.type = &AbstractType::ZoneHandle( 6777 exception_param.type = &AbstractType::ZoneHandle(
6834 ParseType(ClassFinalizer::kCanonicalize)); 6778 ParseType(ClassFinalizer::kCanonicalize));
6835 } else { 6779 } else {
6836 exception_param.type = &AbstractType::ZoneHandle(Type::DynamicType()); 6780 exception_param.type = &AbstractType::ZoneHandle(Type::DynamicType());
6837 } 6781 }
6838 if (CurrentToken() == Token::kCATCH) { 6782 if (CurrentToken() == Token::kCATCH) {
6839 ConsumeToken(); // Consume the 'catch'. 6783 ConsumeToken(); // Consume the 'catch'.
6840 ExpectToken(Token::kLPAREN); 6784 ExpectToken(Token::kLPAREN);
6841 exception_param.token_pos = TokenPos(); 6785 exception_param.token_pos = TokenPos();
6842 exception_param.var = ExpectIdentifier("identifier expected"); 6786 exception_param.name = ExpectIdentifier("identifier expected");
6843 if (CurrentToken() == Token::kCOMMA) { 6787 if (CurrentToken() == Token::kCOMMA) {
6844 ConsumeToken(); 6788 ConsumeToken();
6845 // TODO(hausner): Make implicit type be StackTrace, not dynamic. 6789 // TODO(hausner): Make implicit type be StackTrace, not dynamic.
6846 stack_trace_param.type = 6790 stack_trace_param.type =
6847 &AbstractType::ZoneHandle(Type::DynamicType()); 6791 &AbstractType::ZoneHandle(Type::DynamicType());
6848 stack_trace_param.token_pos = TokenPos(); 6792 stack_trace_param.token_pos = TokenPos();
6849 stack_trace_param.var = ExpectIdentifier("identifier expected"); 6793 stack_trace_param.name = ExpectIdentifier("identifier expected");
6850 } 6794 }
6851 ExpectToken(Token::kRPAREN); 6795 ExpectToken(Token::kRPAREN);
6852 } 6796 }
6853 6797
6854 // Create a block containing the catch clause parameters and 6798 // Create a block containing the catch clause parameters and the
6855 // the following code: 6799 // following code:
6856 // 1) Store exception object and stack trace object into user-defined 6800 // 1) Store exception object and stack trace object into user-defined
6857 // variables (as needed). 6801 // variables (as needed).
6858 // 2) Nested block with source code from catch clause block. 6802 // 2) Nested block with source code from catch clause block.
6859 // 3) Unconditional JUMP to the end of the try block.
6860 OpenBlock(); 6803 OpenBlock();
6861 AddCatchParamsToScope(exception_param, 6804 if (!AddCatchParamsToScope(&exception_param, &stack_trace_param,
6862 stack_trace_param, 6805 current_block_->scope)) {
6863 current_block_->scope); 6806 ErrorMsg(stack_trace_param.token_pos,
6807 "name '%s' already exists in scope",
6808 stack_trace_param.name->ToCString());
6809 }
6864 6810
6865 if (exception_param.var != NULL) { 6811 if (exception_param.var != NULL) {
6866 // Generate code to load the exception object (:exception_var) into 6812 // Generate code to load the exception object (:exception_var) into
6867 // the exception variable specified in this block. 6813 // the exception variable specified in this block.
6868 LocalVariable* var = LookupLocalScope(*exception_param.var); 6814 ASSERT(exception_var != NULL);
6869 ASSERT(var != NULL); 6815 current_block_->statements->Add(
6870 ASSERT(catch_excp_var != NULL); 6816 new StoreLocalNode(catch_pos, exception_param.var,
6871 current_block_->statements->Add( 6817 new LoadLocalNode(catch_pos, exception_var)));
6872 new StoreLocalNode(catch_pos, var,
6873 new LoadLocalNode(catch_pos, catch_excp_var)));
6874 } 6818 }
6875 if (stack_trace_param.var != NULL) { 6819 if (stack_trace_param.var != NULL) {
6876 // A stack trace variable is specified in this block, so generate code 6820 // A stack trace variable is specified in this block, so generate code
6877 // to load the stack trace object (:stacktrace_var) into the stack trace 6821 // to load the stack trace object (:stack_trace_var) into the stack
6878 // variable specified in this block. 6822 // trace variable specified in this block.
6879 needs_stacktrace = true; 6823 *needs_stack_trace = true;
6880 ArgumentListNode* no_args = new ArgumentListNode(catch_pos); 6824 ArgumentListNode* no_args = new ArgumentListNode(catch_pos);
6881 LocalVariable* trace = LookupLocalScope(*stack_trace_param.var); 6825 ASSERT(stack_trace_var != NULL);
6882 ASSERT(catch_trace_var != NULL); 6826 current_block_->statements->Add(
6883 current_block_->statements->Add( 6827 new StoreLocalNode(catch_pos, stack_trace_param.var,
6884 new StoreLocalNode(catch_pos, trace, 6828 new LoadLocalNode(catch_pos, stack_trace_var)));
6885 new LoadLocalNode(catch_pos, catch_trace_var)));
6886 current_block_->statements->Add( 6829 current_block_->statements->Add(
6887 new InstanceCallNode( 6830 new InstanceCallNode(
6888 catch_pos, 6831 catch_pos,
6889 new LoadLocalNode(catch_pos, trace), 6832 new LoadLocalNode(catch_pos, stack_trace_param.var),
6890 Library::PrivateCoreLibName(Symbols::_setupFullStackTrace()), 6833 Library::PrivateCoreLibName(Symbols::_setupFullStackTrace()),
6891 no_args)); 6834 no_args));
6892 } 6835 }
6893 6836
6894 // Add nested block with user-defined code. 6837 // Add nested block with user-defined code. This blocks allows
6838 // declarations in the body to shadow the catch parameters.
6895 CheckToken(Token::kLBRACE); 6839 CheckToken(Token::kLBRACE);
6896 current_block_->statements->Add(ParseNestedStatement(false, NULL)); 6840 current_block_->statements->Add(ParseNestedStatement(false, NULL));
6897 6841 catch_blocks.Add(CloseBlock());
6898 // Add unconditional jump to end of catch clause list. 6842
6899 current_block_->statements->Add( 6843 const bool is_bad_type =
6900 new JumpNode(catch_pos, Token::kCONTINUE, end_catch_label)); 6844 exception_param.type->IsMalformed() ||
6901 6845 exception_param.type->IsMalbounded();
6902 SequenceNode* catch_clause = CloseBlock(); 6846 if (exception_param.type->IsDynamicType() || is_bad_type) {
6903 6847 // There is no exception type or else it is malformed or malbounded.
6904 const bool is_bad_type = exception_param.type->IsMalformed() || 6848 // In the first cast, unconditionally execute the catch body. In the
6905 exception_param.type->IsMalbounded(); 6849 // second case, unconditionally throw.
6906 if (!is_bad_type && !exception_param.type->IsDynamicType()) { 6850 type_tests.Add(NULL);
6907 // Has a type specification that is not malformed or malbounded. 6851 if (is_bad_type) {
6908 // Now form an 'if type check' to guard the catch handler code. 6852 // Replace the body with one that throws.
6853 SequenceNode* block = new SequenceNode(catch_pos, NULL);
6854 block->Add(ThrowTypeError(catch_pos, *exception_param.type));
6855 catch_blocks.Last() = block;
6856 }
6857 // This catch clause will handle all exceptions. We can safely forget
6858 // all previous catch clause types.
6859 handler_types.SetLength(0);
6860 handler_types.Add(*exception_param.type);
6861 } else {
6862 // Has a type specification that is not malformed or malbounded. Now
6863 // form an 'if type check' to guard the catch handler code.
6909 if (!exception_param.type->IsInstantiated() && 6864 if (!exception_param.type->IsInstantiated() &&
6910 (current_block_->scope->function_level() > 0)) { 6865 (current_block_->scope->function_level() > 0)) {
6911 // Make sure that the instantiator is captured. 6866 // Make sure that the instantiator is captured.
6912 CaptureInstantiator(); 6867 CaptureInstantiator();
6913 } 6868 }
6914 TypeNode* exception_type = new TypeNode(catch_pos, *exception_param.type); 6869 TypeNode* exception_type = new TypeNode(catch_pos, *exception_param.type);
6915 AstNode* exception_var = new LoadLocalNode(catch_pos, catch_excp_var); 6870 AstNode* exception_value = new LoadLocalNode(catch_pos, exception_var);
6916 if (!exception_type->type().IsInstantiated()) { 6871 if (!exception_type->type().IsInstantiated()) {
6917 EnsureExpressionTemp(); 6872 EnsureExpressionTemp();
6918 } 6873 }
6919 AstNode* type_cond_expr = new ComparisonNode( 6874 type_tests.Add(new ComparisonNode(catch_pos, Token::kIS, exception_value,
6920 catch_pos, Token::kIS, exception_var, exception_type); 6875 exception_type));
6921 current_block_->statements->Add( 6876
6922 new IfNode(catch_pos, type_cond_expr, catch_clause, NULL)); 6877 // Do not add uninstantiated types (e.g. type parameter T or generic
6923 6878 // type List<T>), since the debugger won't be able to instantiate it
6924 // Do not add uninstantiated types (e.g. type parameter T or 6879 // when walking the stack.
6925 // generic type List<T>), since the debugger won't be able to 6880 //
6926 // instantiate it when walking the stack. 6881 // This means that the debugger is not able to determine whether an
6927 // This means that the debugger is not able to determine whether 6882 // exception is caught if the catch clause uses generic types. It
6928 // an exception is caught if the catch clause uses generic types. 6883 // will report the exception as uncaught when in fact it might be
6929 // It will report the exception as uncaught when in fact it might 6884 // caught and handled when we unwind the stack.
6930 // be caught and handled when we unwind the stack.
6931 if (exception_param.type->IsInstantiated()) { 6885 if (exception_param.type->IsInstantiated()) {
6932 handler_types.Add(*exception_param.type); 6886 handler_types.Add(*exception_param.type);
6933 } 6887 }
6888 }
6889
6890 ASSERT(type_tests.length() == catch_blocks.length());
6891 }
6892
6893 // If the exception is not caught by any clause here, rethrow it. This is
6894 // the last (innermost) else block if it turns out to be reachable.
6895 SequenceNode* current = new SequenceNode(handler_pos, NULL);
6896 current->Add(new ThrowNode(handler_pos,
6897 new LoadLocalNode(handler_pos, exception_var),
6898 new LoadLocalNode(handler_pos, stack_trace_var)));
6899 // Build the if/then/else nest from the inside out.
6900 while (!type_tests.is_empty()) {
6901 ComparisonNode* type_test = type_tests.RemoveLast();
6902 SequenceNode* catch_block = catch_blocks.RemoveLast();
6903 if (type_test == NULL) {
6904 // Unconditional catch, forget the clauses seen so far.
6905 current_block_->statements->Add(catch_block);
6934 } else { 6906 } else {
6935 if (is_bad_type) { 6907 current_block_->statements->Add(
6936 current_block_->statements->Add(ThrowTypeError(catch_pos, 6908 new IfNode(type_test->token_pos(), type_test, catch_block, current));
6937 *exception_param.type)); 6909 }
6938 // We still add the dead code below to satisfy the code generator. 6910 current = CloseBlock();
hausner 2013/11/08 21:52:30 I don't think the way you nest the blocks is corre
Kevin Millikin (Google) 2013/11/11 11:39:57 I don't think that's right. The intent is to repr
6939 } 6911 }
6940 // No exception type exists in the catch clause so execute the 6912 return current;
6941 // catch handler code unconditionally. 6913 }
6942 current_block_->statements->Add(catch_clause); 6914
6943 generic_catch_seen = true; 6915
6944 // This catch clause will handle all exceptions. We can safely forget 6916 AstNode* Parser::ParseTryStatement(String* label_name) {
6945 // all previous catch clause types. 6917 TRACE_PARSER("ParseTryStatement");
6946 handler_types.SetLength(0); 6918
6947 handler_types.Add(*exception_param.type); 6919 // We create three variables for exceptions here:
6948 } 6920 // ':saved_try_context_var' - Used to save the context before the start of
6949 } 6921 // the try block. The context register is
6950 6922 // restored from this variable before
6951 SequenceNode* catch_handler_list = CloseBlock(); 6923 // processing the catch block handler.
6924 // ':exception_var' - Used to save the current exception object that was
6925 // thrown.
6926 // ':stack_trace_var' - Used to save the current stack trace object which
6927 // the stack trace was copied into when an exception
6928 // was thrown.
6929 // :exception_var and :stack_trace_var get set with the exception object
6930 // and the stack trace object when an exception is thrown. These three
6931 // implicit variables can never be captured.
6932 LocalVariable* context_var =
6933 current_block_->scope->LocalLookupVariable(Symbols::SavedTryContextVar());
6934 if (context_var == NULL) {
6935 context_var = new LocalVariable(TokenPos(),
6936 Symbols::SavedTryContextVar(),
6937 Type::ZoneHandle(Type::DynamicType()));
6938 current_block_->scope->AddVariable(context_var);
6939 }
6940 LocalVariable* exception_var =
6941 current_block_->scope->LocalLookupVariable(Symbols::ExceptionVar());
6942 if (exception_var == NULL) {
6943 exception_var = new LocalVariable(TokenPos(),
6944 Symbols::ExceptionVar(),
6945 Type::ZoneHandle(Type::DynamicType()));
6946 current_block_->scope->AddVariable(exception_var);
6947 }
6948 LocalVariable* stack_trace_var =
6949 current_block_->scope->LocalLookupVariable(Symbols::StackTraceVar());
6950 if (stack_trace_var == NULL) {
6951 stack_trace_var = new LocalVariable(TokenPos(),
6952 Symbols::StackTraceVar(),
6953 Type::ZoneHandle(Type::DynamicType()));
6954 current_block_->scope->AddVariable(stack_trace_var);
6955 }
6956
6957 const intptr_t try_pos = TokenPos();
6958 ConsumeToken(); // Consume the 'try'.
6959
6960 SourceLabel* try_label = NULL;
6961 if (label_name != NULL) {
6962 try_label = SourceLabel::New(try_pos, label_name, SourceLabel::kStatement);
6963 OpenBlock();
6964 current_block_->scope->AddLabel(try_label);
6965 }
6966
6967 // Now parse the 'try' block.
6968 OpenBlock();
6969 PushTryBlock(current_block_);
6970 ExpectToken(Token::kLBRACE);
6971 ParseStatementSequence();
6972 ExpectToken(Token::kRBRACE);
6973 SequenceNode* try_block = CloseBlock();
6974
6975 if ((CurrentToken() != Token::kCATCH) && !IsLiteral("on") &&
6976 (CurrentToken() != Token::kFINALLY)) {
6977 ErrorMsg("catch or finally clause expected");
6978 }
6979
6980 // Now parse the 'catch' blocks if any.
6981 try_blocks_list_->enter_catch();
6982 const intptr_t handler_pos = TokenPos();
6983 const GrowableObjectArray& handler_types =
6984 GrowableObjectArray::Handle(GrowableObjectArray::New());
6985 bool needs_stack_trace = false;
6986 SequenceNode* catch_handler_list =
6987 ParseCatchClauses(handler_pos, exception_var, stack_trace_var,
6988 handler_types, &needs_stack_trace);
6989
6952 TryBlocks* inner_try_block = PopTryBlock(); 6990 TryBlocks* inner_try_block = PopTryBlock();
6953 const intptr_t try_index = inner_try_block->try_index(); 6991 const intptr_t try_index = inner_try_block->try_index();
6954 TryBlocks* outer_try_block = try_blocks_list_; 6992 TryBlocks* outer_try_block = try_blocks_list_;
6955 const intptr_t outer_try_index = (outer_try_block != NULL) 6993 const intptr_t outer_try_index = (outer_try_block != NULL)
6956 ? outer_try_block->try_index() 6994 ? outer_try_block->try_index()
6957 : CatchClauseNode::kInvalidTryIndex; 6995 : CatchClauseNode::kInvalidTryIndex;
6958 6996
6959 // Finally parse the 'finally' block. 6997 // Finally parse the 'finally' block.
6960 SequenceNode* finally_block = NULL; 6998 SequenceNode* finally_block = NULL;
6961 if (CurrentToken() == Token::kFINALLY) { 6999 if (CurrentToken() == Token::kFINALLY) {
(...skipping 11 matching lines...) Expand all
6973 context_var, 7011 context_var,
6974 outer_try_index); 7012 outer_try_index);
6975 AddFinallyBlockToNode(node_to_inline, node); 7013 AddFinallyBlockToNode(node_to_inline, node);
6976 node_index += 1; 7014 node_index += 1;
6977 node_to_inline = inner_try_block->GetNodeToInlineFinally(node_index); 7015 node_to_inline = inner_try_block->GetNodeToInlineFinally(node_index);
6978 tokens_iterator_.SetCurrentPosition(finally_pos); 7016 tokens_iterator_.SetCurrentPosition(finally_pos);
6979 } 7017 }
6980 finally_block = ParseFinallyBlock(); 7018 finally_block = ParseFinallyBlock();
6981 } 7019 }
6982 7020
6983 if (!generic_catch_seen) { 7021 CatchClauseNode* catch_clause =
6984 // No generic catch handler exists so rethrow the exception so that
6985 // the next catch handler can deal with it.
6986 catch_handler_list->Add(
6987 new ThrowNode(handler_pos,
6988 new LoadLocalNode(handler_pos, catch_excp_var),
6989 new LoadLocalNode(handler_pos, catch_trace_var)));
6990 }
6991 CatchClauseNode* catch_block =
6992 new CatchClauseNode(handler_pos, 7022 new CatchClauseNode(handler_pos,
6993 catch_handler_list, 7023 catch_handler_list,
6994 Array::ZoneHandle(Array::MakeArray(handler_types)), 7024 Array::ZoneHandle(Array::MakeArray(handler_types)),
6995 context_var, 7025 context_var,
6996 catch_excp_var, 7026 exception_var,
6997 catch_trace_var, 7027 stack_trace_var,
6998 (finally_block != NULL) 7028 (finally_block != NULL)
6999 ? AllocateTryIndex() 7029 ? AllocateTryIndex()
7000 : CatchClauseNode::kInvalidTryIndex, 7030 : CatchClauseNode::kInvalidTryIndex,
7001 needs_stacktrace); 7031 needs_stack_trace);
7002 7032
7003 // Now create the try/catch ast node and return it. If there is a label 7033 // Now create the try/catch ast node and return it. If there is a label
7004 // on the try/catch, close the block that's embedding the try statement 7034 // on the try/catch, close the block that's embedding the try statement
7005 // and attach the label to it. 7035 // and attach the label to it.
7006 AstNode* try_catch_node = 7036 AstNode* try_catch_node =
7007 new TryCatchNode(try_pos, try_block, end_catch_label, 7037 new TryCatchNode(try_pos, try_block, context_var, catch_clause,
7008 context_var, catch_block, finally_block, try_index); 7038 finally_block, try_index);
7009 7039
7010 if (try_label != NULL) { 7040 if (try_label != NULL) {
7011 current_block_->statements->Add(try_catch_node); 7041 current_block_->statements->Add(try_catch_node);
7012 SequenceNode* sequence = CloseBlock(); 7042 SequenceNode* sequence = CloseBlock();
7013 sequence->set_label(try_label); 7043 sequence->set_label(try_label);
7014 try_catch_node = sequence; 7044 try_catch_node = sequence;
7015 } 7045 }
7016 return try_catch_node; 7046 return try_catch_node;
7017 } 7047 }
7018 7048
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
7155 AddNodeForFinallyInlining(statement); 7185 AddNodeForFinallyInlining(statement);
7156 ExpectSemicolon(); 7186 ExpectSemicolon();
7157 } else if (CurrentToken() == Token::kSEMICOLON) { 7187 } else if (CurrentToken() == Token::kSEMICOLON) {
7158 // Empty statement, nothing to do. 7188 // Empty statement, nothing to do.
7159 ConsumeToken(); 7189 ConsumeToken();
7160 } else if (CurrentToken() == Token::kRETHROW) { 7190 } else if (CurrentToken() == Token::kRETHROW) {
7161 // Rethrow of current exception. 7191 // Rethrow of current exception.
7162 ConsumeToken(); 7192 ConsumeToken();
7163 ExpectSemicolon(); 7193 ExpectSemicolon();
7164 // Check if it is ok to do a rethrow. 7194 // Check if it is ok to do a rethrow.
7165 SourceLabel* label = current_block_->scope->LookupInnermostCatchLabel(); 7195 if ((try_blocks_list_ == NULL) || !try_blocks_list_->inside_catch()) {
7166 if (label == NULL ||
7167 label->FunctionLevel() != current_block_->scope->function_level()) {
7168 ErrorMsg(statement_pos, "rethrow of an exception is not valid here"); 7196 ErrorMsg(statement_pos, "rethrow of an exception is not valid here");
7169 } 7197 }
7170 ASSERT(label->owner() != NULL); 7198 // The exception and stack trace variables are bound in the block
7171 LocalScope* scope = label->owner()->parent(); 7199 // containing the try.
7200 LocalScope* scope = try_blocks_list_->try_block()->scope->parent();
7172 ASSERT(scope != NULL); 7201 ASSERT(scope != NULL);
7173 LocalVariable* excp_var = 7202 LocalVariable* excp_var =
7174 scope->LocalLookupVariable(Symbols::ExceptionVar()); 7203 scope->LocalLookupVariable(Symbols::ExceptionVar());
7175 ASSERT(excp_var != NULL); 7204 ASSERT(excp_var != NULL);
7176 LocalVariable* trace_var = 7205 LocalVariable* trace_var =
7177 scope->LocalLookupVariable(Symbols::StacktraceVar()); 7206 scope->LocalLookupVariable(Symbols::StackTraceVar());
7178 ASSERT(trace_var != NULL); 7207 ASSERT(trace_var != NULL);
7179 statement = new ThrowNode(statement_pos, 7208 statement = new ThrowNode(statement_pos,
7180 new LoadLocalNode(statement_pos, excp_var), 7209 new LoadLocalNode(statement_pos, excp_var),
7181 new LoadLocalNode(statement_pos, trace_var)); 7210 new LoadLocalNode(statement_pos, trace_var));
7182 } else { 7211 } else {
7183 statement = ParseExpr(kAllowConst, kConsumeCascades); 7212 statement = ParseExpr(kAllowConst, kConsumeCascades);
7184 ExpectSemicolon(); 7213 ExpectSemicolon();
7185 } 7214 }
7186 return statement; 7215 return statement;
7187 } 7216 }
(...skipping 3544 matching lines...) Expand 10 before | Expand all | Expand 10 after
10732 void Parser::SkipQualIdent() { 10761 void Parser::SkipQualIdent() {
10733 ASSERT(IsIdentifier()); 10762 ASSERT(IsIdentifier());
10734 ConsumeToken(); 10763 ConsumeToken();
10735 if (CurrentToken() == Token::kPERIOD) { 10764 if (CurrentToken() == Token::kPERIOD) {
10736 ConsumeToken(); // Consume the kPERIOD token. 10765 ConsumeToken(); // Consume the kPERIOD token.
10737 ExpectIdentifier("identifier expected after '.'"); 10766 ExpectIdentifier("identifier expected after '.'");
10738 } 10767 }
10739 } 10768 }
10740 10769
10741 } // namespace dart 10770 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/parser.h ('k') | runtime/vm/symbols.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698