-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathcpp_typecheck_resolve.cpp
More file actions
2367 lines (1977 loc) · 63.2 KB
/
cpp_typecheck_resolve.cpp
File metadata and controls
2367 lines (1977 loc) · 63.2 KB
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
/*******************************************************************\
Module: C++ Language Type Checking
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// C++ Language Type Checking
#include "cpp_typecheck_resolve.h"
#ifdef DEBUG
#include <iostream>
#endif
#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/mathematical_types.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>
#include <util/string_constant.h>
#include <ansi-c/anonymous_member.h>
#include <ansi-c/merged_type.h>
#include "cpp_convert_type.h"
#include "cpp_type2name.h"
#include "cpp_typecheck.h"
#include "cpp_typecheck_fargs.h"
#include "cpp_util.h"
#include <algorithm>
cpp_typecheck_resolvet::cpp_typecheck_resolvet(cpp_typecheckt &_cpp_typecheck):
cpp_typecheck(_cpp_typecheck),
original_scope(nullptr) // set in resolve_scope()
{
}
void cpp_typecheck_resolvet::convert_identifiers(
const cpp_scopest::id_sett &id_set,
const cpp_typecheck_fargst &fargs,
resolve_identifierst &identifiers)
{
for(const auto &id_ptr : id_set)
{
const cpp_idt &identifier = *id_ptr;
exprt e=convert_identifier(identifier, fargs);
if(e.is_not_nil())
{
CHECK_RETURN(e.id() != ID_type || e.type().is_not_nil());
identifiers.push_back(e);
}
}
}
void cpp_typecheck_resolvet::apply_template_args(
resolve_identifierst &identifiers,
const cpp_template_args_non_tct &template_args,
const cpp_typecheck_fargst &fargs)
{
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
for(const auto &old_id : old_identifiers)
{
exprt e = old_id;
apply_template_args(e, template_args, fargs);
if(e.is_not_nil())
{
CHECK_RETURN(e.id() != ID_type || e.type().is_not_nil());
identifiers.push_back(e);
}
}
}
/// guess arguments of function templates
void cpp_typecheck_resolvet::guess_function_template_args(
resolve_identifierst &identifiers,
const cpp_typecheck_fargst &fargs)
{
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
for(const auto &old_id : old_identifiers)
{
exprt e = guess_function_template_args(old_id, fargs);
if(e.is_not_nil())
{
CHECK_RETURN(e.id() != ID_type);
identifiers.push_back(e);
}
}
disambiguate_functions(identifiers, fargs);
// there should only be one left, or we have failed to disambiguate
if(identifiers.size()==1)
{
// instantiate that one
exprt e=*identifiers.begin();
CHECK_RETURN(e.id() == ID_template_function_instance);
const symbolt &template_symbol=
cpp_typecheck.lookup(e.type().get(ID_C_template));
const cpp_template_args_tct &template_args=
to_cpp_template_args_tc(e.type().find(ID_C_template_arguments));
// Let's build the instance.
const symbolt &new_symbol=
cpp_typecheck.instantiate_template(
source_location,
template_symbol,
template_args,
template_args);
identifiers.clear();
identifiers.push_back(
symbol_exprt(new_symbol.name, new_symbol.type));
}
}
void cpp_typecheck_resolvet::remove_templates(
resolve_identifierst &identifiers)
{
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
for(const auto &old_id : old_identifiers)
{
const typet &followed =
old_id.type().id() == ID_struct_tag
? static_cast<const typet &>(
cpp_typecheck.follow_tag(to_struct_tag_type(old_id.type())))
: old_id.type().id() == ID_union_tag
? static_cast<const typet &>(
cpp_typecheck.follow_tag(to_union_tag_type(old_id.type())))
: old_id.type().id() == ID_c_enum_tag
? static_cast<const typet &>(
cpp_typecheck.follow_tag(to_c_enum_tag_type(old_id.type())))
: old_id.type();
if(!followed.get_bool(ID_is_template))
identifiers.push_back(old_id);
}
}
void cpp_typecheck_resolvet::remove_duplicates(
resolve_identifierst &identifiers)
{
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
std::set<irep_idt> ids;
std::set<exprt> other;
for(const auto &old_id : old_identifiers)
{
irep_idt id;
if(old_id.id() == ID_symbol)
id = to_symbol_expr(old_id).identifier();
else if(old_id.id() == ID_type && old_id.type().id() == ID_struct_tag)
id = to_struct_tag_type(old_id.type()).get_identifier();
else if(old_id.id() == ID_type && old_id.type().id() == ID_union_tag)
id = to_union_tag_type(old_id.type()).get_identifier();
if(id.empty())
{
if(other.insert(old_id).second)
identifiers.push_back(old_id);
}
else
{
if(ids.insert(id).second)
identifiers.push_back(old_id);
}
}
}
exprt cpp_typecheck_resolvet::convert_template_parameter(
const cpp_idt &identifier)
{
#ifdef DEBUG
std::cout << "RESOLVE MAP:\n";
cpp_typecheck.template_map.print(std::cout);
#endif
// look up the parameter in the template map
exprt e=cpp_typecheck.template_map.lookup(identifier.identifier);
if(e.is_nil() ||
(e.id()==ID_type && e.type().is_nil()))
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error() << "internal error: template parameter "
<< "without instance:\n"
<< identifier << messaget::eom;
throw 0;
}
e.add_source_location()=source_location;
return e;
}
exprt cpp_typecheck_resolvet::convert_identifier(
const cpp_idt &identifier,
const cpp_typecheck_fargst &fargs)
{
if(identifier.id_class==cpp_scopet::id_classt::TEMPLATE_PARAMETER)
return convert_template_parameter(identifier);
exprt e;
if(identifier.is_member &&
!identifier.is_constructor &&
!identifier.is_static_member)
{
// a regular struct or union member
const symbolt &compound_symbol=
cpp_typecheck.lookup(identifier.class_identifier);
CHECK_RETURN(
compound_symbol.type.id() == ID_struct ||
compound_symbol.type.id() == ID_union);
const struct_union_typet &struct_union_type=
to_struct_union_type(compound_symbol.type);
const exprt &component =
struct_union_type.get_component(identifier.identifier);
const typet &type=component.type();
DATA_INVARIANT(type.is_not_nil(), "type must not be nil");
if(identifier.id_class==cpp_scopet::id_classt::TYPEDEF)
{
e=type_exprt(type);
}
else if(identifier.id_class==cpp_scopet::id_classt::SYMBOL)
{
// A non-static, non-type member.
// There has to be an object.
e=exprt(ID_member);
e.set(ID_component_name, identifier.identifier);
e.add_source_location()=source_location;
exprt object;
object.make_nil();
#if 0
std::cout << "I: " << identifier.class_identifier
<< " "
<< cpp_typecheck.cpp_scopes.current_scope().
this_class_identifier << '\n';
#endif
const exprt &this_expr=
original_scope->this_expr;
if(fargs.has_object)
{
// the object is given to us in fargs
PRECONDITION(!fargs.operands.empty());
object=fargs.operands.front();
}
else if(this_expr.is_not_nil())
{
// use this->...
DATA_INVARIANT(
this_expr.type().id() == ID_pointer,
"this argument should be pointer");
object =
exprt(ID_dereference, to_pointer_type(this_expr.type()).base_type());
object.copy_to_operands(this_expr);
object.type().set(
ID_C_constant,
to_pointer_type(this_expr.type())
.base_type()
.get_bool(ID_C_constant));
object.set(ID_C_lvalue, true);
object.add_source_location()=source_location;
}
// check if the member can be applied to the object
if(
(object.type().id() != ID_struct_tag &&
object.type().id() != ID_union_tag) ||
!has_component_rec(object.type(), identifier.identifier, cpp_typecheck))
{
// failed
object.make_nil();
}
if(object.is_not_nil())
{
// we got an object
e.add_to_operands(std::move(object));
bool old_value=cpp_typecheck.disable_access_control;
cpp_typecheck.disable_access_control=true;
cpp_typecheck.typecheck_expr_member(e);
cpp_typecheck.disable_access_control=old_value;
}
else
{
// this has to be a method or form a pointer-to-member expression
if(identifier.is_method)
e=cpp_symbol_expr(cpp_typecheck.lookup(identifier.identifier));
else
{
e.id(ID_ptrmember);
tag_typet class_tag_type{
compound_symbol.type.id() == ID_struct ? ID_struct_tag
: ID_union_tag,
identifier.class_identifier};
e.copy_to_operands(exprt("cpp-this", pointer_type(class_tag_type)));
e.type() = type;
}
}
}
}
else
{
const symbolt &symbol=
cpp_typecheck.lookup(identifier.identifier);
if(symbol.is_type)
{
e.make_nil();
if(symbol.is_macro) // includes typedefs
{
e = type_exprt(symbol.type);
PRECONDITION(symbol.type.is_not_nil());
}
else if(symbol.type.id()==ID_c_enum)
{
e = type_exprt(c_enum_tag_typet(symbol.name));
}
else if(symbol.type.id() == ID_struct)
{
e = type_exprt(struct_tag_typet(symbol.name));
}
else if(symbol.type.id() == ID_union)
{
e = type_exprt(union_tag_typet(symbol.name));
}
}
else if(symbol.is_macro)
{
e=symbol.value;
PRECONDITION(e.is_not_nil());
}
else
{
bool constant = symbol.type.get_bool(ID_C_constant);
if(
constant && symbol.value.is_not_nil() && is_number(symbol.type) &&
symbol.value.is_constant())
{
e=symbol.value;
}
else
{
e=cpp_symbol_expr(symbol);
}
}
}
e.add_source_location()=source_location;
return e;
}
void cpp_typecheck_resolvet::filter(
resolve_identifierst &identifiers,
const wantt want)
{
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
for(const auto &old_id : old_identifiers)
{
bool match=false;
switch(want)
{
case wantt::TYPE:
match = (old_id.id() == ID_type);
break;
case wantt::VAR:
match = (old_id.id() != ID_type);
break;
case wantt::BOTH:
match=true;
break;
default:
UNREACHABLE;
}
if(match)
identifiers.push_back(old_id);
}
}
void cpp_typecheck_resolvet::exact_match_functions(
resolve_identifierst &identifiers,
const cpp_typecheck_fargst &fargs)
{
if(!fargs.in_use)
return;
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
identifiers.clear();
// put in the ones that match precisely
for(const auto &old_id : old_identifiers)
{
unsigned distance;
if(disambiguate_functions(old_id, distance, fargs))
if(distance<=0)
identifiers.push_back(old_id);
}
}
void cpp_typecheck_resolvet::disambiguate_functions(
resolve_identifierst &identifiers,
const cpp_typecheck_fargst &fargs)
{
resolve_identifierst old_identifiers;
old_identifiers.swap(identifiers);
// sort according to distance
std::multimap<std::size_t, exprt> distance_map;
for(const auto &old_id : old_identifiers)
{
unsigned args_distance;
if(disambiguate_functions(old_id, args_distance, fargs))
{
std::size_t template_distance=0;
if(!old_id.type().get(ID_C_template).empty())
template_distance = old_id.type()
.find(ID_C_template_arguments)
.find(ID_arguments)
.get_sub()
.size();
// we give strong preference to functions that have
// fewer template arguments
std::size_t total_distance=
// NOLINTNEXTLINE(whitespace/operators)
1000*template_distance+args_distance;
distance_map.insert({total_distance, old_id});
}
}
old_identifiers.clear();
// put in the top ones
if(!distance_map.empty())
{
auto range = distance_map.equal_range(distance_map.begin()->first);
for(auto it = range.first; it != range.second; ++it)
old_identifiers.push_back(it->second);
}
if(old_identifiers.size() > 1 && fargs.in_use)
{
// try to further disambiguate functions
for(resolve_identifierst::const_iterator old_it = old_identifiers.begin();
old_it != old_identifiers.end();
++old_it)
{
#if 0
std::cout << "I1: " << old_it->get(ID_identifier) << '\n';
#endif
if(old_it->type().id() != ID_code)
{
identifiers.push_back(*old_it);
continue;
}
const code_typet &f1 = to_code_type(old_it->type());
for(resolve_identifierst::const_iterator resolve_it = old_it + 1;
resolve_it != old_identifiers.end();
++resolve_it)
{
if(resolve_it->type().id() != ID_code)
continue;
const code_typet &f2 = to_code_type(resolve_it->type());
// TODO: may fail when using ellipsis
DATA_INVARIANT(
f1.parameters().size() == f2.parameters().size(),
"parameter numbers should match");
bool f1_better=true;
bool f2_better=true;
for(std::size_t i=0;
i<f1.parameters().size() && (f1_better || f2_better);
i++)
{
typet type1=f1.parameters()[i].type();
typet type2=f2.parameters()[i].type();
if(type1 == type2)
continue;
if(is_reference(type1) != is_reference(type2))
continue;
if(type1.id()==ID_pointer)
{
typet tmp = to_pointer_type(type1).base_type();
type1=tmp;
}
if(type2.id()==ID_pointer)
{
typet tmp = to_pointer_type(type2).base_type();
type2=tmp;
}
if(type1.id() != ID_struct_tag || type2.id() != ID_struct_tag)
continue;
if(
f1_better && cpp_typecheck.subtype_typecast(
cpp_typecheck.follow_tag(to_struct_tag_type(type1)),
cpp_typecheck.follow_tag(to_struct_tag_type(type2))))
{
f2_better=false;
}
else if(
f2_better && cpp_typecheck.subtype_typecast(
cpp_typecheck.follow_tag(to_struct_tag_type(type2)),
cpp_typecheck.follow_tag(to_struct_tag_type(type1))))
{
f1_better=false;
}
}
if(!f1_better || f2_better)
identifiers.push_back(*resolve_it);
}
}
}
else
{
identifiers.swap(old_identifiers);
}
remove_duplicates(identifiers);
}
void cpp_typecheck_resolvet::make_constructors(
resolve_identifierst &identifiers)
{
resolve_identifierst new_identifiers;
for(const auto &identifier : identifiers)
{
if(identifier.id() != ID_type)
{
// already an expression
new_identifiers.push_back(identifier);
continue;
}
// is it a POD?
if(cpp_typecheck.cpp_is_pod(identifier.type()))
{
// there are two pod constructors:
// 1. no arguments, default initialization
{
const code_typet t1({}, identifier.type());
exprt pod_constructor1(ID_pod_constructor, t1);
new_identifiers.push_back(pod_constructor1);
}
// 2. one argument, copy/conversion
{
const code_typet t2(
{code_typet::parametert(identifier.type())}, identifier.type());
exprt pod_constructor2(ID_pod_constructor, t2);
new_identifiers.push_back(pod_constructor2);
}
// enums, in addition, can also be constructed from int
if(identifier.type().id() == ID_c_enum_tag)
{
const code_typet t3(
{code_typet::parametert(signed_int_type())}, identifier.type());
exprt pod_constructor3(ID_pod_constructor, t3);
new_identifiers.push_back(pod_constructor3);
}
}
else if(identifier.type().id() == ID_struct_tag)
{
const struct_typet &struct_type =
cpp_typecheck.follow_tag(to_struct_tag_type(identifier.type()));
// go over components
for(const auto &component : struct_type.components())
{
const typet &type=component.type();
if(component.get_bool(ID_from_base))
continue;
if(
type.id() == ID_code &&
to_code_type(type).return_type().id() == ID_constructor)
{
const symbolt &symb =
cpp_typecheck.lookup(component.get_name());
exprt e=cpp_symbol_expr(symb);
e.type()=type;
new_identifiers.push_back(e);
}
}
}
}
identifiers.swap(new_identifiers);
}
void cpp_typecheck_resolvet::resolve_argument(
exprt &argument,
const cpp_typecheck_fargst &fargs)
{
if(argument.id() == ID_ambiguous) // could come from a template parameter
{
// this must be resolved in the template scope
cpp_save_scopet save_scope(cpp_typecheck.cpp_scopes);
cpp_typecheck.cpp_scopes.go_to(*original_scope);
argument = resolve(to_cpp_name(argument.type()), wantt::VAR, fargs, false);
}
}
exprt cpp_typecheck_resolvet::do_builtin(
const irep_idt &base_name,
const cpp_typecheck_fargst &fargs,
const cpp_template_args_non_tct &template_args)
{
exprt dest;
const cpp_template_args_non_tct::argumentst &arguments=
template_args.arguments();
if(base_name==ID_unsignedbv ||
base_name==ID_signedbv)
{
if(arguments.size()!=1)
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error()
<< base_name << " expects one template argument, but got "
<< arguments.size() << messaget::eom;
throw 0;
}
exprt argument=arguments.front(); // copy
if(argument.id()==ID_type)
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error()
<< base_name << " expects one integer template argument, "
<< "but got type" << messaget::eom;
throw 0;
}
resolve_argument(argument, fargs);
const auto i = numeric_cast<mp_integer>(argument);
if(!i.has_value())
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error() << "template argument must be constant"
<< messaget::eom;
throw 0;
}
if(*i < 1)
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error()
<< "template argument must be greater than zero"
<< messaget::eom;
throw 0;
}
dest=type_exprt(typet(base_name));
dest.type().set(ID_width, integer2string(*i));
}
else if(base_name==ID_fixedbv)
{
if(arguments.size()!=2)
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error()
<< base_name << " expects two template arguments, but got "
<< arguments.size() << messaget::eom;
throw 0;
}
exprt argument0=arguments[0];
resolve_argument(argument0, fargs);
exprt argument1=arguments[1];
resolve_argument(argument1, fargs);
if(argument0.id()==ID_type)
{
cpp_typecheck.error().source_location=argument0.find_source_location();
cpp_typecheck.error()
<< base_name << " expects two integer template arguments, "
<< "but got type" << messaget::eom;
throw 0;
}
if(argument1.id()==ID_type)
{
cpp_typecheck.error().source_location=argument1.find_source_location();
cpp_typecheck.error()
<< base_name << " expects two integer template arguments, "
<< "but got type" << messaget::eom;
throw 0;
}
const auto width = numeric_cast<mp_integer>(argument0);
if(!width.has_value())
{
cpp_typecheck.error().source_location=argument0.find_source_location();
cpp_typecheck.error() << "template argument must be constant"
<< messaget::eom;
throw 0;
}
const auto integer_bits = numeric_cast<mp_integer>(argument1);
if(!integer_bits.has_value())
{
cpp_typecheck.error().source_location=argument1.find_source_location();
cpp_typecheck.error() << "template argument must be constant"
<< messaget::eom;
throw 0;
}
if(*width < 1)
{
cpp_typecheck.error().source_location=argument0.find_source_location();
cpp_typecheck.error()
<< "template argument must be greater than zero"
<< messaget::eom;
throw 0;
}
if(*integer_bits < 0)
{
cpp_typecheck.error().source_location=argument1.find_source_location();
cpp_typecheck.error()
<< "template argument must be greater or equal zero"
<< messaget::eom;
throw 0;
}
if(*integer_bits > *width)
{
cpp_typecheck.error().source_location=argument1.find_source_location();
cpp_typecheck.error()
<< "template argument must be smaller or equal width"
<< messaget::eom;
throw 0;
}
dest=type_exprt(typet(base_name));
dest.type().set(ID_width, integer2string(*width));
dest.type().set(ID_integer_bits, integer2string(*integer_bits));
}
else if(base_name==ID_integer)
{
if(!arguments.empty())
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error()
<< base_name << " expects no template arguments"
<< messaget::eom;
throw 0;
}
dest=type_exprt(typet(base_name));
}
else if(base_name.starts_with("constant_infinity"))
{
// ok, but type missing
dest=exprt(ID_infinity, size_type());
}
else if(base_name=="dump_scopes")
{
dest=exprt(ID_constant, typet(ID_empty));
cpp_typecheck.warning() << "Scopes in location "
<< source_location << messaget::eom;
cpp_typecheck.cpp_scopes.get_root_scope().print(
cpp_typecheck.warning());
}
else if(base_name=="current_scope")
{
dest=exprt(ID_constant, typet(ID_empty));
cpp_typecheck.warning() << "Scope in location " << source_location
<< ": " << original_scope->prefix
<< messaget::eom;
}
else if(base_name == ID_size_t)
{
dest=type_exprt(size_type());
}
else if(base_name == ID_ssize_t)
{
dest=type_exprt(signed_size_type());
}
else
{
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error() << "unknown built-in identifier: "
<< base_name << messaget::eom;
throw 0;
}
return dest;
}
/// \par parameters: a cpp_name
/// \return a base_name, and potentially template arguments for the base name;
/// as side-effect, we got to the right scope
cpp_scopet &cpp_typecheck_resolvet::resolve_scope(
const cpp_namet &cpp_name,
irep_idt &base_name,
cpp_template_args_non_tct &template_args)
{
PRECONDITION(!cpp_name.get_sub().empty());
original_scope=&cpp_typecheck.cpp_scopes.current_scope();
source_location=cpp_name.source_location();
irept::subt::const_iterator pos=cpp_name.get_sub().begin();
bool recursive=true;
// check if we need to go to the root scope
if(pos->id()=="::")
{
pos++;
cpp_typecheck.cpp_scopes.go_to_root_scope();
recursive=false;
}
std::string final_base_name;
template_args.make_nil();
while(pos!=cpp_name.get_sub().end())
{
if(pos->id()==ID_name)
final_base_name+=pos->get_string(ID_identifier);
else if(pos->id()==ID_template_args)
template_args=to_cpp_template_args_non_tc(*pos);
else if(pos->id()=="::")
{
if(template_args.is_not_nil())
{
const auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
final_base_name,
recursive ? cpp_scopet::RECURSIVE : cpp_scopet::QUALIFIED,
cpp_idt::id_classt::TEMPLATE);
#ifdef DEBUG
std::cout << "S: "
<< cpp_typecheck.cpp_scopes.current_scope().identifier
<< '\n';
cpp_typecheck.cpp_scopes.current_scope().print(std::cout);
std::cout << "X: " << id_set.size() << '\n';
#endif
struct_tag_typet instance =
disambiguate_template_classes(final_base_name, id_set, template_args);
instance.add_source_location()=source_location;
// the "::" triggers template elaboration
cpp_typecheck.elaborate_class_template(instance);
cpp_typecheck.cpp_scopes.go_to(
cpp_typecheck.cpp_scopes.get_scope(instance.get_identifier()));
template_args.make_nil();
}
else
{
auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
final_base_name,
recursive ? cpp_scopet::RECURSIVE : cpp_scopet::QUALIFIED);
filter_for_named_scopes(id_set);
if(id_set.empty())
{
cpp_typecheck.show_instantiation_stack(cpp_typecheck.error());
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error()
<< "scope '" << final_base_name << "' not found" << messaget::eom;
throw 0;
}
else if(id_set.size()>=2)
{
cpp_typecheck.show_instantiation_stack(cpp_typecheck.error());
cpp_typecheck.error().source_location=source_location;
cpp_typecheck.error() << "scope '" << final_base_name
<< "' is ambiguous" << messaget::eom;
throw 0;
}
CHECK_RETURN(id_set.size() == 1);
cpp_typecheck.cpp_scopes.go_to(**id_set.begin());
// the "::" triggers template elaboration
if(!cpp_typecheck.cpp_scopes.current_scope().class_identifier.empty())
{
struct_tag_typet instance(
cpp_typecheck.cpp_scopes.current_scope().class_identifier);
cpp_typecheck.elaborate_class_template(instance);
}
}
// we start from fresh
final_base_name.clear();
}
else if(pos->id()==ID_operator)
{
final_base_name+="operator";
irept::subt::const_iterator next=pos+1;
CHECK_RETURN(next != cpp_name.get_sub().end());
if(
next->id() == ID_cpp_name || next->id() == ID_pointer ||
next->id() == ID_int || next->id() == ID_char ||
next->id() == ID_c_bool || next->id() == ID_merged_type)
{
// it's a cast operator
irept next_ir=*next;
typet op_name;
op_name.swap(next_ir);
cpp_typecheck.typecheck_type(op_name);
final_base_name+="("+cpp_type2name(op_name)+")";
pos++;
}
}
else
final_base_name+=pos->id_string();
pos++;
}
base_name=final_base_name;
return cpp_typecheck.cpp_scopes.current_scope();
}
/// disambiguate partial specialization
struct_tag_typet cpp_typecheck_resolvet::disambiguate_template_classes(