cprover
cpp_typecheck_template.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C++ Language Type Checking
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
11 
12 #include "cpp_typecheck.h"
13 
14 #include <util/base_exceptions.h>
15 #include <util/simplify_expr.h>
16 
17 #include "cpp_type2name.h"
19 #include "cpp_template_type.h"
20 #include "cpp_convert_type.h"
21 #include "cpp_template_args.h"
22 
24  const template_typet &old_type,
25  template_typet &new_type)
26 {
27  const template_typet::template_parameterst &old_parameters=
28  old_type.template_parameters();
30  new_type.template_parameters();
31 
32  for(std::size_t i=0; i<new_parameters.size(); i++)
33  {
34  if(i<old_parameters.size() &&
35  old_parameters[i].has_default_argument() &&
36  !new_parameters[i].has_default_argument())
37  {
38  // TODO The default may depend on previous parameters!!
39  new_parameters[i].default_argument()=old_parameters[i].default_argument();
40  }
41  }
42 }
43 
45  cpp_declarationt &declaration)
46 {
47  // Do template parameters. This also sets up the template scope.
48  cpp_scopet &template_scope=
50 
51  typet &type=declaration.type();
52  template_typet &template_type=declaration.template_type();
53 
54  bool has_body=type.find(ID_body).is_not_nil();
55 
56  const cpp_namet &cpp_name=
57  static_cast<const cpp_namet &>(type.find(ID_tag));
58 
59  if(cpp_name.is_nil())
60  {
62  error() << "class templates must not be anonymous" << eom;
63  throw 0;
64  }
65 
66  if(!cpp_name.is_simple_name())
67  {
69  error() << "simple name expected as class template tag" << eom;
70  throw 0;
71  }
72 
73  irep_idt base_name=cpp_name.get_base_name();
74 
75  const cpp_template_args_non_tct &partial_specialization_args=
76  declaration.partial_specialization_args();
77 
78  const irep_idt symbol_name=
80  base_name, template_type, partial_specialization_args);
81 
82  #if 0
83  // Check if the name is already used by a different template
84  // in the same scope.
85  {
86  const auto id_set=
88  base_name,
90  cpp_scopet::TEMPLATE);
91 
92  if(!id_set.empty())
93  {
94  const symbolt &previous=lookup((*id_set.begin())->identifier);
95  if(previous.name!=symbol_name || id_set.size()>1)
96  {
98  str << "template declaration of `" << base_name.c_str()
99  << " does not match previous declaration\n";
100  str << "location of previous definition: " << previous.location;
101  throw 0;
102  }
103  }
104  }
105  #endif
106 
107  // check if we have it already
108 
109  if(const auto maybe_symbol=symbol_table.get_writeable(symbol_name))
110  {
111  // there already
112  symbolt &previous_symbol=*maybe_symbol;
113  cpp_declarationt &previous_declaration=
114  to_cpp_declaration(previous_symbol.type);
115 
116  bool previous_has_body=
117  previous_declaration.type().find(ID_body).is_not_nil();
118 
119  // check if we have 2 bodies
120  if(has_body && previous_has_body)
121  {
123  error() << "template struct `" << base_name
124  << "' defined previously\n"
125  << "location of previous definition: "
126  << previous_symbol.location << eom;
127  throw 0;
128  }
129 
130  if(has_body)
131  {
132  // We replace the template!
133  // We have to retain any default parameters from the previous declaration.
135  previous_declaration.template_type(),
136  declaration.template_type());
137 
138  previous_symbol.type.swap(declaration);
139 
140  #if 0
141  std::cout << "*****\n";
142  std::cout << *cpp_scopes.id_map[symbol_name];
143  std::cout << "*****\n";
144  std::cout << "II: " << symbol_name << '\n';
145  #endif
146 
147  // We also replace the template scope (the old one could be deleted).
148  cpp_scopes.id_map[symbol_name]=&template_scope;
149 
150  // We also fix the parent scope in order to see the new
151  // template arguments
152  }
153  else
154  {
155  // just update any default arguments
157  declaration.template_type(),
158  previous_declaration.template_type());
159  }
160 
161  assert(cpp_scopes.id_map[symbol_name]->id_class ==
163  return;
164  }
165 
166  // it's not there yet
167 
168  symbolt symbol;
169 
170  symbol.name=symbol_name;
171  symbol.base_name=base_name;
172  symbol.location=cpp_name.source_location();
173  symbol.mode=ID_cpp;
174  symbol.module=module;
175  symbol.type.swap(declaration);
176  symbol.is_macro=false;
177  symbol.value = exprt(ID_template_decls);
178 
179  symbol.pretty_name=
181 
182  symbolt *new_symbol;
183  if(symbol_table.move(symbol, new_symbol))
184  {
185  error().source_location=symbol.location;
186  error() << "cpp_typecheckt::typecheck_compound_type: "
187  << "symbol_table.move() failed"
188  << eom;
189  throw 0;
190  }
191 
192  // put into current scope
193  cpp_idt &id=cpp_scopes.put_into_scope(*new_symbol);
195  id.prefix=cpp_scopes.current_scope().prefix+
196  id2string(new_symbol->base_name);
197 
198  // link the template symbol with the template scope
199  cpp_scopes.id_map[symbol_name]=&template_scope;
200  assert(cpp_scopes.id_map[symbol_name]->id_class ==
202 }
203 
206  cpp_declarationt &declaration)
207 {
208  assert(declaration.declarators().size()==1);
209 
210  cpp_declaratort &declarator=declaration.declarators()[0];
211  const cpp_namet &cpp_name = declarator.name();
212 
213  // do template arguments
214  // this also sets up the template scope
215  cpp_scopet &template_scope=
217 
218  if(!cpp_name.is_simple_name())
219  {
220  error().source_location=declaration.source_location();
221  error() << "function template must have simple name" << eom;
222  throw 0;
223  }
224 
225  irep_idt base_name=cpp_name.get_base_name();
226 
227  template_typet &template_type=declaration.template_type();
228 
229  typet function_type=
230  declarator.merge_type(declaration.type());
231 
232  cpp_convert_plain_type(function_type);
233 
234  irep_idt symbol_name=
236  base_name,
237  template_type,
238  function_type);
239 
240  bool has_value=declarator.find(ID_value).is_not_nil();
241 
242  // check if we have it already
243 
244  symbol_tablet::symbolst::const_iterator previous_symbol=
245  symbol_table.symbols.find(symbol_name);
246 
247  if(previous_symbol!=symbol_table.symbols.end())
248  {
249  bool previous_has_value =
250  to_cpp_declaration(previous_symbol->second.type).
251  declarators()[0].find(ID_value).is_not_nil();
252 
253  if(has_value && previous_has_value)
254  {
256  error() << "function template symbol `" << base_name
257  << "' declared previously\n"
258  << "location of previous definition: "
259  << previous_symbol->second.location << eom;
260  throw 0;
261  }
262 
263  if(has_value)
264  {
265  symbol_table.get_writeable_ref(symbol_name).type.swap(declaration);
266  cpp_scopes.id_map[symbol_name]=&template_scope;
267  }
268 
269  // todo: the old template scope now is useless,
270  // and thus, we could delete it
271  return;
272  }
273 
274  symbolt symbol;
275  symbol.name=symbol_name;
276  symbol.base_name=base_name;
277  symbol.location=cpp_name.source_location();
278  symbol.mode=ID_cpp;
279  symbol.module=module;
280  symbol.value.make_nil();
281 
282  symbol.type.swap(declaration);
283  symbol.pretty_name=
285 
286  symbolt *new_symbol;
287  if(symbol_table.move(symbol, new_symbol))
288  {
289  error().source_location=symbol.location;
290  error() << "cpp_typecheckt::typecheck_compound_type: "
291  << "symbol_table.move() failed"
292  << eom;
293  throw 0;
294  }
295 
296  // put into scope
297  cpp_idt &id=cpp_scopes.put_into_scope(*new_symbol);
299  id.prefix=cpp_scopes.current_scope().prefix+
300  id2string(new_symbol->base_name);
301 
302  // link the template symbol with the template scope
303  assert(template_scope.id_class==cpp_idt::id_classt::TEMPLATE_SCOPE);
304  cpp_scopes.id_map[symbol_name] = &template_scope;
305 }
306 
309  cpp_declarationt &declaration)
310 {
311  assert(declaration.declarators().size()==1);
312 
313  cpp_declaratort &declarator=declaration.declarators()[0];
314  const cpp_namet &cpp_name = declarator.name();
315 
316  assert(cpp_name.is_qualified() ||
317  cpp_name.has_template_args());
318 
319  // must be of the form: name1<template_args>::name2
320  // or: name1<template_args>::operator X
321  if(cpp_name.get_sub().size()==4 &&
322  cpp_name.get_sub()[0].id()==ID_name &&
323  cpp_name.get_sub()[1].id()==ID_template_args &&
324  cpp_name.get_sub()[2].id()=="::" &&
325  cpp_name.get_sub()[3].id()==ID_name)
326  {
327  }
328  else if(cpp_name.get_sub().size()==5 &&
329  cpp_name.get_sub()[0].id()==ID_name &&
330  cpp_name.get_sub()[1].id()==ID_template_args &&
331  cpp_name.get_sub()[2].id()=="::" &&
332  cpp_name.get_sub()[3].id()==ID_operator)
333  {
334  }
335  else
336  {
337  return; // TODO
338 
339 #if 0
341  error() << "bad template name" << eom;
342  throw 0;
343 #endif
344  }
345 
346  // let's find the class template this function template belongs to.
347  const auto id_set = cpp_scopes.current_scope().lookup(
348  cpp_name.get_sub().front().get(ID_identifier),
349  cpp_scopet::SCOPE_ONLY, // look only in current scope
350  cpp_scopet::id_classt::TEMPLATE); // must be template
351 
352  if(id_set.empty())
353  {
356  error() << "class template `"
357  << cpp_name.get_sub().front().get(ID_identifier)
358  << "' not found" << eom;
359  throw 0;
360  }
361  else if(id_set.size()>1)
362  {
364  error() << "class template `"
365  << cpp_name.get_sub().front().get(ID_identifier)
366  << "' is ambiguous" << eom;
367  throw 0;
368  }
369  else if((*(id_set.begin()))->id_class!=cpp_idt::id_classt::TEMPLATE)
370  {
371  // std::cerr << *(*id_set.begin()) << '\n';
373  error() << "class template `"
374  << cpp_name.get_sub().front().get(ID_identifier)
375  << "' is not a template" << eom;
376  throw 0;
377  }
378 
379  const cpp_idt &cpp_id=**(id_set.begin());
380  symbolt &template_symbol=
382 
383  exprt &template_methods =
384  static_cast<exprt &>(template_symbol.value.add(ID_template_methods));
385 
386  template_methods.copy_to_operands(declaration);
387 
388  // save current scope
389  cpp_save_scopet cpp_saved_scope(cpp_scopes);
390 
391  const irept &instantiated_with =
392  template_symbol.value.add(ID_instantiated_with);
393 
394  for(std::size_t i=0; i<instantiated_with.get_sub().size(); i++)
395  {
396  const cpp_template_args_tct &tc_template_args=
397  static_cast<const cpp_template_args_tct &>(
398  instantiated_with.get_sub()[i]);
399 
400  cpp_declarationt decl_tmp=declaration;
401 
402  // do template arguments
403  // this also sets up the template scope of the method
404  cpp_scopet &method_scope=
406 
407  cpp_scopes.go_to(method_scope);
408 
409  // mapping from template arguments to values/types
410  template_map.build(decl_tmp.template_type(), tc_template_args);
411 
412  decl_tmp.remove(ID_template_type);
413  decl_tmp.remove(ID_is_template);
414 
415  convert(decl_tmp);
416  cpp_saved_scope.restore();
417  }
418 }
419 
421  const irep_idt &base_name,
422  const template_typet &template_type,
423  const cpp_template_args_non_tct &partial_specialization_args)
424 {
425  std::string identifier=
427  "template."+id2string(base_name) + "<";
428 
429  int counter=0;
430 
431  // these are probably not needed -- templates
432  // should be unique in a namespace
433  for(template_typet::template_parameterst::const_iterator
434  it=template_type.template_parameters().begin();
435  it!=template_type.template_parameters().end();
436  it++)
437  {
438  if(counter!=0)
439  identifier+=',';
440 
441  if(it->id()==ID_type)
442  identifier+="Type"+std::to_string(counter);
443  else
444  identifier+="Non_Type"+std::to_string(counter);
445 
446  counter++;
447  }
448 
449  identifier += ">";
450 
451  if(!partial_specialization_args.arguments().empty())
452  {
453  identifier+="_specialized_to_<";
454 
455  counter=0;
456  for(cpp_template_args_non_tct::argumentst::const_iterator
457  it=partial_specialization_args.arguments().begin();
458  it!=partial_specialization_args.arguments().end();
459  it++, counter++)
460  {
461  if(counter!=0)
462  identifier+=',';
463 
464  // These are not yet typechecked, as they may depend
465  // on unassigned template parameters.
466 
467  if(it->id() == ID_type || it->id() == ID_ambiguous)
468  identifier+=cpp_type2name(it->type());
469  else
470  identifier+=cpp_expr2name(*it);
471  }
472 
473  identifier+='>';
474  }
475 
476  return identifier;
477 }
478 
480  const irep_idt &base_name,
481  const template_typet &template_type,
482  const typet &function_type)
483 {
484  // we first build something without function arguments
485  cpp_template_args_non_tct partial_specialization_args;
486  std::string identifier=
487  class_template_identifier(base_name, template_type,
488  partial_specialization_args);
489 
490  // we must also add the signature of the function to the identifier
491  identifier+=cpp_type2name(function_type);
492 
493  return identifier;
494 }
495 
497  cpp_declarationt &declaration)
498 {
499  cpp_save_scopet saved_scope(cpp_scopes);
500 
501  typet &type=declaration.type();
502 
503  assert(type.id()==ID_struct);
504 
505  cpp_namet &cpp_name=
506  static_cast<cpp_namet &>(type.add(ID_tag));
507 
508  if(cpp_name.is_qualified())
509  {
511  error() << "qualifiers not expected here" << eom;
512  throw 0;
513  }
514 
515  if(cpp_name.get_sub().size()!=2 ||
516  cpp_name.get_sub()[0].id()!=ID_name ||
517  cpp_name.get_sub()[1].id()!=ID_template_args)
518  {
519  // currently we are more restrictive
520  // than the standard
522  error() << "bad template-class-specialization name" << eom;
523  throw 0;
524  }
525 
526  irep_idt base_name=
527  cpp_name.get_sub()[0].get(ID_identifier);
528 
529  // copy the template arguments
530  const cpp_template_args_non_tct template_args_non_tc=
531  to_cpp_template_args_non_tc(cpp_name.get_sub()[1]);
532 
533  // Remove the template arguments from the name.
534  cpp_name.get_sub().pop_back();
535 
536  // get the template symbol
537 
538  auto id_set = cpp_scopes.current_scope().lookup(
540 
541  // remove any specializations
542  for(cpp_scopest::id_sett::iterator
543  it=id_set.begin();
544  it!=id_set.end();
545  ) // no it++
546  {
547  cpp_scopest::id_sett::iterator next=it;
548  next++;
549 
550  if(lookup((*it)->identifier).type.find(ID_specialization_of).is_not_nil())
551  id_set.erase(it);
552 
553  it=next;
554  }
555 
556  // only one should be left
557  if(id_set.empty())
558  {
560  error() << "class template `" << base_name << "' not found" << eom;
561  throw 0;
562  }
563  else if(id_set.size()>1)
564  {
566  error() << "class template `" << base_name << "' is ambiguous"
567  << eom;
568  throw 0;
569  }
570 
571  symbol_tablet::symbolst::const_iterator s_it=
572  symbol_table.symbols.find((*id_set.begin())->identifier);
573 
574  assert(s_it!=symbol_table.symbols.end());
575 
576  const symbolt &template_symbol=s_it->second;
577 
578  if(!template_symbol.type.get_bool(ID_is_template))
579  {
581  error() << "expected a template" << eom;
582  }
583 
584  #if 0
585  // is this partial specialization?
586  if(declaration.template_type().parameters().empty())
587  {
588  // typecheck arguments -- these are for the 'primary' template!
589  cpp_template_args_tct template_args_tc=
591  declaration.source_location(),
592  to_cpp_declaration(template_symbol.type).template_type(),
593  template_args_non_tc);
594 
595  // Full specialization, i.e., template<>.
596  // We instantiate.
598  cpp_name.source_location(),
599  template_symbol,
600  template_args_tc,
601  type);
602  }
603  else // NOLINT(readability/braces)
604  #endif
605 
606  {
607  // partial specialization -- we typecheck
608  declaration.partial_specialization_args()=template_args_non_tc;
609  declaration.set_specialization_of(template_symbol.name);
610 
611  typecheck_class_template(declaration);
612  }
613 }
614 
616  cpp_declarationt &declaration)
617 {
618  cpp_save_scopet saved_scope(cpp_scopes);
619 
620  if(declaration.declarators().size()!=1 ||
621  declaration.declarators().front().type().id()!=ID_function_type)
622  {
623  error().source_location=declaration.type().source_location();
624  error() << "expected function template specialization" << eom;
625  throw 0;
626  }
627 
628  assert(declaration.declarators().size()==1);
629  cpp_declaratort declarator=declaration.declarators().front();
630  cpp_namet &cpp_name=declarator.name();
631 
632  // There is specialization (instantiation with template arguments)
633  // but also function overloading (no template arguments)
634 
635  assert(!cpp_name.get_sub().empty());
636 
637  if(cpp_name.get_sub().back().id()==ID_template_args)
638  {
639  // proper specialization with arguments
640  if(cpp_name.get_sub().size()!=2 ||
641  cpp_name.get_sub()[0].id()!=ID_name ||
642  cpp_name.get_sub()[1].id()!=ID_template_args)
643  {
644  // currently we are more restrictive
645  // than the standard
647  error() << "bad template-function-specialization name" << eom;
648  throw 0;
649  }
650 
651  std::string base_name=
652  cpp_name.get_sub()[0].get(ID_identifier).c_str();
653 
654  const auto id_set =
656 
657  if(id_set.empty())
658  {
660  error() << "template function `" << base_name << "' not found"
661  << eom;
662  throw 0;
663  }
664  else if(id_set.size()>1)
665  {
667  error() << "template function `" << base_name
668  << "' is ambiguous" << eom;
669  throw 0;
670  }
671 
672  const symbolt &template_symbol=
673  lookup((*id_set.begin())->identifier);
674 
675  cpp_template_args_tct template_args=
677  declaration.source_location(),
678  template_symbol,
679  to_cpp_template_args_non_tc(cpp_name.get_sub()[1]));
680 
681  cpp_name.get_sub().pop_back();
682 
683  typet specialization;
684  specialization.swap(declarator);
685 
687  cpp_name.source_location(),
688  template_symbol,
689  template_args,
690  template_args,
691  specialization);
692  }
693  else
694  {
695  // Just overloading, but this is still a template
696  // for disambiguation purposes!
697  // http://www.gotw.ca/publications/mill17.htm
698  cpp_declarationt new_declaration=declaration;
699 
700  new_declaration.remove(ID_template_type);
701  new_declaration.remove(ID_is_template);
702  new_declaration.set(ID_C_template, ""); // todo, get identifier
703 
704  convert_non_template_declaration(new_declaration);
705  }
706 }
707 
709  template_typet &type)
710 {
711  cpp_save_scopet cpp_saved_scope(cpp_scopes);
712 
713  assert(type.id()==ID_template);
714 
715  std::string id_suffix="template::"+std::to_string(template_counter++);
716 
717  // produce a new scope for the template parameters
718  cpp_scopet &template_scope=
720  cpp_scopes.current_scope().prefix+id_suffix);
721 
722  template_scope.prefix=template_scope.get_parent().prefix+id_suffix;
724 
725  cpp_scopes.go_to(template_scope);
726 
727  // put template parameters into this scope
729  type.template_parameters();
730 
731  unsigned anon_count=0;
732 
733  for(template_typet::template_parameterst::iterator
734  it=parameters.begin();
735  it!=parameters.end();
736  it++)
737  {
738  exprt &parameter=*it;
739 
740  cpp_declarationt declaration;
741  declaration.swap(static_cast<cpp_declarationt &>(parameter));
742 
743  cpp_declarator_convertert cpp_declarator_converter(*this);
744 
745  // there must be _one_ declarator
746  assert(declaration.declarators().size()==1);
747 
748  cpp_declaratort &declarator=declaration.declarators().front();
749 
750  // it may be anonymous
751  if(declarator.name().is_nil())
752  declarator.name() = cpp_namet("anon#" + std::to_string(++anon_count));
753 
754  #if 1
755  // The declarator needs to be just a name
756  if(declarator.name().get_sub().size()!=1 ||
757  declarator.name().get_sub().front().id()!=ID_name)
758  {
759  error().source_location=declaration.source_location();
760  error() << "template parameter must be simple name" << eom;
761  throw 0;
762  }
763 
765 
766  irep_idt base_name=declarator.name().get_sub().front().get(ID_identifier);
767  irep_idt identifier=scope.prefix+id2string(base_name);
768 
769  // add to scope
770  cpp_idt &id=scope.insert(base_name);
771  id.identifier=identifier;
773 
774  // is it a type or not?
775  if(declaration.get_bool(ID_is_type))
776  {
777  parameter = exprt(ID_type, symbol_typet(identifier));
778  parameter.type().add_source_location()=declaration.find_source_location();
779  }
780  else
781  {
782  // The type is not checked, as it might depend
783  // on earlier parameters.
784  parameter = symbol_exprt(identifier, declaration.type());
785  }
786 
787  // There might be a default type or default value.
788  // We store it for later, as it can't be typechecked now
789  // because of possible dependencies on earlier parameters!
790  if(declarator.value().is_not_nil())
791  parameter.add(ID_C_default_value)=declarator.value();
792 
793  #else
794  // is it a type or not?
795  cpp_declarator_converter.is_typedef=declaration.get_bool(ID_is_type);
796 
797  // say it a template parameter
798  cpp_declarator_converter.is_template_parameter=true;
799 
800  // There might be a default type or default value.
801  // We store it for later, as it can't be typechecked now
802  // because of possible dependencies on earlier parameters!
803  exprt default_value=declarator.value();
804  declarator.value().make_nil();
805 
806  const symbolt &symbol=
807  cpp_declarator_converter.convert(declaration, declarator);
808 
809  if(cpp_declarator_converter.is_typedef)
810  {
811  parameter = exprt(ID_type, symbol_typet(symbol.name));
812  parameter.type().add_source_location()=declaration.find_location();
813  }
814  else
815  parameter=symbol.symbol_expr();
816 
817  // set (non-typechecked) default value
818  if(default_value.is_not_nil())
819  parameter.add(ID_C_default_value)=default_value;
820 
821  parameter.add_source_location()=declaration.find_location();
822  #endif
823  }
824 
825  // continue without adding to the prefix
826  template_scope.prefix=template_scope.get_parent().prefix;
827 
828  return template_scope;
829 }
830 
834  const source_locationt &source_location,
835  const symbolt &template_symbol,
836  const cpp_template_args_non_tct &template_args)
837 {
838  // old stuff
839  assert(template_args.id()!=ID_already_typechecked);
840 
841  assert(template_symbol.type.get_bool(ID_is_template));
842 
843  const template_typet &template_type=
844  to_cpp_declaration(template_symbol.type).template_type();
845 
846  // bad re-cast, but better than copying the args one by one
848  (const cpp_template_args_tct &)(template_args);
849 
851  result.arguments();
852 
853  const template_typet::template_parameterst &parameters=
854  template_type.template_parameters();
855 
856  if(parameters.size()<args.size())
857  {
858  error().source_location=source_location;
859  error() << "too many template arguments (expected "
860  << parameters.size() << ", but got "
861  << args.size() << ")" << eom;
862  throw 0;
863  }
864 
865  // we will modify the template map
866  template_mapt old_template_map;
867  old_template_map=template_map;
868 
869  // check for default arguments
870  for(std::size_t i=0; i<parameters.size(); i++)
871  {
872  const template_parametert &parameter=parameters[i];
873  cpp_save_scopet cpp_saved_scope(cpp_scopes);
874 
875  if(i>=args.size())
876  {
877  // Check for default argument for the parameter.
878  // These may depend on previous arguments.
879  if(!parameter.has_default_argument())
880  {
881  error().source_location=source_location;
882  error() << "not enough template arguments (expected "
883  << parameters.size() << ", but got " << args.size()
884  << ")" << eom;
885  throw 0;
886  }
887 
888  args.push_back(parameter.default_argument());
889 
890  // these need to be typechecked in the scope of the template,
891  // not in the current scope!
892  cpp_idt *template_scope=cpp_scopes.id_map[template_symbol.name];
894  template_scope!=nullptr, nullptr_exceptiont, "template_scope is null");
895  cpp_scopes.go_to(*template_scope);
896  }
897 
898  assert(i<args.size());
899 
900  exprt &arg=args[i];
901 
902  if(parameter.id()==ID_type)
903  {
904  if(arg.id()==ID_type)
905  {
906  typecheck_type(arg.type());
907  }
908  else if(arg.id() == ID_ambiguous)
909  {
910  typecheck_type(arg.type());
911  typet t=arg.type();
912  arg=exprt(ID_type, t);
913  }
914  else
915  {
917  error() << "expected type, but got expression" << eom;
918  throw 0;
919  }
920  }
921  else // expression
922  {
923  if(arg.id()==ID_type)
924  {
926  error() << "expected expression, but got type" << eom;
927  throw 0;
928  }
929  else if(arg.id() == ID_ambiguous)
930  {
931  exprt e;
932  e.swap(arg.type());
933  arg.swap(e);
934  }
935 
936  typet type=parameter.type();
937 
938  // First check the parameter type (might have earlier
939  // type parameters in it). Needs to be checked in scope
940  // of template.
941  {
942  cpp_save_scopet cpp_saved_scope_before_parameter_typecheck(cpp_scopes);
943  cpp_idt *template_scope=cpp_scopes.id_map[template_symbol.name];
945  template_scope!=nullptr,
947  "template_scope is null");
948  cpp_scopes.go_to(*template_scope);
949  typecheck_type(type);
950  }
951 
952  // Now check the argument to match that.
953  typecheck_expr(arg);
954  simplify(arg, *this);
955  implicit_typecast(arg, type);
956  }
957 
958  // Set right away -- this is for the benefit of default
959  // arguments and later parameters whose type might
960  // depend on an earlier parameter.
961 
962  template_map.set(parameter, arg);
963  }
964 
965  // restore template map
966  template_map.swap(old_template_map);
967 
968  // now the numbers should match
969  assert(args.size()==parameters.size());
970 
971  return result;
972 }
973 
975  cpp_declarationt &declaration)
976 {
977  assert(declaration.is_template());
978 
979  if(declaration.member_spec().is_virtual())
980  {
981  error().source_location=declaration.source_location();
982  error() << "invalid use of 'virtual' in template declaration"
983  << eom;
984  throw 0;
985  }
986 
987  if(declaration.is_typedef())
988  {
989  error().source_location=declaration.source_location();
990  error() << "template declaration for typedef" << eom;
991  throw 0;
992  }
993 
994  typet &type=declaration.type();
995 
996  // there are
997  // 1) function templates
998  // 2) class templates
999  // 3) template members of class templates (static or methods)
1000  // 4) variable templates (C++14)
1001 
1002  if(declaration.is_class_template())
1003  {
1004  // there should not be declarators
1005  if(!declaration.declarators().empty())
1006  {
1007  error().source_location=declaration.source_location();
1008  error() << "class template not expected to have declarators"
1009  << eom;
1010  throw 0;
1011  }
1012 
1013  // it needs to be a class template
1014  if(type.id()!=ID_struct)
1015  {
1016  error().source_location=declaration.source_location();
1017  error() << "expected class template" << eom;
1018  throw 0;
1019  }
1020 
1021  // Is it class template specialization?
1022  // We can tell if there are template arguments in the class name,
1023  // like template<...> class tag<stuff> ...
1024  if((static_cast<const cpp_namet &>(
1025  type.find(ID_tag))).has_template_args())
1026  {
1028  return;
1029  }
1030 
1031  typecheck_class_template(declaration);
1032  return;
1033  }
1034  // maybe function template, maybe class template member, maybe
1035  // template variable
1036  else
1037  {
1038  // there should be declarators in either case
1039  if(declaration.declarators().empty())
1040  {
1041  error().source_location=declaration.source_location();
1042  error() << "non-class template is expected to have a declarator"
1043  << eom;
1044  throw 0;
1045  }
1046 
1047  // Is it function template specialization?
1048  // Only full specialization is allowed!
1049  if(declaration.template_type().template_parameters().empty())
1050  {
1052  return;
1053  }
1054 
1055  // Explicit qualification is forbidden for function templates,
1056  // which we can use to distinguish them.
1057 
1058  assert(declaration.declarators().size()>=1);
1059 
1060  cpp_declaratort &declarator=declaration.declarators()[0];
1061  const cpp_namet &cpp_name = declarator.name();
1062 
1063  if(cpp_name.is_qualified() ||
1064  cpp_name.has_template_args())
1065  return typecheck_class_template_member(declaration);
1066 
1067  // must be function template
1068  typecheck_function_template(declaration);
1069  return;
1070  }
1071 }
cpp_scopet::new_scope
class cpp_scopet & new_scope(const irep_idt &new_scope_name)
Definition: cpp_scope.cpp:192
cpp_typecheckt::convert
void convert(cpp_linkage_spect &)
Definition: cpp_typecheck_linkage_spec.cpp:14
exprt::copy_to_operands
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:123
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
cpp_typecheckt::instantiate_template
const symbolt & instantiate_template(const source_locationt &source_location, const symbolt &symbol, const cpp_template_args_tct &specialization_template_args, const cpp_template_args_tct &full_template_args, const typet &specialization=typet(ID_nil))
Definition: cpp_instantiate_template.cpp:216
cpp_typecheckt::convert_non_template_declaration
void convert_non_template_declaration(cpp_declarationt &declaration)
Definition: cpp_typecheck_declaration.cpp:101
dstringt::c_str
const char * c_str() const
Definition: dstring.h:86
cpp_scopet::get_parent
cpp_scopet & get_parent() const
Definition: cpp_scope.h:93
cpp_declarationt::is_typedef
bool is_typedef() const
Definition: cpp_declaration.h:135
cpp_scopet
Definition: cpp_scope.h:20
template_parametert
Definition: cpp_template_parameter.h:19
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
cpp_typecheckt::salvage_default_arguments
void salvage_default_arguments(const template_typet &old_type, template_typet &new_type)
Definition: cpp_typecheck_template.cpp:23
cpp_save_scopet
Definition: cpp_scopes.h:128
cpp_scopest::id_map
id_mapt id_map
Definition: cpp_scopes.h:69
cpp_idt::id_classt::TEMPLATE
@ TEMPLATE
cpp_typecheckt::cpp_scopes
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:109
cpp_scopet::lookup
id_sett lookup(const irep_idt &base_name_to_lookup, lookup_kindt kind)
Definition: cpp_scope.h:32
irept::make_nil
void make_nil()
Definition: irep.h:315
typet
The type of an expression, extends irept.
Definition: type.h:27
to_cpp_template_args_non_tc
cpp_template_args_non_tct & to_cpp_template_args_non_tc(irept &irep)
Definition: cpp_template_args.h:48
to_cpp_declaration
cpp_declarationt & to_cpp_declaration(irept &irep)
Definition: cpp_declaration.h:148
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
cpp_typecheckt::typecheck_class_template
void typecheck_class_template(cpp_declarationt &declaration)
Definition: cpp_typecheck_template.cpp:44
cpp_typecheckt::typecheck_template_parameters
cpp_scopet & typecheck_template_parameters(template_typet &type)
Definition: cpp_typecheck_template.cpp:708
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:305
cpp_idt::identifier
irep_idt identifier
Definition: cpp_id.h:73
cpp_type2name.h
cpp_declaratort::name
cpp_namet & name()
Definition: cpp_declarator.h:36
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:284
cpp_declarator_convertert::convert
symbolt & convert(const typet &type, const cpp_storage_spect &storage_spec, const cpp_member_spect &member_spec, cpp_declaratort &declarator)
Definition: cpp_declarator_converter.cpp:34
exprt
Base class for all expressions.
Definition: expr.h:54
cpp_declarationt::partial_specialization_args
cpp_template_args_non_tct & partial_specialization_args()
Definition: cpp_declaration.h:108
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
cpp_typecheckt::convert_class_template_specialization
void convert_class_template_specialization(cpp_declarationt &declaration)
Definition: cpp_typecheck_template.cpp:496
nullptr_exceptiont
Definition: base_exceptions.h:29
cpp_typecheckt::template_counter
unsigned template_counter
Definition: cpp_typecheck.h:227
cpp_scopet::SCOPE_ONLY
@ SCOPE_ONLY
Definition: cpp_scope.h:30
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:53
messaget::eom
static eomt eom
Definition: message.h:284
cpp_template_args.h
cpp_template_args_tct
Definition: cpp_template_args.h:64
cpp_declarationt::is_class_template
bool is_class_template() const
Definition: cpp_declaration.h:57
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:143
cpp_scopest::put_into_scope
cpp_idt & put_into_scope(const symbolt &symbol, cpp_scopet &scope, bool is_friend=false)
Definition: cpp_scopes.cpp:22
cpp_idt
Definition: cpp_id.h:28
cpp_declarationt::declarators
const declaratorst & declarators() const
Definition: cpp_declaration.h:64
cpp_declarationt::member_spec
const cpp_member_spect & member_spec() const
Definition: cpp_declaration.h:86
cpp_typecheckt::class_template_identifier
std::string class_template_identifier(const irep_idt &base_name, const template_typet &template_type, const cpp_template_args_non_tct &partial_specialization_args)
Definition: cpp_typecheck_template.cpp:420
symbolt::pretty_name
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
INVARIANT_STRUCTURED
#define INVARIANT_STRUCTURED(CONDITION, TYPENAME,...)
Definition: invariant.h:382
base_exceptions.h
template_mapt::swap
void swap(template_mapt &template_map)
Definition: template_map.h:35
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:68
cpp_typecheckt::function_template_identifier
std::string function_template_identifier(const irep_idt &base_name, const template_typet &template_type, const typet &function_type)
Definition: cpp_typecheck_template.cpp:479
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166
cpp_typecheckt::typecheck_type
void typecheck_type(typet &) override
Definition: cpp_typecheck_type.cpp:23
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:239
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:173
c_typecheck_baset::module
const irep_idt module
Definition: c_typecheck_base.h:68
cpp_member_spect::is_virtual
bool is_virtual() const
Definition: cpp_member_spec.h:23
cpp_typecheckt::typecheck_class_template_member
void typecheck_class_template_member(cpp_declarationt &declaration)
typecheck class template members; these can be methods or static members
Definition: cpp_typecheck_template.cpp:308
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
messaget::result
mstreamt & result() const
Definition: message.h:396
messaget::error
mstreamt & error() const
Definition: message.h:386
cpp_typecheckt::typecheck_function_template
void typecheck_function_template(cpp_declarationt &declaration)
typecheck function templates
Definition: cpp_typecheck_template.cpp:205
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:110
cpp_idt::id_classt::TEMPLATE_PARAMETER
@ TEMPLATE_PARAMETER
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
cpp_scopest::current_scope
cpp_scopet & current_scope()
Definition: cpp_scopes.h:33
cpp_template_args_baset::arguments
argumentst & arguments()
Definition: cpp_template_args.h:31
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:236
cpp_declarator_convertert
Definition: cpp_declarator_converter.h:25
template_typet::template_parameters
template_parameterst & template_parameters()
Definition: cpp_template_type.h:27
typet::source_location
const source_locationt & source_location() const
Definition: type.h:62
exprt::find_source_location
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:229
cpp_declarator_convertert::is_typedef
bool is_typedef
Definition: cpp_declarator_converter.h:31
template_typet::template_parameterst
std::vector< template_parametert > template_parameterst
Definition: cpp_template_type.h:25
cpp_namet::is_simple_name
bool is_simple_name() const
Definition: cpp_name.h:89
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
simplify
bool simplify(exprt &expr, const namespacet &ns)
Definition: simplify_expr.cpp:2320
c_typecheck_baset::symbol_table
symbol_tablet & symbol_table
Definition: c_typecheck_base.h:67
cpp_declarator_convertert::is_template_parameter
bool is_template_parameter
Definition: cpp_declarator_converter.h:33
cpp_declarationt::set_specialization_of
void set_specialization_of(const irep_idt &id)
Definition: cpp_declaration.h:120
irept::swap
void swap(irept &irep)
Definition: irep.h:303
cpp_convert_type.h
cpp_declarationt
Definition: cpp_declaration.h:23
irept::is_nil
bool is_nil() const
Definition: irep.h:172
irept::id
const irep_idt & id() const
Definition: irep.h:259
irept::remove
void remove(const irep_namet &name)
Definition: irep.cpp:269
cpp_idt::id_classt::TEMPLATE_SCOPE
@ TEMPLATE_SCOPE
cpp_scopest::go_to
void go_to(cpp_idt &id)
Definition: cpp_scopes.h:104
cpp_declarationt::is_template
bool is_template() const
Definition: cpp_declaration.h:52
typet::add_source_location
source_locationt & add_source_location()
Definition: type.h:67
cpp_typecheckt::convert_template_declaration
void convert_template_declaration(cpp_declarationt &declaration)
Definition: cpp_typecheck_template.cpp:974
cpp_typecheck.h
cpp_namet::is_qualified
bool is_qualified() const
Definition: cpp_name.h:109
cpp_template_args_non_tct
Definition: cpp_template_args.h:44
symbol_tablet::move
virtual bool move(symbolt &symbol, symbolt *&new_symbol) override
Move a symbol into the symbol table.
Definition: symbol_table.cpp:63
source_locationt
Definition: source_location.h:20
simplify_expr.h
cpp_typecheckt::typecheck_template_args
cpp_template_args_tct typecheck_template_args(const source_locationt &source_location, const symbolt &template_symbol, const cpp_template_args_non_tct &template_args)
Definition: cpp_typecheck_template.cpp:833
symbol_tablet::get_writeable
virtual symbolt * get_writeable(const irep_idt &name) override
Find a symbol in the symbol table for read-write access.
Definition: symbol_table.h:93
cpp_typecheckt::typecheck_expr
void typecheck_expr(exprt &) override
Definition: cpp_typecheck_expr.cpp:2684
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
template_parametert::has_default_argument
bool has_default_argument() const
Definition: cpp_template_parameter.h:58
cpp_namet::has_template_args
bool has_template_args() const
Definition: cpp_name.h:122
cpp_namet::source_location
const source_locationt & source_location() const
Definition: cpp_name.h:73
cpp_save_scopet::restore
void restore()
Definition: cpp_scopes.h:142
cpp_type2name
std::string cpp_type2name(const typet &type)
Definition: cpp_type2name.cpp:92
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
cpp_convert_plain_type
void cpp_convert_plain_type(typet &type)
Definition: cpp_convert_type.cpp:585
symbolt
Symbol table entry.
Definition: symbol.h:27
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:286
template_parametert::default_argument
exprt & default_argument()
Definition: cpp_template_parameter.h:48
symbol_table_baset::symbols
const symbolst & symbols
Definition: symbol_table_base.h:27
cpp_idt::id_class
id_classt id_class
Definition: cpp_id.h:51
template_mapt
Definition: template_map.h:23
cpp_typecheckt::implicit_typecast
void implicit_typecast(exprt &expr, const typet &type) override
Definition: cpp_typecheck_conversions.cpp:1493
irept::get_sub
subt & get_sub()
Definition: irep.h:317
cpp_declaratort::value
exprt & value()
Definition: cpp_declarator.h:42
cpp_idt::prefix
std::string prefix
Definition: cpp_id.h:80
template_mapt::set
void set(const template_parametert &parameter, const exprt &value)
Definition: template_map.cpp:185
irept
Base class for tree-like data structures with sharing.
Definition: irep.h:156
template_typet
Definition: cpp_template_type.h:18
symbol_typet
A reference into the symbol table.
Definition: std_types.h:62
cpp_template_args_baset::argumentst
exprt::operandst argumentst
Definition: cpp_template_args.h:29
cpp_typecheckt::convert_template_function_or_member_specialization
void convert_template_function_or_member_specialization(cpp_declarationt &declaration)
Definition: cpp_typecheck_template.cpp:615
template_mapt::build
void build(const template_typet &template_type, const cpp_template_args_tct &template_args)
Definition: template_map.cpp:142
cpp_expr2name
std::string cpp_expr2name(const exprt &expr)
Definition: cpp_type2name.cpp:176
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:233
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
cpp_namet::get_base_name
irep_idt get_base_name() const
Definition: cpp_name.cpp:17
cpp_typecheckt::template_map
template_mapt template_map
Definition: cpp_typecheck.h:230
cpp_namet
Definition: cpp_name.h:16
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:228
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
cpp_declaratort
Definition: cpp_declarator.h:19
dstringt::size
size_t size() const
Definition: dstring.h:91
cpp_declarationt::template_type
template_typet & template_type()
Definition: cpp_declaration.h:98
cpp_template_type.h
cpp_scopet::insert
cpp_idt & insert(const irep_idt &_base_name)
Definition: cpp_scope.h:52
cpp_declarator_converter.h
cpp_declaratort::merge_type
typet merge_type(const typet &declaration_type) const
Definition: cpp_declarator.cpp:28