cppScope.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppScope.cxx
  10. * @author drose
  11. * @date 1999-10-21
  12. */
  13. #include "cppScope.h"
  14. #include "cppParser.h"
  15. #include "cppDeclaration.h"
  16. #include "cppNamespace.h"
  17. #include "cppTypedefType.h"
  18. #include "cppTypeDeclaration.h"
  19. #include "cppExtensionType.h"
  20. #include "cppEnumType.h"
  21. #include "cppInstance.h"
  22. #include "cppInstanceIdentifier.h"
  23. #include "cppIdentifier.h"
  24. #include "cppStructType.h"
  25. #include "cppFunctionGroup.h"
  26. #include "cppPreprocessor.h"
  27. #include "cppTemplateScope.h"
  28. #include "cppClassTemplateParameter.h"
  29. #include "cppFunctionType.h"
  30. #include "cppConstType.h"
  31. #include "cppUsing.h"
  32. #include "cppBisonDefs.h"
  33. #include "indent.h"
  34. using std::ostream;
  35. using std::ostringstream;
  36. using std::pair;
  37. using std::string;
  38. /**
  39. *
  40. */
  41. CPPScope::
  42. CPPScope(CPPScope *parent_scope,
  43. const CPPNameComponent &name, CPPVisibility starting_vis) :
  44. _name(name),
  45. _parent_scope(parent_scope),
  46. _current_vis(starting_vis)
  47. {
  48. _struct_type = nullptr;
  49. _is_fully_specified = false;
  50. _fully_specified_known = false;
  51. _is_fully_specified_recursive_protect = false;
  52. _subst_decl_recursive_protect = false;
  53. }
  54. /**
  55. *
  56. */
  57. CPPScope::
  58. ~CPPScope() {
  59. }
  60. /**
  61. * Sets the struct or class that owns this scope. This should only be done
  62. * once, when the scope and its associated struct are created. It's provided
  63. * so the scope can check the struct's ancestry for inherited symbols.
  64. */
  65. void CPPScope::
  66. set_struct_type(CPPStructType *struct_type) {
  67. _struct_type = struct_type;
  68. }
  69. /**
  70. * Returns the class or struct that defines this scope, if any.
  71. */
  72. CPPStructType *CPPScope::
  73. get_struct_type() const {
  74. return _struct_type;
  75. }
  76. /**
  77. * Returns the parent scope of this scope, if any.
  78. */
  79. CPPScope *CPPScope::
  80. get_parent_scope() const {
  81. return _parent_scope;
  82. }
  83. /**
  84. *
  85. */
  86. void CPPScope::
  87. set_current_vis(CPPVisibility current_vis) {
  88. _current_vis = current_vis;
  89. }
  90. /**
  91. *
  92. */
  93. CPPVisibility CPPScope::
  94. get_current_vis() const {
  95. return _current_vis;
  96. }
  97. /**
  98. *
  99. */
  100. void CPPScope::
  101. add_declaration(CPPDeclaration *decl, CPPScope *global_scope,
  102. CPPPreprocessor *preprocessor, const cppyyltype &pos) {
  103. decl->_vis = _current_vis;
  104. // Get the recent comments from the preprocessor. These are the comments
  105. // that appeared preceding this particular declaration; they might be
  106. // relevant to the declaration.
  107. if (decl->_leading_comment == nullptr) {
  108. decl->_leading_comment =
  109. preprocessor->get_comment_before(pos.first_line, pos.file);
  110. }
  111. _declarations.push_back(decl);
  112. handle_declaration(decl, global_scope, preprocessor);
  113. }
  114. /**
  115. *
  116. */
  117. void CPPScope::
  118. add_enum_value(CPPInstance *inst) {
  119. inst->_vis = _current_vis;
  120. string name = inst->get_simple_name();
  121. if (!name.empty()) {
  122. _enum_values[name] = inst;
  123. }
  124. }
  125. /**
  126. *
  127. */
  128. void CPPScope::
  129. define_typedef_type(CPPTypedefType *type, CPPPreprocessor *error_sink) {
  130. string name = type->get_simple_name();
  131. pair<Types::iterator, bool> result =
  132. _types.insert(Types::value_type(name, type));
  133. if (!result.second) {
  134. CPPType *other_type = result.first->second;
  135. CPPTypedefType *other_td = other_type->as_typedef_type();
  136. // We don't do redefinitions of typedefs. But we don't complain as long
  137. // as this is actually a typedef to the previous definition.
  138. if (other_type != type->_type &&
  139. (other_td == nullptr || !other_td->_type->is_equivalent(*type->_type))) {
  140. if (error_sink != nullptr) {
  141. ostringstream errstr;
  142. type->output(errstr, 0, nullptr, false);
  143. errstr << " has conflicting declaration as ";
  144. other_type->output(errstr, 0, nullptr, true);
  145. error_sink->error(errstr.str(), type->_ident->_loc);
  146. if (other_td != nullptr && other_td->_ident != nullptr) {
  147. error_sink->error("previous definition is here",
  148. other_td->_ident->_loc);
  149. }
  150. }
  151. }
  152. } else {
  153. _types[name] = type;
  154. }
  155. // This might be a templated "using" definition.
  156. if (type->is_template()) {
  157. CPPTemplateScope *scope = type->get_template_scope();
  158. if (scope->_parameters._parameters.size() == 0) {
  159. return;
  160. }
  161. string simple_name = type->get_simple_name();
  162. pair<Templates::iterator, bool> result =
  163. _templates.insert(Templates::value_type(simple_name, type));
  164. if (!result.second) {
  165. // The template was not inserted because we already had a template
  166. // definition with the given name. If the previous definition was
  167. // incomplete, replace it.
  168. CPPDeclaration *old_templ = (*result.first).second;
  169. CPPType *old_templ_type = old_templ->as_type();
  170. if (old_templ_type == nullptr || old_templ_type->is_incomplete()) {
  171. // The previous template definition was incomplete, maybe a forward
  172. // reference; replace it with the good one.
  173. (*result.first).second = type;
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. *
  180. */
  181. void CPPScope::
  182. define_extension_type(CPPExtensionType *type, CPPPreprocessor *error_sink) {
  183. assert(type != nullptr);
  184. string name = type->get_local_name(this);
  185. if (name.empty()) {
  186. return;
  187. }
  188. switch (type->_type) {
  189. case CPPExtensionType::T_class:
  190. _classes[name] = type;
  191. break;
  192. case CPPExtensionType::T_struct:
  193. _structs[name] = type;
  194. break;
  195. case CPPExtensionType::T_union:
  196. _unions[name] = type;
  197. break;
  198. case CPPExtensionType::T_enum:
  199. case CPPExtensionType::T_enum_struct:
  200. case CPPExtensionType::T_enum_class:
  201. _enums[name] = type;
  202. break;
  203. }
  204. // Create an implicit typedef for the extension. CPPTypedefType *td = new
  205. // CPPTypedefType(type, name);
  206. pair<Types::iterator, bool> result =
  207. _types.insert(Types::value_type(name, type));
  208. if (!result.second) {
  209. // There's already a typedef for this extension. This one overrides it
  210. // only if the other is a forward declaration.
  211. CPPType *other_type = (*result.first).second;
  212. if (other_type->get_subtype() == CPPDeclaration::ST_extension) {
  213. CPPExtensionType *other_ext = other_type->as_extension_type();
  214. if (other_ext->_type != type->_type) {
  215. if (error_sink != nullptr) {
  216. ostringstream errstr;
  217. errstr << type->_type << " " << type->get_fully_scoped_name()
  218. << " was previously declared as " << other_ext->_type;
  219. error_sink->error(errstr.str(), type->_ident->_loc);
  220. if (other_ext->_ident != nullptr) {
  221. error_sink->error("previous declaration is here",
  222. other_ext->_ident->_loc);
  223. }
  224. }
  225. }
  226. (*result.first).second = type;
  227. } else {
  228. CPPTypedefType *other_td = other_type->as_typedef_type();
  229. // Error out if the declaration is different than the previous one.
  230. if (other_type != type &&
  231. (other_td == nullptr || other_td->_type != type)) {
  232. if (error_sink != nullptr) {
  233. ostringstream errstr;
  234. if (!cppparser_output_class_keyword) {
  235. errstr << type->_type << " ";
  236. }
  237. type->output(errstr, 0, nullptr, false);
  238. errstr << " has conflicting definition as ";
  239. other_type->output(errstr, 0, nullptr, true);
  240. error_sink->error(errstr.str(), type->_ident->_loc);
  241. CPPExtensionType *other_ext = other_type->as_extension_type();
  242. if (other_ext != nullptr && other_ext->_ident != nullptr) {
  243. error_sink->error("previous definition is here",
  244. other_ext->_ident->_loc);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. if (type->is_template()) {
  251. CPPTemplateScope *scope = type->get_template_scope();
  252. if (scope->_parameters._parameters.size() == 0) {
  253. return;
  254. }
  255. string simple_name = type->get_simple_name();
  256. pair<Templates::iterator, bool> result =
  257. _templates.insert(Templates::value_type(simple_name, type));
  258. if (!result.second) {
  259. // The template was not inserted because we already had a template
  260. // definition with the given name. If the previous definition was
  261. // incomplete, replace it.
  262. CPPDeclaration *old_templ = (*result.first).second;
  263. CPPType *old_templ_type = old_templ->as_type();
  264. if (old_templ_type == nullptr || old_templ_type->is_incomplete()) {
  265. // The previous template definition was incomplete, maybe a forward
  266. // reference; replace it with the good one.
  267. (*result.first).second = type;
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. *
  274. */
  275. void CPPScope::
  276. define_namespace(CPPNamespace *ns) {
  277. string name = ns->get_simple_name();
  278. _namespaces[name] = ns;
  279. if (ns->_is_inline) {
  280. // Add an implicit using declaration for an inline namespace.
  281. _using.insert(ns->get_scope());
  282. }
  283. }
  284. /**
  285. *
  286. */
  287. void CPPScope::
  288. add_using(CPPUsing *using_decl, CPPScope *global_scope,
  289. CPPPreprocessor *error_sink) {
  290. if (using_decl->_full_namespace) {
  291. CPPScope *scope =
  292. using_decl->_ident->find_scope(this, global_scope);
  293. if (scope != nullptr) {
  294. _using.insert(scope);
  295. } else {
  296. if (error_sink != nullptr) {
  297. error_sink->warning("Attempt to use undefined namespace: " + using_decl->_ident->get_fully_scoped_name(), using_decl->_ident->_loc);
  298. }
  299. }
  300. } else {
  301. CPPDeclaration *decl = using_decl->_ident->find_symbol(this, global_scope);
  302. if (decl != nullptr) {
  303. handle_declaration(decl, global_scope, error_sink);
  304. } else {
  305. if (error_sink != nullptr) {
  306. error_sink->warning("Attempt to use unknown symbol: " + using_decl->_ident->get_fully_scoped_name(), using_decl->_ident->_loc);
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * Returns true if this declaration is an actual, factual declaration, or
  313. * false if some part of the declaration depends on a template parameter which
  314. * has not yet been instantiated.
  315. */
  316. bool CPPScope::
  317. is_fully_specified() const {
  318. if (_fully_specified_known) {
  319. return _is_fully_specified;
  320. }
  321. if (_is_fully_specified_recursive_protect) {
  322. // We're already executing this block.
  323. return true;
  324. }
  325. ((CPPScope *)this)->_is_fully_specified_recursive_protect = true;
  326. bool specified = true;
  327. if (_parent_scope != nullptr && !_parent_scope->is_fully_specified()) {
  328. specified = false;
  329. }
  330. Declarations::const_iterator di;
  331. for (di = _declarations.begin();
  332. di != _declarations.end() && specified;
  333. ++di) {
  334. if (!(*di)->is_fully_specified()) {
  335. specified = false;
  336. }
  337. }
  338. ((CPPScope *)this)->_fully_specified_known = true;
  339. ((CPPScope *)this)->_is_fully_specified = specified;
  340. ((CPPScope *)this)->_is_fully_specified_recursive_protect = false;
  341. return specified;
  342. }
  343. /**
  344. *
  345. */
  346. CPPScope *CPPScope::
  347. instantiate(const CPPTemplateParameterList *actual_params,
  348. CPPScope *current_scope, CPPScope *global_scope,
  349. CPPPreprocessor *error_sink) const {
  350. CPPScope *this_scope = (CPPScope *)this;
  351. if (_parent_scope == nullptr ||
  352. _parent_scope->as_template_scope() == nullptr) {
  353. if (error_sink != nullptr) {
  354. error_sink->warning("Ignoring template parameters for scope " +
  355. get_local_name());
  356. }
  357. return this_scope;
  358. }
  359. if (is_fully_specified()) {
  360. return this_scope;
  361. }
  362. Instantiations::const_iterator ii;
  363. ii = _instantiations.find(actual_params);
  364. if (ii != _instantiations.end()) {
  365. // We've already instantiated this scope with these parameters. Return
  366. // that.
  367. return (*ii).second;
  368. }
  369. /*
  370. cerr << "Instantiating " << get_simple_name()
  371. << "<" << *actual_params << ">\n";
  372. */
  373. // Build the mapping of formal parameters to actual parameters.
  374. CPPTemplateScope *tscope = _parent_scope->as_template_scope();
  375. CPPDeclaration::SubstDecl subst;
  376. actual_params->build_subst_decl(tscope->_parameters, subst,
  377. current_scope, global_scope);
  378. CPPScope *scope;
  379. if (subst.empty()) {
  380. scope = (CPPScope *)this;
  381. } else {
  382. CPPNameComponent name = _name;
  383. name.set_templ(new CPPTemplateParameterList(*actual_params));
  384. // scope = new CPPScope(current_scope, name, V_public);
  385. scope = new CPPScope(_parent_scope, name, V_public);
  386. copy_substitute_decl(scope, subst, global_scope);
  387. // Also define any new template parameter types, in case we "instantiated"
  388. // this scope with another template parameter.
  389. CPPTemplateParameterList::Parameters::const_iterator pi;
  390. for (pi = actual_params->_parameters.begin();
  391. pi != actual_params->_parameters.end();
  392. ++pi) {
  393. CPPDeclaration *decl = (*pi);
  394. CPPClassTemplateParameter *ctp = decl->as_class_template_parameter();
  395. if (ctp != nullptr) {
  396. // CPPTypedefType *td = new CPPTypedefType(ctp, ctp->_ident);
  397. // scope->_typedefs.insert(Typedefs::value_type
  398. // (ctp->_ident->get_local_name(), td));
  399. scope->_types.insert(Types::value_type
  400. (ctp->_ident->get_local_name(),
  401. ctp));
  402. }
  403. }
  404. }
  405. // Finally, record this particular instantiation for future reference, so we
  406. // don't have to do this again.
  407. ((CPPScope *)this)->_instantiations.insert(Instantiations::value_type(actual_params, scope));
  408. return scope;
  409. }
  410. /**
  411. *
  412. */
  413. CPPScope *CPPScope::
  414. substitute_decl(CPPDeclaration::SubstDecl &subst,
  415. CPPScope *current_scope, CPPScope *global_scope) const {
  416. CPPScope *this_scope = (CPPScope *)this;
  417. if (is_fully_specified()) {
  418. return this_scope;
  419. }
  420. if (_subst_decl_recursive_protect) {
  421. // We're already executing this block.
  422. return this_scope;
  423. }
  424. ((CPPScope *)this)->_subst_decl_recursive_protect = true;
  425. CPPScope *rep = new CPPScope(current_scope, _name, V_public);
  426. bool anything_changed;
  427. if (_parent_scope != nullptr &&
  428. _parent_scope->as_template_scope() != nullptr) {
  429. // If the parent of this scope is a template scope--e.g. this scope has
  430. // template parameters--then we must first remove any of the template
  431. // parameters from the subst list. These will later get substituted
  432. // properly during instantiation.
  433. const CPPTemplateParameterList &p =
  434. _parent_scope->as_template_scope()->_parameters;
  435. CPPDeclaration::SubstDecl new_subst = subst;
  436. CPPTemplateParameterList::Parameters::const_iterator pi;
  437. for (pi = p._parameters.begin(); pi != p._parameters.end(); ++pi) {
  438. new_subst.erase(*pi);
  439. }
  440. anything_changed = copy_substitute_decl(rep, new_subst, global_scope);
  441. } else {
  442. anything_changed = copy_substitute_decl(rep, subst, global_scope);
  443. }
  444. if (!anything_changed && rep->_parent_scope == _parent_scope) {
  445. delete rep;
  446. rep = (CPPScope *)this;
  447. }
  448. ((CPPScope *)this)->_subst_decl_recursive_protect = false;
  449. return rep;
  450. }
  451. /**
  452. *
  453. */
  454. CPPType *CPPScope::
  455. find_type(const string &name, bool recurse) const {
  456. Types::const_iterator ti;
  457. ti = _types.find(name);
  458. if (ti != _types.end()) {
  459. return ti->second;
  460. }
  461. Using::const_iterator ui;
  462. for (ui = _using.begin(); ui != _using.end(); ++ui) {
  463. CPPType *type = (*ui)->find_type(name, false);
  464. if (type != nullptr) {
  465. return type;
  466. }
  467. }
  468. if (_struct_type != nullptr) {
  469. CPPStructType::Derivation::const_iterator di;
  470. for (di = _struct_type->_derivation.begin();
  471. di != _struct_type->_derivation.end();
  472. ++di) {
  473. CPPStructType *st = (*di)._base->as_struct_type();
  474. if (st != nullptr) {
  475. CPPType *type = st->_scope->find_type(name, false);
  476. if (type != nullptr) {
  477. return type;
  478. }
  479. }
  480. }
  481. }
  482. if (recurse && _parent_scope != nullptr) {
  483. return _parent_scope->find_type(name);
  484. }
  485. return nullptr;
  486. }
  487. /**
  488. *
  489. */
  490. CPPType *CPPScope::
  491. find_type(const string &name, CPPDeclaration::SubstDecl &subst,
  492. CPPScope *global_scope, bool recurse) const {
  493. Types::const_iterator ti;
  494. ti = _types.find(name);
  495. if (ti != _types.end()) {
  496. CPPScope *current_scope = (CPPScope *)this;
  497. return (*ti).second->substitute_decl
  498. (subst, current_scope, global_scope)->as_type();
  499. }
  500. Using::const_iterator ui;
  501. for (ui = _using.begin(); ui != _using.end(); ++ui) {
  502. CPPType *type = (*ui)->find_type(name, subst, global_scope, false);
  503. if (type != nullptr) {
  504. return type;
  505. }
  506. }
  507. if (_struct_type != nullptr) {
  508. CPPStructType::Derivation::const_iterator di;
  509. for (di = _struct_type->_derivation.begin();
  510. di != _struct_type->_derivation.end();
  511. ++di) {
  512. CPPStructType *st = (*di)._base->as_struct_type();
  513. if (st != nullptr) {
  514. CPPType *type = st->_scope->find_type(name, subst, global_scope,
  515. false);
  516. if (type != nullptr) {
  517. return type;
  518. }
  519. }
  520. }
  521. }
  522. if (recurse && _parent_scope != nullptr) {
  523. return _parent_scope->find_type(name, subst, global_scope);
  524. }
  525. return nullptr;
  526. }
  527. /**
  528. *
  529. */
  530. CPPScope *CPPScope::
  531. find_scope(const string &name, CPPScope *global_scope, bool recurse) const {
  532. Namespaces::const_iterator ni = _namespaces.find(name);
  533. if (ni != _namespaces.end()) {
  534. return (*ni).second->get_scope();
  535. }
  536. CPPType *type = nullptr;
  537. Types::const_iterator ti;
  538. ti = _types.find(name);
  539. if (ti != _types.end()) {
  540. type = (*ti).second;
  541. // Resolve if this is a typedef or const, or a TBD type.
  542. while (type->get_subtype() == CPPDeclaration::ST_const ||
  543. type->get_subtype() == CPPDeclaration::ST_typedef ||
  544. type->get_subtype() == CPPDeclaration::ST_tbd) {
  545. if (type->as_typedef_type() != nullptr) {
  546. type = type->as_typedef_type()->_type;
  547. } else if (type->as_const_type() != nullptr) {
  548. type = type->as_const_type()->_wrapped_around;
  549. } else {
  550. CPPType *new_type = type->resolve_type((CPPScope *)this, global_scope);
  551. if (new_type != type) {
  552. type = new_type;
  553. } else {
  554. break;
  555. }
  556. }
  557. }
  558. } else if (_struct_type != nullptr) {
  559. CPPStructType::Derivation::const_iterator di;
  560. for (di = _struct_type->_derivation.begin();
  561. di != _struct_type->_derivation.end();
  562. ++di) {
  563. CPPStructType *st = (*di)._base->as_struct_type();
  564. if (st != nullptr) {
  565. type = st->_scope->find_type(name, false);
  566. }
  567. }
  568. }
  569. if (type != nullptr) {
  570. CPPStructType *st = type->as_struct_type();
  571. if (st != nullptr) {
  572. return st->_scope;
  573. }
  574. CPPEnumType *et = type->as_enum_type();
  575. if (et != nullptr) {
  576. return et->_scope;
  577. }
  578. }
  579. Using::const_iterator ui;
  580. for (ui = _using.begin(); ui != _using.end(); ++ui) {
  581. CPPScope *scope = (*ui)->find_scope(name, global_scope, false);
  582. if (scope != nullptr) {
  583. return scope;
  584. }
  585. }
  586. if (recurse && _parent_scope != nullptr) {
  587. return _parent_scope->find_scope(name, global_scope);
  588. }
  589. return nullptr;
  590. }
  591. /**
  592. *
  593. */
  594. CPPScope *CPPScope::
  595. find_scope(const string &name, CPPDeclaration::SubstDecl &subst,
  596. CPPScope *global_scope, bool recurse) const {
  597. CPPType *type = find_type(name, subst, global_scope, recurse);
  598. if (type == nullptr) {
  599. return nullptr;
  600. }
  601. // Resolve if this is a typedef or const.
  602. while (type->get_subtype() == CPPDeclaration::ST_const ||
  603. type->get_subtype() == CPPDeclaration::ST_typedef) {
  604. if (type->as_typedef_type() != nullptr) {
  605. type = type->as_typedef_type()->_type;
  606. } else {
  607. type = type->as_const_type()->_wrapped_around;
  608. }
  609. }
  610. CPPStructType *st = type->as_struct_type();
  611. if (st != nullptr) {
  612. return st->_scope;
  613. }
  614. CPPEnumType *et = type->as_enum_type();
  615. if (et != nullptr) {
  616. return et->_scope;
  617. }
  618. return nullptr;
  619. }
  620. /**
  621. *
  622. */
  623. CPPDeclaration *CPPScope::
  624. find_symbol(const string &name, bool recurse) const {
  625. if (_struct_type != nullptr && name == get_simple_name()) {
  626. return _struct_type;
  627. }
  628. Functions::const_iterator fi;
  629. fi = _functions.find(name);
  630. if (fi != _functions.end()) {
  631. return (*fi).second;
  632. }
  633. Types::const_iterator ti;
  634. ti = _types.find(name);
  635. if (ti != _types.end()) {
  636. return (*ti).second;
  637. }
  638. Variables::const_iterator vi;
  639. vi = _variables.find(name);
  640. if (vi != _variables.end()) {
  641. return (*vi).second;
  642. }
  643. vi = _enum_values.find(name);
  644. if (vi != _enum_values.end()) {
  645. return (*vi).second;
  646. }
  647. Using::const_iterator ui;
  648. for (ui = _using.begin(); ui != _using.end(); ++ui) {
  649. CPPDeclaration *decl = (*ui)->find_symbol(name, false);
  650. if (decl != nullptr) {
  651. return decl;
  652. }
  653. }
  654. if (_struct_type != nullptr) {
  655. CPPStructType::Derivation::const_iterator di;
  656. for (di = _struct_type->_derivation.begin();
  657. di != _struct_type->_derivation.end();
  658. ++di) {
  659. CPPStructType *st = (*di)._base->as_struct_type();
  660. if (st != nullptr) {
  661. CPPDeclaration *decl = st->_scope->find_symbol(name, false);
  662. if (decl != nullptr) {
  663. return decl;
  664. }
  665. }
  666. }
  667. }
  668. if (recurse && _parent_scope != nullptr) {
  669. return _parent_scope->find_symbol(name);
  670. }
  671. return nullptr;
  672. }
  673. /**
  674. *
  675. */
  676. CPPDeclaration *CPPScope::
  677. find_template(const string &name, bool recurse) const {
  678. Templates::const_iterator ti;
  679. ti = _templates.find(name);
  680. if (ti != _templates.end()) {
  681. return (*ti).second;
  682. }
  683. Using::const_iterator ui;
  684. for (ui = _using.begin(); ui != _using.end(); ++ui) {
  685. CPPDeclaration *decl = (*ui)->find_template(name, false);
  686. if (decl != nullptr) {
  687. return decl;
  688. }
  689. }
  690. if (_struct_type != nullptr) {
  691. CPPStructType::Derivation::const_iterator di;
  692. for (di = _struct_type->_derivation.begin();
  693. di != _struct_type->_derivation.end();
  694. ++di) {
  695. CPPStructType *st = (*di)._base->as_struct_type();
  696. if (st != nullptr) {
  697. CPPDeclaration *decl = st->_scope->find_template(name, false);
  698. if (decl != nullptr) {
  699. return decl;
  700. }
  701. }
  702. }
  703. }
  704. if (recurse && _parent_scope != nullptr) {
  705. return _parent_scope->find_template(name);
  706. }
  707. return nullptr;
  708. }
  709. /**
  710. *
  711. */
  712. string CPPScope::
  713. get_simple_name() const {
  714. /*
  715. if (_struct_type != (CPPStructType *)NULL) {
  716. return _struct_type->get_simple_name();
  717. }
  718. */
  719. return _name.get_name();
  720. }
  721. /**
  722. *
  723. */
  724. string CPPScope::
  725. get_local_name(CPPScope *scope) const {
  726. /*
  727. if (_struct_type != (CPPStructType *)NULL) {
  728. return _struct_type->get_local_name(scope);
  729. }
  730. */
  731. if (scope != nullptr && _parent_scope != nullptr/* && _parent_scope != scope*/) {
  732. string parent_scope_name = _parent_scope->get_local_name(scope);
  733. if (parent_scope_name.empty()) {
  734. return _name.get_name_with_templ();
  735. } else {
  736. return parent_scope_name + "::" +
  737. _name.get_name_with_templ();
  738. }
  739. } else {
  740. return _name.get_name_with_templ();
  741. }
  742. }
  743. /**
  744. *
  745. */
  746. string CPPScope::
  747. get_fully_scoped_name() const {
  748. /*
  749. if (_struct_type != (CPPStructType *)NULL) {
  750. return _struct_type->get_fully_scoped_name();
  751. }
  752. */
  753. if (_parent_scope != nullptr) {
  754. return _parent_scope->get_fully_scoped_name() + "::" +
  755. _name.get_name_with_templ();
  756. } else {
  757. return _name.get_name_with_templ();
  758. }
  759. }
  760. /**
  761. *
  762. */
  763. void CPPScope::
  764. output(ostream &out, CPPScope *scope) const {
  765. // out << get_local_name(scope);
  766. if (_parent_scope != nullptr && _parent_scope != scope) {
  767. _parent_scope->output(out, scope);
  768. out << "::";
  769. }
  770. out << _name;
  771. }
  772. /**
  773. *
  774. */
  775. void CPPScope::
  776. write(ostream &out, int indent_level, CPPScope *scope) const {
  777. CPPVisibility vis = V_unknown;
  778. Declarations::const_iterator di;
  779. for (di = _declarations.begin(); di != _declarations.end(); ++di) {
  780. CPPDeclaration *cd = (*di);
  781. if (cd->_vis != vis && indent_level > 0) {
  782. vis = cd->_vis;
  783. indent(out, indent_level - 2) << vis << ":\n";
  784. }
  785. bool complete = false;
  786. if (cd->as_type() != nullptr || cd->as_namespace() != nullptr) {
  787. complete = true;
  788. }
  789. indent(out, indent_level);
  790. cd->output(out, indent_level, scope, complete);
  791. out << ";\n";
  792. }
  793. }
  794. /**
  795. * Returns the nearest ancestor of this scope that is a template scope, or
  796. * NULL if the scope is fully specified.
  797. */
  798. CPPTemplateScope *CPPScope::
  799. get_template_scope() {
  800. if (as_template_scope()) {
  801. return as_template_scope();
  802. }
  803. if (_parent_scope != nullptr) {
  804. return _parent_scope->get_template_scope();
  805. }
  806. return nullptr;
  807. }
  808. /**
  809. *
  810. */
  811. CPPTemplateScope *CPPScope::
  812. as_template_scope() {
  813. return nullptr;
  814. }
  815. /**
  816. * This is in support of both substitute_decl() and instantiate(). It's
  817. * similar in purpose to substitute_decl(), but this function assumes the
  818. * caller has already created a new, empty scope. All of the declarations in
  819. * this scope are copied to the new scope, filtering through the subst decl.
  820. *
  821. * The return value is true if the scope is changed, false if it is not.
  822. */
  823. bool CPPScope::
  824. copy_substitute_decl(CPPScope *to_scope, CPPDeclaration::SubstDecl &subst,
  825. CPPScope *global_scope) const {
  826. bool anything_changed = false;
  827. if (_struct_type != nullptr) {
  828. CPPScope *native_scope = nullptr;
  829. if (_struct_type->_ident != nullptr) {
  830. native_scope = _struct_type->_ident->_native_scope;
  831. }
  832. to_scope->_struct_type =
  833. new CPPStructType(_struct_type->_type,
  834. new CPPIdentifier(to_scope->_name, _struct_type->_file),
  835. native_scope, to_scope, _struct_type->_file);
  836. to_scope->_struct_type->_incomplete = false;
  837. // Copy the derivation to the new type.
  838. CPPStructType::Derivation::const_iterator di;
  839. for (di = _struct_type->_derivation.begin();
  840. di != _struct_type->_derivation.end();
  841. ++di) {
  842. CPPStructType::Base b = (*di);
  843. b._base =
  844. (*di)._base->substitute_decl(subst, to_scope, global_scope)->as_type();
  845. to_scope->_struct_type->_derivation.push_back(b);
  846. if (b._base != (*di)._base) {
  847. anything_changed = true;
  848. }
  849. }
  850. }
  851. Declarations::const_iterator di;
  852. for (di = _declarations.begin(); di != _declarations.end(); ++di) {
  853. CPPDeclaration *decl =
  854. (*di)->substitute_decl(subst, to_scope, global_scope);
  855. to_scope->_declarations.push_back(decl);
  856. if (decl != (*di)) {
  857. anything_changed = true;
  858. }
  859. }
  860. ExtensionTypes::const_iterator ei;
  861. for (ei = _structs.begin(); ei != _structs.end(); ++ei) {
  862. string name = (*ei).first;
  863. CPPType *source_type = (*ei).second;
  864. CPPDeclaration *decl =
  865. source_type->substitute_decl(subst, to_scope, global_scope);
  866. assert(decl != nullptr);
  867. CPPType *new_type = decl->as_type();
  868. assert(new_type != nullptr);
  869. to_scope->_structs.insert(ExtensionTypes::value_type(name, new_type));
  870. if (new_type != source_type) {
  871. anything_changed = true;
  872. }
  873. }
  874. for (ei = _classes.begin(); ei != _classes.end(); ++ei) {
  875. string name = (*ei).first;
  876. CPPType *source_type = (*ei).second;
  877. CPPDeclaration *decl =
  878. source_type->substitute_decl(subst, to_scope, global_scope);
  879. assert(decl != nullptr);
  880. CPPType *new_type = decl->as_type();
  881. assert(new_type != nullptr);
  882. to_scope->_classes.insert(ExtensionTypes::value_type(name, new_type));
  883. if (new_type != source_type) {
  884. anything_changed = true;
  885. }
  886. }
  887. for (ei = _unions.begin(); ei != _unions.end(); ++ei) {
  888. string name = (*ei).first;
  889. CPPType *source_type = (*ei).second;
  890. CPPDeclaration *decl =
  891. source_type->substitute_decl(subst, to_scope, global_scope);
  892. assert(decl != nullptr);
  893. CPPType *new_type = decl->as_type();
  894. assert(new_type != nullptr);
  895. to_scope->_unions.insert(ExtensionTypes::value_type(name, new_type));
  896. if (new_type != source_type) {
  897. anything_changed = true;
  898. }
  899. }
  900. for (ei = _enums.begin(); ei != _enums.end(); ++ei) {
  901. string name = (*ei).first;
  902. CPPType *source_type = (*ei).second;
  903. CPPDeclaration *decl =
  904. source_type->substitute_decl(subst, to_scope, global_scope);
  905. assert(decl != nullptr);
  906. CPPType *new_type = decl->as_type();
  907. assert(new_type != nullptr);
  908. to_scope->_enums.insert(ExtensionTypes::value_type(name, new_type));
  909. if (new_type != source_type) {
  910. anything_changed = true;
  911. }
  912. }
  913. Functions::const_iterator fi;
  914. for (fi = _functions.begin(); fi != _functions.end(); ++fi) {
  915. CPPFunctionGroup *fgroup = (*fi).second;
  916. string name = fgroup->_name;
  917. CPPFunctionGroup *&to_fgroup = to_scope->_functions[name];
  918. if (to_fgroup == nullptr) {
  919. to_fgroup = new CPPFunctionGroup(name);
  920. }
  921. CPPFunctionGroup::Instances::const_iterator ii;
  922. for (ii = fgroup->_instances.begin();
  923. ii != fgroup->_instances.end();
  924. ++ii) {
  925. CPPInstance *inst =
  926. (*ii)->substitute_decl(subst, to_scope, global_scope)->as_instance();
  927. to_fgroup->_instances.push_back(inst);
  928. if (inst != (*ii)) {
  929. anything_changed = true;
  930. }
  931. }
  932. }
  933. Types::const_iterator ti;
  934. for (ti = _types.begin(); ti != _types.end(); ++ti) {
  935. CPPType *td =
  936. (*ti).second->substitute_decl(subst, to_scope, global_scope)->as_type();
  937. to_scope->_types.insert(Types::value_type((*ti).first, td));
  938. if (td != (*ti).second) {
  939. anything_changed = true;
  940. }
  941. }
  942. Variables::const_iterator vi;
  943. for (vi = _variables.begin(); vi != _variables.end(); ++vi) {
  944. CPPInstance *inst =
  945. (*vi).second->substitute_decl(subst, to_scope, global_scope)->as_instance();
  946. to_scope->_variables.insert(Variables::value_type((*vi).first, inst));
  947. if (inst != (*vi).second) {
  948. anything_changed = true;
  949. }
  950. }
  951. for (vi = _enum_values.begin(); vi != _enum_values.end(); ++vi) {
  952. CPPInstance *inst =
  953. (*vi).second->substitute_decl(subst, to_scope, global_scope)->as_instance();
  954. to_scope->_enum_values.insert(Variables::value_type((*vi).first, inst));
  955. if (inst != (*vi).second) {
  956. anything_changed = true;
  957. }
  958. }
  959. Templates::const_iterator tmi;
  960. for (tmi = _templates.begin(); tmi != _templates.end(); ++tmi) {
  961. CPPDeclaration *decl =
  962. (*tmi).second->substitute_decl(subst, to_scope, global_scope);
  963. to_scope->_templates.insert(Templates::value_type((*tmi).first, decl));
  964. if (decl != (*tmi).second) {
  965. anything_changed = true;
  966. }
  967. }
  968. return anything_changed;
  969. }
  970. /**
  971. * Does the right thing with a newly given declaration: adds it to the typedef
  972. * list, or variables or functions, or whatever.
  973. */
  974. void CPPScope::
  975. handle_declaration(CPPDeclaration *decl, CPPScope *global_scope,
  976. CPPPreprocessor *error_sink) {
  977. CPPTypedefType *def = decl->as_typedef_type();
  978. if (def != nullptr) {
  979. define_typedef_type(def, error_sink);
  980. CPPExtensionType *et = def->_type->as_extension_type();
  981. if (et != nullptr) {
  982. define_extension_type(et, error_sink);
  983. }
  984. return;
  985. }
  986. CPPTypeDeclaration *typedecl = decl->as_type_declaration();
  987. if (typedecl != nullptr) {
  988. CPPExtensionType *et = typedecl->_type->as_extension_type();
  989. if (et != nullptr) {
  990. define_extension_type(et, error_sink);
  991. }
  992. return;
  993. }
  994. CPPInstance *inst = decl->as_instance();
  995. if (inst != nullptr) {
  996. inst->check_for_constructor(this, global_scope);
  997. if (inst->_ident != nullptr) {
  998. // Not sure if this is the best place to assign this. However, this
  999. // fixes a bug with variables in expressions not having the proper
  1000. // scoping prefix. ~rdb
  1001. inst->_ident->_native_scope = this;
  1002. }
  1003. string name = inst->get_simple_name();
  1004. if (!name.empty() && inst->get_scope(this, global_scope) == this) {
  1005. if (inst->_type->as_function_type()) {
  1006. // This is a function declaration; hence it gets added to the
  1007. // _functions member. But we must be careful to share common-named
  1008. // functions.
  1009. CPPFunctionGroup *fgroup;
  1010. Functions::const_iterator fi;
  1011. fi = _functions.find(name);
  1012. if (fi == _functions.end()) {
  1013. fgroup = new CPPFunctionGroup(name);
  1014. _functions.insert(Functions::value_type(name, fgroup));
  1015. } else {
  1016. fgroup = (*fi).second;
  1017. }
  1018. fgroup->_instances.push_back(inst);
  1019. } else {
  1020. // This is not a function declaration; hence it gets added to the
  1021. // _variables member.
  1022. _variables[name] = inst;
  1023. }
  1024. if (inst->is_template()) {
  1025. // Don't add a new template definition if we already had one by the
  1026. // same name in another scope.
  1027. if (find_template(name) == nullptr) {
  1028. _templates.insert(Templates::value_type(name, inst));
  1029. }
  1030. /*
  1031. if (inst->_type->as_function_type() == NULL ||
  1032. (inst->_type->as_function_type()->_flags &
  1033. CPPFunctionType::F_constructor) == 0) {
  1034. _templates.insert(Templates::value_type(name, inst));
  1035. }
  1036. */
  1037. }
  1038. }
  1039. return;
  1040. }
  1041. CPPExtensionType *et = decl->as_extension_type();
  1042. if (et != nullptr) {
  1043. define_extension_type(et, error_sink);
  1044. }
  1045. }