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 | 10 |
10 namespace dart { | 11 namespace dart { |
11 | 12 |
12 // Operator precedence table | 13 // Operator precedence table |
13 // | 14 // |
14 // 14 multiplicative * / ~/ % | 15 // 14 multiplicative * / ~/ % |
15 // 13 additive + - | 16 // 13 additive + - |
16 // 12 shift << >> | 17 // 12 shift << >> |
17 // 11 bitwise and & | 18 // 11 bitwise and & |
18 // 10 bitwise xor ^ | 19 // 10 bitwise xor ^ |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 KW(kTHIS, "this", 0, kKeyword) \ | 187 KW(kTHIS, "this", 0, kKeyword) \ |
187 KW(kTHROW, "throw", 0, kKeyword) \ | 188 KW(kTHROW, "throw", 0, kKeyword) \ |
188 KW(kTRUE, "true", 0, kKeyword) \ | 189 KW(kTRUE, "true", 0, kKeyword) \ |
189 KW(kTRY, "try", 0, kKeyword) \ | 190 KW(kTRY, "try", 0, kKeyword) \ |
190 KW(kTYPEDEF, "typedef", 0, kPseudoKeyword) \ | 191 KW(kTYPEDEF, "typedef", 0, kPseudoKeyword) \ |
191 KW(kVAR, "var", 0, kKeyword) \ | 192 KW(kVAR, "var", 0, kKeyword) \ |
192 KW(kVOID, "void", 0, kKeyword) \ | 193 KW(kVOID, "void", 0, kKeyword) \ |
193 KW(kWHILE, "while", 0, kKeyword) \ | 194 KW(kWHILE, "while", 0, kKeyword) \ |
194 KW(kWITH, "with", 0, kKeyword) /* == kLastKeyword */ | 195 KW(kWITH, "with", 0, kKeyword) /* == kLastKeyword */ |
195 | 196 |
| 197 class String; |
196 | 198 |
197 class String; | 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 |
198 | 245 |
199 class Token { | 246 class Token { |
200 public: | 247 public: |
201 #define T(t, s, p, a) t, | 248 #define T(t, s, p, a) t, |
202 enum Kind { | 249 enum Kind { |
203 DART_TOKEN_LIST(T) | 250 DART_TOKEN_LIST(T) |
204 DART_KEYWORD_LIST(T) | 251 DART_KEYWORD_LIST(T) |
205 kNumTokens | 252 kNumTokens |
206 }; | 253 }; |
207 #undef T | 254 #undef T |
208 | 255 |
209 enum Attribute { | 256 enum Attribute { |
210 kNoAttribute = 0, | 257 kNoAttribute = 0, |
211 kKeyword = 1 << 0, | 258 kKeyword = 1 << 0, |
212 kPseudoKeyword = 1 << 1, | 259 kPseudoKeyword = 1 << 1, |
213 }; | 260 }; |
214 | 261 |
| 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 |
215 static const Kind kFirstKeyword = kABSTRACT; | 334 static const Kind kFirstKeyword = kABSTRACT; |
216 static const Kind kLastKeyword = kWITH; | 335 static const Kind kLastKeyword = kWITH; |
217 static const int kNumKeywords = kLastKeyword - kFirstKeyword + 1; | 336 static const int kNumKeywords = kLastKeyword - kFirstKeyword + 1; |
218 | 337 |
219 static bool IsAssignmentOperator(Kind tok) { | 338 static bool IsAssignmentOperator(Kind tok) { |
220 return kASSIGN <= tok && tok <= kASSIGN_COND; | 339 return kASSIGN <= tok && tok <= kASSIGN_COND; |
221 } | 340 } |
222 | 341 |
223 static bool IsRelationalOperator(Kind tok) { | 342 static bool IsRelationalOperator(Kind tok) { |
224 return kLT <= tok && tok <= kGTE; | 343 return kLT <= tok && tok <= kGTE; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 } | 440 } |
322 } | 441 } |
323 | 442 |
324 private: | 443 private: |
325 static const char* name_[]; | 444 static const char* name_[]; |
326 static const char* tok_str_[]; | 445 static const char* tok_str_[]; |
327 static const uint8_t precedence_[]; | 446 static const uint8_t precedence_[]; |
328 static const Attribute attributes_[]; | 447 static const Attribute attributes_[]; |
329 }; | 448 }; |
330 | 449 |
331 | |
332 } // namespace dart | 450 } // namespace dart |
333 | 451 |
334 #endif // VM_TOKEN_H_ | 452 #endif // VM_TOKEN_H_ |
OLD | NEW |