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

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

Issue 10787021: Fix type checking of void type. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/debugger_api_impl_test.cc ('k') | runtime/vm/flow_graph_compiler_ia32.cc » ('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/flow_graph_builder.h" 5 #include "vm/flow_graph_builder.h"
6 6
7 #include "vm/ast_printer.h" 7 #include "vm/ast_printer.h"
8 #include "vm/bit_vector.h" 8 #include "vm/bit_vector.h"
9 #include "vm/code_descriptors.h" 9 #include "vm/code_descriptors.h"
10 #include "vm/dart_entry.h" 10 #include "vm/dart_entry.h"
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 // specific than the given dst_type. 333 // specific than the given dst_type.
334 static bool IsStaticTypeMoreSpecific(Value* value, 334 static bool IsStaticTypeMoreSpecific(Value* value,
335 const AbstractType& dst_type) { 335 const AbstractType& dst_type) {
336 ASSERT(!dst_type.IsMalformed()); 336 ASSERT(!dst_type.IsMalformed());
337 337
338 // Any type is more specific than the Dynamic type and than the Object type. 338 // Any type is more specific than the Dynamic type and than the Object type.
339 if (dst_type.IsDynamicType() || dst_type.IsObjectType()) { 339 if (dst_type.IsDynamicType() || dst_type.IsObjectType()) {
340 return true; 340 return true;
341 } 341 }
342 342
343 // It is a compile-time error to explicitly return a value (including null)
344 // from a void function. However, functions that do not explicitly return a
345 // value, implicitly return null. This includes void functions. Therefore, we
346 // skip the type test here and trust the parser to only return null in void
347 // function.
348 if (dst_type.IsVoidType()) {
349 // TODO(regis): Should we perform this null test at run-time?
350 return true;
351 }
352
353 // Do not perform type check elimination if this optimization is turned off. 343 // Do not perform type check elimination if this optimization is turned off.
354 if (!FLAG_eliminate_type_checks) { 344 if (!FLAG_eliminate_type_checks) {
355 return false; 345 return false;
356 } 346 }
357 347
358 // If nothing is known about the value, as is the case for passed-in 348 // If nothing is known about the value, as is the case for passed-in
359 // parameters, and since dst_type is not one of the tested cases above, then 349 // parameters, and since dst_type is not one of the tested cases above, then
360 // the type test cannot be eliminated. 350 // the type test cannot be eliminated.
361 if (value == NULL) { 351 if (value == NULL) {
362 return false; 352 return false;
363 } 353 }
364 354
355 // If the value is the null constant, its type (NullType) is more specific
356 // than the destination type, even if the destination type is the void type,
357 // since a void function is allowed to return null.
358 if (value->IsConstant() && value->AsConstant()->value().IsNull()) {
359 return true;
360 }
361
362 // Functions that do not explicitly return a value, implicitly return null,
363 // except generative constructors, which return the object being constructed.
364 // It is therefore acceptable for void functions to return null.
365 // In case of a null constant, we have already returned true above, else we
366 // return false here.
367 if (dst_type.IsVoidType()) {
368 return false;
369 }
370
365 // Consider the static type of the value. 371 // Consider the static type of the value.
366 const AbstractType& static_type = AbstractType::Handle(value->StaticType()); 372 const AbstractType& static_type = AbstractType::Handle(value->StaticType());
367 ASSERT(!static_type.IsMalformed()); 373 ASSERT(!static_type.IsMalformed());
368 374
369 // If the static type of the value is void, the only allowed value is null, 375 // If the static type of the value is void, we are type checking the result of
370 // which must be verified by the type test. 376 // a void function, which was checked to be null at the return statement
371 // TODO(regis): Eliminate the test if the value is constant null. 377 // inside the function.
372 if (static_type.IsVoidType()) { 378 if (static_type.IsVoidType()) {
373 return false; 379 return true;
374 } 380 }
375 381
376 // If the static type of the value is NullType, the type test is eliminated. 382 // If the static type of the value is NullType, the type test is eliminated.
377 // There are only three instances that can be of Class Null: 383 // There are only three instances that can be of Class Null:
378 // Object::null(), Object::sentinel(), and Object::transition_sentinel(). 384 // Object::null(), Object::sentinel(), and Object::transition_sentinel().
379 // The inline code and run time code performing the type check will never 385 // The inline code and run time code performing the type check will never
380 // encounter the 2 sentinel values. The type check of a sentinel value 386 // encounter the 2 sentinel values. The type check of a sentinel value
381 // will always be eliminated here, because these sentinel values can only 387 // will always be eliminated here, because these sentinel values can only
382 // be encountered as constants, never as actual value of a heap object 388 // be encountered as constants, never as actual value of a heap object
383 // being type checked. 389 // being type checked.
(...skipping 2281 matching lines...) Expand 10 before | Expand all | Expand 10 after
2665 char* chars = reinterpret_cast<char*>( 2671 char* chars = reinterpret_cast<char*>(
2666 Isolate::Current()->current_zone()->Allocate(len)); 2672 Isolate::Current()->current_zone()->Allocate(len));
2667 OS::SNPrint(chars, len, kFormat, function_name, reason); 2673 OS::SNPrint(chars, len, kFormat, function_name, reason);
2668 const Error& error = Error::Handle( 2674 const Error& error = Error::Handle(
2669 LanguageError::New(String::Handle(String::New(chars)))); 2675 LanguageError::New(String::Handle(String::New(chars))));
2670 Isolate::Current()->long_jump_base()->Jump(1, error); 2676 Isolate::Current()->long_jump_base()->Jump(1, error);
2671 } 2677 }
2672 2678
2673 2679
2674 } // namespace dart 2680 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/debugger_api_impl_test.cc ('k') | runtime/vm/flow_graph_compiler_ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698