2
0

cppScope.cxx 38 KB

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