39 original_scope(nullptr)
48 for(
const auto &id_ptr : id_set)
50 const cpp_idt &identifier = *id_ptr;
58 identifiers.push_back(e);
69 old_identifiers.swap(identifiers);
71 for(
const auto &old_id : old_identifiers)
81 identifiers.push_back(e);
92 old_identifiers.swap(identifiers);
94 for(
const auto &old_id : old_identifiers)
100 assert(e.
id()!=ID_type);
101 identifiers.push_back(e);
108 if(identifiers.size()==1)
111 exprt e=*identifiers.begin();
112 assert(e.
id()==ID_template_function_instance);
114 const symbolt &template_symbol=
130 identifiers.push_back(
139 old_identifiers.swap(identifiers);
141 for(
const auto &old_id : old_identifiers)
144 identifiers.push_back(old_id);
152 old_identifiers.swap(identifiers);
154 std::set<irep_idt> ids;
155 std::set<exprt> other;
157 for(
const auto &old_id : old_identifiers)
161 if(old_id.id() == ID_symbol)
163 else if(old_id.id() == ID_type && old_id.type().id() == ID_symbol_type)
165 else if(old_id.id() == ID_type && old_id.type().id() == ID_struct_tag)
167 else if(old_id.id() == ID_type && old_id.type().id() == ID_union_tag)
172 if(other.insert(old_id).second)
173 identifiers.push_back(old_id);
177 if(ids.insert(
id).second)
178 identifiers.push_back(old_id);
187 std::cout <<
"RESOLVE MAP:" << std::endl;
199 <<
"without instance:\n"
224 const symbolt &compound_symbol=
227 assert(compound_symbol.
type.
id()==ID_struct ||
228 compound_symbol.
type.
id()==ID_union);
258 this_class_identifier <<
'\n';
261 const exprt &this_expr=
273 assert(this_expr.
type().
id()==ID_pointer);
275 object.copy_to_operands(this_expr);
276 object.type().set(ID_C_constant,
278 object.set(ID_C_lvalue,
true);
285 if(object_type.
id()==ID_struct ||
286 object_type.
id()==ID_union)
297 if(
object.is_not_nil())
336 else if(symbol.
type.
id()==ID_c_enum)
353 bool constant=followed_type.
get_bool(ID_C_constant);
355 while(followed_type.
id() == ID_symbol_type)
359 constant |= followed_type.
get_bool(ID_C_constant);
365 symbol.
value.
id() == ID_constant)
386 old_identifiers.swap(identifiers);
388 for(
const auto &old_id : old_identifiers)
395 match = (old_id.id() == ID_type);
399 match = (old_id.id() != ID_type);
411 identifiers.push_back(old_id);
423 old_identifiers.swap(identifiers);
428 for(
const auto &old_id : old_identifiers)
433 identifiers.push_back(old_id);
442 old_identifiers.swap(identifiers);
445 std::multimap<std::size_t, exprt> distance_map;
447 for(
const auto &old_id : old_identifiers)
449 unsigned args_distance;
453 std::size_t template_distance=0;
455 if(old_id.type().get(ID_C_template) !=
"")
456 template_distance = old_id.type()
457 .find(ID_C_template_arguments)
464 std::size_t total_distance=
466 1000*template_distance+args_distance;
468 distance_map.insert({total_distance, old_id});
475 if(!distance_map.empty())
477 std::size_t distance=distance_map.begin()->first;
479 for(std::multimap<std::size_t, exprt>::const_iterator
480 it=distance_map.begin();
481 it!=distance_map.end() && it->first==distance;
483 identifiers.push_back(it->second);
486 if(identifiers.size()>1 && fargs.
in_use)
490 for(resolve_identifierst::iterator
491 it1=identifiers.begin();
492 it1!=identifiers.end();
495 if(it1->type().id()!=ID_code)
501 for(resolve_identifierst::iterator it2=
503 it2!=identifiers.end();
512 if(it2->type().id()!=ID_code)
528 i<f1.
parameters().size() && (f1_better || f2_better);
540 if(type1.
id()==ID_pointer)
546 if(type2.
id()==ID_pointer)
555 if(followed1.
id() != ID_struct || followed2.
id() != ID_struct)
571 resolve_identifierst::iterator prev_it=it2;
574 if(f1_better && !f2_better)
575 identifiers.erase(prev_it);
586 for(
const auto &identifier : identifiers)
588 if(identifier.id() != ID_type)
591 new_identifiers.push_back(identifier);
606 exprt pod_constructor1(ID_pod_constructor, t1);
607 new_identifiers.push_back(pod_constructor1);
614 exprt pod_constructor2(ID_pod_constructor, t2);
615 new_identifiers.push_back(pod_constructor2);
619 if(symbol_type.
id()==ID_c_enum_tag)
623 exprt pod_constructor3(ID_pod_constructor, t3);
624 new_identifiers.push_back(pod_constructor3);
627 else if(symbol_type.
id()==ID_struct)
640 type.
id() == ID_code &&
641 to_code_type(type).return_type().
id() == ID_constructor)
647 new_identifiers.push_back(e);
653 identifiers.
swap(new_identifiers);
660 if(argument.
id() == ID_ambiguous)
680 if(base_name==ID_unsignedbv ||
681 base_name==ID_signedbv)
683 if(arguments.size()!=1)
687 << base_name <<
" expects one template argument, but got "
692 exprt argument=arguments.front();
694 if(argument.
id()==ID_type)
698 << base_name <<
" expects one integer template argument, "
718 <<
"template argument must be greater than zero"
726 else if(base_name==ID_fixedbv)
728 if(arguments.size()!=2)
732 << base_name <<
" expects two template arguments, but got "
737 exprt argument0=arguments[0];
739 exprt argument1=arguments[1];
742 if(argument0.
id()==ID_type)
746 << base_name <<
" expects two integer template arguments, "
751 if(argument1.
id()==ID_type)
755 << base_name <<
" expects two integer template arguments, "
782 <<
"template argument must be greater than zero"
791 <<
"template argument must be greater or equal zero"
796 if(integer_bits>width)
800 <<
"template argument must be smaller or equal width"
809 else if(base_name==ID_integer)
811 if(!arguments.empty())
815 << base_name <<
" expects no template arguments"
827 else if(base_name==
"dump_scopes")
835 else if(base_name==
"current_scope")
842 else if(base_name == ID_size_t)
846 else if(base_name == ID_ssize_t)
869 assert(!cpp_name.
get_sub().empty());
874 irept::subt::const_iterator
pos=cpp_name.
get_sub().begin();
886 std::string final_base_name=
"";
891 if(
pos->id()==ID_name)
892 final_base_name+=
pos->get_string(ID_identifier);
893 else if(
pos->id()==ID_template_args)
895 else if(
pos->id()==
"::")
909 std::cout <<
"X: " << id_set.size() <<
'\n';
940 else if(id_set.size()>=2)
945 << final_base_name <<
"' is ambiguous"
950 assert(id_set.size()==1);
964 final_base_name.clear();
966 else if(
pos->id()==ID_operator)
968 final_base_name+=
"operator";
970 irept::subt::const_iterator next=
pos+1;
971 assert(next != cpp_name.
get_sub().end());
974 next->id() == ID_cpp_name || next->id() == ID_pointer ||
975 next->id() == ID_int || next->id() == ID_char ||
976 next->id() == ID_c_bool || next->id() == ID_merged_type)
981 op_name.
swap(next_ir);
988 final_base_name+=
pos->id_string();
993 base_name=final_base_name;
1013 std::set<irep_idt> primary_templates;
1015 for(
const auto &id_ptr : id_set)
1017 const irep_idt id = id_ptr->identifier;
1025 if(specialization_of!=
"")
1026 primary_templates.insert(specialization_of);
1028 primary_templates.insert(
id);
1031 assert(!primary_templates.empty());
1033 if(primary_templates.size()>=2)
1042 const symbolt &primary_template_symbol=
1055 full_template_args_tc=
1058 primary_template_symbol,
1059 full_template_args);
1065 std::vector<matcht> matches;
1069 matcht(full_template_args_tc, full_template_args_tc,
1070 primary_template_symbol.
name));
1072 for(
const auto &id_ptr : id_set)
1074 const irep_idt id = id_ptr->identifier;
1095 assert(full_template_args_tc.
arguments().size()==
1096 partial_specialization_args.
arguments().size());
1104 if(template_scope==
nullptr)
1108 <<
"class template instantiation error"
1116 for(std::size_t i=0; i<full_template_args_tc.
arguments().size(); i++)
1118 if(full_template_args_tc.
arguments()[i].id()==ID_type)
1120 full_template_args_tc.
arguments()[i].type());
1139 primary_template_symbol,
1140 partial_specialization_args);
1144 assert(partial_specialization_args_tc.
arguments().size()==
1145 full_template_args_tc.
arguments().size());
1147 if(partial_specialization_args_tc==
1148 full_template_args_tc)
1150 matches.push_back(
matcht(
1151 guessed_template_args, full_template_args_tc,
id));
1156 assert(!matches.empty());
1158 std::sort(matches.begin(), matches.end());
1161 for(std::vector<matcht>::const_iterator
1162 m_it=matches.begin();
1163 m_it!=matches.end();
1166 std::cout <<
"M: " << m_it->cost
1167 <<
" " << m_it->id <<
'\n';
1173 const matcht &match=*matches.begin();
1187 if(instance.
type.
id()!=ID_struct &&
1188 instance.
type.
id()!=ID_incomplete_struct)
1192 << base_name <<
"' is not a class";
1242 else if(id_set.size()==1)
1262 for(
const auto &id_expr : identifiers)
1266 if(id_expr.id()==ID_type)
1274 if(id_expr.type().get_bool(ID_is_template))
1277 if(id_expr.id()==ID_member)
1282 else if(id_expr.id() == ID_pod_constructor)
1284 out <<
"constructor ";
1287 else if(id_expr.id()==ID_template_function_instance)
1297 if(id_expr.type().get_bool(ID_is_template))
1300 else if(id_expr.type().id()==ID_code)
1306 out <<
" " <<
id <<
"(";
1310 for(
const auto ¶meter : parameters)
1312 const typet ¶meter_type = parameter.type();
1324 if(!parameters.empty())
1334 if(id_expr.id()==ID_symbol)
1337 out <<
" (" << symbol.
location <<
")";
1339 else if(id_expr.id()==ID_template_function_instance)
1343 out <<
" (" << symbol.
location <<
")";
1355 bool fail_with_exception)
1368 std::cout <<
"base name: " << base_name << std::endl;
1369 std::cout <<
"template args: " << template_args.
pretty() << std::endl;
1371 std::cout <<
"scope: "
1381 return do_builtin(base_name, fargs, template_args);
1385 if(base_name==
"__func__" ||
1386 base_name==
"__FUNCTION__" ||
1387 base_name==
"__PRETTY_FUNCTION__")
1394 return std::move(s);
1403 if(template_args.
is_nil())
1415 id_set.insert(&builtin_id);
1431 if(!fail_with_exception)
1441 << base_name <<
"' not found";
1454 << base_name <<
"' is unknown";
1469 bool have_classes=
false, have_methods=
false;
1471 for(
const auto &id_ptr : id_set)
1473 const irep_idt id = id_ptr->identifier;
1482 if(want==
wantt::BOTH && have_classes && have_methods)
1484 if(!fail_with_exception)
1490 <<
"template symbol `"
1502 identifiers.push_back(
exprt(ID_type, instance));
1508 id_set, fargs, identifiers);
1511 identifiers, template_args, fargs);
1517 id_set, fargs, identifiers);
1524 filter(identifiers, want);
1527 std::cout <<
"P0 " << base_name <<
" " << identifiers.
size() <<
"\n";
1540 std::cout <<
"P1 " << base_name <<
" " << new_identifiers.
size() <<
"\n";
1549 std::cout <<
"P2 " << base_name <<
" " << new_identifiers.
size() <<
"\n";
1555 if(new_identifiers.empty())
1557 new_identifiers=identifiers;
1559 if(template_args.
is_nil())
1563 if(new_identifiers.empty())
1564 new_identifiers=identifiers;
1570 std::cout <<
"P3 " << base_name <<
" " << new_identifiers.
size() <<
"\n";
1579 std::cout <<
"P4 " << base_name <<
" " << new_identifiers.
size() <<
"\n";
1584 if(new_identifiers.size()==1)
1586 result=*new_identifiers.begin();
1591 if(!fail_with_exception)
1594 if(new_identifiers.empty())
1598 <<
"found no match for symbol `" << base_name
1599 <<
"', candidates are:\n";
1606 <<
"symbol `" << base_name
1607 <<
"' does not uniquely resolve:\n";
1611 exprt e1=*new_identifiers.begin();
1612 exprt e2=*(++new_identifiers.begin());
1619 << (e1.
get(ID_identifier)==e2.
get(ID_identifier))
1630 for(
const auto &op : fargs.
operands)
1647 if(result.
get_bool(ID_C_not_accessible))
1650 if(!fail_with_exception)
1655 <<
"error: member `" << result.
get(ID_component_name)
1656 <<
"' is not accessible";
1666 if(!fail_with_exception)
1672 <<
"error: expected expression, but got type `"
1681 if(result.
id()!=ID_type)
1683 if(!fail_with_exception)
1689 <<
"error: expected type, but got expression `"
1705 const exprt &template_expr,
1706 const exprt &desired_expr)
1708 if(template_expr.
id()==ID_cpp_name)
1725 for(
const auto &id_ptr : id_set)
1733 if(e.
id()==ID_unassigned)
1737 if(e.
type()!=old_type)
1747 const typet &template_type,
1748 const typet &desired_type)
1781 std::cout <<
"TT: " << template_type.
pretty() <<
'\n';
1782 std::cout <<
"DT: " << desired_type.
pretty() <<
'\n';
1785 if(template_type.
id()==ID_cpp_name)
1812 for(
const auto &id_ptr : id_set)
1821 if(t.
id()==ID_unassigned)
1829 std::cout <<
"ASSIGN " <<
id.identifier <<
" := "
1838 else if(template_type.
id()==ID_merged_type)
1851 else if(template_type.
id()==ID_pointer)
1853 const typet &desired_type_followed=
1856 if(desired_type_followed.
id()==ID_pointer)
1860 else if(template_type.
id()==ID_array)
1862 const typet &desired_type_followed=
1865 if(desired_type_followed.
id()==ID_array)
1870 desired_type_followed.
subtype());
1890 assert(expr.
id()==ID_symbol);
1910 const symbolt &template_symbol=
1927 if(function_declarator.
type().
id()!=ID_function_type)
1931 <<
"expected function type for function template"
1943 if(template_scope==
nullptr)
1947 << template_identifier <<
'\n'
1948 <<
"function template instantiation error"
1960 exprt::operandst::const_iterator it=fargs.
operands.begin();
1961 for(
const auto ¶meter : parameters)
1966 if(parameter.id()==ID_cpp_declaration)
1977 merge_type(arg_declaration.
type());
2001 typet function_type=
2008 function_type.
set(ID_C_template, template_symbol.
name);
2009 function_type.
set(ID_C_template_arguments, template_args);
2013 exprt template_function_instance(
2014 ID_template_function_instance, function_type);
2016 return template_function_instance;
2024 if(expr.
id()!=ID_symbol)
2027 const symbolt &template_symbol =
2034 if(template_args_non_tc.
is_nil())
2055 template_args_non_tc);
2094 code_type.
parameters()[0].get(ID_C_base_name)==ID_this)
2101 fargs.
operands.begin()->type().get(ID_identifier));
2103 assert(type_symb.
type.
id()==ID_struct);
2109 "method should exist in struct");
2128 unsigned &args_distance,
2138 if(expr.
id()==ID_member ||
2148 assert(parameter.
get(ID_C_base_name)==ID_this);
2155 object.set(ID_C_lvalue,
true);
2165 fargs.
operands.size() == parameters.size())
2197 for(
const auto &id_ptr : id_set)
2201 if(
id.is_class() ||
id.is_enum() ||
id.is_namespace())
2204 assert(
id.is_scope);
2205 new_set.insert(&
id);
2207 else if(
id.is_typedef())
2219 assert(pcomp.
get_bool(ID_is_type));
2221 assert(type.
id()!=ID_struct);
2223 if(type.
id() == ID_symbol_type)
2235 if(symbol.
type.
id()==ID_struct)
2242 new_set.insert(&class_id);
2245 else if(symbol.
type.
id() == ID_symbol_type)
2259 if(symbol.
type.
id() == ID_struct)
2261 id.print(std::cout);
2262 assert(
id.is_scope);
2263 new_set.insert(&
id);
2282 std::cout <<
"E: " << e.
pretty() <<
'\n';
2288 if(e.
type().
id() == ID_symbol_type)
2299 if(symbol.
type.
id() == ID_symbol_type)
2301 else if(symbol.
type.
id()==ID_struct ||
2302 symbol.
type.
id()==ID_incomplete_struct ||
2303 symbol.
type.
id()==ID_union ||
2304 symbol.
type.
id()==ID_incomplete_union ||
2305 symbol.
type.
id()==ID_c_enum)
2312 new_set.insert(&class_id);
2322 id_set.swap(new_set);
2329 for(cpp_scopest::id_sett::iterator
2334 if((*it)->is_namespace())
2338 cpp_scopest::id_sett::iterator old(it);
2351 for(
const auto &arg : fargs.
operands)
2355 if(final_type.
id()!=ID_struct && final_type.
id()!=ID_union)
2361 id_set.insert(tmp_set.begin(), tmp_set.end());