cppScope.cxx 34 KB

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