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
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_CLASS_FINALIZER_H_
6#define RUNTIME_VM_CLASS_FINALIZER_H_
7
8#include <memory>
9
10#include "vm/allocation.h"
11#include "vm/growable_array.h"
12#include "vm/object.h"
13
14namespace dart {
15
16// Traverses all pending, unfinalized classes, validates and marks them as
17// finalized.
18class ClassFinalizer : public AllStatic {
19 public:
20 typedef ZoneGrowableHandlePtrArray<const AbstractType> PendingTypes;
21
22 // Modes for finalization. The ordering is relevant.
23 enum FinalizationKind {
24 kFinalize, // Finalize type and type arguments.
25 kCanonicalize // Finalize and canonicalize.
26 };
27
28 // Finalize given type while parsing class cls.
29 // Also canonicalize and bound check type if applicable.
30 static AbstractTypePtr FinalizeType(
31 const Class& cls,
32 const AbstractType& type,
33 FinalizationKind finalization = kCanonicalize,
34 PendingTypes* pending_types = NULL);
35
36 // Finalize the types in the functions's signature while parsing class cls.
37 static void FinalizeSignature(const Class& cls,
38 const Function& function,
39 FinalizationKind finalization = kCanonicalize);
40
41 // Return false if we still have classes pending to be finalized.
42 static bool AllClassesFinalized();
43
44 // Useful for sorting classes to make dispatch faster.
45 static void SortClasses();
46 static void RemapClassIds(intptr_t* old_to_new_cid);
47 static void RehashTypes();
48 static void ClearAllCode(bool including_nonchanging_cids = false);
49
50 // Return whether processing pending classes (ObjectStore::pending_classes_)
51 // failed. The function returns true if the processing was successful.
52 // If processing fails, an error message is set in the sticky error field
53 // in the object store.
54 static bool ProcessPendingClasses();
55
56 // Finalize the types appearing in the declaration of class 'cls', i.e. its
57 // type parameters and their upper bounds, its super type and interfaces.
58 // Note that the fields and functions have not been parsed yet (unless cls
59 // is an anonymous top level class).
60 static void FinalizeTypesInClass(const Class& cls);
61
62 // Register class in the lists of direct subclasses and direct implementors.
63 static void RegisterClassInHierarchy(Zone* zone, const Class& cls);
64
65 // Ensures members of the class are loaded, class layout is finalized and size
66 // registered in class table.
67 static void FinalizeClass(const Class& cls);
68 // Makes class instantiatable and usable by generated code.
69 static ErrorPtr AllocateFinalizeClass(const Class& cls);
70
71 // Completes loading of the class, this populates the function
72 // and fields of the class.
73 //
74 // Returns Error::null() if there is no loading error.
75 static ErrorPtr LoadClassMembers(const Class& cls);
76
77#if !defined(DART_PRECOMPILED_RUNTIME)
78 // Verify that the classes have been properly prefinalized. This is
79 // needed during bootstrapping where the classes have been preloaded.
80 static void VerifyBootstrapClasses();
81#endif // !defined(DART_PRECOMPILED_RUNTIME)
82
83 private:
84 static void AllocateEnumValues(const Class& enum_cls);
85 static void FinalizeTypeParameters(const Class& cls);
86 static intptr_t ExpandAndFinalizeTypeArguments(const Class& cls,
87 const AbstractType& type,
88 PendingTypes* pending_types);
89 static void FinalizeTypeArguments(const Class& cls,
90 const TypeArguments& arguments,
91 intptr_t num_uninitialized_arguments,
92 PendingTypes* pending_types,
93 TrailPtr trail);
94 static void CheckRecursiveType(const Class& cls,
95 const AbstractType& type,
96 PendingTypes* pending_types);
97 static void FinalizeUpperBounds(
98 const Class& cls,
99 FinalizationKind finalization = kCanonicalize);
100 static void FinalizeMemberTypes(const Class& cls);
101 static void PrintClassInformation(const Class& cls);
102
103 static void ReportError(const Error& error);
104 static void ReportError(const Class& cls,
105 TokenPosition token_pos,
106 const char* format,
107 ...) PRINTF_ATTRIBUTE(3, 4);
108 static void ReportErrors(const Error& prev_error,
109 const Class& cls,
110 TokenPosition token_pos,
111 const char* format,
112 ...) PRINTF_ATTRIBUTE(4, 5);
113
114 // Verify implicit offsets recorded in the VM for direct access to fields of
115 // Dart instances (e.g: _TypedListView, _ByteDataView).
116 static void VerifyImplicitFieldOffsets();
117};
118
119} // namespace dart
120
121#endif // RUNTIME_VM_CLASS_FINALIZER_H_
122