| 49 | typedef Map<FixStr, Str *> InternMap; |
| 50 | |
| 51 | Str *Intern(const char *ptr, Int len) { |
| 52 | static InternMap *map = NULL; |
| 53 | if (!map) { |
| 54 | GCVar(map, new InternMap()); |
| 55 | } |
| 56 | FixStr fs; |
| 57 | fs.str = ptr; |
| 58 | fs.len = len; |
| 59 | Str *result = map->get(fs, NULL); |
| 60 | if (!result) { |
| 61 | result = new Str(ptr, len); |
| 62 | // `ptr` above is an interior pointer whose contents may disappear |
| 63 | // due to GC once the `SourceFile` object containing it is no |
| 64 | // longer reachable. |
| 65 | // |
| 66 | // Therefore, we replace it with `result->c_str()`. This is not only |
| 67 | // not an interior pointer, but is kept alive by the value that the |
| 68 | // key is in use for. |
| 69 | fs.str = result->c_str(); |
| 70 | map->add(fs, result); |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | #define PUSH_TOKEN(s) \ |
| 76 | token.sym = s; \ |