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);
273 object.copy_to_operands(this_expr);
274 object.type().set(ID_C_constant,
276 object.set(ID_C_lvalue,
true);
283 if(object_type.
id()==ID_struct ||
284 object_type.
id()==ID_union)
295 if(
object.is_not_nil())
334 else if(symbol.
type.
id()==ID_c_enum)
338 else if(symbol.
type.
id() == ID_struct)
342 else if(symbol.
type.
id() == ID_union)
358 symbol.
value.
id() == ID_constant)
379 old_identifiers.swap(identifiers);
381 for(
const auto &old_id : old_identifiers)
388 match = (old_id.id() == ID_type);
392 match = (old_id.id() != ID_type);
404 identifiers.push_back(old_id);
416 old_identifiers.swap(identifiers);
421 for(
const auto &old_id : old_identifiers)
426 identifiers.push_back(old_id);
435 old_identifiers.swap(identifiers);
438 std::multimap<std::size_t, exprt> distance_map;
440 for(
const auto &old_id : old_identifiers)
442 unsigned args_distance;
446 std::size_t template_distance=0;
448 if(!old_id.type().get(ID_C_template).empty())
449 template_distance = old_id.type()
450 .find(ID_C_template_arguments)
457 std::size_t total_distance=
459 1000*template_distance+args_distance;
461 distance_map.insert({total_distance, old_id});
465 old_identifiers.clear();
468 if(!distance_map.empty())
470 auto range = distance_map.equal_range(distance_map.begin()->first);
471 for(
auto it = range.first; it != range.second; ++it)
472 old_identifiers.push_back(it->second);
475 if(old_identifiers.size() > 1 && fargs.
in_use)
479 for(resolve_identifierst::const_iterator old_it = old_identifiers.begin();
480 old_it != old_identifiers.end();
484 std::cout <<
"I1: " << old_it->get(ID_identifier) <<
'\n';
487 if(old_it->type().id() != ID_code)
489 identifiers.push_back(*old_it);
495 for(resolve_identifierst::const_iterator resolve_it = old_it + 1;
496 resolve_it != old_identifiers.end();
499 if(resolve_it->type().id() != ID_code)
511 i<f1.
parameters().size() && (f1_better || f2_better);
523 if(type1.
id()==ID_pointer)
529 if(type2.
id()==ID_pointer)
538 if(followed1.
id() != ID_struct || followed2.
id() != ID_struct)
554 if(!f1_better || f2_better)
555 identifiers.push_back(*resolve_it);
561 identifiers.swap(old_identifiers);
572 for(
const auto &identifier : identifiers)
574 if(identifier.id() != ID_type)
577 new_identifiers.push_back(identifier);
592 exprt pod_constructor1(ID_pod_constructor, t1);
593 new_identifiers.push_back(pod_constructor1);
600 exprt pod_constructor2(ID_pod_constructor, t2);
601 new_identifiers.push_back(pod_constructor2);
605 if(symbol_type.
id()==ID_c_enum_tag)
609 exprt pod_constructor3(ID_pod_constructor, t3);
610 new_identifiers.push_back(pod_constructor3);
613 else if(symbol_type.
id()==ID_struct)
626 type.
id() == ID_code &&
627 to_code_type(type).return_type().
id() == ID_constructor)
633 new_identifiers.push_back(e);
639 identifiers.
swap(new_identifiers);
646 if(argument.
id() == ID_ambiguous)
666 if(base_name==ID_unsignedbv ||
667 base_name==ID_signedbv)
669 if(arguments.size()!=1)
673 << base_name <<
" expects one template argument, but got "
678 exprt argument=arguments.front();
680 if(argument.
id()==ID_type)
684 << base_name <<
" expects one integer template argument, "
691 const auto i = numeric_cast<mp_integer>(argument);
704 <<
"template argument must be greater than zero"
712 else if(base_name==ID_fixedbv)
714 if(arguments.size()!=2)
718 << base_name <<
" expects two template arguments, but got "
723 exprt argument0=arguments[0];
725 exprt argument1=arguments[1];
728 if(argument0.
id()==ID_type)
732 << base_name <<
" expects two integer template arguments, "
737 if(argument1.
id()==ID_type)
741 << base_name <<
" expects two integer template arguments, "
746 const auto width = numeric_cast<mp_integer>(argument0);
748 if(!width.has_value())
756 const auto integer_bits = numeric_cast<mp_integer>(argument1);
758 if(!integer_bits.has_value())
770 <<
"template argument must be greater than zero"
775 if(*integer_bits < 0)
779 <<
"template argument must be greater or equal zero"
784 if(*integer_bits > *width)
788 <<
"template argument must be smaller or equal width"
797 else if(base_name==ID_integer)
799 if(!arguments.empty())
803 << base_name <<
" expects no template arguments"
815 else if(base_name==
"dump_scopes")
823 else if(base_name==
"current_scope")
830 else if(base_name == ID_size_t)
834 else if(base_name == ID_ssize_t)
857 assert(!cpp_name.
get_sub().empty());
862 irept::subt::const_iterator
pos=cpp_name.
get_sub().begin();
874 std::string final_base_name;
879 if(
pos->id()==ID_name)
880 final_base_name+=
pos->get_string(ID_identifier);
881 else if(
pos->id()==ID_template_args)
883 else if(
pos->id()==
"::")
897 std::cout <<
"X: " << id_set.size() <<
'\n';
925 <<
"scope '" << final_base_name <<
"' not found" <<
messaget::eom;
928 else if(id_set.size()>=2)
937 assert(id_set.size()==1);
951 final_base_name.clear();
953 else if(
pos->id()==ID_operator)
955 final_base_name+=
"operator";
957 irept::subt::const_iterator next=
pos+1;
958 assert(next != cpp_name.
get_sub().end());
961 next->id() == ID_cpp_name || next->id() == ID_pointer ||
962 next->id() == ID_int || next->id() == ID_char ||
963 next->id() == ID_c_bool || next->id() == ID_merged_type)
968 op_name.
swap(next_ir);
975 final_base_name+=
pos->id_string();
980 base_name=final_base_name;
1000 std::set<irep_idt> primary_templates;
1002 for(
const auto &id_ptr : id_set)
1004 const irep_idt id = id_ptr->identifier;
1012 if(!specialization_of.
empty())
1013 primary_templates.insert(specialization_of);
1015 primary_templates.insert(
id);
1018 assert(!primary_templates.empty());
1020 if(primary_templates.size()>=2)
1029 const symbolt &primary_template_symbol=
1042 full_template_args_tc=
1045 primary_template_symbol,
1046 full_template_args);
1048 for(
auto &arg : full_template_args_tc.
arguments())
1050 if(arg.id() == ID_type)
1052 if(arg.id() == ID_symbol)
1072 std::vector<matcht> matches;
1076 matcht(full_template_args_tc, full_template_args_tc,
1077 primary_template_symbol.
name));
1079 for(
const auto &id_ptr : id_set)
1081 const irep_idt id = id_ptr->identifier;
1102 assert(full_template_args_tc.
arguments().size()==
1103 partial_specialization_args.
arguments().size());
1111 if(template_scope==
nullptr)
1115 <<
"class template instantiation error"
1123 for(std::size_t i=0; i<full_template_args_tc.
arguments().size(); i++)
1125 if(full_template_args_tc.
arguments()[i].id()==ID_type)
1127 full_template_args_tc.
arguments()[i].type());
1146 primary_template_symbol,
1147 partial_specialization_args);
1151 assert(partial_specialization_args_tc.
arguments().size()==
1152 full_template_args_tc.
arguments().size());
1154 if(partial_specialization_args_tc==
1155 full_template_args_tc)
1157 matches.push_back(
matcht(
1158 guessed_template_args, full_template_args_tc,
id));
1163 assert(!matches.empty());
1165 std::sort(matches.begin(), matches.end());
1168 for(std::vector<matcht>::const_iterator
1169 m_it=matches.begin();
1170 m_it!=matches.end();
1173 std::cout <<
"M: " << m_it->cost
1174 <<
" " << m_it->id <<
'\n';
1180 const matcht &match=*matches.begin();
1194 if(instance.
type.
id()!=ID_struct)
1247 else if(id_set.size()==1)
1266 for(
const auto &id_expr : identifiers)
1270 if(id_expr.id()==ID_type)
1278 if(id_expr.type().get_bool(ID_is_template))
1281 if(id_expr.id()==ID_member)
1286 else if(id_expr.id() == ID_pod_constructor)
1288 out <<
"constructor ";
1291 else if(id_expr.id()==ID_template_function_instance)
1301 if(id_expr.type().get_bool(ID_is_template))
1304 else if(id_expr.type().id()==ID_code)
1310 out <<
" " <<
id <<
"(";
1314 for(
const auto ¶meter : parameters)
1316 const typet ¶meter_type = parameter.type();
1328 if(!parameters.empty())
1338 if(id_expr.id()==ID_symbol)
1341 out <<
" (" << symbol.
location <<
")";
1343 else if(id_expr.id()==ID_template_function_instance)
1347 out <<
" (" << symbol.
location <<
")";
1359 bool fail_with_exception)
1372 std::cout <<
"base name: " << base_name <<
'\n';
1373 std::cout <<
"template args: " << template_args.
pretty() <<
'\n';
1385 return do_builtin(base_name, fargs, template_args);
1389 if(base_name==
"__func__" ||
1390 base_name==
"__FUNCTION__" ||
1391 base_name==
"__PRETTY_FUNCTION__")
1397 return std::move(s);
1406 if(template_args.
is_nil())
1418 id_set.insert(&builtin_id);
1434 if(!fail_with_exception)
1468 bool have_classes=
false, have_methods=
false;
1470 for(
const auto &id_ptr : id_set)
1472 const irep_idt id = id_ptr->identifier;
1481 if(want==
wantt::BOTH && have_classes && have_methods)
1483 if(!fail_with_exception)
1500 identifiers.push_back(
exprt(ID_type, instance));
1506 id_set, fargs, identifiers);
1509 identifiers, template_args, fargs);
1515 id_set, fargs, identifiers);
1522 filter(identifiers, want);
1525 std::cout <<
"P0 " << base_name <<
" " << identifiers.size() <<
'\n';
1538 std::cout <<
"P1 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1547 std::cout <<
"P2 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1553 if(new_identifiers.empty())
1555 new_identifiers=identifiers;
1557 if(template_args.
is_nil())
1561 if(new_identifiers.empty())
1562 new_identifiers=identifiers;
1568 std::cout <<
"P3 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1577 std::cout <<
"P4 " << base_name <<
" " << new_identifiers.size() <<
'\n';
1582 if(new_identifiers.size()==1)
1584 result=*new_identifiers.begin();
1589 if(!fail_with_exception)
1592 if(new_identifiers.empty())
1596 <<
"found no match for symbol '" << base_name <<
"', candidates are:\n";
1603 <<
"symbol '" << base_name <<
"' does not uniquely resolve:\n";
1607 exprt e1=*new_identifiers.begin();
1608 exprt e2=*(++new_identifiers.begin());
1611 <<
"e1.type==e2.type: " << (e1.
type() == e2.
type()) <<
'\n';
1613 <<
"e1.id()==e2.id(): " << (e1.
id() == e2.
id()) <<
'\n';
1615 <<
"e1.iden==e2.iden: "
1616 << (e1.
get(ID_identifier) == e2.
get(ID_identifier)) <<
'\n';
1626 for(
const auto &op : fargs.
operands)
1643 if(result.
get_bool(ID_C_not_accessible))
1646 if(!fail_with_exception)
1651 <<
"error: member '" << result.
get(ID_component_name)
1652 <<
"' is not accessible";
1662 if(!fail_with_exception)
1668 <<
"error: expected expression, but got type '"
1676 if(result.
id()!=ID_type)
1678 if(!fail_with_exception)
1684 <<
"error: expected type, but got expression '"
1699 const exprt &template_expr,
1700 const exprt &desired_expr)
1702 if(template_expr.
id()==ID_cpp_name)
1719 for(
const auto &id_ptr : id_set)
1727 if(e.
id()==ID_unassigned)
1739 const typet &template_type,
1740 const typet &desired_type)
1773 std::cout <<
"TT: " << template_type.
pretty() <<
'\n';
1774 std::cout <<
"DT: " << desired_type.
pretty() <<
'\n';
1777 if(template_type.
id()==ID_cpp_name)
1804 for(
const auto &id_ptr : id_set)
1813 if(t.
id()==ID_unassigned)
1821 std::cout <<
"ASSIGN " <<
id.identifier <<
" := "
1830 else if(template_type.
id()==ID_merged_type)
1843 else if(template_type.
id()==ID_pointer)
1845 if(desired_type.
id() == ID_pointer)
1848 else if(template_type.
id()==ID_array)
1850 if(desired_type.
id() == ID_array)
1873 assert(expr.
id()==ID_symbol);
1893 const symbolt &template_symbol=
1910 if(function_declarator.
type().
id()!=ID_function_type)
1914 <<
"expected function type for function template"
1926 if(template_scope==
nullptr)
1930 << template_identifier <<
'\n'
1931 <<
"function template instantiation error"
1943 exprt::operandst::const_iterator it=fargs.
operands.begin();
1944 for(
const auto ¶meter : parameters)
1949 if(parameter.id()==ID_cpp_declaration)
1960 merge_type(arg_declaration.
type());
1984 typet function_type=
1991 function_type.
set(ID_C_template, template_symbol.
name);
1992 function_type.
set(ID_C_template_arguments, template_args);
1996 exprt template_function_instance(
1997 ID_template_function_instance, function_type);
1999 return template_function_instance;
2007 if(expr.
id()!=ID_symbol)
2010 const symbolt &template_symbol =
2017 if(template_args_non_tc.
is_nil())
2038 template_args_non_tc);
2085 fargs.
operands.begin()->type().get(ID_identifier));
2087 assert(type_symb.
type.
id()==ID_struct);
2093 "method should exist in struct");
2112 unsigned &args_distance,
2122 if(expr.
id()==ID_member ||
2139 object.set(ID_C_lvalue,
true);
2149 fargs.
operands.size() == parameters.size())
2181 for(
const auto &id_ptr : id_set)
2185 if(
id.is_class() ||
id.is_enum() ||
id.is_namespace())
2188 assert(
id.is_scope);
2189 new_set.insert(&
id);
2191 else if(
id.is_typedef())
2205 if(symbol.
type.
id()==ID_struct)
2212 new_set.insert(&class_id);
2227 if(symbol.
type.
id() == ID_struct)
2229 id.print(std::cout);
2230 assert(
id.is_scope);
2231 new_set.insert(&
id);
2250 std::cout <<
"E: " << e.
pretty() <<
'\n';
2256 if(e.
type().
id() == ID_template_parameter_symbol_type)
2262 irep_idt identifier=type.get_identifier();
2267 if(symbol.
type.
id() == ID_template_parameter_symbol_type)
2269 else if(symbol.
type.
id()==ID_struct ||
2270 symbol.
type.
id()==ID_union ||
2271 symbol.
type.
id()==ID_c_enum)
2278 new_set.insert(&class_id);
2288 id_set.swap(new_set);
2295 for(cpp_scopest::id_sett::iterator
2300 if((*it)->is_namespace())
2304 cpp_scopest::id_sett::iterator old(it);
2317 for(
const auto &arg : fargs.
operands)
2321 if(final_type.
id()!=ID_struct && final_type.
id()!=ID_union)
2327 id_set.insert(tmp_set.begin(), tmp_set.end());