-
Notifications
You must be signed in to change notification settings - Fork 0
/
libcp1plugin.cc
3771 lines (3229 loc) · 102 KB
/
libcp1plugin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Library interface to C++ front end.
Copyright (C) 2014-2020 Free Software Foundation, Inc.
This file is part of GCC. As it interacts with GDB through libcc1,
they all become a single program as regards the GNU GPL's requirements.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include <cc1plugin-config.h>
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include "../gcc/config.h"
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#include "gcc-plugin.h"
#include "system.h"
#include "coretypes.h"
#include "stringpool.h"
#include "gcc-interface.h"
#include "hash-set.h"
#include "machmode.h"
#include "vec.h"
#include "double-int.h"
#include "input.h"
#include "alias.h"
#include "symtab.h"
#include "options.h"
#include "wide-int.h"
#include "inchash.h"
#include "tree.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "cp-tree.h"
#include "toplev.h"
#include "timevar.h"
#include "hash-table.h"
#include "tm.h"
#include "c-family/c-pragma.h"
// #include "c-lang.h"
#include "diagnostic.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "decl.h"
#include "function.h"
#undef cfun // we want to assign to it, and function.h won't let us
#include "callbacks.hh"
#include "connection.hh"
#include "marshall-cp.hh"
#include "rpc.hh"
#ifdef __GNUC__
#pragma GCC visibility push(default)
#endif
int plugin_is_GPL_compatible;
#ifdef __GNUC__
#pragma GCC visibility pop
#endif
static int ATTRIBUTE_UNUSED
check_symbol_mask[GCC_CP_SYMBOL_MASK >= GCC_CP_SYMBOL_END ? 1 : -1];
// This is put into the lang hooks when the plugin starts.
static void
plugin_print_error_function (diagnostic_context *context, const char *file,
diagnostic_info *diagnostic)
{
if (current_function_decl != NULL_TREE
&& DECL_NAME (current_function_decl) != NULL_TREE
&& strcmp (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
GCC_FE_WRAPPER_FUNCTION) == 0)
return;
lhd_print_error_function (context, file, diagnostic);
}
static unsigned long long
convert_out (tree t)
{
return (unsigned long long) (uintptr_t) t;
}
static tree
convert_in (unsigned long long v)
{
return (tree) (uintptr_t) v;
}
struct decl_addr_value
{
tree decl;
tree address;
};
struct decl_addr_hasher : free_ptr_hash<decl_addr_value>
{
static inline hashval_t hash (const decl_addr_value *);
static inline bool equal (const decl_addr_value *, const decl_addr_value *);
};
inline hashval_t
decl_addr_hasher::hash (const decl_addr_value *e)
{
return DECL_UID (e->decl);
}
inline bool
decl_addr_hasher::equal (const decl_addr_value *p1, const decl_addr_value *p2)
{
return p1->decl == p2->decl;
}
struct string_hasher : nofree_ptr_hash<const char>
{
static inline hashval_t hash (const char *s)
{
return htab_hash_string (s);
}
static inline bool equal (const char *p1, const char *p2)
{
return strcmp (p1, p2) == 0;
}
};
struct plugin_context : public cc1_plugin::connection
{
plugin_context (int fd);
// Map decls to addresses.
hash_table<decl_addr_hasher> address_map;
// A collection of trees that are preserved for the GC.
hash_table< nofree_ptr_hash<tree_node> > preserved;
// File name cache.
hash_table<string_hasher> file_names;
// Perform GC marking.
void mark ();
// Preserve a tree during the plugin's operation.
tree preserve (tree t)
{
tree_node **slot = preserved.find_slot (t, INSERT);
*slot = t;
return t;
}
location_t get_location_t (const char *filename,
unsigned int line_number)
{
if (filename == NULL)
return UNKNOWN_LOCATION;
filename = intern_filename (filename);
linemap_add (line_table, LC_ENTER, false, filename, line_number);
location_t loc = linemap_line_start (line_table, line_number, 0);
linemap_add (line_table, LC_LEAVE, false, NULL, 0);
return loc;
}
private:
// Add a file name to FILE_NAMES and return the canonical copy.
const char *intern_filename (const char *filename)
{
const char **slot = file_names.find_slot (filename, INSERT);
if (*slot == NULL)
{
/* The file name must live as long as the line map, which
effectively means as long as this compilation. So, we copy
the string here but never free it. */
*slot = xstrdup (filename);
}
return *slot;
}
};
static plugin_context *current_context;
plugin_context::plugin_context (int fd)
: cc1_plugin::connection (fd),
address_map (30),
preserved (30),
file_names (30)
{
}
void
plugin_context::mark ()
{
for (hash_table<decl_addr_hasher>::iterator it = address_map.begin ();
it != address_map.end ();
++it)
{
ggc_mark ((*it)->decl);
ggc_mark ((*it)->address);
}
for (hash_table< nofree_ptr_hash<tree_node> >::iterator
it = preserved.begin (); it != preserved.end (); ++it)
ggc_mark (&*it);
}
static void
plugin_binding_oracle (enum cp_oracle_request kind, tree identifier)
{
enum gcc_cp_oracle_request request;
gcc_assert (current_context != NULL);
switch (kind)
{
case CP_ORACLE_IDENTIFIER:
request = GCC_CP_ORACLE_IDENTIFIER;
break;
default:
abort ();
}
int ignore;
cc1_plugin::call (current_context, "binding_oracle", &ignore,
request, IDENTIFIER_POINTER (identifier));
}
static int push_count;
/* at_function_scope_p () tests cfun, indicating we're actually
compiling the function, but we don't even set it when pretending to
enter a function scope. We use this distinction to tell these two
cases apart: we don't want to define e.g. class names in the user
expression function's scope, when they're local to the original
function, because they'd get the wrong linkage name. */
static bool
at_fake_function_scope_p ()
{
return (!cfun || cfun->decl != current_function_decl)
&& current_scope () == current_function_decl;
}
static void
push_fake_function (tree fndecl, scope_kind kind = sk_function_parms)
{
current_function_decl = fndecl;
begin_scope (kind, fndecl);
++function_depth;
begin_scope (sk_block, NULL);
}
static void
pop_scope ()
{
if (toplevel_bindings_p () && current_namespace == global_namespace)
pop_from_top_level ();
else if (at_namespace_scope_p ())
pop_namespace ();
else if (at_class_scope_p ())
popclass ();
else
{
gcc_assert (at_fake_function_scope_p ());
gcc_assert (!at_function_scope_p ());
gcc_assert (current_binding_level->kind == sk_block
&& current_binding_level->this_entity == NULL);
leave_scope ();
--function_depth;
gcc_assert (current_binding_level->this_entity
== current_function_decl);
leave_scope ();
current_function_decl = NULL;
for (cp_binding_level *scope = current_binding_level;
scope; scope = scope->level_chain)
if (scope->kind == sk_function_parms)
{
current_function_decl = scope->this_entity;
break;
}
}
}
static void
supplement_binding (cxx_binding *binding, tree decl)
{
/* FIXME: this is pretty much a copy of supplement_binding_1 in
../gcc/cp/name-lookup.c; the few replaced/removed bits are marked
with "// _1:". */
tree bval = binding->value;
bool ok = true;
tree target_bval = strip_using_decl (bval);
tree target_decl = strip_using_decl (decl);
if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
&& target_decl != target_bval
&& (TREE_CODE (target_bval) != TYPE_DECL
/* We allow pushing an enum multiple times in a class
template in order to handle late matching of underlying
type on an opaque-enum-declaration followed by an
enum-specifier. */
|| (processing_template_decl
&& TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
&& TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
&& (dependent_type_p (ENUM_UNDERLYING_TYPE
(TREE_TYPE (target_decl)))
|| dependent_type_p (ENUM_UNDERLYING_TYPE
(TREE_TYPE (target_bval)))))))
/* The new name is the type name. */
binding->type = decl;
else if (/* TARGET_BVAL is null when push_class_level_binding moves
an inherited type-binding out of the way to make room
for a new value binding. */
!target_bval
/* TARGET_BVAL is error_mark_node when TARGET_DECL's name
has been used in a non-class scope prior declaration.
In that case, we should have already issued a
diagnostic; for graceful error recovery purpose, pretend
this was the intended declaration for that name. */
|| target_bval == error_mark_node
/* If TARGET_BVAL is anticipated but has not yet been
declared, pretend it is not there at all. */
|| (TREE_CODE (target_bval) == FUNCTION_DECL
&& DECL_ANTICIPATED (target_bval)
&& !DECL_HIDDEN_FRIEND_P (target_bval)))
binding->value = decl;
else if (TREE_CODE (target_bval) == TYPE_DECL
&& DECL_ARTIFICIAL (target_bval)
&& target_decl != target_bval
&& (TREE_CODE (target_decl) != TYPE_DECL
|| same_type_p (TREE_TYPE (target_decl),
TREE_TYPE (target_bval))))
{
/* The old binding was a type name. It was placed in
VALUE field because it was thought, at the point it was
declared, to be the only entity with such a name. Move the
type name into the type slot; it is now hidden by the new
binding. */
binding->type = bval;
binding->value = decl;
binding->value_is_inherited = false;
}
else if (TREE_CODE (target_bval) == TYPE_DECL
&& TREE_CODE (target_decl) == TYPE_DECL
&& DECL_NAME (target_decl) == DECL_NAME (target_bval)
&& binding->scope->kind != sk_class
&& (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
/* If either type involves template parameters, we must
wait until instantiation. */
|| uses_template_parms (TREE_TYPE (target_decl))
|| uses_template_parms (TREE_TYPE (target_bval))))
/* We have two typedef-names, both naming the same type to have
the same name. In general, this is OK because of:
[dcl.typedef]
In a given scope, a typedef specifier can be used to redefine
the name of any type declared in that scope to refer to the
type to which it already refers.
However, in class scopes, this rule does not apply due to the
stricter language in [class.mem] prohibiting redeclarations of
members. */
ok = false;
/* There can be two block-scope declarations of the same variable,
so long as they are `extern' declarations. However, there cannot
be two declarations of the same static data member:
[class.mem]
A member shall not be declared twice in the
member-specification. */
else if (VAR_P (target_decl)
&& VAR_P (target_bval)
&& DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
&& !DECL_CLASS_SCOPE_P (target_decl))
{
duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
ok = false;
}
else if (TREE_CODE (decl) == NAMESPACE_DECL
&& TREE_CODE (bval) == NAMESPACE_DECL
&& DECL_NAMESPACE_ALIAS (decl)
&& DECL_NAMESPACE_ALIAS (bval)
&& ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
/* [namespace.alias]
In a declarative region, a namespace-alias-definition can be
used to redefine a namespace-alias declared in that declarative
region to refer only to the namespace to which it already
refers. */
ok = false;
else
{
// _1: diagnose_name_conflict (decl, bval);
ok = false;
}
gcc_assert (ok); // _1: return ok;
}
static void
reactivate_decl (tree decl, cp_binding_level *b)
{
bool in_function_p = TREE_CODE (b->this_entity) == FUNCTION_DECL;
gcc_assert (in_function_p
|| (b == current_binding_level
&& !at_class_scope_p ()));
tree id = DECL_NAME (decl);
tree type = NULL_TREE;
if (TREE_CODE (decl) == TYPE_DECL)
type = TREE_TYPE (decl);
if (type && TYPE_NAME (type) == decl
&& (RECORD_OR_UNION_CODE_P (TREE_CODE (type))
|| TREE_CODE (type) == ENUMERAL_TYPE))
{
gcc_assert (in_function_p && DECL_CONTEXT (decl) == b->this_entity);
type = TREE_TYPE (decl);
}
else
{
gcc_assert (DECL_CONTEXT (decl) == b->this_entity
|| DECL_CONTEXT (decl) == global_namespace
|| TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL);
type = NULL_TREE;
}
/* Adjust IDENTIFIER_BINDING to what it would have been if we were
at binding level B. Save the binding chain up to that point in
[binding, *chainp), and take note of the outermost bindings found
before B. */
cxx_binding *binding = IDENTIFIER_BINDING (id), **chainp = NULL;
tree *shadowing_type_p = NULL;
if (binding)
{
cp_binding_level *bc = current_binding_level;
for (cxx_binding *prev_binding = binding;
prev_binding; prev_binding = prev_binding->previous)
{
while (bc != b && bc != prev_binding->scope)
bc = bc->level_chain;
if (bc == b)
{
if (!chainp)
binding = NULL;
break;
}
chainp = &prev_binding->previous;
if (type)
for (tree tshadow = prev_binding->scope->type_shadowed;
tshadow; tshadow = TREE_CHAIN (tshadow))
if (TREE_PURPOSE (tshadow) == id)
{
shadowing_type_p = &TREE_VALUE (tshadow);
break;
}
}
}
if (chainp)
{
IDENTIFIER_BINDING (id) = *chainp;
*chainp = NULL;
}
/* Like push_local_binding, supplement or add a binding to the
desired level. */
if (IDENTIFIER_BINDING (id) && IDENTIFIER_BINDING (id)->scope == b)
supplement_binding (IDENTIFIER_BINDING (id), decl);
else
push_binding (id, decl, b);
/* Now restore the binding chain we'd temporarily removed. */
if (chainp)
{
*chainp = IDENTIFIER_BINDING (id);
IDENTIFIER_BINDING (id) = binding;
if (type)
{
/* Insert the new type binding in the shadowing_type_p
TREE_VALUE chain. */
tree shadowed_type = NULL_TREE;
if (shadowing_type_p)
{
shadowed_type = *shadowing_type_p;
*shadowing_type_p = type;
}
b->type_shadowed = tree_cons (id, shadowed_type, b->type_shadowed);
TREE_TYPE (b->type_shadowed) = type;
}
}
else if (type)
{
/* Our new binding is the active one, so shadow the earlier
binding. */
b->type_shadowed = tree_cons (id, REAL_IDENTIFIER_TYPE_VALUE (id),
b->type_shadowed);
TREE_TYPE (b->type_shadowed) = type;
SET_IDENTIFIER_TYPE_VALUE (id, type);
}
/* Record that we have a binding for ID, like add_decl_to_level. */
tree node = build_tree_list (NULL_TREE, decl);
TREE_CHAIN (node) = b->names;
b->names = node;
}
static void
plugin_pragma_push_user_expression (cpp_reader *)
{
if (push_count++)
return;
gcc_assert (!current_class_ptr);
gcc_assert (!current_class_ref);
gcc_assert (!cp_binding_oracle);
cp_binding_oracle = plugin_binding_oracle;
/* Make the function containing the user expression a global
friend, so as to bypass access controls in it. */
if (at_function_scope_p ())
set_global_friend (current_function_decl);
gcc_assert (at_function_scope_p ());
function *save_cfun = cfun;
cp_binding_level *orig_binding_level = current_binding_level;
{
int success;
cc1_plugin::call (current_context, "enter_scope", &success);
}
gcc_assert (at_fake_function_scope_p () || at_function_scope_p ());
function *unchanged_cfun = cfun;
tree changed_func_decl = current_function_decl;
gcc_assert (current_class_type == DECL_CONTEXT (current_function_decl)
|| !(RECORD_OR_UNION_CODE_P
(TREE_CODE (DECL_CONTEXT (current_function_decl)))));
push_fake_function (save_cfun->decl, sk_block);
current_class_type = NULL_TREE;
if (unchanged_cfun)
{
/* If we get here, GDB did NOT change the context. */
gcc_assert (cfun == save_cfun);
gcc_assert (at_function_scope_p ());
gcc_assert (orig_binding_level
== current_binding_level->level_chain->level_chain);
}
else
{
cfun = save_cfun;
gcc_assert (at_function_scope_p ());
cp_binding_level *b = current_binding_level->level_chain;
gcc_assert (b->this_entity == cfun->decl);
/* Reactivate local names from the previous context. Use
IDENTIFIER_MARKED to avoid reactivating shadowed names. */
for (cp_binding_level *level = orig_binding_level;;)
{
for (tree name = level->names;
name; name = TREE_CHAIN (name))
{
tree decl = name;
if (TREE_CODE (decl) == TREE_LIST)
decl = TREE_VALUE (decl);
if (IDENTIFIER_MARKED (DECL_NAME (decl)))
continue;
IDENTIFIER_MARKED (DECL_NAME (decl)) = 1;
reactivate_decl (decl, b);
}
if (level->kind == sk_function_parms
&& level->this_entity == cfun->decl)
break;
gcc_assert (!level->this_entity);
level = level->level_chain;
}
/* Now, clear the markers. */
for (tree name = b->names; name; name = TREE_CHAIN (name))
{
tree decl = name;
if (TREE_CODE (decl) == TREE_LIST)
decl = TREE_VALUE (decl);
gcc_assert (IDENTIFIER_MARKED (DECL_NAME (decl)));
IDENTIFIER_MARKED (DECL_NAME (decl)) = 0;
}
}
if (unchanged_cfun || DECL_NONSTATIC_MEMBER_FUNCTION_P (changed_func_decl))
{
/* Check whether the oracle supplies us with a "this", and if
so, arrange for data members and this itself to be
usable. */
tree this_val = lookup_name (get_identifier ("this"));
current_class_ref = !this_val ? NULL_TREE
: cp_build_indirect_ref (input_location, this_val, RO_NULL,
tf_warning_or_error);
current_class_ptr = this_val;
}
}
static void
plugin_pragma_pop_user_expression (cpp_reader *)
{
if (--push_count)
return;
gcc_assert (cp_binding_oracle);
gcc_assert (at_function_scope_p ());
function *save_cfun = cfun;
current_class_ptr = NULL_TREE;
current_class_ref = NULL_TREE;
cfun = NULL;
pop_scope ();
if (RECORD_OR_UNION_CODE_P (TREE_CODE (DECL_CONTEXT (current_function_decl))))
current_class_type = DECL_CONTEXT (current_function_decl);
{
int success;
cc1_plugin::call (current_context, "leave_scope", &success);
}
if (!cfun)
cfun = save_cfun;
else
gcc_assert (cfun == save_cfun);
cp_binding_oracle = NULL;
gcc_assert (at_function_scope_p ());
}
static void
plugin_init_extra_pragmas (void *, void *)
{
c_register_pragma ("GCC", "push_user_expression", plugin_pragma_push_user_expression);
c_register_pragma ("GCC", "pop_user_expression", plugin_pragma_pop_user_expression);
/* FIXME: this one should go once we get GDB to use push and pop. */
c_register_pragma ("GCC", "user_expression", plugin_pragma_push_user_expression);
}
static decl_addr_value
build_decl_addr_value (tree decl, gcc_address address)
{
decl_addr_value value = {
decl,
build_int_cst_type (ptr_type_node, address)
};
return value;
}
static decl_addr_value *
record_decl_address (plugin_context *ctx, decl_addr_value value)
{
decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
gcc_assert (*slot == NULL);
*slot
= static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
**slot = value;
/* We don't want GCC to warn about e.g. static functions
without a code definition. */
TREE_NO_WARNING (value.decl) = 1;
return *slot;
}
// Maybe rewrite a decl to its address.
static tree
address_rewriter (tree *in, int *walk_subtrees, void *arg)
{
plugin_context *ctx = (plugin_context *) arg;
if (!DECL_P (*in)
|| TREE_CODE (*in) == NAMESPACE_DECL
|| DECL_NAME (*in) == NULL_TREE)
return NULL_TREE;
decl_addr_value value;
value.decl = *in;
decl_addr_value *found_value = ctx->address_map.find (&value);
if (found_value != NULL)
;
else if (HAS_DECL_ASSEMBLER_NAME_P (*in))
{
gcc_address address;
if (!cc1_plugin::call (ctx, "address_oracle", &address,
IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (*in))))
return NULL_TREE;
if (address == 0)
return NULL_TREE;
// Insert the decl into the address map in case it is referenced
// again.
value = build_decl_addr_value (value.decl, address);
found_value = record_decl_address (ctx, value);
}
else
return NULL_TREE;
if (found_value->address != error_mark_node)
{
// We have an address for the decl, so rewrite the tree.
tree ptr_type = build_pointer_type (TREE_TYPE (*in));
*in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
fold_build1 (CONVERT_EXPR, ptr_type,
found_value->address));
}
*walk_subtrees = 0;
return NULL_TREE;
}
// When generating code for gdb, we want to be able to use absolute
// addresses to refer to otherwise external objects that gdb knows
// about. gdb passes in these addresses when building decls, and then
// before gimplification we go through the trees, rewriting uses to
// the equivalent of "*(TYPE *) ADDR".
static void
rewrite_decls_to_addresses (void *function_in, void *)
{
tree function = (tree) function_in;
// Do nothing if we're not in gdb.
if (current_context == NULL)
return;
walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
NULL);
}
static inline tree
safe_push_template_decl (tree decl)
{
void (*save_oracle) (enum cp_oracle_request, tree identifier);
save_oracle = cp_binding_oracle;
cp_binding_oracle = NULL;
tree ret = push_template_decl (decl);
cp_binding_oracle = save_oracle;
return ret;
}
static inline tree
safe_pushtag (tree name, tree type, tag_scope scope)
{
void (*save_oracle) (enum cp_oracle_request, tree identifier);
save_oracle = cp_binding_oracle;
cp_binding_oracle = NULL;
tree ret = pushtag (name, type, scope);
cp_binding_oracle = save_oracle;
return ret;
}
static inline tree
safe_pushdecl_maybe_friend (tree decl, bool is_friend)
{
void (*save_oracle) (enum cp_oracle_request, tree identifier);
save_oracle = cp_binding_oracle;
cp_binding_oracle = NULL;
tree ret = pushdecl (decl, is_friend);
cp_binding_oracle = save_oracle;
return ret;
}
int
plugin_push_namespace (cc1_plugin::connection *,
const char *name)
{
if (name && !*name)
push_to_top_level ();
else
push_namespace (name ? get_identifier (name) : NULL);
return 1;
}
int
plugin_push_class (cc1_plugin::connection *,
gcc_type type_in)
{
tree type = convert_in (type_in);
gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (type)));
gcc_assert (TYPE_CONTEXT (type) == FROB_CONTEXT (current_scope ()));
pushclass (type);
return 1;
}
int
plugin_push_function (cc1_plugin::connection *,
gcc_decl function_decl_in)
{
tree fndecl = convert_in (function_decl_in);
gcc_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
gcc_assert (DECL_CONTEXT (fndecl) == FROB_CONTEXT (current_scope ()));
push_fake_function (fndecl);
return 1;
}
int
plugin_pop_binding_level (cc1_plugin::connection *)
{
pop_scope ();
return 1;
}
int
plugin_reactivate_decl (cc1_plugin::connection *,
gcc_decl decl_in,
gcc_decl scope_in)
{
tree decl = convert_in (decl_in);
tree scope = convert_in (scope_in);
gcc_assert (TREE_CODE (decl) == VAR_DECL
|| TREE_CODE (decl) == FUNCTION_DECL
|| TREE_CODE (decl) == TYPE_DECL);
cp_binding_level *b;
if (scope)
{
gcc_assert (TREE_CODE (scope) == FUNCTION_DECL);
for (b = current_binding_level;
b->this_entity != scope;
b = b->level_chain)
gcc_assert (b->this_entity != global_namespace);
}
else
{
gcc_assert (!at_class_scope_p ());
b = current_binding_level;
}
reactivate_decl (decl, b);
return 1;
}
static tree
get_current_scope ()
{
tree decl;
if (at_namespace_scope_p ())
decl = current_namespace;
else if (at_class_scope_p ())
decl = TYPE_NAME (current_class_type);
else if (at_fake_function_scope_p () || at_function_scope_p ())
decl = current_function_decl;
else
gcc_unreachable ();
return decl;
}
gcc_decl
plugin_get_current_binding_level_decl (cc1_plugin::connection *)
{
tree decl = get_current_scope ();
return convert_out (decl);
}
int
plugin_make_namespace_inline (cc1_plugin::connection *)
{
tree inline_ns = current_namespace;
gcc_assert (toplevel_bindings_p ());
gcc_assert (inline_ns != global_namespace);
tree parent_ns = CP_DECL_CONTEXT (inline_ns);
if (DECL_NAMESPACE_INLINE_P (inline_ns))
return 0;
DECL_NAMESPACE_INLINE_P (inline_ns) = true;
vec_safe_push (DECL_NAMESPACE_INLINEES (parent_ns), inline_ns);
return 1;
}
int
plugin_add_using_namespace (cc1_plugin::connection *,
gcc_decl used_ns_in)
{
tree used_ns = convert_in (used_ns_in);
gcc_assert (TREE_CODE (used_ns) == NAMESPACE_DECL);
finish_using_directive (used_ns, NULL_TREE);
return 1;
}
int
plugin_add_namespace_alias (cc1_plugin::connection *,
const char *id,
gcc_decl target_in)
{
tree name = get_identifier (id);
tree target = convert_in (target_in);
do_namespace_alias (name, target);
return 1;
}
static inline void
set_access_flags (tree decl, enum gcc_cp_symbol_kind flags)
{
gcc_assert (!(flags & GCC_CP_ACCESS_MASK) == !DECL_CLASS_SCOPE_P (decl));
switch (flags & GCC_CP_ACCESS_MASK)
{
case GCC_CP_ACCESS_PRIVATE:
TREE_PRIVATE (decl) = true;
current_access_specifier = access_private_node;
break;
case GCC_CP_ACCESS_PROTECTED:
TREE_PROTECTED (decl) = true;
current_access_specifier = access_protected_node;
break;
case GCC_CP_ACCESS_PUBLIC:
current_access_specifier = access_public_node;
break;
default:
break;
}
}
int
plugin_add_using_decl (cc1_plugin::connection *,
enum gcc_cp_symbol_kind flags,
gcc_decl target_in)
{
tree target = convert_in (target_in);
gcc_assert ((flags & GCC_CP_SYMBOL_MASK) == GCC_CP_SYMBOL_USING);
gcc_assert (!(flags & GCC_CP_FLAG_MASK));
enum gcc_cp_symbol_kind acc_flags;
acc_flags = (enum gcc_cp_symbol_kind) (flags & GCC_CP_ACCESS_MASK);
gcc_assert (!template_parm_scope_p ());