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_struct_tag)
165 else if(old_id.id() == ID_type && old_id.type().id() == ID_union_tag)
170 if(other.insert(old_id).second)
171 identifiers.push_back(old_id);
175 if(ids.insert(
id).second)
176 identifiers.push_back(old_id);
185 std::cout <<
"RESOLVE MAP:\n";
197 <<
"without instance:\n"
222 const symbolt &compound_symbol=
225 assert(compound_symbol.
type.
id()==ID_struct ||
226 compound_symbol.
type.
id()==ID_union);
256 this_class_identifier <<
'\n';
259 const exprt &this_expr=
271 assert(this_expr.
type().
id()==ID_pointer);
274 object.copy_to_operands(this_expr);
280 object.set(ID_C_lvalue,
true);
287 if(object_type.
id()==ID_struct ||
288 object_type.
id()==ID_union)
299 if(
object.is_not_nil())
338 else if(symbol.
type.
id()==ID_c_enum)
342 else if(symbol.
type.
id() == ID_struct)
346 else if(symbol.
type.
id() == ID_union)
362 symbol.
value.
id() == ID_constant)
383 old_identifiers.swap(identifiers);
385 for(
const auto &old_id : old_identifiers)
392 match = (old_id.id() == ID_type);
396 match = (old_id.id() != ID_type);
408 identifiers.push_back(old_id);
420 old_identifiers.swap(identifiers);
425 for(
const auto &old_id : old_identifiers)
430 identifiers.push_back(old_id);
439 old_identifiers.swap(identifiers);
442 std::multimap<std::size_t, exprt> distance_map;
444 for(
const auto &old_id : old_identifiers)
446 unsigned args_distance;
450 std::size_t template_distance=0;
452 if(!old_id.type().get(ID_C_template).empty())
453 template_distance = old_id.type()
454 .find(ID_C_template_arguments)
461 std::size_t total_distance=
463 1000*template_distance+args_distance;
465 distance_map.insert({total_distance, old_id});
469 old_identifiers.clear();
472 if(!distance_map.empty())
474 auto range = distance_map.equal_range(distance_map.begin()->first);
475 for(
auto it = range.first; it != range.second; ++it)
476 old_identifiers.push_back(it->second);
479 if(old_identifiers.size() > 1 && fargs.
in_use)
483 for(resolve_identifierst::const_iterator old_it = old_identifiers.begin();
484 old_it != old_identifiers.end();
488 std::cout <<
"I1: " << old_it->get(ID_identifier) <<
'\n';
491 if(old_it->type().id() != ID_code)
493 identifiers.push_back(*old_it);
499 for(resolve_identifierst::const_iterator resolve_it = old_it + 1;
500 resolve_it != old_identifiers.end();
503 if(resolve_it->type().id() != ID_code)
515 i<f1.
parameters().size() && (f1_better || f2_better);
527 if(type1.
id()==ID_pointer)
533 if(type2.
id()==ID_pointer)
542 if(followed1.
id() != ID_struct || followed2.
id() != ID_struct)
558 if(!f1_better || f2_better)
559 identifiers.push_back(*resolve_it);
565 identifiers.swap(old_identifiers);
576 for(
const auto &identifier : identifiers)
578 if(identifier.id() != ID_type)
581 new_identifiers.push_back(identifier);
596 exprt pod_constructor1(ID_pod_constructor, t1);
597 new_identifiers.push_back(pod_constructor1);
604 exprt pod_constructor2(ID_pod_constructor, t2);
605 new_identifiers.push_back(pod_constructor2);
609 if(symbol_type.
id()==ID_c_enum_tag)
613 exprt pod_constructor3(ID_pod_constructor, t3);
614 new_identifiers.push_back(pod_constructor3);
617 else if(symbol_type.
id()==ID_struct)
630 type.
id() == ID_code &&
631 to_code_type(type).return_type().
id() == ID_constructor)
637 new_identifiers.push_back(e);
643 identifiers.
swap(new_identifiers);
650 if(argument.
id() == ID_ambiguous)
670 if(base_name==ID_unsignedbv ||
671 base_name==ID_signedbv)
673 if(arguments.size()!=1)
677 << base_name <<
" expects one template argument, but got "
682 exprt argument=arguments.front();
684 if(argument.
id()==ID_type)
688 << base_name <<
" expects one integer template argument, "
695 const auto i = numeric_cast<mp_integer>(argument);
708 <<
"template argument must be greater than zero"
716 else if(base_name==ID_fixedbv)
718 if(arguments.size()!=2)
722 << base_name <<
" expects two template arguments, but got "
727 exprt argument0=arguments[0];
729 exprt argument1=arguments[1];
732 if(argument0.
id()==ID_type)
736 << base_name <<
" expects two integer template arguments, "
741 if(argument1.
id()==ID_type)
745 << base_name <<
" expects two integer template arguments, "
750 const auto width = numeric_cast<mp_integer>(argument0);
752 if(!width.has_value())
760 const auto integer_bits = numeric_cast<mp_integer>(argument1);
762 if(!integer_bits.has_value())
774 <<
"template argument must be greater than zero"
779 if(*integer_bits < 0)
783 <<
"template argument must be greater or equal zero"
788 if(*integer_bits > *width)
792 <<
"template argument must be smaller or equal width"
801 else if(base_name==ID_integer)
803 if(!arguments.empty())
807 << base_name <<
" expects no template arguments"
819 else if(base_name==
"dump_scopes")
827 else if(base_name==
"current_scope")
834 else if(base_name == ID_size_t)
838 else if(base_name == ID_ssize_t)
861 assert(!cpp_name.
get_sub().empty());
866 irept::subt::const_iterator
pos=cpp_name.
get_sub().begin();
878 std::string final_base_name;
883 if(
pos->id()==ID_name)
884 final_base_name+=
pos->get_string(ID_identifier);
885 else if(
pos->id()==ID_template_args)
887 else if(
pos->id()==
"::")
901 std::cout <<
"X: " << id_set.size() <<
'\n';
929 <<
"scope '" << final_base_name <<
"' not found" <<
messaget::eom;
932 else if(id_set.size()>=2)
941 assert(id_set.size()==1);
955 final_base_name.clear();
957 else if(
pos->id()==ID_operator)
959 final_base_name+=
"operator";
961 irept::subt::const_iterator next=
pos+1;
962 assert(next != cpp_name.
get_sub().end());
965 next->id() == ID_cpp_name || next->id() == ID_pointer ||
966 next->id() == ID_int || next->id() == ID_char ||
967 next->id() == ID_c_bool || next->id() == ID_merged_type)
972 op_name.
swap(next_ir);
979 final_base_name+=
pos->id_string();
984 base_name=final_base_name;
1004 std::set<irep_idt> primary_templates;
1006 for(
const auto &id_ptr : id_set)
1008 const irep_idt id = id_ptr->identifier;
1016 if(!specialization_of.
empty())
1017 primary_templates.insert(specialization_of);
1019 primary_templates.insert(
id);
1022 assert(!primary_templates.empty());
1024 if(primary_templates.size()>=2)
1033 const symbolt &primary_template_symbol=
1046 full_template_args_tc=
1049 primary_template_symbol,
1050 full_template_args);
1052 for(
auto &arg : full_template_args_tc.
arguments())
1054 if(arg.id() == ID_type)
1056 if(arg.id() == ID_symbol)
1076 std::vector<matcht> matches;
1080 matcht(full_template_args_tc, full_template_args_tc,
1081 primary_template_symbol.
name));
1083 for(
const auto &id_ptr : id_set)
1085 const irep_idt id = id_ptr->identifier;
1106 assert(full_template_args_tc.
arguments().size()==
1107 partial_specialization_args.
arguments().size());
1115 if(template_scope==
nullptr)
1119 <<
"class template instantiation error"
1127 for(std::size_t i=0; i<full_template_args_tc.
arguments().size(); i++)
1129 if(full_template_args_tc.
arguments()[i].id()==ID_type)
1131 full_template_args_tc.
arguments()[i].type());
1150 primary_template_symbol,
1151 partial_specialization_args);
1155 assert(partial_specialization_args_tc.
arguments().size()==
1156 full_template_args_tc.
arguments().size());
1158 if(partial_specialization_args_tc==
1159 full_template_args_tc)
1161 matches.push_back(
matcht(
1162 guessed_template_args, full_template_args_tc,
id));
1167 assert(!matches.empty());
1169 std::sort(matches.begin(), matches.end());
1172 for(std::vector<matcht>::const_iterator
1173 m_it=matches.begin();
1174 m_it!=matches.end();
1177 std::cout <<
"M: " << m_it->cost
1178 <<
" " << m_it->id <<
'\n';
1184 const matcht &match=*matches.begin();
1198 if(instance.
type.
id()!=ID_struct)
1251 else if(id_set.size()==1)
1270 for(
const auto &id_expr : identifiers)
1274 if(id_expr.id()==ID_type)
1282 if(id_expr.type().get_bool(ID_is_template))
1285 if(id_expr.id()==ID_member)
1290 else if(id_expr.id() == ID_pod_constructor)
1292 out <<
"constructor ";
1295 else if(id_expr.id()==ID_template_function_instance)
1305 if(id_expr.type().get_bool(ID_is_template))
1308 else if(id_expr.type().id()==ID_code)
1314 out <<
" " <<
id <<
"(";
1318 for(
const auto ¶meter : parameters)
1320 const typet ¶meter_type = parameter.type();
1332 if(!parameters.empty())
1342 if(id_expr.id()==ID_symbol)
1345 out <<
" (" << symbol.
location <<
")";
1347 else if(id_expr.id()==ID_template_function_instance)
1351 out <<
" (" << symbol.
location <<
")";
1363 bool fail_with_exception)
1376 std::cout <<
"base name: " << base_name <<
'\n';
1377 std::cout <<
"template args: " << template_args.
pretty() <<
'\n';
1389 return do_builtin(base_name, fargs, template_args);
1393 if(base_name==
"__func__" ||
1394 base_name==
"__FUNCTION__" ||
1395 base_name==
"__PRETTY_FUNCTION__")
1401 return std::move(s);
1410 if(template_args.
is_nil())
1422 id_set.insert(&builtin_id);
1438 if(!fail_with_exception)
1472 bool have_classes=
false, have_methods=
false;
1474 for(
const auto &id_ptr : id_set)
1476 const irep_idt id = id_ptr->identifier;
1485 if(want==
wantt::BOTH && have_classes && have_methods)
1487 if(!fail_with_exception)
1504 identifiers.push_back(
exprt(ID_type, instance));
1510 id_set, fargs, identifiers);
1513 identifiers, template_args, fargs);
1519 id_set, fargs, identifiers);
1526 filter(identifiers, want);
1529 std::cout <<
"P0 " << base_name <<
" " << identifiers.size() <<
'\n';
1542 std::cout <<
"P1 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1551 std::cout <<
"P2 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1557 if(new_identifiers.empty())
1559 new_identifiers=identifiers;
1561 if(template_args.
is_nil())
1565 if(new_identifiers.empty())
1566 new_identifiers=identifiers;
1572 std::cout <<
"P3 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1581 std::cout <<
"P4 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1586 if(new_identifiers.size()==1)
1588 result=*new_identifiers.begin();
1593 if(!fail_with_exception)
1596 if(new_identifiers.empty())
1600 <<
"found no match for symbol '" << base_name <<
"', candidates are:\n";
1607 <<
"symbol '" << base_name <<
"' does not uniquely resolve:\n";
1611 exprt e1=*new_identifiers.begin();
1612 exprt e2=*(++new_identifiers.begin());
1615 <<
"e1.type==e2.type: " << (e1.
type() == e2.
type()) <<
'\n';
1617 <<
"e1.id()==e2.id(): " << (e1.
id() == e2.
id()) <<
'\n';
1619 <<
"e1.iden==e2.iden: "
1620 << (e1.
get(ID_identifier) == e2.
get(ID_identifier)) <<
'\n';
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 '"
1680 if(result.
id()!=ID_type)
1682 if(!fail_with_exception)
1688 <<
"error: expected type, but got expression '"
1703 const exprt &template_expr,
1704 const exprt &desired_expr)
1706 if(template_expr.
id()==ID_cpp_name)
1723 for(
const auto &id_ptr : id_set)
1731 if(e.
id()==ID_unassigned)
1743 const typet &template_type,
1744 const typet &desired_type)
1777 std::cout <<
"TT: " << template_type.
pretty() <<
'\n';
1778 std::cout <<
"DT: " << desired_type.
pretty() <<
'\n';
1781 if(template_type.
id()==ID_cpp_name)
1808 for(
const auto &id_ptr : id_set)
1817 if(t.
id()==ID_unassigned)
1825 std::cout <<
"ASSIGN " <<
id.identifier <<
" := "
1834 else if(template_type.
id()==ID_merged_type)
1848 else if(template_type.
id()==ID_pointer)
1850 if(desired_type.
id() == ID_pointer)
1855 else if(template_type.
id()==ID_array)
1857 if(desired_type.
id() == ID_array)
1882 assert(expr.
id()==ID_symbol);
1902 const symbolt &template_symbol=
1919 if(function_declarator.
type().
id()!=ID_function_type)
1923 <<
"expected function type for function template"
1935 if(template_scope==
nullptr)
1939 << template_identifier <<
'\n'
1940 <<
"function template instantiation error"
1952 exprt::operandst::const_iterator it=fargs.
operands.begin();
1953 for(
const auto ¶meter : parameters)
1958 if(parameter.id()==ID_cpp_declaration)
1969 merge_type(arg_declaration.
type());
1993 typet function_type=
2000 function_type.
set(ID_C_template, template_symbol.
name);
2001 function_type.
set(ID_C_template_arguments, template_args);
2005 exprt template_function_instance(
2006 ID_template_function_instance, function_type);
2008 return template_function_instance;
2016 if(expr.
id()!=ID_symbol)
2019 const symbolt &template_symbol =
2026 if(template_args_non_tc.
is_nil())
2047 template_args_non_tc);
2094 fargs.
operands.begin()->type().get(ID_identifier));
2096 assert(type_symb.
type.
id()==ID_struct);
2102 "method should exist in struct");
2121 unsigned &args_distance,
2131 if(expr.
id()==ID_member ||
2146 const typet &object_type =
2149 object.set(ID_C_lvalue,
true);
2159 fargs.
operands.size() == parameters.size())
2191 for(
const auto &id_ptr : id_set)
2195 if(
id.is_class() ||
id.is_enum() ||
id.is_namespace())
2198 assert(
id.is_scope);
2199 new_set.insert(&
id);
2201 else if(
id.is_typedef())
2215 if(symbol.
type.
id()==ID_struct)
2222 new_set.insert(&class_id);
2237 if(symbol.
type.
id() == ID_struct)
2239 id.print(std::cout);
2240 assert(
id.is_scope);
2241 new_set.insert(&
id);
2260 std::cout <<
"E: " << e.
pretty() <<
'\n';
2266 if(e.
type().
id() == ID_template_parameter_symbol_type)
2272 irep_idt identifier=type.get_identifier();
2277 if(symbol.
type.
id() == ID_template_parameter_symbol_type)
2279 else if(symbol.
type.
id()==ID_struct ||
2280 symbol.
type.
id()==ID_union ||
2281 symbol.
type.
id()==ID_c_enum)
2288 new_set.insert(&class_id);
2298 id_set.swap(new_set);
2305 for(cpp_scopest::id_sett::iterator
2310 if((*it)->is_namespace())
2314 cpp_scopest::id_sett::iterator old(it);
2327 for(
const auto &arg : fargs.
operands)
2331 if(final_type.
id()!=ID_struct && final_type.
id()!=ID_union)
2337 id_set.insert(tmp_set.begin(), tmp_set.end());