cppScope.cxx 33 KB

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