OLD | NEW |
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 #ifndef VM_TOKEN_H_ | 5 #ifndef VM_TOKEN_H_ |
6 #define VM_TOKEN_H_ | 6 #define VM_TOKEN_H_ |
7 | 7 |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
10 | 10 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 KW(kTRUE, "true", 0, kKeyword) \ | 189 KW(kTRUE, "true", 0, kKeyword) \ |
190 KW(kTRY, "try", 0, kKeyword) \ | 190 KW(kTRY, "try", 0, kKeyword) \ |
191 KW(kTYPEDEF, "typedef", 0, kPseudoKeyword) \ | 191 KW(kTYPEDEF, "typedef", 0, kPseudoKeyword) \ |
192 KW(kVAR, "var", 0, kKeyword) \ | 192 KW(kVAR, "var", 0, kKeyword) \ |
193 KW(kVOID, "void", 0, kKeyword) \ | 193 KW(kVOID, "void", 0, kKeyword) \ |
194 KW(kWHILE, "while", 0, kKeyword) \ | 194 KW(kWHILE, "while", 0, kKeyword) \ |
195 KW(kWITH, "with", 0, kKeyword) /* == kLastKeyword */ | 195 KW(kWITH, "with", 0, kKeyword) /* == kLastKeyword */ |
196 | 196 |
197 class String; | 197 class String; |
198 | 198 |
199 // The token space is organized as follows: | |
200 // | |
201 // Sentinel values start at -1 and move towards negative infinity: | |
202 // kNoSourcePos -> -1 | |
203 // ClassifyingTokenPositions 1 -> -1 - 1 | |
204 // ClassifyingTokenPositions N -> -1 - N | |
205 // | |
206 // Synthetically created AstNodes are given real source positions but encoded | |
207 // as negative numbers from [kSmiMin32, -1 - N]. For example: | |
208 // | |
209 // A source position of 0 in a synthetic AstNode would be encoded as -2 - N. | |
210 // A source position of 1 in a synthetic AstNode would be encoded as -3 - N. | |
211 // | |
212 // All other AstNodes are given real source positions encoded as positive | |
213 // integers. | |
214 // | |
215 // This organization allows for ~1 billion token positions. | |
216 // | |
217 // NOTE: While token positions are passed around as an intptr_t they are encoded | |
218 // into the snapshot as an int32_t. | |
219 | |
220 // These token positions are used to classify instructions that can't be | |
221 // directly tied to an actual source position. | |
222 #define CLASSIFYING_TOKEN_POSITIONS(V) \ | |
223 V(Private, -2) \ | |
224 V(Box, -3) \ | |
225 V(ParallelMove, -4) \ | |
226 V(TempMove, -5) \ | |
227 V(Constant, -6) \ | |
228 V(PushArgument, -7) \ | |
229 V(ControlFlow, -8) \ | |
230 V(Context, -9) \ | |
231 V(MethodExtractor, -10) \ | |
232 V(Last, -11) // Always keep this at the end. | |
233 | |
234 | |
235 class ClassifyingTokenPositions : public AllStatic { | |
236 public: | |
237 #define DEFINE_VALUES(name, value) \ | |
238 static const intptr_t k##name = value; | |
239 CLASSIFYING_TOKEN_POSITIONS(DEFINE_VALUES); | |
240 #undef DEFINE_VALUES | |
241 | |
242 static const char* ToCString(intptr_t token_pos); | |
243 }; | |
244 | |
245 | |
246 class Token { | 199 class Token { |
247 public: | 200 public: |
248 #define T(t, s, p, a) t, | 201 #define T(t, s, p, a) t, |
249 enum Kind { | 202 enum Kind { |
250 DART_TOKEN_LIST(T) | 203 DART_TOKEN_LIST(T) |
251 DART_KEYWORD_LIST(T) | 204 DART_KEYWORD_LIST(T) |
252 kNumTokens | 205 kNumTokens |
253 }; | 206 }; |
254 #undef T | 207 #undef T |
255 | 208 |
256 enum Attribute { | 209 enum Attribute { |
257 kNoAttribute = 0, | 210 kNoAttribute = 0, |
258 kKeyword = 1 << 0, | 211 kKeyword = 1 << 0, |
259 kPseudoKeyword = 1 << 1, | 212 kPseudoKeyword = 1 << 1, |
260 }; | 213 }; |
261 | 214 |
262 // Token position constants. | |
263 static const intptr_t kNoSourcePos = -1; | |
264 static const intptr_t kMinSourcePos = 0; | |
265 static const intptr_t kMaxSourcePos = | |
266 kSmiMax32 - (-ClassifyingTokenPositions::kLast) - 2; | |
267 | |
268 // Is |token_pos| a classifying sentinel source position? | |
269 static bool IsClassifying(intptr_t token_pos) { | |
270 return (token_pos >= ClassifyingTokenPositions::kPrivate) && | |
271 (token_pos <= ClassifyingTokenPositions::kLast); | |
272 } | |
273 | |
274 // Is |token_pos| a synthetic source position? | |
275 static bool IsSynthetic(intptr_t token_pos) { | |
276 if (token_pos >= kMinSourcePos) { | |
277 return false; | |
278 } | |
279 if (token_pos < ClassifyingTokenPositions::kLast) { | |
280 return true; | |
281 } | |
282 return false; | |
283 } | |
284 | |
285 // Is |token_pos| the no source position sentinel? | |
286 static bool IsNoSource(intptr_t token_pos) { | |
287 return token_pos == kNoSourcePos; | |
288 } | |
289 | |
290 // Is |token_pos| a real source position? | |
291 static bool IsReal(intptr_t token_pos) { | |
292 return token_pos >= kMinSourcePos; | |
293 } | |
294 | |
295 // Is |token_pos| a source position? | |
296 static bool IsSourcePosition(intptr_t token_pos) { | |
297 return IsReal(token_pos) || IsNoSource(token_pos) || IsSynthetic(token_pos); | |
298 } | |
299 | |
300 // Is |token_pos| a debug pause source position? | |
301 static bool IsDebugPause(intptr_t token_pos) { | |
302 return IsReal(token_pos); | |
303 } | |
304 | |
305 // Encode |token_pos| into a synthetic source position. | |
306 static intptr_t ToSynthetic(intptr_t token_pos) { | |
307 if (IsClassifying(token_pos) || IsNoSource(token_pos)) { | |
308 return token_pos; | |
309 } | |
310 if (IsSynthetic(token_pos)) { | |
311 return token_pos; | |
312 } | |
313 ASSERT(!IsSynthetic(token_pos)); | |
314 const intptr_t value = (ClassifyingTokenPositions::kLast - 1) - token_pos; | |
315 ASSERT(IsSynthetic(value)); | |
316 ASSERT(value < ClassifyingTokenPositions::kLast); | |
317 return value; | |
318 } | |
319 | |
320 // Decode |token_pos| from a synthetic source position. | |
321 static intptr_t FromSynthetic(intptr_t token_pos) { | |
322 if (IsClassifying(token_pos) || IsNoSource(token_pos)) { | |
323 return token_pos; | |
324 } | |
325 if (!IsSynthetic(token_pos)) { | |
326 return token_pos; | |
327 } | |
328 ASSERT(IsSynthetic(token_pos)); | |
329 const intptr_t value = -token_pos + (ClassifyingTokenPositions::kLast - 1); | |
330 ASSERT(!IsSynthetic(value)); | |
331 return value; | |
332 } | |
333 | |
334 static const Kind kFirstKeyword = kABSTRACT; | 215 static const Kind kFirstKeyword = kABSTRACT; |
335 static const Kind kLastKeyword = kWITH; | 216 static const Kind kLastKeyword = kWITH; |
336 static const int kNumKeywords = kLastKeyword - kFirstKeyword + 1; | 217 static const int kNumKeywords = kLastKeyword - kFirstKeyword + 1; |
337 | 218 |
338 static bool IsAssignmentOperator(Kind tok) { | 219 static bool IsAssignmentOperator(Kind tok) { |
339 return kASSIGN <= tok && tok <= kASSIGN_COND; | 220 return kASSIGN <= tok && tok <= kASSIGN_COND; |
340 } | 221 } |
341 | 222 |
342 static bool IsRelationalOperator(Kind tok) { | 223 static bool IsRelationalOperator(Kind tok) { |
343 return kLT <= tok && tok <= kGTE; | 224 return kLT <= tok && tok <= kGTE; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 private: | 324 private: |
444 static const char* name_[]; | 325 static const char* name_[]; |
445 static const char* tok_str_[]; | 326 static const char* tok_str_[]; |
446 static const uint8_t precedence_[]; | 327 static const uint8_t precedence_[]; |
447 static const Attribute attributes_[]; | 328 static const Attribute attributes_[]; |
448 }; | 329 }; |
449 | 330 |
450 } // namespace dart | 331 } // namespace dart |
451 | 332 |
452 #endif // VM_TOKEN_H_ | 333 #endif // VM_TOKEN_H_ |
OLD | NEW |