Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
dart::kernel::ConstantReader Class Reference

#include <constant_reader.h>

Public Member Functions

 ConstantReader (KernelReaderHelper *helper, ActiveClass *active_class)
 
virtual ~ConstantReader ()
 
bool IsPragmaInstanceConstant (intptr_t constant_index, intptr_t *pragma_name_constant_index, intptr_t *pragma_options_constant_index)
 
bool IsStringConstant (intptr_t constant_index, const char *name)
 
bool GetStringConstant (intptr_t constant_index, String *out_value)
 
InstancePtr ReadConstantInitializer ()
 
InstancePtr ReadConstantExpression ()
 
ObjectPtr ReadAnnotations ()
 
bool IsInstanceConstant (intptr_t constant_index, const Class &clazz)
 
InstancePtr ReadConstant (intptr_t constant_index)
 
intptr_t NumConstants ()
 

Detailed Description

Definition at line 20 of file constant_reader.h.

Constructor & Destructor Documentation

◆ ConstantReader()

dart::kernel::ConstantReader::ConstantReader ( KernelReaderHelper helper,
ActiveClass active_class 
)

Definition at line 19 of file constant_reader.cc.

21 : helper_(helper),
22 zone_(helper->zone_),
23 translation_helper_(helper->translation_helper_),
24 active_class_(active_class),
25 result_(Object::Handle(zone_)) {}
static Object & Handle()
Definition: object.h:407

◆ ~ConstantReader()

virtual dart::kernel::ConstantReader::~ConstantReader ( )
inlinevirtual

Definition at line 24 of file constant_reader.h.

24{}

Member Function Documentation

◆ GetStringConstant()

bool dart::kernel::ConstantReader::GetStringConstant ( intptr_t  constant_index,
String out_value 
)

Definition at line 87 of file constant_reader.cc.

88 {
89 KernelReaderHelper reader(Z, &H, H.constants_table(), 0);
90 NavigateToIndex(&reader, constant_index);
91
92 if (reader.ReadByte() == kStringConstant) {
93 const StringIndex index = reader.ReadStringReference();
94 *out_value = H.DartSymbolPlain(index).ptr();
95 return true;
96 }
97 return false;
98}
#define Z
Definition: SkMD5.cpp:130

◆ IsInstanceConstant()

bool dart::kernel::ConstantReader::IsInstanceConstant ( intptr_t  constant_index,
const Class clazz 
)

Definition at line 193 of file constant_reader.cc.

194 {
195 // Get reader directly into raw bytes of constant table/constant mapping.
196 KernelReaderHelper reader(Z, &H, H.constants_table(), 0);
197 NavigateToIndex(&reader, constant_index);
198
199 // Peek for an instance of the given clazz.
200 if (reader.ReadByte() == kInstanceConstant) {
201 const NameIndex index = reader.ReadCanonicalNameReference();
202 return H.LookupClassByKernelClass(index) == clazz.ptr();
203 }
204 return false;
205}

◆ IsPragmaInstanceConstant()

bool dart::kernel::ConstantReader::IsPragmaInstanceConstant ( intptr_t  constant_index,
intptr_t *  pragma_name_constant_index,
intptr_t *  pragma_options_constant_index 
)

Definition at line 27 of file constant_reader.cc.

30 {
31 KernelReaderHelper reader(Z, &H, H.constants_table(), 0);
32 NavigateToIndex(&reader, constant_index);
33
34 if (reader.ReadByte() == kInstanceConstant) {
35 NameIndex index = reader.ReadCanonicalNameReference();
36 if (H.IsRoot(index) ||
37 !H.StringEquals(H.CanonicalNameString(index), "pragma")) {
38 return false;
39 }
40 index = H.CanonicalNameParent(index);
41 if (H.IsRoot(index) ||
42 !H.StringEquals(H.CanonicalNameString(index), "dart:core")) {
43 return false;
44 }
45 const intptr_t num_type_args = reader.ReadUInt();
46 if (num_type_args != 0) return false;
47
48 const intptr_t num_fields = reader.ReadUInt();
49 if (num_fields != 2) return false;
50
51 const NameIndex field0_name = reader.ReadCanonicalNameReference();
52 if (H.IsRoot(field0_name) ||
53 !H.StringEquals(H.CanonicalNameString(field0_name), "name")) {
54 return false;
55 }
56 const intptr_t name_index = reader.ReadUInt();
57 if (pragma_name_constant_index != nullptr) {
58 *pragma_name_constant_index = name_index;
59 }
60
61 const NameIndex field1_name = reader.ReadCanonicalNameReference();
62 if (H.IsRoot(field1_name) ||
63 !H.StringEquals(H.CanonicalNameString(field1_name), "options")) {
64 return false;
65 }
66 const intptr_t options_index = reader.ReadUInt();
67 if (pragma_options_constant_index != nullptr) {
68 *pragma_options_constant_index = options_index;
69 }
70 return true;
71 }
72 return false;
73}

◆ IsStringConstant()

bool dart::kernel::ConstantReader::IsStringConstant ( intptr_t  constant_index,
const char *  name 
)

Definition at line 75 of file constant_reader.cc.

76 {
77 KernelReaderHelper reader(Z, &H, H.constants_table(), 0);
78 NavigateToIndex(&reader, constant_index);
79
80 if (reader.ReadByte() == kStringConstant) {
81 const StringIndex index = reader.ReadStringReference();
82 return H.StringEquals(index, name);
83 }
84 return false;
85}
const char *const name

◆ NumConstants()

intptr_t dart::kernel::ConstantReader::NumConstants ( )

Definition at line 207 of file constant_reader.cc.

207 {
208 ASSERT(!H.constants_table().IsNull());
209 KernelReaderHelper reader(Z, &H, H.constants_table(), 0);
210 return NumConstants(&reader);
211}
#define ASSERT(E)

◆ ReadAnnotations()

ObjectPtr dart::kernel::ConstantReader::ReadAnnotations ( )

Definition at line 147 of file constant_reader.cc.

147 {
148 intptr_t list_length = helper_->ReadListLength(); // read list length.
149 const auto& metadata_values =
150 Array::Handle(Z, ImmutableArray::New(list_length, H.allocation_space()));
151 Instance& value = Instance::Handle(Z);
152 for (intptr_t i = 0; i < list_length; ++i) {
153 // This will read the expression.
155 metadata_values.SetAt(i, value);
156 }
157 return H.Canonicalize(metadata_values);
158}
static ImmutableArrayPtr New(intptr_t len, Heap::Space space=Heap::kNew)
Definition: object.cc:24985
uint8_t value

◆ ReadConstant()

InstancePtr dart::kernel::ConstantReader::ReadConstant ( intptr_t  constant_index)

Definition at line 160 of file constant_reader.cc.

160 {
161 ASSERT(!H.constants().IsNull());
162 ASSERT(!H.constants_table().IsNull()); // raw bytes
163
164 // For kernel-level cache (in contrast with script-level caching),
165 // we need to access the raw constants array inside the shared
166 // KernelProgramInfo directly, so that all scripts will see the
167 // results after new insertions. These accesses at kernel-level
168 // must be locked since mutator and background compiler can
169 // access the array at the same time.
170 {
171 SafepointMutexLocker ml(
172 H.thread()->isolate_group()->kernel_constants_mutex());
173 const auto& constants_array =
174 Array::Handle(Z, H.GetKernelProgramInfo().constants());
175 ASSERT(constant_index < constants_array.Length());
176 result_ = constants_array.At(constant_index);
177 }
178
179 // On miss, evaluate, and insert value.
180 if (result_.ptr() == Object::sentinel().ptr()) {
181 LeaveCompilerScope cs(H.thread());
182 result_ = ReadConstantInternal(constant_index);
183 SafepointMutexLocker ml(
184 H.thread()->isolate_group()->kernel_constants_mutex());
185 const auto& constants_array =
186 Array::Handle(Z, H.GetKernelProgramInfo().constants());
187 ASSERT(constant_index < constants_array.Length());
188 constants_array.SetAt(constant_index, result_);
189 }
190 return Instance::RawCast(result_.ptr());
191}
ObjectPtr ptr() const
Definition: object.h:332
static ObjectPtr RawCast(ObjectPtr obj)
Definition: object.h:325

◆ ReadConstantExpression()

InstancePtr dart::kernel::ConstantReader::ReadConstantExpression ( )

Definition at line 114 of file constant_reader.cc.

114 {
115 Tag tag = helper_->ReadTag(); // read tag.
116 switch (tag) {
117 case kConstantExpression:
118 helper_->ReadPosition();
119 helper_->SkipDartType();
120 result_ = ReadConstant(helper_->ReadUInt());
121 break;
122 case kFileUriConstantExpression:
123 helper_->ReadPosition();
124 helper_->ReadUInt();
125 helper_->SkipDartType();
126 result_ = ReadConstant(helper_->ReadUInt());
127 break;
128 case kInvalidExpression: {
129 helper_->ReadPosition(); // Skip position.
130 const String& message = H.DartString(helper_->ReadStringReference());
131 const auto& script = Script::Handle(Z, Script());
132 // Invalid expression message has pointer to the source code, no need to
133 // report it twice.
134 H.ReportError(script, TokenPosition::kNoSource, "%s",
135 message.ToCString());
136 break;
137 }
138 default:
139 const auto& script = Script::Handle(Z, Script());
140 H.ReportError(script, TokenPosition::kNoSource,
141 "Not a constant expression: unexpected kernel tag %s (%d)",
142 Reader::TagName(tag), tag);
143 }
144 return Instance::RawCast(result_.ptr());
145}
InstancePtr ReadConstant(intptr_t constant_index)
Tag ReadTag(uint8_t *payload=nullptr)
static const char * TagName(Tag tag)
Win32Message message

◆ ReadConstantInitializer()

InstancePtr dart::kernel::ConstantReader::ReadConstantInitializer ( )

Definition at line 100 of file constant_reader.cc.

100 {
101 Tag tag = helper_->ReadTag(); // read tag.
102 switch (tag) {
103 case kSomething:
104 return ReadConstantExpression();
105 default:
106 const auto& script = Script::Handle(Z, Script());
107 H.ReportError(script, TokenPosition::kNoSource,
108 "Not a constant expression: unexpected kernel tag %s (%d)",
109 Reader::TagName(tag), tag);
110 }
111 return Instance::RawCast(result_.ptr());
112}

The documentation for this class was generated from the following files: