cppScope.cxx 32 KB

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