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

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: Incorporated final review comments. 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/scopes.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 5493 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 CatchParamDesc {
6649 CatchParamDesc() 6654 CatchParamDesc()
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 void Parser::AddCatchParamsToScope(CatchParamDesc* exception_param,
6659 const CatchParamDesc& stack_trace_param, 6665 CatchParamDesc* stack_trace_param,
6660 LocalScope* scope) { 6666 LocalScope* scope) {
6661 if (exception_param.var != NULL) { 6667 if (exception_param->name != NULL) {
6662 LocalVariable* var = new LocalVariable(exception_param.token_pos, 6668 LocalVariable* var = new LocalVariable(exception_param->token_pos,
6663 *exception_param.var, 6669 *exception_param->name,
6664 *exception_param.type); 6670 *exception_param->type);
6665 var->set_is_final(); 6671 var->set_is_final();
6666 bool added_to_scope = scope->AddVariable(var); 6672 bool added_to_scope = scope->AddVariable(var);
6667 ASSERT(added_to_scope); 6673 ASSERT(added_to_scope);
6674 exception_param->var = var;
6668 } 6675 }
6669 if (stack_trace_param.var != NULL) { 6676 if (stack_trace_param->name != NULL) {
6670 LocalVariable* var = new LocalVariable(TokenPos(), 6677 LocalVariable* var = new LocalVariable(stack_trace_param->token_pos,
6671 *stack_trace_param.var, 6678 *stack_trace_param->name,
6672 *stack_trace_param.type); 6679 *stack_trace_param->type);
6673 var->set_is_final(); 6680 var->set_is_final();
6674 bool added_to_scope = scope->AddVariable(var); 6681 bool added_to_scope = scope->AddVariable(var);
6675 if (!added_to_scope) { 6682 if (!added_to_scope) {
6676 ErrorMsg(stack_trace_param.token_pos, 6683 ErrorMsg(stack_trace_param->token_pos,
6677 "name '%s' already exists in scope", 6684 "name '%s' already exists in scope",
6678 stack_trace_param.var->ToCString()); 6685 stack_trace_param->name->ToCString());
6679 } 6686 }
6687 stack_trace_param->var = var;
6680 } 6688 }
6681 } 6689 }
6682 6690
6683 6691
6684 SequenceNode* Parser::ParseFinallyBlock() { 6692 SequenceNode* Parser::ParseFinallyBlock() {
6685 TRACE_PARSER("ParseFinallyBlock"); 6693 TRACE_PARSER("ParseFinallyBlock");
6686 OpenBlock(); 6694 OpenBlock();
6687 ExpectToken(Token::kLBRACE); 6695 ExpectToken(Token::kLBRACE);
6688 ParseStatementSequence(); 6696 ParseStatementSequence();
6689 ExpectToken(Token::kRBRACE); 6697 ExpectToken(Token::kRBRACE);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
6738 InlinedFinallyNode* finally_node) { 6746 InlinedFinallyNode* finally_node) {
6739 if (node->IsReturnNode()) { 6747 if (node->IsReturnNode()) {
6740 node->AsReturnNode()->AddInlinedFinallyNode(finally_node); 6748 node->AsReturnNode()->AddInlinedFinallyNode(finally_node);
6741 } else { 6749 } else {
6742 ASSERT(node->IsJumpNode()); 6750 ASSERT(node->IsJumpNode());
6743 node->AsJumpNode()->AddInlinedFinallyNode(finally_node); 6751 node->AsJumpNode()->AddInlinedFinallyNode(finally_node);
6744 } 6752 }
6745 } 6753 }
6746 6754
6747 6755
6748 AstNode* Parser::ParseTryStatement(String* label_name) { 6756 SequenceNode* Parser::ParseCatchClauses(
6749 TRACE_PARSER("ParseTryStatement"); 6757 intptr_t handler_pos,
6750 6758 LocalVariable* exception_var,
6751 // We create three stack slots for exceptions here: 6759 LocalVariable* stack_trace_var,
6752 // ':saved_try_context_var' - Used to save the context before start of the try 6760 const GrowableObjectArray& handler_types,
6753 // block. The context register is restored from 6761 bool* needs_stack_trace) {
6754 // this slot before processing the catch block 6762 // All catch blocks are merged into an if-then-else sequence of the
6755 // handler. 6763 // different types specified using the 'is' operator. While parsing
6756 // ':exception_var' - Used to save the current exception object that was 6764 // record the type tests (either a ComparisonNode or else the LiteralNode
6757 // thrown. 6765 // true for a generic catch) and the catch bodies in a pair of parallel
6758 // ':stacktrace_var' - Used to save the current stack trace object into which 6766 // lists. Afterward, construct the nested if-then-else.
6759 // the stack trace was copied into when an exception was 6767 bool generic_catch_seen = false;
6760 // thrown. 6768 GrowableArray<AstNode*> type_tests;
6761 // :exception_var and :stacktrace_var get set with the exception object 6769 GrowableArray<SequenceNode*> catch_blocks;
6762 // and the stacktrace object when an exception is thrown. 6770 while ((CurrentToken() == Token::kCATCH) || IsLiteral("on")) {
6763 // These three implicit variables can never be captured variables. 6771 // Open a block that contains the if or an unconditional body. It's
6764 LocalVariable* context_var = 6772 // closed in the loop that builds the if-then-else nest.
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(); 6773 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(); 6774 const intptr_t catch_pos = TokenPos();
6829 CatchParamDesc exception_param; 6775 CatchParamDesc exception_param;
6830 CatchParamDesc stack_trace_param; 6776 CatchParamDesc stack_trace_param;
6831 if (IsLiteral("on")) { 6777 if (IsLiteral("on")) {
6832 ConsumeToken(); 6778 ConsumeToken();
6833 exception_param.type = &AbstractType::ZoneHandle( 6779 exception_param.type = &AbstractType::ZoneHandle(
6834 ParseType(ClassFinalizer::kCanonicalize)); 6780 ParseType(ClassFinalizer::kCanonicalize));
6835 } else { 6781 } else {
6836 exception_param.type = &AbstractType::ZoneHandle(Type::DynamicType()); 6782 exception_param.type = &AbstractType::ZoneHandle(Type::DynamicType());
6837 } 6783 }
6838 if (CurrentToken() == Token::kCATCH) { 6784 if (CurrentToken() == Token::kCATCH) {
6839 ConsumeToken(); // Consume the 'catch'. 6785 ConsumeToken(); // Consume the 'catch'.
6840 ExpectToken(Token::kLPAREN); 6786 ExpectToken(Token::kLPAREN);
6841 exception_param.token_pos = TokenPos(); 6787 exception_param.token_pos = TokenPos();
6842 exception_param.var = ExpectIdentifier("identifier expected"); 6788 exception_param.name = ExpectIdentifier("identifier expected");
6843 if (CurrentToken() == Token::kCOMMA) { 6789 if (CurrentToken() == Token::kCOMMA) {
6844 ConsumeToken(); 6790 ConsumeToken();
6845 // TODO(hausner): Make implicit type be StackTrace, not dynamic. 6791 // TODO(hausner): Make implicit type be StackTrace, not dynamic.
6846 stack_trace_param.type = 6792 stack_trace_param.type =
6847 &AbstractType::ZoneHandle(Type::DynamicType()); 6793 &AbstractType::ZoneHandle(Type::DynamicType());
6848 stack_trace_param.token_pos = TokenPos(); 6794 stack_trace_param.token_pos = TokenPos();
6849 stack_trace_param.var = ExpectIdentifier("identifier expected"); 6795 stack_trace_param.name = ExpectIdentifier("identifier expected");
6850 } 6796 }
6851 ExpectToken(Token::kRPAREN); 6797 ExpectToken(Token::kRPAREN);
6852 } 6798 }
6853 6799
6854 // Create a block containing the catch clause parameters and 6800 // Create a block containing the catch clause parameters and the
6855 // the following code: 6801 // following code:
6856 // 1) Store exception object and stack trace object into user-defined 6802 // 1) Store exception object and stack trace object into user-defined
6857 // variables (as needed). 6803 // variables (as needed).
6858 // 2) Nested block with source code from catch clause block. 6804 // 2) Nested block with source code from catch clause block.
6859 // 3) Unconditional JUMP to the end of the try block.
6860 OpenBlock(); 6805 OpenBlock();
6861 AddCatchParamsToScope(exception_param, 6806 AddCatchParamsToScope(&exception_param, &stack_trace_param,
6862 stack_trace_param,
6863 current_block_->scope); 6807 current_block_->scope);
6864 6808
6865 if (exception_param.var != NULL) { 6809 if (exception_param.var != NULL) {
6866 // Generate code to load the exception object (:exception_var) into 6810 // Generate code to load the exception object (:exception_var) into
6867 // the exception variable specified in this block. 6811 // the exception variable specified in this block.
6868 LocalVariable* var = LookupLocalScope(*exception_param.var); 6812 ASSERT(exception_var != NULL);
6869 ASSERT(var != NULL);
6870 ASSERT(catch_excp_var != NULL);
6871 current_block_->statements->Add( 6813 current_block_->statements->Add(
6872 new StoreLocalNode(catch_pos, var, 6814 new StoreLocalNode(catch_pos, exception_param.var,
6873 new LoadLocalNode(catch_pos, catch_excp_var))); 6815 new LoadLocalNode(catch_pos, exception_var)));
6874 } 6816 }
6875 if (stack_trace_param.var != NULL) { 6817 if (stack_trace_param.var != NULL) {
6876 // A stack trace variable is specified in this block, so generate code 6818 // 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 6819 // to load the stack trace object (:stack_trace_var) into the stack
6878 // variable specified in this block. 6820 // trace variable specified in this block.
6879 needs_stacktrace = true; 6821 *needs_stack_trace = true;
6880 ArgumentListNode* no_args = new ArgumentListNode(catch_pos); 6822 ArgumentListNode* no_args = new ArgumentListNode(catch_pos);
6881 LocalVariable* trace = LookupLocalScope(*stack_trace_param.var); 6823 ASSERT(stack_trace_var != NULL);
6882 ASSERT(catch_trace_var != NULL);
6883 current_block_->statements->Add( 6824 current_block_->statements->Add(
6884 new StoreLocalNode(catch_pos, trace, 6825 new StoreLocalNode(catch_pos, stack_trace_param.var,
6885 new LoadLocalNode(catch_pos, catch_trace_var))); 6826 new LoadLocalNode(catch_pos, stack_trace_var)));
6886 current_block_->statements->Add( 6827 current_block_->statements->Add(
6887 new InstanceCallNode( 6828 new InstanceCallNode(
6888 catch_pos, 6829 catch_pos,
6889 new LoadLocalNode(catch_pos, trace), 6830 new LoadLocalNode(catch_pos, stack_trace_param.var),
6890 Library::PrivateCoreLibName(Symbols::_setupFullStackTrace()), 6831 Library::PrivateCoreLibName(Symbols::_setupFullStackTrace()),
6891 no_args)); 6832 no_args));
6892 } 6833 }
6893 6834
6894 // Add nested block with user-defined code. 6835 // Add nested block with user-defined code. This blocks allows
6836 // declarations in the body to shadow the catch parameters.
6895 CheckToken(Token::kLBRACE); 6837 CheckToken(Token::kLBRACE);
6896 current_block_->statements->Add(ParseNestedStatement(false, NULL)); 6838 current_block_->statements->Add(ParseNestedStatement(false, NULL));
6897 6839 catch_blocks.Add(CloseBlock());
6898 // Add unconditional jump to end of catch clause list. 6840
6899 current_block_->statements->Add( 6841 const bool is_bad_type =
6900 new JumpNode(catch_pos, Token::kCONTINUE, end_catch_label)); 6842 exception_param.type->IsMalformed() ||
6901 6843 exception_param.type->IsMalbounded();
6902 SequenceNode* catch_clause = CloseBlock(); 6844 if (exception_param.type->IsDynamicType() || is_bad_type) {
6903 6845 // There is no exception type or else it is malformed or malbounded.
6904 const bool is_bad_type = exception_param.type->IsMalformed() || 6846 // In the first case, unconditionally execute the catch body. In the
6905 exception_param.type->IsMalbounded(); 6847 // second case, unconditionally throw.
6906 if (!is_bad_type && !exception_param.type->IsDynamicType()) { 6848 generic_catch_seen = true;
6907 // Has a type specification that is not malformed or malbounded. 6849 type_tests.Add(new LiteralNode(catch_pos, Bool::True()));
6908 // Now form an 'if type check' to guard the catch handler code. 6850 if (is_bad_type) {
6851 // Replace the body with one that throws.
6852 SequenceNode* block = new SequenceNode(catch_pos, NULL);
6853 block->Add(ThrowTypeError(catch_pos, *exception_param.type));
6854 catch_blocks.Last() = block;
6855 }
6856 // This catch clause will handle all exceptions. We can safely forget
6857 // all previous catch clause types.
6858 handler_types.SetLength(0);
6859 handler_types.Add(*exception_param.type);
6860 } else {
6861 // Has a type specification that is not malformed or malbounded. Now
6862 // form an 'if type check' to guard the catch handler code.
6909 if (!exception_param.type->IsInstantiated() && 6863 if (!exception_param.type->IsInstantiated() &&
6910 (current_block_->scope->function_level() > 0)) { 6864 (current_block_->scope->function_level() > 0)) {
6911 // Make sure that the instantiator is captured. 6865 // Make sure that the instantiator is captured.
6912 CaptureInstantiator(); 6866 CaptureInstantiator();
6913 } 6867 }
6914 TypeNode* exception_type = new TypeNode(catch_pos, *exception_param.type); 6868 TypeNode* exception_type = new TypeNode(catch_pos, *exception_param.type);
6915 AstNode* exception_var = new LoadLocalNode(catch_pos, catch_excp_var); 6869 AstNode* exception_value = new LoadLocalNode(catch_pos, exception_var);
6916 if (!exception_type->type().IsInstantiated()) { 6870 if (!exception_type->type().IsInstantiated()) {
6917 EnsureExpressionTemp(); 6871 EnsureExpressionTemp();
6918 } 6872 }
6919 AstNode* type_cond_expr = new ComparisonNode( 6873 type_tests.Add(new ComparisonNode(catch_pos, Token::kIS, exception_value,
6920 catch_pos, Token::kIS, exception_var, exception_type); 6874 exception_type));
6921 current_block_->statements->Add( 6875
6922 new IfNode(catch_pos, type_cond_expr, catch_clause, NULL)); 6876 // Do not add uninstantiated types (e.g. type parameter T or generic
6923 6877 // 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 6878 // when walking the stack.
6925 // generic type List<T>), since the debugger won't be able to 6879 //
6926 // instantiate it when walking the stack. 6880 // This means that the debugger is not able to determine whether an
6927 // This means that the debugger is not able to determine whether 6881 // exception is caught if the catch clause uses generic types. It
6928 // an exception is caught if the catch clause uses generic types. 6882 // 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 6883 // caught and handled when we unwind the stack.
6930 // be caught and handled when we unwind the stack. 6884 if (!generic_catch_seen && exception_param.type->IsInstantiated()) {
6931 if (exception_param.type->IsInstantiated()) {
6932 handler_types.Add(*exception_param.type); 6885 handler_types.Add(*exception_param.type);
6933 } 6886 }
6934 } else { 6887 }
6935 if (is_bad_type) { 6888
6936 current_block_->statements->Add(ThrowTypeError(catch_pos, 6889 ASSERT(type_tests.length() == catch_blocks.length());
6937 *exception_param.type)); 6890 }
6938 // We still add the dead code below to satisfy the code generator. 6891
6939 } 6892 // Build the if/then/else nest from the inside out. Keep the AST simple
6940 // No exception type exists in the catch clause so execute the 6893 // for the case of a single generic catch clause. The initial value of
6941 // catch handler code unconditionally. 6894 // current is the last (innermost) else block if there were any catch
6942 current_block_->statements->Add(catch_clause); 6895 // clauses.
6943 generic_catch_seen = true; 6896 SequenceNode* current = NULL;
6944 // This catch clause will handle all exceptions. We can safely forget 6897 if (!generic_catch_seen) {
6945 // all previous catch clause types. 6898 // There isn't a generic catch clause so create a clause body that
6946 handler_types.SetLength(0); 6899 // rethrows the exception. This includes the case that there were no
6947 handler_types.Add(*exception_param.type); 6900 // catch clauses.
6948 } 6901 current = new SequenceNode(handler_pos, NULL);
6949 } 6902 current->Add(
6950 6903 new ThrowNode(handler_pos,
6951 SequenceNode* catch_handler_list = CloseBlock(); 6904 new LoadLocalNode(handler_pos, exception_var),
6905 new LoadLocalNode(handler_pos, stack_trace_var)));
6906 } else if (type_tests.Last()->IsLiteralNode()) {
6907 ASSERT(type_tests.Last()->AsLiteralNode()->literal().raw() ==
6908 Bool::True().raw());
6909 // The last body is entered unconditionally. Start building the
6910 // if/then/else nest with that body as the innermost else block.
6911 // Note that it is nested inside an extra block which we opened
6912 // before we knew the body was entered unconditionally.
6913 type_tests.RemoveLast();
6914 current_block_->statements->Add(catch_blocks.RemoveLast());
6915 current = CloseBlock();
6916 }
6917 // If the last body was entered conditionally and there is no need to add
6918 // a rethrow, use an empty else body (current = NULL above).
6919
6920 while (!type_tests.is_empty()) {
6921 AstNode* type_test = type_tests.RemoveLast();
6922 SequenceNode* catch_block = catch_blocks.RemoveLast();
6923 current_block_->statements->Add(
6924 new IfNode(type_test->token_pos(), type_test, catch_block, current));
6925 current = CloseBlock();
6926 }
6927 return current;
6928 }
6929
6930
6931 AstNode* Parser::ParseTryStatement(String* label_name) {
6932 TRACE_PARSER("ParseTryStatement");
6933
6934 // We create three variables for exceptions here:
6935 // ':saved_try_context_var' - Used to save the context before the start of
6936 // the try block. The context register is
6937 // restored from this variable before
6938 // processing the catch block handler.
6939 // ':exception_var' - Used to save the current exception object that was
6940 // thrown.
6941 // ':stack_trace_var' - Used to save the current stack trace object which
6942 // the stack trace was copied into when an exception
6943 // was thrown.
6944 // :exception_var and :stack_trace_var get set with the exception object
6945 // and the stack trace object when an exception is thrown. These three
6946 // implicit variables can never be captured.
6947 LocalVariable* context_var =
6948 current_block_->scope->LocalLookupVariable(Symbols::SavedTryContextVar());
6949 if (context_var == NULL) {
6950 context_var = new LocalVariable(TokenPos(),
6951 Symbols::SavedTryContextVar(),
6952 Type::ZoneHandle(Type::DynamicType()));
6953 current_block_->scope->AddVariable(context_var);
6954 }
6955 LocalVariable* exception_var =
6956 current_block_->scope->LocalLookupVariable(Symbols::ExceptionVar());
6957 if (exception_var == NULL) {
6958 exception_var = new LocalVariable(TokenPos(),
6959 Symbols::ExceptionVar(),
6960 Type::ZoneHandle(Type::DynamicType()));
6961 current_block_->scope->AddVariable(exception_var);
6962 }
6963 LocalVariable* stack_trace_var =
6964 current_block_->scope->LocalLookupVariable(Symbols::StackTraceVar());
6965 if (stack_trace_var == NULL) {
6966 stack_trace_var = new LocalVariable(TokenPos(),
6967 Symbols::StackTraceVar(),
6968 Type::ZoneHandle(Type::DynamicType()));
6969 current_block_->scope->AddVariable(stack_trace_var);
6970 }
6971
6972 const intptr_t try_pos = TokenPos();
6973 ConsumeToken(); // Consume the 'try'.
6974
6975 SourceLabel* try_label = NULL;
6976 if (label_name != NULL) {
6977 try_label = SourceLabel::New(try_pos, label_name, SourceLabel::kStatement);
6978 OpenBlock();
6979 current_block_->scope->AddLabel(try_label);
6980 }
6981
6982 // Now parse the 'try' block.
6983 OpenBlock();
6984 PushTryBlock(current_block_);
6985 ExpectToken(Token::kLBRACE);
6986 ParseStatementSequence();
6987 ExpectToken(Token::kRBRACE);
6988 SequenceNode* try_block = CloseBlock();
6989
6990 if ((CurrentToken() != Token::kCATCH) && !IsLiteral("on") &&
6991 (CurrentToken() != Token::kFINALLY)) {
6992 ErrorMsg("catch or finally clause expected");
6993 }
6994
6995 // Now parse the 'catch' blocks if any.
6996 try_blocks_list_->enter_catch();
6997 const intptr_t handler_pos = TokenPos();
6998 const GrowableObjectArray& handler_types =
6999 GrowableObjectArray::Handle(GrowableObjectArray::New());
7000 bool needs_stack_trace = false;
7001 SequenceNode* catch_handler_list =
7002 ParseCatchClauses(handler_pos, exception_var, stack_trace_var,
7003 handler_types, &needs_stack_trace);
7004
6952 TryBlocks* inner_try_block = PopTryBlock(); 7005 TryBlocks* inner_try_block = PopTryBlock();
6953 const intptr_t try_index = inner_try_block->try_index(); 7006 const intptr_t try_index = inner_try_block->try_index();
6954 TryBlocks* outer_try_block = try_blocks_list_; 7007 TryBlocks* outer_try_block = try_blocks_list_;
6955 const intptr_t outer_try_index = (outer_try_block != NULL) 7008 const intptr_t outer_try_index = (outer_try_block != NULL)
6956 ? outer_try_block->try_index() 7009 ? outer_try_block->try_index()
6957 : CatchClauseNode::kInvalidTryIndex; 7010 : CatchClauseNode::kInvalidTryIndex;
6958 7011
6959 // Finally parse the 'finally' block. 7012 // Finally parse the 'finally' block.
6960 SequenceNode* finally_block = NULL; 7013 SequenceNode* finally_block = NULL;
6961 if (CurrentToken() == Token::kFINALLY) { 7014 if (CurrentToken() == Token::kFINALLY) {
(...skipping 11 matching lines...) Expand all
6973 context_var, 7026 context_var,
6974 outer_try_index); 7027 outer_try_index);
6975 AddFinallyBlockToNode(node_to_inline, node); 7028 AddFinallyBlockToNode(node_to_inline, node);
6976 node_index += 1; 7029 node_index += 1;
6977 node_to_inline = inner_try_block->GetNodeToInlineFinally(node_index); 7030 node_to_inline = inner_try_block->GetNodeToInlineFinally(node_index);
6978 tokens_iterator_.SetCurrentPosition(finally_pos); 7031 tokens_iterator_.SetCurrentPosition(finally_pos);
6979 } 7032 }
6980 finally_block = ParseFinallyBlock(); 7033 finally_block = ParseFinallyBlock();
6981 } 7034 }
6982 7035
6983 if (!generic_catch_seen) { 7036 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, 7037 new CatchClauseNode(handler_pos,
6993 catch_handler_list, 7038 catch_handler_list,
6994 Array::ZoneHandle(Array::MakeArray(handler_types)), 7039 Array::ZoneHandle(Array::MakeArray(handler_types)),
6995 context_var, 7040 context_var,
6996 catch_excp_var, 7041 exception_var,
6997 catch_trace_var, 7042 stack_trace_var,
6998 (finally_block != NULL) 7043 (finally_block != NULL)
6999 ? AllocateTryIndex() 7044 ? AllocateTryIndex()
7000 : CatchClauseNode::kInvalidTryIndex, 7045 : CatchClauseNode::kInvalidTryIndex,
7001 needs_stacktrace); 7046 needs_stack_trace);
7002 7047
7003 // Now create the try/catch ast node and return it. If there is a label 7048 // 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 7049 // on the try/catch, close the block that's embedding the try statement
7005 // and attach the label to it. 7050 // and attach the label to it.
7006 AstNode* try_catch_node = 7051 AstNode* try_catch_node =
7007 new TryCatchNode(try_pos, try_block, end_catch_label, 7052 new TryCatchNode(try_pos, try_block, context_var, catch_clause,
7008 context_var, catch_block, finally_block, try_index); 7053 finally_block, try_index);
7009 7054
7010 if (try_label != NULL) { 7055 if (try_label != NULL) {
7011 current_block_->statements->Add(try_catch_node); 7056 current_block_->statements->Add(try_catch_node);
7012 SequenceNode* sequence = CloseBlock(); 7057 SequenceNode* sequence = CloseBlock();
7013 sequence->set_label(try_label); 7058 sequence->set_label(try_label);
7014 try_catch_node = sequence; 7059 try_catch_node = sequence;
7015 } 7060 }
7016 return try_catch_node; 7061 return try_catch_node;
7017 } 7062 }
7018 7063
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
7155 AddNodeForFinallyInlining(statement); 7200 AddNodeForFinallyInlining(statement);
7156 ExpectSemicolon(); 7201 ExpectSemicolon();
7157 } else if (CurrentToken() == Token::kSEMICOLON) { 7202 } else if (CurrentToken() == Token::kSEMICOLON) {
7158 // Empty statement, nothing to do. 7203 // Empty statement, nothing to do.
7159 ConsumeToken(); 7204 ConsumeToken();
7160 } else if (CurrentToken() == Token::kRETHROW) { 7205 } else if (CurrentToken() == Token::kRETHROW) {
7161 // Rethrow of current exception. 7206 // Rethrow of current exception.
7162 ConsumeToken(); 7207 ConsumeToken();
7163 ExpectSemicolon(); 7208 ExpectSemicolon();
7164 // Check if it is ok to do a rethrow. 7209 // Check if it is ok to do a rethrow.
7165 SourceLabel* label = current_block_->scope->LookupInnermostCatchLabel(); 7210 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"); 7211 ErrorMsg(statement_pos, "rethrow of an exception is not valid here");
7169 } 7212 }
7170 ASSERT(label->owner() != NULL); 7213 // The exception and stack trace variables are bound in the block
7171 LocalScope* scope = label->owner()->parent(); 7214 // containing the try.
7215 LocalScope* scope = try_blocks_list_->try_block()->scope->parent();
7172 ASSERT(scope != NULL); 7216 ASSERT(scope != NULL);
7173 LocalVariable* excp_var = 7217 LocalVariable* excp_var =
7174 scope->LocalLookupVariable(Symbols::ExceptionVar()); 7218 scope->LocalLookupVariable(Symbols::ExceptionVar());
7175 ASSERT(excp_var != NULL); 7219 ASSERT(excp_var != NULL);
7176 LocalVariable* trace_var = 7220 LocalVariable* trace_var =
7177 scope->LocalLookupVariable(Symbols::StacktraceVar()); 7221 scope->LocalLookupVariable(Symbols::StackTraceVar());
7178 ASSERT(trace_var != NULL); 7222 ASSERT(trace_var != NULL);
7179 statement = new ThrowNode(statement_pos, 7223 statement = new ThrowNode(statement_pos,
7180 new LoadLocalNode(statement_pos, excp_var), 7224 new LoadLocalNode(statement_pos, excp_var),
7181 new LoadLocalNode(statement_pos, trace_var)); 7225 new LoadLocalNode(statement_pos, trace_var));
7182 } else { 7226 } else {
7183 statement = ParseExpr(kAllowConst, kConsumeCascades); 7227 statement = ParseExpr(kAllowConst, kConsumeCascades);
7184 ExpectSemicolon(); 7228 ExpectSemicolon();
7185 } 7229 }
7186 return statement; 7230 return statement;
7187 } 7231 }
(...skipping 3557 matching lines...) Expand 10 before | Expand all | Expand 10 after
10745 void Parser::SkipQualIdent() { 10789 void Parser::SkipQualIdent() {
10746 ASSERT(IsIdentifier()); 10790 ASSERT(IsIdentifier());
10747 ConsumeToken(); 10791 ConsumeToken();
10748 if (CurrentToken() == Token::kPERIOD) { 10792 if (CurrentToken() == Token::kPERIOD) {
10749 ConsumeToken(); // Consume the kPERIOD token. 10793 ConsumeToken(); // Consume the kPERIOD token.
10750 ExpectIdentifier("identifier expected after '.'"); 10794 ExpectIdentifier("identifier expected after '.'");
10751 } 10795 }
10752 } 10796 }
10753 10797
10754 } // namespace dart 10798 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/parser.h ('k') | runtime/vm/scopes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698