OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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/class_finalizer.h" | 5 #include "vm/class_finalizer.h" |
6 | 6 |
7 #include "vm/code_generator.h" | 7 #include "vm/code_generator.h" |
8 #include "vm/flags.h" | 8 #include "vm/flags.h" |
9 #include "vm/heap.h" | 9 #include "vm/heap.h" |
10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
(...skipping 2501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2512 } | 2512 } |
2513 | 2513 |
2514 | 2514 |
2515 // Allocate instances for each enumeration value, and populate the | 2515 // Allocate instances for each enumeration value, and populate the |
2516 // static field 'values'. | 2516 // static field 'values'. |
2517 // By allocating the instances programmatically, we save an implicit final | 2517 // By allocating the instances programmatically, we save an implicit final |
2518 // getter function object for each enumeration value and for the | 2518 // getter function object for each enumeration value and for the |
2519 // values field. We also don't have to generate the code for these getters | 2519 // values field. We also don't have to generate the code for these getters |
2520 // from thin air (no source code is available). | 2520 // from thin air (no source code is available). |
2521 void ClassFinalizer::AllocateEnumValues(const Class &enum_cls) { | 2521 void ClassFinalizer::AllocateEnumValues(const Class &enum_cls) { |
2522 Thread* thread = Thread::Current(); | |
2523 Zone* zone = thread->zone(); | |
2524 const Field& index_field = | 2522 const Field& index_field = |
2525 Field::Handle(zone, enum_cls.LookupInstanceField(Symbols::Index())); | 2523 Field::Handle(enum_cls.LookupInstanceField(Symbols::Index())); |
2526 ASSERT(!index_field.IsNull()); | 2524 ASSERT(!index_field.IsNull()); |
2527 const Field& values_field = | 2525 const Field& values_field = |
2528 Field::Handle(zone, enum_cls.LookupStaticField(Symbols::Values())); | 2526 Field::Handle(enum_cls.LookupStaticField(Symbols::Values())); |
2529 ASSERT(!values_field.IsNull()); | 2527 ASSERT(!values_field.IsNull()); |
2530 ASSERT(Instance::Handle(zone, values_field.StaticValue()).IsArray()); | 2528 ASSERT(Instance::Handle(values_field.StaticValue()).IsArray()); |
2531 Array& values_list = Array::Handle( | 2529 Array& values_list = Array::Handle( |
2532 zone, Array::RawCast(values_field.StaticValue())); | 2530 Array::RawCast(values_field.StaticValue())); |
2533 | 2531 |
2534 const Array& fields = Array::Handle(zone, enum_cls.fields()); | 2532 const Array& fields = Array::Handle(enum_cls.fields()); |
2535 Field& field = Field::Handle(zone); | 2533 Field& field = Field::Handle(); |
2536 Instance& ordinal_value = Instance::Handle(zone); | 2534 Instance& ordinal_value = Instance::Handle(); |
2537 Instance& enum_value = Instance::Handle(zone); | 2535 Instance& enum_value = Instance::Handle(); |
2538 | 2536 |
2539 for (intptr_t i = 0; i < fields.Length(); i++) { | 2537 for (intptr_t i = 0; i < fields.Length(); i++) { |
2540 field = Field::RawCast(fields.At(i)); | 2538 field = Field::RawCast(fields.At(i)); |
2541 if (!field.is_static()) continue; | 2539 if (!field.is_static()) continue; |
2542 ordinal_value = field.StaticValue(); | 2540 ordinal_value = field.StaticValue(); |
2543 // The static fields that need to be initialized with enum instances | 2541 // The static fields that need to be initialized with enum instances |
2544 // contain the smi value of the ordinal number, which was stored in | 2542 // contain the smi value of the ordinal number, which was stored in |
2545 // the field by the parser. Other fields contain non-smi values. | 2543 // the field by the parser. Other fields contain non-smi values. |
2546 if (!ordinal_value.IsSmi()) continue; | 2544 if (!ordinal_value.IsSmi()) continue; |
2547 enum_value = Instance::New(enum_cls, Heap::kOld); | 2545 enum_value = Instance::New(enum_cls, Heap::kOld); |
2548 enum_value.SetField(index_field, ordinal_value); | 2546 enum_value.SetField(index_field, ordinal_value); |
2549 const char* error_msg = ""; | 2547 const char* error_msg = ""; |
2550 enum_value = enum_value.CheckAndCanonicalize(thread, &error_msg); | 2548 enum_value = enum_value.CheckAndCanonicalize(&error_msg); |
2551 ASSERT(!enum_value.IsNull()); | 2549 ASSERT(!enum_value.IsNull()); |
2552 ASSERT(enum_value.IsCanonical()); | 2550 ASSERT(enum_value.IsCanonical()); |
2553 field.SetStaticValue(enum_value, true); | 2551 field.SetStaticValue(enum_value, true); |
2554 field.RecordStore(enum_value); | 2552 field.RecordStore(enum_value); |
2555 intptr_t ord = Smi::Cast(ordinal_value).Value(); | 2553 intptr_t ord = Smi::Cast(ordinal_value).Value(); |
2556 ASSERT(ord < values_list.Length()); | 2554 ASSERT(ord < values_list.Length()); |
2557 values_list.SetAt(ord, enum_value); | 2555 values_list.SetAt(ord, enum_value); |
2558 } | 2556 } |
2559 values_list.MakeImmutable(); | 2557 values_list.MakeImmutable(); |
2560 const char* error_msg = NULL; | 2558 const char* error_msg = NULL; |
2561 values_list ^= values_list.CheckAndCanonicalize(thread, &error_msg); | 2559 values_list ^= values_list.CheckAndCanonicalize(&error_msg); |
2562 ASSERT(!values_list.IsNull()); | 2560 ASSERT(!values_list.IsNull()); |
2563 } | 2561 } |
2564 | 2562 |
2565 | 2563 |
2566 bool ClassFinalizer::IsSuperCycleFree(const Class& cls) { | 2564 bool ClassFinalizer::IsSuperCycleFree(const Class& cls) { |
2567 Class& test1 = Class::Handle(cls.raw()); | 2565 Class& test1 = Class::Handle(cls.raw()); |
2568 Class& test2 = Class::Handle(cls.SuperClass()); | 2566 Class& test2 = Class::Handle(cls.SuperClass()); |
2569 // A finalized class has been checked for cycles. | 2567 // A finalized class has been checked for cycles. |
2570 // Using the hare and tortoise algorithm for locating cycles. | 2568 // Using the hare and tortoise algorithm for locating cycles. |
2571 while (!test1.is_type_finalized() && | 2569 while (!test1.is_type_finalized() && |
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3320 ASSERT(fields_array.Length() == ByteBuffer::NumberOfFields()); | 3318 ASSERT(fields_array.Length() == ByteBuffer::NumberOfFields()); |
3321 field ^= fields_array.At(0); | 3319 field ^= fields_array.At(0); |
3322 ASSERT(field.Offset() == ByteBuffer::data_offset()); | 3320 ASSERT(field.Offset() == ByteBuffer::data_offset()); |
3323 name ^= field.name(); | 3321 name ^= field.name(); |
3324 expected_name ^= String::New("_data"); | 3322 expected_name ^= String::New("_data"); |
3325 ASSERT(String::EqualsIgnoringPrivateKey(name, expected_name)); | 3323 ASSERT(String::EqualsIgnoringPrivateKey(name, expected_name)); |
3326 #endif | 3324 #endif |
3327 } | 3325 } |
3328 | 3326 |
3329 } // namespace dart | 3327 } // namespace dart |
OLD | NEW |