xref: /netbsd-src/external/gpl3/gcc.old/dist/libcpp/include/line-map.h (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2    Copyright (C) 2001-2017 Free Software Foundation, Inc.
3 
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3.  If not see
16 <http://www.gnu.org/licenses/>.
17 
18  In other words, you are welcome to use, share and improve this program.
19  You are forbidden to forbid anyone else to use, share and improve
20  what you give them.   Help stamp out software-hoarding!  */
21 
22 #ifndef LIBCPP_LINE_MAP_H
23 #define LIBCPP_LINE_MAP_H
24 
25 #ifndef GTY
26 #define GTY(x) /* nothing */
27 #endif
28 
29 /* Reason for creating a new line map with linemap_add.  LC_ENTER is
30    when including a new file, e.g. a #include directive in C.
31    LC_LEAVE is when reaching a file's end.  LC_RENAME is when a file
32    name or line number changes for neither of the above reasons
33    (e.g. a #line directive in C); LC_RENAME_VERBATIM is like LC_RENAME
34    but a filename of "" is not specially interpreted as standard
35    input. LC_ENTER_MACRO is when a macro expansion is about to start.  */
36 enum lc_reason
37 {
38   LC_ENTER = 0,
39   LC_LEAVE,
40   LC_RENAME,
41   LC_RENAME_VERBATIM,
42   LC_ENTER_MACRO
43   /* FIXME: add support for stringize and paste.  */
44 };
45 
46 /* The type of line numbers.  */
47 typedef unsigned int linenum_type;
48 
49 /* The typedef "source_location" is a key within the location database,
50    identifying a source location or macro expansion, along with range
51    information, and (optionally) a pointer for use by gcc.
52 
53    This key only has meaning in relation to a line_maps instance.  Within
54    gcc there is a single line_maps instance: "line_table", declared in
55    gcc/input.h and defined in gcc/input.c.
56 
57    The values of the keys are intended to be internal to libcpp,
58    but for ease-of-understanding the implementation, they are currently
59    assigned as follows:
60 
61   Actual     | Value                         | Meaning
62   -----------+-------------------------------+-------------------------------
63   0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
64   -----------+-------------------------------+-------------------------------
65   0x00000001 | BUILTINS_LOCATION             | The location for declarations
66              |   (gcc/input.h)               | in "<built-in>"
67   -----------+-------------------------------+-------------------------------
68   0x00000002 | RESERVED_LOCATION_COUNT       | The first location to be
69              | (also                         | handed out, and the
70              |  ordmap[0]->start_location)   | first line in ordmap 0
71   -----------+-------------------------------+-------------------------------
72              | ordmap[1]->start_location     | First line in ordmap 1
73              | ordmap[1]->start_location+32  | First column in that line
74              |   (assuming range_bits == 5)  |
75              | ordmap[1]->start_location+64  | 2nd column in that line
76              | ordmap[1]->start_location+4096| Second line in ordmap 1
77              |   (assuming column_bits == 12)
78              |
79              |   Subsequent lines are offset by (1 << column_bits),
80              |   e.g. 4096 for 12 bits, with a column value of 0 representing
81              |   "the whole line".
82              |
83              |   Within a line, the low "range_bits" (typically 5) are used for
84              |   storing short ranges, so that there's an offset of
85              |     (1 << range_bits) between individual columns within a line,
86              |   typically 32.
87              |   The low range_bits store the offset of the end point from the
88              |   start point, and the start point is found by masking away
89              |   the range bits.
90              |
91              |   For example:
92              |      ordmap[1]->start_location+64    "2nd column in that line"
93              |   above means a caret at that location, with a range
94              |   starting and finishing at the same place (the range bits
95              |   are 0), a range of length 1.
96              |
97              |   By contrast:
98              |      ordmap[1]->start_location+68
99              |   has range bits 0x4, meaning a caret with a range starting at
100              |   that location, but with endpoint 4 columns further on: a range
101              |   of length 5.
102              |
103              |   Ranges that have caret != start, or have an endpoint too
104              |   far away to fit in range_bits are instead stored as ad-hoc
105              |   locations.  Hence for range_bits == 5 we can compactly store
106              |   tokens of length <= 32 without needing to use the ad-hoc
107              |   table.
108              |
109              |   This packing scheme means we effectively have
110              |     (column_bits - range_bits)
111              |   of bits for the columns, typically (12 - 5) = 7, for 128
112              |   columns; longer line widths are accomodated by starting a
113              |   new ordmap with a higher column_bits.
114              |
115              | ordmap[2]->start_location-1   | Final location in ordmap 1
116   -----------+-------------------------------+-------------------------------
117              | ordmap[2]->start_location     | First line in ordmap 2
118              | ordmap[3]->start_location-1   | Final location in ordmap 2
119   -----------+-------------------------------+-------------------------------
120              |                               | (etc)
121   -----------+-------------------------------+-------------------------------
122              | ordmap[n-1]->start_location   | First line in final ord map
123              |                               | (etc)
124              | set->highest_location - 1     | Final location in that ordmap
125   -----------+-------------------------------+-------------------------------
126              | set->highest_location         | Location of the where the next
127              |                               | ordinary linemap would start
128   -----------+-------------------------------+-------------------------------
129              |                               |
130              |                  VVVVVVVVVVVVVVVVVVVVVVVVVVV
131              |                  Ordinary maps grow this way
132              |
133              |                    (unallocated integers)
134              |
135   0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
136              |   Beyond this point, ordinary linemaps have 0 bits per column:
137              |   each increment of the value corresponds to a new source line.
138              |
139   0x70000000 | LINE_MAP_MAX_SOURCE_LOCATION
140              |   Beyond the point, we give up on ordinary maps; attempts to
141              |   create locations in them lead to UNKNOWN_LOCATION (0).
142              |
143              |                    (unallocated integers)
144              |
145              |                   Macro maps grow this way
146              |                   ^^^^^^^^^^^^^^^^^^^^^^^^
147              |                               |
148   -----------+-------------------------------+-------------------------------
149              | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
150              | macromap[m-1]->start_location | Start of last macro map
151              |                               |
152   -----------+-------------------------------+-------------------------------
153              | macromap[m-2]->start_location | Start of penultimate macro map
154   -----------+-------------------------------+-------------------------------
155              | macromap[1]->start_location   | Start of macro map 1
156   -----------+-------------------------------+-------------------------------
157              | macromap[0]->start_location   | Start of macro map 0
158   0x7fffffff | MAX_SOURCE_LOCATION           | Also used as a mask for
159              |                               | accessing the ad-hoc data table
160   -----------+-------------------------------+-------------------------------
161   0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
162   ...        | into the line_table->location_adhoc_data_map.data array.
163   0xffffffff | UINT_MAX                      |
164   -----------+-------------------------------+-------------------------------
165 
166    Examples of location encoding.
167 
168    Packed ranges
169    =============
170 
171    Consider encoding the location of a token "foo", seen underlined here
172    on line 523, within an ordinary line_map that starts at line 500:
173 
174                  11111111112
175         12345678901234567890
176      522
177      523   return foo + bar;
178                   ^~~
179      524
180 
181    The location's caret and start are both at line 523, column 11; the
182    location's finish is on the same line, at column 13 (an offset of 2
183    columns, for length 3).
184 
185    Line 523 is offset 23 from the starting line of the ordinary line_map.
186 
187    caret == start, and the offset of the finish fits within 5 bits, so
188    this can be stored as a packed range.
189 
190    This is encoded as:
191       ordmap->start
192          + (line_offset << ordmap->m_column_and_range_bits)
193          + (column << ordmap->m_range_bits)
194          + (range_offset);
195    i.e. (for line offset 23, column 11, range offset 2):
196       ordmap->start
197          + (23 << 12)
198          + (11 << 5)
199          + 2;
200    i.e.:
201       ordmap->start + 0x17162
202    assuming that the line_map uses the default of 7 bits for columns and
203    5 bits for packed range (giving 12 bits for m_column_and_range_bits).
204 
205 
206    "Pure" locations
207    ================
208 
209    These are a special case of the above, where
210       caret == start == finish
211    They are stored as packed ranges with offset == 0.
212    For example, the location of the "f" of "foo" could be stored
213    as above, but with range offset 0, giving:
214       ordmap->start
215          + (23 << 12)
216          + (11 << 5)
217          + 0;
218    i.e.:
219       ordmap->start + 0x17160
220 
221 
222    Unoptimized ranges
223    ==================
224 
225    Consider encoding the location of the binary expression
226    below:
227 
228                  11111111112
229         12345678901234567890
230      522
231      523   return foo + bar;
232                   ~~~~^~~~~
233      524
234 
235    The location's caret is at the "+", line 523 column 15, but starts
236    earlier, at the "f" of "foo" at column 11.  The finish is at the "r"
237    of "bar" at column 19.
238 
239    This can't be stored as a packed range since start != caret.
240    Hence it is stored as an ad-hoc location e.g. 0x80000003.
241 
242    Stripping off the top bit gives us an index into the ad-hoc
243    lookaside table:
244 
245      line_table->location_adhoc_data_map.data[0x3]
246 
247    from which the caret, start and finish can be looked up,
248    encoded as "pure" locations:
249 
250      start  == ordmap->start + (23 << 12) + (11 << 5)
251             == ordmap->start + 0x17160  (as above; the "f" of "foo")
252 
253      caret  == ordmap->start + (23 << 12) + (15 << 5)
254             == ordmap->start + 0x171e0
255 
256      finish == ordmap->start + (23 << 12) + (19 << 5)
257             == ordmap->start + 0x17260
258 
259    To further see how source_location works in practice, see the
260    worked example in libcpp/location-example.txt.  */
261 typedef unsigned int source_location;
262 
263 /* Do not pack ranges if locations get higher than this.
264    If you change this, update:
265      gcc.dg/plugin/location-overflow-test-*.c.  */
266 const source_location LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
267 
268 /* Do not track column numbers if locations get higher than this.
269    If you change this, update:
270      gcc.dg/plugin/location-overflow-test-*.c.  */
271 const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
272 
273 /* A range of source locations.
274 
275    Ranges are closed:
276    m_start is the first location within the range,
277    m_finish is the last location within the range.
278 
279    We may need a more compact way to store these, but for now,
280    let's do it the simple way, as a pair.  */
281 struct GTY(()) source_range
282 {
283   source_location m_start;
284   source_location m_finish;
285 
286   /* We avoid using constructors, since various structs that
287      don't yet have constructors will embed instances of
288      source_range.  */
289 
290   /* Make a source_range from a source_location.  */
291   static source_range from_location (source_location loc)
292   {
293     source_range result;
294     result.m_start = loc;
295     result.m_finish = loc;
296     return result;
297   }
298 
299   /* Make a source_range from a pair of source_location.  */
300   static source_range from_locations (source_location start,
301 				      source_location finish)
302   {
303     source_range result;
304     result.m_start = start;
305     result.m_finish = finish;
306     return result;
307   }
308 
309   /* Is there any part of this range on the given line?  */
310   bool intersects_line_p (const char *file, int line) const;
311 };
312 
313 /* Memory allocation function typedef.  Works like xrealloc.  */
314 typedef void *(*line_map_realloc) (void *, size_t);
315 
316 /* Memory allocator function that returns the actual allocated size,
317    for a given requested allocation.  */
318 typedef size_t (*line_map_round_alloc_size_func) (size_t);
319 
320 /* A line_map encodes a sequence of locations.
321    There are two kinds of maps. Ordinary maps and macro expansion
322    maps, a.k.a macro maps.
323 
324    A macro map encodes source locations of tokens that are part of a
325    macro replacement-list, at a macro expansion point. E.g, in:
326 
327             #define PLUS(A,B) A + B
328 
329    No macro map is going to be created there, because we are not at a
330    macro expansion point. We are at a macro /definition/ point. So the
331    locations of the tokens of the macro replacement-list (i.e, A + B)
332    will be locations in an ordinary map, not a macro map.
333 
334    On the other hand, if we later do:
335 
336         int a = PLUS (1,2);
337 
338    The invocation of PLUS here is a macro expansion. So we are at a
339    macro expansion point. The preprocessor expands PLUS (1,2) and
340    replaces it with the tokens of its replacement-list: 1 + 2. A macro
341    map is going to be created to hold (or rather to map, haha ...) the
342    locations of the tokens 1, + and 2. The macro map also records the
343    location of the expansion point of PLUS. That location is mapped in
344    the map that is active right before the location of the invocation
345    of PLUS.  */
346 struct GTY((tag ("0"), desc ("%h.reason == LC_ENTER_MACRO ? 2 : 1"))) line_map {
347   source_location start_location;
348 
349   /* The reason for creation of this line map.  */
350   ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
351 };
352 
353 /* An ordinary line map encodes physical source locations. Those
354    physical source locations are called "spelling locations".
355 
356    Physical source file TO_FILE at line TO_LINE at column 0 is represented
357    by the logical START_LOCATION.  TO_LINE+L at column C is represented by
358    START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as
359    long as C<(1<<effective range bits), and the result_location is less than
360    the next line_map's start_location.
361    (The top line is line 1 and the leftmost column is column 1; line/column 0
362    means "entire file/line" or "unknown line/column" or "not applicable".)
363 
364    The highest possible source location is MAX_SOURCE_LOCATION.  */
365 struct GTY((tag ("1"))) line_map_ordinary : public line_map {
366   const char *to_file;
367   linenum_type to_line;
368 
369   /* An index into the set that gives the line mapping at whose end
370      the current one was included.  File(s) at the bottom of the
371      include stack have this set to -1.  */
372   int included_from;
373 
374   /* SYSP is one for a system header, two for a C system header file
375      that therefore needs to be extern "C" protected in C++, and zero
376      otherwise.  This field isn't really needed now that it's in
377      cpp_buffer.  */
378   unsigned char sysp;
379 
380   /* Number of the low-order source_location bits used for column numbers
381      and ranges.  */
382   unsigned int m_column_and_range_bits : 8;
383 
384   /* Number of the low-order "column" bits used for storing short ranges
385      inline, rather than in the ad-hoc table.
386      MSB                                                                 LSB
387      31                                                                    0
388      +-------------------------+-------------------------------------------+
389      |                         |<---map->column_and_range_bits (e.g. 12)-->|
390      +-------------------------+-----------------------+-------------------+
391      |                         | column_and_range_bits | map->range_bits   |
392      |                         |   - range_bits        |                   |
393      +-------------------------+-----------------------+-------------------+
394      | row bits                | effective column bits | short range bits  |
395      |                         |    (e.g. 7)           |   (e.g. 5)        |
396      +-------------------------+-----------------------+-------------------+ */
397   unsigned int m_range_bits : 8;
398 };
399 
400 /* This is the highest possible source location encoded within an
401    ordinary or macro map.  */
402 const source_location MAX_SOURCE_LOCATION = 0x7FFFFFFF;
403 
404 struct cpp_hashnode;
405 
406 /* A macro line map encodes location of tokens coming from a macro
407    expansion.
408 
409    The offset from START_LOCATION is used to index into
410    MACRO_LOCATIONS; this holds the original location of the token.  */
411 struct GTY((tag ("2"))) line_map_macro : public line_map {
412   /* The cpp macro which expansion gave birth to this macro map.  */
413   struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
414 				   "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
415 				   "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
416     macro;
417 
418   /* The number of tokens inside the replacement-list of MACRO.  */
419   unsigned int n_tokens;
420 
421   /* This array of location is actually an array of pairs of
422      locations. The elements inside it thus look like:
423 
424            x0,y0, x1,y1, x2,y2, ...., xn,yn.
425 
426      where n == n_tokens;
427 
428      Remember that these xI,yI are collected when libcpp is about to
429      expand a given macro.
430 
431      yI is the location in the macro definition, either of the token
432      itself or of a macro parameter that it replaces.
433 
434      Imagine this:
435 
436 	#define PLUS(A, B) A + B  <--- #1
437 
438 	int a = PLUS (1,2); <--- #2
439 
440      There is a macro map for the expansion of PLUS in #2.  PLUS is
441      expanded into its expansion-list.  The expansion-list is the
442      replacement-list of PLUS where the macro parameters are replaced
443      with their arguments.  So the replacement-list of PLUS is made of
444      the tokens:
445 
446         A, +, B
447 
448      and the expansion-list is made of the tokens:
449 
450         1, +, 2
451 
452      Let's consider the case of token "+".  Its y1 [yI for I == 1] is
453      its spelling location in #1.
454 
455      y0 (thus for token "1") is the spelling location of A in #1.
456 
457      And y2 (of token "2") is the spelling location of B in #1.
458 
459      When the token is /not/ an argument for a macro, xI is the same
460      location as yI.  Otherwise, xI is the location of the token
461      outside this macro expansion.  If this macro was expanded from
462      another macro expansion, xI is a virtual location representing
463      the token in that macro expansion; otherwise, it is the spelling
464      location of the token.
465 
466      Note that a virtual location is a location returned by
467      linemap_add_macro_token.  It encodes the relevant locations (x,y
468      pairs) of that token across the macro expansions from which it
469      (the token) might come from.
470 
471      In the example above x1 (for token "+") is going to be the same
472      as y1.  x0 is the spelling location for the argument token "1",
473      and x2 is the spelling location for the argument token "2".  */
474   source_location * GTY((atomic)) macro_locations;
475 
476   /* This is the location of the expansion point of the current macro
477      map.  It's the location of the macro name.  That location is held
478      by the map that was current right before the current one. It
479      could have been either a macro or an ordinary map, depending on
480      if we are in a nested expansion context not.  */
481   source_location expansion;
482 };
483 
484 #if CHECKING_P && (GCC_VERSION >= 2007)
485 
486 /* Assertion macro to be used in line-map code.  */
487 #define linemap_assert(EXPR)                  \
488   do {                                                \
489     if (! (EXPR))                             \
490       abort ();                                       \
491   } while (0)
492 
493 /* Assert that becomes a conditional expression when checking is disabled at
494    compilation time.  Use this for conditions that should not happen but if
495    they happen, it is better to handle them gracefully rather than crash
496    randomly later.
497    Usage:
498 
499    if (linemap_assert_fails(EXPR)) handle_error(); */
500 #define linemap_assert_fails(EXPR) __extension__ \
501   ({linemap_assert (EXPR); false;})
502 
503 #else
504 /* Include EXPR, so that unused variable warnings do not occur.  */
505 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
506 #define linemap_assert_fails(EXPR) (! (EXPR))
507 #endif
508 
509 /* Return TRUE if MAP encodes locations coming from a macro
510    replacement-list at macro expansion point.  */
511 bool
512 linemap_macro_expansion_map_p (const struct line_map *);
513 
514 /* Assert that MAP encodes locations of tokens that are not part of
515    the replacement-list of a macro expansion, downcasting from
516    line_map * to line_map_ordinary *.  */
517 
518 inline line_map_ordinary *
519 linemap_check_ordinary (struct line_map *map)
520 {
521   linemap_assert (!linemap_macro_expansion_map_p (map));
522   return (line_map_ordinary *)map;
523 }
524 
525 /* Assert that MAP encodes locations of tokens that are not part of
526    the replacement-list of a macro expansion, downcasting from
527    const line_map * to const line_map_ordinary *.  */
528 
529 inline const line_map_ordinary *
530 linemap_check_ordinary (const struct line_map *map)
531 {
532   linemap_assert (!linemap_macro_expansion_map_p (map));
533   return (const line_map_ordinary *)map;
534 }
535 
536 /* Assert that MAP is a macro expansion and downcast to the appropriate
537    subclass.  */
538 
539 inline line_map_macro *linemap_check_macro (line_map *map)
540 {
541   linemap_assert (linemap_macro_expansion_map_p (map));
542   return (line_map_macro *)map;
543 }
544 
545 /* Assert that MAP is a macro expansion and downcast to the appropriate
546    subclass.  */
547 
548 inline const line_map_macro *
549 linemap_check_macro (const line_map *map)
550 {
551   linemap_assert (linemap_macro_expansion_map_p (map));
552   return (const line_map_macro *)map;
553 }
554 
555 /* Read the start location of MAP.  */
556 
557 inline source_location
558 MAP_START_LOCATION (const line_map *map)
559 {
560   return map->start_location;
561 }
562 
563 /* Get the starting line number of ordinary map MAP.  */
564 
565 inline linenum_type
566 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
567 {
568   return ord_map->to_line;
569 }
570 
571 /* Get the index of the ordinary map at whose end
572    ordinary map MAP was included.
573 
574    File(s) at the bottom of the include stack have this set.  */
575 
576 inline int
577 ORDINARY_MAP_INCLUDER_FILE_INDEX (const line_map_ordinary *ord_map)
578 {
579   return ord_map->included_from;
580 }
581 
582 /* Return a positive value if map encodes locations from a system
583    header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
584    in a system header and 2 if it encodes locations in a C system header
585    that therefore needs to be extern "C" protected in C++.  */
586 
587 inline unsigned char
588 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
589 {
590   return ord_map->sysp;
591 }
592 
593 /* Get the filename of ordinary map MAP.  */
594 
595 inline const char *
596 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
597 {
598   return ord_map->to_file;
599 }
600 
601 /* Get the cpp macro whose expansion gave birth to macro map MAP.  */
602 
603 inline cpp_hashnode *
604 MACRO_MAP_MACRO (const line_map_macro *macro_map)
605 {
606   return macro_map->macro;
607 }
608 
609 /* Get the number of tokens inside the replacement-list of the macro
610    that led to macro map MAP.  */
611 
612 inline unsigned int
613 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
614 {
615   return macro_map->n_tokens;
616 }
617 
618 /* Get the array of pairs of locations within macro map MAP.
619    See the declaration of line_map_macro for more information.  */
620 
621 inline source_location *
622 MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
623 {
624   return macro_map->macro_locations;
625 }
626 
627 /* Get the location of the expansion point of the macro map MAP.  */
628 
629 inline source_location
630 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
631 {
632   return macro_map->expansion;
633 }
634 
635 /* The abstraction of a set of location maps. There can be several
636    types of location maps. This abstraction contains the attributes
637    that are independent from the type of the map.
638 
639    Essentially this is just a vector of T_linemap_subclass,
640    which can only ever grow in size.  */
641 
642 struct GTY(()) maps_info_ordinary {
643   /* This array contains the "ordinary" line maps, for all
644      events other than macro expansion
645      (e.g. when a new preprocessing unit starts or ends).  */
646   line_map_ordinary * GTY ((length ("%h.used"))) maps;
647 
648   /* The total number of allocated maps.  */
649   unsigned int allocated;
650 
651   /* The number of elements used in maps. This number is smaller
652      or equal to ALLOCATED.  */
653   unsigned int used;
654 
655   unsigned int cache;
656 };
657 
658 struct GTY(()) maps_info_macro {
659   /* This array contains the macro line maps.
660      A macro line map is created whenever a macro expansion occurs.  */
661   line_map_macro * GTY ((length ("%h.used"))) maps;
662 
663   /* The total number of allocated maps.  */
664   unsigned int allocated;
665 
666   /* The number of elements used in maps. This number is smaller
667      or equal to ALLOCATED.  */
668   unsigned int used;
669 
670   unsigned int cache;
671 };
672 
673 /* Data structure to associate a source_range together with an arbitrary
674    data pointer with a source location.  */
675 struct GTY(()) location_adhoc_data {
676   source_location locus;
677   source_range src_range;
678   void * GTY((skip)) data;
679 };
680 
681 struct htab;
682 
683 /* The following data structure encodes a location with some adhoc data
684    and maps it to a new unsigned integer (called an adhoc location)
685    that replaces the original location to represent the mapping.
686 
687    The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
688    highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
689    the original location. Once identified as the adhoc_loc, the lower 31
690    bits of the integer is used to index the location_adhoc_data array,
691    in which the locus and associated data is stored.  */
692 
693 struct GTY(()) location_adhoc_data_map {
694   struct htab * GTY((skip)) htab;
695   source_location curr_loc;
696   unsigned int allocated;
697   struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
698 };
699 
700 /* A set of chronological line_map structures.  */
701 struct GTY(()) line_maps {
702 
703   ~line_maps ();
704 
705   maps_info_ordinary info_ordinary;
706 
707   maps_info_macro info_macro;
708 
709   /* Depth of the include stack, including the current file.  */
710   unsigned int depth;
711 
712   /* If true, prints an include trace a la -H.  */
713   bool trace_includes;
714 
715   /* Highest source_location "given out".  */
716   source_location highest_location;
717 
718   /* Start of line of highest source_location "given out".  */
719   source_location highest_line;
720 
721   /* The maximum column number we can quickly allocate.  Higher numbers
722      may require allocating a new line_map.  */
723   unsigned int max_column_hint;
724 
725   /* If non-null, the allocator to use when resizing 'maps'.  If null,
726      xrealloc is used.  */
727   line_map_realloc reallocator;
728 
729   /* The allocators' function used to know the actual size it
730      allocated, for a certain allocation size requested.  */
731   line_map_round_alloc_size_func round_alloc_size;
732 
733   struct location_adhoc_data_map location_adhoc_data_map;
734 
735   /* The special location value that is used as spelling location for
736      built-in tokens.  */
737   source_location builtin_location;
738 
739   /* True if we've seen a #line or # 44 "file" directive.  */
740   bool seen_line_directive;
741 
742   /* The default value of range_bits in ordinary line maps.  */
743   unsigned int default_range_bits;
744 
745   unsigned int num_optimized_ranges;
746   unsigned int num_unoptimized_ranges;
747 };
748 
749 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
750    if we are interested in macro maps, FALSE otherwise.  */
751 inline unsigned int
752 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
753 {
754   if (map_kind)
755     return set->info_macro.allocated;
756   else
757     return set->info_ordinary.allocated;
758 }
759 
760 /* As above, but by reference (e.g. as an lvalue).  */
761 
762 inline unsigned int &
763 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
764 {
765   if (map_kind)
766     return set->info_macro.allocated;
767   else
768     return set->info_ordinary.allocated;
769 }
770 
771 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
772    we are interested in macro maps, FALSE otherwise.*/
773 inline unsigned int
774 LINEMAPS_USED (const line_maps *set, bool map_kind)
775 {
776   if (map_kind)
777     return set->info_macro.used;
778   else
779     return set->info_ordinary.used;
780 }
781 
782 /* As above, but by reference (e.g. as an lvalue).  */
783 
784 inline unsigned int &
785 LINEMAPS_USED (line_maps *set, bool map_kind)
786 {
787   if (map_kind)
788     return set->info_macro.used;
789   else
790     return set->info_ordinary.used;
791 }
792 
793 /* Returns the index of the last map that was looked up with
794    linemap_lookup. MAP_KIND shall be TRUE if we are interested in
795    macro maps, FALSE otherwise.  */
796 inline unsigned int
797 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
798 {
799   if (map_kind)
800     return set->info_macro.cache;
801   else
802     return set->info_ordinary.cache;
803 }
804 
805 /* As above, but by reference (e.g. as an lvalue).  */
806 
807 inline unsigned int &
808 LINEMAPS_CACHE (line_maps *set, bool map_kind)
809 {
810   if (map_kind)
811     return set->info_macro.cache;
812   else
813     return set->info_ordinary.cache;
814 }
815 
816 /* Return the map at a given index.  */
817 inline line_map *
818 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
819 {
820   if (map_kind)
821     return &set->info_macro.maps[index];
822   else
823     return &set->info_ordinary.maps[index];
824 }
825 
826 /* Returns the last map used in the line table SET. MAP_KIND
827    shall be TRUE if we are interested in macro maps, FALSE
828    otherwise.*/
829 inline line_map *
830 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
831 {
832   return LINEMAPS_MAP_AT (set, map_kind,
833 			  LINEMAPS_USED (set, map_kind) - 1);
834 }
835 
836 /* Returns the last map that was allocated in the line table SET.
837    MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
838    otherwise.*/
839 inline line_map *
840 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
841 {
842   return LINEMAPS_MAP_AT (set, map_kind,
843 			  LINEMAPS_ALLOCATED (set, map_kind) - 1);
844 }
845 
846 /* Returns a pointer to the memory region where ordinary maps are
847    allocated in the line table SET.  */
848 inline line_map_ordinary *
849 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
850 {
851   return set->info_ordinary.maps;
852 }
853 
854 /* Returns the INDEXth ordinary map.  */
855 inline line_map_ordinary *
856 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
857 {
858   linemap_assert (index >= 0);
859   linemap_assert ((unsigned int)index < set->info_ordinary.used);
860   return &set->info_ordinary.maps[index];
861 }
862 
863 /* Return the number of ordinary maps allocated in the line table
864    SET.  */
865 inline unsigned int
866 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
867 {
868   return LINEMAPS_ALLOCATED (set, false);
869 }
870 
871 /* Return the number of ordinary maps used in the line table SET.  */
872 inline unsigned int
873 LINEMAPS_ORDINARY_USED (const line_maps *set)
874 {
875   return LINEMAPS_USED (set, false);
876 }
877 
878 /* Return the index of the last ordinary map that was looked up with
879    linemap_lookup.  */
880 inline unsigned int
881 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
882 {
883   return LINEMAPS_CACHE (set, false);
884 }
885 
886 /* As above, but by reference (e.g. as an lvalue).  */
887 
888 inline unsigned int &
889 LINEMAPS_ORDINARY_CACHE (line_maps *set)
890 {
891   return LINEMAPS_CACHE (set, false);
892 }
893 
894 /* Returns a pointer to the last ordinary map used in the line table
895    SET.  */
896 inline line_map_ordinary *
897 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
898 {
899   return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
900 }
901 
902 /* Returns a pointer to the last ordinary map allocated the line table
903    SET.  */
904 inline line_map_ordinary *
905 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
906 {
907   return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
908 }
909 
910 /* Returns a pointer to the beginning of the region where macro maps
911    are allocated.  */
912 inline line_map_macro *
913 LINEMAPS_MACRO_MAPS (const line_maps *set)
914 {
915   return set->info_macro.maps;
916 }
917 
918 /* Returns the INDEXth macro map.  */
919 inline line_map_macro *
920 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
921 {
922   linemap_assert (index >= 0);
923   linemap_assert ((unsigned int)index < set->info_macro.used);
924   return &set->info_macro.maps[index];
925 }
926 
927 /* Returns the number of macro maps that were allocated in the line
928    table SET.  */
929 inline unsigned int
930 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
931 {
932   return LINEMAPS_ALLOCATED (set, true);
933 }
934 
935 /* Returns the number of macro maps used in the line table SET.  */
936 inline unsigned int
937 LINEMAPS_MACRO_USED (const line_maps *set)
938 {
939   return LINEMAPS_USED (set, true);
940 }
941 
942 /* Returns the index of the last macro map looked up with
943    linemap_lookup.  */
944 inline unsigned int
945 LINEMAPS_MACRO_CACHE (const line_maps *set)
946 {
947   return LINEMAPS_CACHE (set, true);
948 }
949 
950 /* As above, but by reference (e.g. as an lvalue).  */
951 
952 inline unsigned int &
953 LINEMAPS_MACRO_CACHE (line_maps *set)
954 {
955   return LINEMAPS_CACHE (set, true);
956 }
957 
958 /* Returns the last macro map used in the line table SET.  */
959 inline line_map_macro *
960 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
961 {
962   return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
963 }
964 
965 /* Returns the lowest location [of a token resulting from macro
966    expansion] encoded in this line table.  */
967 inline source_location
968 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
969 {
970   return LINEMAPS_MACRO_USED (set)
971          ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
972          : MAX_SOURCE_LOCATION;
973 }
974 
975 /* Returns the last macro map allocated in the line table SET.  */
976 inline line_map_macro *
977 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
978 {
979   return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
980 }
981 
982 extern source_location get_combined_adhoc_loc (struct line_maps *,
983 					       source_location,
984 					       source_range,
985 					       void *);
986 extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
987 extern source_location get_location_from_adhoc_loc (struct line_maps *,
988 						    source_location);
989 
990 extern source_range get_range_from_loc (line_maps *set, source_location loc);
991 
992 /* Get whether location LOC is an ad-hoc location.  */
993 
994 inline bool
995 IS_ADHOC_LOC (source_location loc)
996 {
997   return (loc & MAX_SOURCE_LOCATION) != loc;
998 }
999 
1000 /* Get whether location LOC is a "pure" location, or
1001    whether it is an ad-hoc location, or embeds range information.  */
1002 
1003 bool
1004 pure_location_p (line_maps *set, source_location loc);
1005 
1006 /* Given location LOC within SET, strip away any packed range information
1007    or ad-hoc information.  */
1008 
1009 extern source_location get_pure_location (line_maps *set,
1010 					  source_location loc);
1011 
1012 /* Combine LOC and BLOCK, giving a combined adhoc location.  */
1013 
1014 inline source_location
1015 COMBINE_LOCATION_DATA (struct line_maps *set,
1016 		       source_location loc,
1017 		       source_range src_range,
1018 		       void *block)
1019 {
1020   return get_combined_adhoc_loc (set, loc, src_range, block);
1021 }
1022 
1023 extern void rebuild_location_adhoc_htab (struct line_maps *);
1024 
1025 /* Initialize a line map set.  SET is the line map set to initialize
1026    and BUILTIN_LOCATION is the special location value to be used as
1027    spelling location for built-in tokens.  This BUILTIN_LOCATION has
1028    to be strictly less than RESERVED_LOCATION_COUNT.  */
1029 extern void linemap_init (struct line_maps *set,
1030 			  source_location builtin_location);
1031 
1032 /* Check for and warn about line_maps entered but not exited.  */
1033 
1034 extern void linemap_check_files_exited (struct line_maps *);
1035 
1036 /* Return a source_location for the start (i.e. column==0) of
1037    (physical) line TO_LINE in the current source file (as in the
1038    most recent linemap_add).   MAX_COLUMN_HINT is the highest column
1039    number we expect to use in this line (but it does not change
1040    the highest_location).  */
1041 
1042 extern source_location linemap_line_start
1043 (struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
1044 
1045 /* Add a mapping of logical source line to physical source file and
1046    line number. This function creates an "ordinary map", which is a
1047    map that records locations of tokens that are not part of macro
1048    replacement-lists present at a macro expansion point.
1049 
1050    The text pointed to by TO_FILE must have a lifetime
1051    at least as long as the lifetime of SET.  An empty
1052    TO_FILE means standard input.  If reason is LC_LEAVE, and
1053    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
1054    natural values considering the file we are returning to.
1055 
1056    A call to this function can relocate the previous set of
1057    maps, so any stored line_map pointers should not be used.  */
1058 extern const struct line_map *linemap_add
1059   (struct line_maps *, enum lc_reason, unsigned int sysp,
1060    const char *to_file, linenum_type to_line);
1061 
1062 /* Given a logical source location, returns the map which the
1063    corresponding (source file, line, column) triplet can be deduced
1064    from. Since the set is built chronologically, the logical lines are
1065    monotonic increasing, and so the list is sorted and we can use a
1066    binary search. If no line map have been allocated yet, this
1067    function returns NULL.  */
1068 extern const struct line_map *linemap_lookup
1069   (struct line_maps *, source_location);
1070 
1071 /* Returns TRUE if the line table set tracks token locations across
1072    macro expansion, FALSE otherwise.  */
1073 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
1074 
1075 /* Return the name of the macro associated to MACRO_MAP.  */
1076 const char* linemap_map_get_macro_name (const line_map_macro *);
1077 
1078 /* Return a positive value if LOCATION is the locus of a token that is
1079    located in a system header, O otherwise. It returns 1 if LOCATION
1080    is the locus of a token that is located in a system header, and 2
1081    if LOCATION is the locus of a token located in a C system header
1082    that therefore needs to be extern "C" protected in C++.
1083 
1084    Note that this function returns 1 if LOCATION belongs to a token
1085    that is part of a macro replacement-list defined in a system
1086    header, but expanded in a non-system file.  */
1087 int linemap_location_in_system_header_p (struct line_maps *,
1088 					 source_location);
1089 
1090 /* Return TRUE if LOCATION is a source code location of a token that is part of
1091    a macro expansion, FALSE otherwise.  */
1092 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
1093 					      source_location);
1094 
1095 /* TRUE if LOCATION is a source code location of a token that is part of the
1096    definition of a macro, FALSE otherwise.  */
1097 bool linemap_location_from_macro_definition_p (struct line_maps *,
1098 					       source_location);
1099 
1100 /* With the precondition that LOCATION is the locus of a token that is
1101    an argument of a function-like macro MACRO_MAP and appears in the
1102    expansion of MACRO_MAP, return the locus of that argument in the
1103    context of the caller of MACRO_MAP.  */
1104 
1105 extern source_location linemap_macro_map_loc_unwind_toward_spelling
1106   (line_maps *set, const line_map_macro *macro_map, source_location location);
1107 
1108 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
1109    be reserved for libcpp user as special values, no token from libcpp
1110    will contain any of those locations.  */
1111 const source_location RESERVED_LOCATION_COUNT = 2;
1112 
1113 /* Converts a map and a source_location to source line.  */
1114 inline linenum_type
1115 SOURCE_LINE (const line_map_ordinary *ord_map, source_location loc)
1116 {
1117   return ((loc - ord_map->start_location)
1118 	  >> ord_map->m_column_and_range_bits) + ord_map->to_line;
1119 }
1120 
1121 /* Convert a map and source_location to source column number.  */
1122 inline linenum_type
1123 SOURCE_COLUMN (const line_map_ordinary *ord_map, source_location loc)
1124 {
1125   return ((loc - ord_map->start_location)
1126 	  & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
1127 }
1128 
1129 /* Return the location of the last source line within an ordinary
1130    map.  */
1131 inline source_location
1132 LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
1133 {
1134   return (((map[1].start_location - 1
1135 	    - map->start_location)
1136 	   & ~((1 << map->m_column_and_range_bits) - 1))
1137 	  + map->start_location);
1138 }
1139 
1140 /* Returns the last source line number within an ordinary map.  This
1141    is the (last) line of the #include, or other directive, that caused
1142    a map change.  */
1143 inline linenum_type
1144 LAST_SOURCE_LINE (const line_map_ordinary *map)
1145 {
1146   return SOURCE_LINE (map, LAST_SOURCE_LINE_LOCATION (map));
1147 }
1148 
1149 /* Return the last column number within an ordinary map.  */
1150 
1151 inline linenum_type
1152 LAST_SOURCE_COLUMN (const line_map_ordinary *map)
1153 {
1154   return SOURCE_COLUMN (map, LAST_SOURCE_LINE_LOCATION (map));
1155 }
1156 
1157 /* Returns the map a given map was included from, or NULL if the map
1158    belongs to the main file, i.e, a file that wasn't included by
1159    another one.  */
1160 inline line_map_ordinary *
1161 INCLUDED_FROM (struct line_maps *set, const line_map_ordinary *ord_map)
1162 {
1163   return ((ord_map->included_from == -1)
1164 	  ? NULL
1165 	  : LINEMAPS_ORDINARY_MAP_AT (set, ord_map->included_from));
1166 }
1167 
1168 /* True if the map is at the bottom of the include stack.  */
1169 
1170 inline bool
1171 MAIN_FILE_P (const line_map_ordinary *ord_map)
1172 {
1173   return ord_map->included_from < 0;
1174 }
1175 
1176 /* Encode and return a source_location from a column number. The
1177    source line considered is the last source line used to call
1178    linemap_line_start, i.e, the last source line which a location was
1179    encoded from.  */
1180 extern source_location
1181 linemap_position_for_column (struct line_maps *, unsigned int);
1182 
1183 /* Encode and return a source location from a given line and
1184    column.  */
1185 source_location
1186 linemap_position_for_line_and_column (line_maps *set,
1187 				      const line_map_ordinary *,
1188 				      linenum_type, unsigned int);
1189 
1190 /* Encode and return a source_location starting from location LOC and
1191    shifting it by OFFSET columns.  This function does not support
1192    virtual locations.  */
1193 source_location
1194 linemap_position_for_loc_and_offset (struct line_maps *set,
1195 				     source_location loc,
1196 				     unsigned int offset);
1197 
1198 /* Return the file this map is for.  */
1199 inline const char *
1200 LINEMAP_FILE (const line_map_ordinary *ord_map)
1201 {
1202   return ord_map->to_file;
1203 }
1204 
1205 /* Return the line number this map started encoding location from.  */
1206 inline linenum_type
1207 LINEMAP_LINE (const line_map_ordinary *ord_map)
1208 {
1209   return ord_map->to_line;
1210 }
1211 
1212 /* Return a positive value if map encodes locations from a system
1213    header, 0 otherwise. Returns 1 if MAP encodes locations in a
1214    system header and 2 if it encodes locations in a C system header
1215    that therefore needs to be extern "C" protected in C++.  */
1216 inline unsigned char
1217 LINEMAP_SYSP (const line_map_ordinary *ord_map)
1218 {
1219   return ord_map->sysp;
1220 }
1221 
1222 /* Return a positive value if PRE denotes the location of a token that
1223    comes before the token of POST, 0 if PRE denotes the location of
1224    the same token as the token for POST, and a negative value
1225    otherwise.  */
1226 int linemap_compare_locations (struct line_maps *set,
1227 			       source_location   pre,
1228 			       source_location   post);
1229 
1230 /* Return TRUE if LOC_A denotes the location a token that comes
1231    topogically before the token denoted by location LOC_B, or if they
1232    are equal.  */
1233 inline bool
1234 linemap_location_before_p (struct line_maps *set,
1235 			   source_location loc_a,
1236 			   source_location loc_b)
1237 {
1238   return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1239 }
1240 
1241 typedef struct
1242 {
1243   /* The name of the source file involved.  */
1244   const char *file;
1245 
1246   /* The line-location in the source file.  */
1247   int line;
1248 
1249   int column;
1250 
1251   void *data;
1252 
1253   /* In a system header?. */
1254   bool sysp;
1255 } expanded_location;
1256 
1257 /* Both gcc and emacs number source *lines* starting at 1, but
1258    they have differing conventions for *columns*.
1259 
1260    GCC uses a 1-based convention for source columns,
1261    whereas Emacs's M-x column-number-mode uses a 0-based convention.
1262 
1263    For example, an error in the initial, left-hand
1264    column of source line 3 is reported by GCC as:
1265 
1266       some-file.c:3:1: error: ...etc...
1267 
1268    On navigating to the location of that error in Emacs
1269    (e.g. via "next-error"),
1270    the locus is reported in the Mode Line
1271    (assuming M-x column-number-mode) as:
1272 
1273      some-file.c   10%   (3, 0)
1274 
1275    i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs.  */
1276 
1277 /* A location within a rich_location: a caret&range, with
1278    the caret potentially flagged for display.  */
1279 
1280 struct location_range
1281 {
1282   source_location m_loc;
1283 
1284   /* Should a caret be drawn for this range?  Typically this is
1285      true for the 0th range, and false for subsequent ranges,
1286      but the Fortran frontend overrides this for rendering things like:
1287 
1288        x = x + y
1289            1   2
1290        Error: Shapes for operands at (1) and (2) are not conformable
1291 
1292      where "1" and "2" are notionally carets.  */
1293   bool m_show_caret_p;
1294 };
1295 
1296 /* A partially-embedded vec for use within rich_location for storing
1297    ranges and fix-it hints.
1298 
1299    Elements [0..NUM_EMBEDDED) are allocated within m_embed, after
1300    that they are within the dynamically-allocated m_extra.
1301 
1302    This allows for static allocation in the common case, whilst
1303    supporting the rarer case of an arbitrary number of elements.
1304 
1305    Dynamic allocation is not performed unless it's needed.  */
1306 
1307 template <typename T, int NUM_EMBEDDED>
1308 class semi_embedded_vec
1309 {
1310  public:
1311   semi_embedded_vec ();
1312   ~semi_embedded_vec ();
1313 
1314   unsigned int count () const { return m_num; }
1315   T& operator[] (int idx);
1316   const T& operator[] (int idx) const;
1317 
1318   void push (const T&);
1319   void truncate (int len);
1320 
1321  private:
1322   int m_num;
1323   T m_embedded[NUM_EMBEDDED];
1324   int m_alloc;
1325   T *m_extra;
1326 };
1327 
1328 /* Constructor for semi_embedded_vec.  In particular, no dynamic allocation
1329    is done.  */
1330 
1331 template <typename T, int NUM_EMBEDDED>
1332 semi_embedded_vec<T, NUM_EMBEDDED>::semi_embedded_vec ()
1333 : m_num (0), m_alloc (0), m_extra (NULL)
1334 {
1335 }
1336 
1337 /* semi_embedded_vec's dtor.  Release any dynamically-allocated memory.  */
1338 
1339 template <typename T, int NUM_EMBEDDED>
1340 semi_embedded_vec<T, NUM_EMBEDDED>::~semi_embedded_vec ()
1341 {
1342   XDELETEVEC (m_extra);
1343 }
1344 
1345 /* Look up element IDX, mutably.  */
1346 
1347 template <typename T, int NUM_EMBEDDED>
1348 T&
1349 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx)
1350 {
1351   linemap_assert (idx < m_num);
1352   if (idx < NUM_EMBEDDED)
1353     return m_embedded[idx];
1354   else
1355     {
1356       linemap_assert (m_extra != NULL);
1357       return m_extra[idx - NUM_EMBEDDED];
1358     }
1359 }
1360 
1361 /* Look up element IDX (const).  */
1362 
1363 template <typename T, int NUM_EMBEDDED>
1364 const T&
1365 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx) const
1366 {
1367   linemap_assert (idx < m_num);
1368   if (idx < NUM_EMBEDDED)
1369     return m_embedded[idx];
1370   else
1371     {
1372       linemap_assert (m_extra != NULL);
1373       return m_extra[idx - NUM_EMBEDDED];
1374     }
1375 }
1376 
1377 /* Append VALUE to the end of the semi_embedded_vec.  */
1378 
1379 template <typename T, int NUM_EMBEDDED>
1380 void
1381 semi_embedded_vec<T, NUM_EMBEDDED>::push (const T& value)
1382 {
1383   int idx = m_num++;
1384   if (idx < NUM_EMBEDDED)
1385     m_embedded[idx] = value;
1386   else
1387     {
1388       /* Offset "idx" to be an index within m_extra.  */
1389       idx -= NUM_EMBEDDED;
1390       if (NULL == m_extra)
1391 	{
1392 	  linemap_assert (m_alloc == 0);
1393 	  m_alloc = 16;
1394 	  m_extra = XNEWVEC (T, m_alloc);
1395 	}
1396       else if (idx >= m_alloc)
1397 	{
1398 	  linemap_assert (m_alloc > 0);
1399 	  m_alloc *= 2;
1400 	  m_extra = XRESIZEVEC (T, m_extra, m_alloc);
1401 	}
1402       linemap_assert (m_extra);
1403       linemap_assert (idx < m_alloc);
1404       m_extra[idx] = value;
1405     }
1406 }
1407 
1408 /* Truncate to length LEN.  No deallocation is performed.  */
1409 
1410 template <typename T, int NUM_EMBEDDED>
1411 void
1412 semi_embedded_vec<T, NUM_EMBEDDED>::truncate (int len)
1413 {
1414   linemap_assert (len <= m_num);
1415   m_num = len;
1416 }
1417 
1418 class fixit_hint;
1419   class fixit_insert;
1420   class fixit_replace;
1421 
1422 /* A "rich" source code location, for use when printing diagnostics.
1423    A rich_location has one or more carets&ranges, where the carets
1424    are optional.  These are referred to as "ranges" from here.
1425    Typically the zeroth range has a caret; other ranges sometimes
1426    have carets.
1427 
1428    The "primary" location of a rich_location is the caret of range 0,
1429    used for determining the line/column when printing diagnostic
1430    text, such as:
1431 
1432       some-file.c:3:1: error: ...etc...
1433 
1434    Additional ranges may be added to help the user identify other
1435    pertinent clauses in a diagnostic.
1436 
1437    rich_location instances are intended to be allocated on the stack
1438    when generating diagnostics, and to be short-lived.
1439 
1440    Examples of rich locations
1441    --------------------------
1442 
1443    Example A
1444    *********
1445       int i = "foo";
1446               ^
1447    This "rich" location is simply a single range (range 0), with
1448    caret = start = finish at the given point.
1449 
1450    Example B
1451    *********
1452       a = (foo && bar)
1453           ~~~~~^~~~~~~
1454    This rich location has a single range (range 0), with the caret
1455    at the first "&", and the start/finish at the parentheses.
1456    Compare with example C below.
1457 
1458    Example C
1459    *********
1460       a = (foo && bar)
1461            ~~~ ^~ ~~~
1462    This rich location has three ranges:
1463    - Range 0 has its caret and start location at the first "&" and
1464      end at the second "&.
1465    - Range 1 has its start and finish at the "f" and "o" of "foo";
1466      the caret is not flagged for display, but is perhaps at the "f"
1467      of "foo".
1468    - Similarly, range 2 has its start and finish at the "b" and "r" of
1469      "bar"; the caret is not flagged for display, but is perhaps at the
1470      "b" of "bar".
1471    Compare with example B above.
1472 
1473    Example D (Fortran frontend)
1474    ****************************
1475        x = x + y
1476            1   2
1477    This rich location has range 0 at "1", and range 1 at "2".
1478    Both are flagged for caret display.  Both ranges have start/finish
1479    equal to their caret point.  The frontend overrides the diagnostic
1480    context's default caret character for these ranges.
1481 
1482    Example E
1483    *********
1484       printf ("arg0: %i  arg1: %s arg2: %i",
1485                                ^~
1486               100, 101, 102);
1487                    ~~~
1488    This rich location has two ranges:
1489    - range 0 is at the "%s" with start = caret = "%" and finish at
1490      the "s".
1491    - range 1 has start/finish covering the "101" and is not flagged for
1492      caret printing; it is perhaps at the start of "101".
1493 
1494 
1495    Fix-it hints
1496    ------------
1497 
1498    Rich locations can also contain "fix-it hints", giving suggestions
1499    for the user on how to edit their code to fix a problem.  These
1500    can be expressed as insertions, replacements, and removals of text.
1501    The edits by default are relative to the zeroth range within the
1502    rich_location, but optionally they can be expressed relative to
1503    other locations (using various overloaded methods of the form
1504    rich_location::add_fixit_*).
1505 
1506    For example:
1507 
1508    Example F: fix-it hint: insert_before
1509    *************************************
1510       ptr = arr[0];
1511 	    ^~~~~~
1512 	    &
1513    This rich location has a single range (range 0) covering "arr[0]",
1514    with the caret at the start.  The rich location has a single
1515    insertion fix-it hint, inserted before range 0, added via
1516      richloc.add_fixit_insert_before ("&");
1517 
1518    Example G: multiple fix-it hints: insert_before and insert_after
1519    ****************************************************************
1520       #define FN(ARG0, ARG1, ARG2) fn(ARG0, ARG1, ARG2)
1521 				      ^~~~  ^~~~  ^~~~
1522 				      (   ) (   ) (   )
1523    This rich location has three ranges, covering "arg0", "arg1",
1524    and "arg2", all with caret-printing enabled.
1525    The rich location has 6 insertion fix-it hints: each arg
1526    has a pair of insertion fix-it hints, suggesting wrapping
1527    them with parentheses: one a '(' inserted before,
1528    the other a ')' inserted after, added via
1529      richloc.add_fixit_insert_before (LOC, "(");
1530    and
1531      richloc.add_fixit_insert_after (LOC, ")");
1532 
1533    Example H: fix-it hint: removal
1534    *******************************
1535      struct s {int i};;
1536 		      ^
1537 		      -
1538    This rich location has a single range at the stray trailing
1539    semicolon, along with a single removal fix-it hint, covering
1540    the same range, added via:
1541      richloc.add_fixit_remove ();
1542 
1543    Example I: fix-it hint: replace
1544    *******************************
1545       c = s.colour;
1546 	    ^~~~~~
1547 	    color
1548    This rich location has a single range (range 0) covering "colour",
1549    and a single "replace" fix-it hint, covering the same range,
1550    added via
1551      richloc.add_fixit_replace ("color");
1552 
1553    Adding a fix-it hint can fail: for example, attempts to insert content
1554    at the transition between two line maps may fail due to there being no
1555    source_location (aka location_t) value to express the new location.
1556 
1557    Attempts to add a fix-it hint within a macro expansion will fail.
1558 
1559    We do not yet support newlines in fix-it text; attempts to do so will fail.
1560 
1561    The rich_location API handles these failures gracefully, so that
1562    diagnostics can attempt to add fix-it hints without each needing
1563    extensive checking.
1564 
1565    Fix-it hints within a rich_location are "atomic": if any hints can't
1566    be applied, none of them will be (tracked by the m_seen_impossible_fixit
1567    flag), and no fix-its hints will be displayed for that rich_location.
1568    This implies that diagnostic messages need to be worded in such a way
1569    that they make sense whether or not the fix-it hints are displayed,
1570    or that richloc.seen_impossible_fixit_p () should be checked before
1571    issuing the diagnostics.  */
1572 
1573 class rich_location
1574 {
1575  public:
1576   /* Constructors.  */
1577 
1578   /* Constructing from a location.  */
1579   rich_location (line_maps *set, source_location loc);
1580 
1581   /* Destructor.  */
1582   ~rich_location ();
1583 
1584   /* Accessors.  */
1585   source_location get_loc () const { return get_loc (0); }
1586   source_location get_loc (unsigned int idx) const;
1587 
1588   void
1589   add_range (source_location loc,  bool show_caret_p);
1590 
1591   void
1592   set_range (line_maps *set, unsigned int idx, source_location loc,
1593 	     bool show_caret_p);
1594 
1595   unsigned int get_num_locations () const { return m_ranges.count (); }
1596 
1597   const location_range *get_range (unsigned int idx) const;
1598   location_range *get_range (unsigned int idx);
1599 
1600   expanded_location get_expanded_location (unsigned int idx);
1601 
1602   void
1603   override_column (int column);
1604 
1605   /* Fix-it hints.  */
1606 
1607   /* Methods for adding insertion fix-it hints.  */
1608 
1609   /* Suggest inserting NEW_CONTENT immediately before the primary
1610      range's start.  */
1611   void
1612   add_fixit_insert_before (const char *new_content);
1613 
1614   /* Suggest inserting NEW_CONTENT immediately before the start of WHERE.  */
1615   void
1616   add_fixit_insert_before (source_location where,
1617 			   const char *new_content);
1618 
1619   /* Suggest inserting NEW_CONTENT immediately after the end of the primary
1620      range.  */
1621   void
1622   add_fixit_insert_after (const char *new_content);
1623 
1624   /* Suggest inserting NEW_CONTENT immediately after the end of WHERE.  */
1625   void
1626   add_fixit_insert_after (source_location where,
1627 			  const char *new_content);
1628 
1629   /* Methods for adding removal fix-it hints.  */
1630 
1631   /* Suggest removing the content covered by range 0.  */
1632   void
1633   add_fixit_remove ();
1634 
1635   /* Suggest removing the content covered between the start and finish
1636      of WHERE.  */
1637   void
1638   add_fixit_remove (source_location where);
1639 
1640   /* Suggest removing the content covered by SRC_RANGE.  */
1641   void
1642   add_fixit_remove (source_range src_range);
1643 
1644   /* Methods for adding "replace" fix-it hints.  */
1645 
1646   /* Suggest replacing the content covered by range 0 with NEW_CONTENT.  */
1647   void
1648   add_fixit_replace (const char *new_content);
1649 
1650   /* Suggest replacing the content between the start and finish of
1651      WHERE with NEW_CONTENT.  */
1652   void
1653   add_fixit_replace (source_location where,
1654 		     const char *new_content);
1655 
1656   /* Suggest replacing the content covered by SRC_RANGE with
1657      NEW_CONTENT.  */
1658   void
1659   add_fixit_replace (source_range src_range,
1660 		     const char *new_content);
1661 
1662   unsigned int get_num_fixit_hints () const { return m_fixit_hints.count (); }
1663   fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
1664   fixit_hint *get_last_fixit_hint () const;
1665   bool seen_impossible_fixit_p () const { return m_seen_impossible_fixit; }
1666 
1667 private:
1668   bool reject_impossible_fixit (source_location where);
1669   void stop_supporting_fixits ();
1670   void add_fixit (fixit_hint *hint);
1671 
1672 public:
1673   static const int STATICALLY_ALLOCATED_RANGES = 3;
1674 
1675 protected:
1676   line_maps *m_line_table;
1677   semi_embedded_vec <location_range, STATICALLY_ALLOCATED_RANGES> m_ranges;
1678 
1679   int m_column_override;
1680 
1681   bool m_have_expanded_location;
1682   expanded_location m_expanded_location;
1683 
1684   static const int MAX_STATIC_FIXIT_HINTS = 2;
1685   semi_embedded_vec <fixit_hint *, MAX_STATIC_FIXIT_HINTS> m_fixit_hints;
1686 
1687   bool m_seen_impossible_fixit;
1688 };
1689 
1690 class fixit_hint
1691 {
1692 public:
1693   enum kind {INSERT, REPLACE};
1694 
1695   virtual ~fixit_hint () {}
1696 
1697   virtual enum kind get_kind () const = 0;
1698   virtual bool affects_line_p (const char *file, int line) const = 0;
1699   virtual source_location get_start_loc () const = 0;
1700   virtual bool maybe_get_end_loc (source_location *out) const = 0;
1701   /* Vfunc for consolidating successor fixits.  */
1702   virtual bool maybe_append_replace (line_maps *set,
1703 				     source_range src_range,
1704 				     const char *new_content) = 0;
1705 };
1706 
1707 class fixit_insert : public fixit_hint
1708 {
1709  public:
1710   fixit_insert (source_location where,
1711 		const char *new_content);
1712   ~fixit_insert ();
1713   enum kind get_kind () const { return INSERT; }
1714   bool affects_line_p (const char *file, int line) const;
1715   source_location get_start_loc () const { return m_where; }
1716   bool maybe_get_end_loc (source_location *) const { return false; }
1717   bool maybe_append_replace (line_maps *set,
1718 			     source_range src_range,
1719 			     const char *new_content);
1720 
1721   source_location get_location () const { return m_where; }
1722   const char *get_string () const { return m_bytes; }
1723   size_t get_length () const { return m_len; }
1724 
1725  private:
1726   source_location m_where;
1727   char *m_bytes;
1728   size_t m_len;
1729 };
1730 
1731 class fixit_replace : public fixit_hint
1732 {
1733  public:
1734   fixit_replace (source_range src_range,
1735                  const char *new_content);
1736   ~fixit_replace ();
1737 
1738   enum kind get_kind () const { return REPLACE; }
1739   bool affects_line_p (const char *file, int line) const;
1740   source_location get_start_loc () const { return m_src_range.m_start; }
1741   bool maybe_get_end_loc (source_location *out) const
1742   {
1743     *out = m_src_range.m_finish;
1744     return true;
1745   }
1746   bool maybe_append_replace (line_maps *set,
1747 			     source_range src_range,
1748 			     const char *new_content);
1749 
1750   source_range get_range () const { return m_src_range; }
1751   const char *get_string () const { return m_bytes; }
1752   size_t get_length () const { return m_len; }
1753 
1754  private:
1755   source_range m_src_range;
1756   char *m_bytes;
1757   size_t m_len;
1758 };
1759 
1760 
1761 /* This is enum is used by the function linemap_resolve_location
1762    below.  The meaning of the values is explained in the comment of
1763    that function.  */
1764 enum location_resolution_kind
1765 {
1766   LRK_MACRO_EXPANSION_POINT,
1767   LRK_SPELLING_LOCATION,
1768   LRK_MACRO_DEFINITION_LOCATION
1769 };
1770 
1771 /* Resolve a virtual location into either a spelling location, an
1772    expansion point location or a token argument replacement point
1773    location.  Return the map that encodes the virtual location as well
1774    as the resolved location.
1775 
1776    If LOC is *NOT* the location of a token resulting from the
1777    expansion of a macro, then the parameter LRK (which stands for
1778    Location Resolution Kind) is ignored and the resulting location
1779    just equals the one given in argument.
1780 
1781    Now if LOC *IS* the location of a token resulting from the
1782    expansion of a macro, this is what happens.
1783 
1784    * If LRK is set to LRK_MACRO_EXPANSION_POINT
1785    -------------------------------
1786 
1787    The virtual location is resolved to the first macro expansion point
1788    that led to this macro expansion.
1789 
1790    * If LRK is set to LRK_SPELLING_LOCATION
1791    -------------------------------------
1792 
1793    The virtual location is resolved to the locus where the token has
1794    been spelled in the source.   This can follow through all the macro
1795    expansions that led to the token.
1796 
1797    * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1798    --------------------------------------
1799 
1800    The virtual location is resolved to the locus of the token in the
1801    context of the macro definition.
1802 
1803    If LOC is the locus of a token that is an argument of a
1804    function-like macro [replacing a parameter in the replacement list
1805    of the macro] the virtual location is resolved to the locus of the
1806    parameter that is replaced, in the context of the definition of the
1807    macro.
1808 
1809    If LOC is the locus of a token that is not an argument of a
1810    function-like macro, then the function behaves as if LRK was set to
1811    LRK_SPELLING_LOCATION.
1812 
1813    If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1814    returned location.  Note that if the returned location wasn't originally
1815    encoded by a map, the *MAP is set to NULL.  This can happen if LOC
1816    resolves to a location reserved for the client code, like
1817    UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC.  */
1818 
1819 source_location linemap_resolve_location (struct line_maps *,
1820 					  source_location loc,
1821 					  enum location_resolution_kind lrk,
1822 					  const line_map_ordinary **loc_map);
1823 
1824 /* Suppose that LOC is the virtual location of a token coming from the
1825    expansion of a macro M.  This function then steps up to get the
1826    location L of the point where M got expanded.  If L is a spelling
1827    location inside a macro expansion M', then this function returns
1828    the point where M' was expanded.  LOC_MAP is an output parameter.
1829    When non-NULL, *LOC_MAP is set to the map of the returned
1830    location.  */
1831 source_location linemap_unwind_toward_expansion (struct line_maps *,
1832 						 source_location loc,
1833 						 const struct line_map **loc_map);
1834 
1835 /* If LOC is the virtual location of a token coming from the expansion
1836    of a macro M and if its spelling location is reserved (e.g, a
1837    location for a built-in token), then this function unwinds (using
1838    linemap_unwind_toward_expansion) the location until a location that
1839    is not reserved and is not in a system header is reached.  In other
1840    words, this unwinds the reserved location until a location that is
1841    in real source code is reached.
1842 
1843    Otherwise, if the spelling location for LOC is not reserved or if
1844    LOC doesn't come from the expansion of a macro, the function
1845    returns LOC as is and *MAP is not touched.
1846 
1847    *MAP is set to the map of the returned location if the later is
1848    different from LOC.  */
1849 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1850 							  source_location loc,
1851 							  const struct line_map **map);
1852 
1853 /* Expand source code location LOC and return a user readable source
1854    code location.  LOC must be a spelling (non-virtual) location.  If
1855    it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1856    location is returned.  */
1857 expanded_location linemap_expand_location (struct line_maps *,
1858 					   const struct line_map *,
1859 					   source_location loc);
1860 
1861 /* Statistics about maps allocation and usage as returned by
1862    linemap_get_statistics.  */
1863 struct linemap_stats
1864 {
1865   long num_ordinary_maps_allocated;
1866   long num_ordinary_maps_used;
1867   long ordinary_maps_allocated_size;
1868   long ordinary_maps_used_size;
1869   long num_expanded_macros;
1870   long num_macro_tokens;
1871   long num_macro_maps_used;
1872   long macro_maps_allocated_size;
1873   long macro_maps_used_size;
1874   long macro_maps_locations_size;
1875   long duplicated_macro_maps_locations_size;
1876   long adhoc_table_size;
1877   long adhoc_table_entries_used;
1878 };
1879 
1880 /* Return the highest location emitted for a given file for which
1881    there is a line map in SET.  FILE_NAME is the file name to
1882    consider.  If the function returns TRUE, *LOC is set to the highest
1883    location emitted for that file.  */
1884 bool linemap_get_file_highest_location (struct line_maps * set,
1885 					const char *file_name,
1886 					source_location *loc);
1887 
1888 /* Compute and return statistics about the memory consumption of some
1889    parts of the line table SET.  */
1890 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
1891 
1892 /* Dump debugging information about source location LOC into the file
1893    stream STREAM. SET is the line map set LOC comes from.  */
1894 void linemap_dump_location (struct line_maps *, source_location, FILE *);
1895 
1896 /* Dump line map at index IX in line table SET to STREAM.  If STREAM
1897    is NULL, use stderr.  IS_MACRO is true if the caller wants to
1898    dump a macro map, false otherwise.  */
1899 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
1900 
1901 /* Dump line table SET to STREAM.  If STREAM is NULL, stderr is used.
1902    NUM_ORDINARY specifies how many ordinary maps to dump.  NUM_MACRO
1903    specifies how many macro maps to dump.  */
1904 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
1905 
1906 /* The rich_location class requires a way to expand source_location instances.
1907    We would directly use expand_location_to_spelling_point, which is
1908    implemented in gcc/input.c, but we also need to use it for rich_location
1909    within genmatch.c.
1910    Hence we require client code of libcpp to implement the following
1911    symbol.  */
1912 extern expanded_location
1913 linemap_client_expand_location_to_spelling_point (source_location );
1914 
1915 #endif /* !LIBCPP_LINE_MAP_H  */
1916