cppStructType.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // Filename: cppStructType.cxx
  2. // Created by: drose (19Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, 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://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "cppStructType.h"
  19. #include "cppTypedef.h"
  20. #include "cppScope.h"
  21. #include "cppTypeProxy.h"
  22. #include "cppTemplateScope.h"
  23. #include "cppFunctionGroup.h"
  24. #include "cppFunctionType.h"
  25. #include "cppTBDType.h"
  26. #include "indent.h"
  27. #include "cppParser.h"
  28. ////////////////////////////////////////////////////////////////////
  29. // Function: CPPStructType::Base::output
  30. // Access: Public
  31. // Description:
  32. ////////////////////////////////////////////////////////////////////
  33. void CPPStructType::Base::
  34. output(ostream &out) const {
  35. if (_is_virtual) {
  36. out << "virtual ";
  37. }
  38. out << _vis << " " << *_base;
  39. }
  40. ////////////////////////////////////////////////////////////////////
  41. // Function: CPPStructType::Constructor
  42. // Access: Public
  43. // Description:
  44. ////////////////////////////////////////////////////////////////////
  45. CPPStructType::
  46. CPPStructType(CPPStructType::Type type, CPPIdentifier *ident,
  47. CPPScope *current_scope, CPPScope *scope,
  48. const CPPFile &file) :
  49. CPPExtensionType(type, ident, current_scope, file),
  50. _scope(scope)
  51. {
  52. _subst_decl_recursive_protect = false;
  53. _incomplete = true;
  54. }
  55. ////////////////////////////////////////////////////////////////////
  56. // Function: CPPStructType::Copy Constructor
  57. // Access: Public
  58. // Description:
  59. ////////////////////////////////////////////////////////////////////
  60. CPPStructType::
  61. CPPStructType(const CPPStructType &copy) :
  62. CPPExtensionType(copy),
  63. _scope(copy._scope),
  64. _incomplete(copy._incomplete),
  65. _derivation(copy._derivation)
  66. {
  67. _subst_decl_recursive_protect = false;
  68. }
  69. ////////////////////////////////////////////////////////////////////
  70. // Function: CPPStructType::Copy Assignment Operator
  71. // Access: Public
  72. // Description:
  73. ////////////////////////////////////////////////////////////////////
  74. void CPPStructType::
  75. operator = (const CPPStructType &copy) {
  76. CPPExtensionType::operator = (copy);
  77. _scope = copy._scope;
  78. _incomplete = copy._incomplete;
  79. _derivation = copy._derivation;
  80. }
  81. ////////////////////////////////////////////////////////////////////
  82. // Function: CPPStructType::append_derivation
  83. // Access: Public
  84. // Description: A handy function used while parsing to add a new base
  85. // class to the list of classes (or structs) this class
  86. // derives from.
  87. ////////////////////////////////////////////////////////////////////
  88. void CPPStructType::
  89. append_derivation(CPPType *base, CPPVisibility vis, bool is_virtual) {
  90. if (base != NULL) {
  91. Base b;
  92. b._base = base;
  93. b._vis = vis;
  94. b._is_virtual = is_virtual;
  95. _derivation.push_back(b);
  96. }
  97. }
  98. ////////////////////////////////////////////////////////////////////
  99. // Function: CPPStructType::get_scope
  100. // Access: Public
  101. // Description:
  102. ////////////////////////////////////////////////////////////////////
  103. CPPScope *CPPStructType::
  104. get_scope() const {
  105. return _scope;
  106. }
  107. ////////////////////////////////////////////////////////////////////
  108. // Function: CPPStructType::is_abstract
  109. // Access: Public
  110. // Description: Returns true if this struct declaration is abstract,
  111. // e.g. it contains or inherits at least one method that
  112. // is pure virtual.
  113. ////////////////////////////////////////////////////////////////////
  114. bool CPPStructType::
  115. is_abstract() const {
  116. VFunctions funcs;
  117. get_pure_virtual_funcs(funcs);
  118. return !funcs.empty();
  119. }
  120. ////////////////////////////////////////////////////////////////////
  121. // Function: CPPStructType::check_virtual
  122. // Access: Public
  123. // Description: Ensures all functions are correctly marked with the
  124. // "virtual" flag if they are truly virtual by virtue of
  125. // inheritance, rather than simply being labeled
  126. // virtual.
  127. //
  128. // This also sets the CPPInstance::SC_inherited_virtual
  129. // flags on those virtual methods that override a
  130. // virtual method defined in a parent class (as opposed
  131. // to those that appear for this first time in this
  132. // class). It is sometimes useful to know whether a
  133. // given virtual method represents the first time that
  134. // particular method appears.
  135. //
  136. // The return value is true if this class defines or
  137. // inherits any virtual methods (and thus requires a
  138. // virtual function pointer), or false otherwise.
  139. ////////////////////////////////////////////////////////////////////
  140. bool CPPStructType::
  141. check_virtual() {
  142. VFunctions funcs;
  143. get_virtual_funcs(funcs);
  144. return !funcs.empty();
  145. }
  146. ////////////////////////////////////////////////////////////////////
  147. // Function: CPPStructType::is_fully_specified
  148. // Access: Public, Virtual
  149. // Description: Returns true if this declaration is an actual,
  150. // factual declaration, or false if some part of the
  151. // declaration depends on a template parameter which has
  152. // not yet been instantiated.
  153. ////////////////////////////////////////////////////////////////////
  154. bool CPPStructType::
  155. is_fully_specified() const {
  156. if (_scope != NULL && !_scope->is_fully_specified()) {
  157. return false;
  158. }
  159. return CPPType::is_fully_specified();
  160. }
  161. ////////////////////////////////////////////////////////////////////
  162. // Function: CPPStructType::is_incomplete
  163. // Access: Public, Virtual
  164. // Description: Returns true if the type has not yet been fully
  165. // specified, false if it has.
  166. ////////////////////////////////////////////////////////////////////
  167. bool CPPStructType::
  168. is_incomplete() const {
  169. return _incomplete;
  170. }
  171. ////////////////////////////////////////////////////////////////////
  172. // Function: CPPStructType::get_destructor
  173. // Access: Public
  174. // Description: Returns the destructor defined for the struct type,
  175. // if any, or NULL if no destructor is found.
  176. ////////////////////////////////////////////////////////////////////
  177. CPPInstance *CPPStructType::
  178. get_destructor() const {
  179. // Iterate through all the functions that begin with '~' until we
  180. // find one that claims to be a destructor. In theory, there should
  181. // only be one such function.
  182. CPPScope::Functions::const_iterator fi;
  183. fi = _scope->_functions.lower_bound("~");
  184. while (fi != _scope->_functions.end() &&
  185. (*fi).first[0] == '~') {
  186. CPPFunctionGroup *fgroup = (*fi).second;
  187. CPPFunctionGroup::Instances::const_iterator ii;
  188. for (ii = fgroup->_instances.begin();
  189. ii != fgroup->_instances.end();
  190. ++ii) {
  191. CPPInstance *inst = (*ii);
  192. assert(inst->_type != (CPPType *)NULL);
  193. CPPFunctionType *ftype = inst->_type->as_function_type();
  194. assert(ftype != (CPPFunctionType *)NULL);
  195. if ((ftype->_flags & CPPFunctionType::F_destructor) != 0) {
  196. return inst;
  197. }
  198. }
  199. ++fi;
  200. }
  201. return (CPPInstance *)NULL;
  202. }
  203. ////////////////////////////////////////////////////////////////////
  204. // Function: CPPStructType::instantiate
  205. // Access: Public, Virtual
  206. // Description:
  207. ////////////////////////////////////////////////////////////////////
  208. CPPDeclaration *CPPStructType::
  209. instantiate(const CPPTemplateParameterList *actual_params,
  210. CPPScope *current_scope, CPPScope *global_scope,
  211. CPPPreprocessor *error_sink) const {
  212. // I *think* this assertion is no longer valid. Who knows.
  213. // assert(!_incomplete);
  214. if (_scope == NULL) {
  215. if (error_sink != NULL) {
  216. error_sink->warning("Ignoring template parameters for class " +
  217. get_local_name());
  218. }
  219. return (CPPDeclaration *)this;
  220. }
  221. CPPScope *scope =
  222. _scope->instantiate(actual_params, current_scope, global_scope, error_sink);
  223. if (scope->get_struct_type()->get_scope() != scope) {
  224. // Hmm, this type seems to be not completely defined. We must be
  225. // in the middle of recursively instantiating the scope. Thus, we
  226. // don't yet know what its associated struct type will be.
  227. // Postpone the evaluation of this type.
  228. CPPIdentifier *ident = new CPPIdentifier(get_fully_scoped_name());
  229. return CPPType::new_type(new CPPTBDType(ident));
  230. }
  231. CPPType *result = scope->get_struct_type();
  232. result = CPPType::new_type(result);
  233. if (result != (CPPType *)this) {
  234. // This really means the method ought to be non-const. But I'm
  235. // too lazy to propagate this change all the way back right now,
  236. // so this hack is here.
  237. ((CPPStructType *)this)->_instantiations.insert(result);
  238. }
  239. return result;
  240. }
  241. ////////////////////////////////////////////////////////////////////
  242. // Function: CPPStructType::substitute_decl
  243. // Access: Public, Virtual
  244. // Description:
  245. ////////////////////////////////////////////////////////////////////
  246. CPPDeclaration *CPPStructType::
  247. substitute_decl(CPPDeclaration::SubstDecl &subst,
  248. CPPScope *current_scope, CPPScope *global_scope) {
  249. SubstDecl::const_iterator si = subst.find(this);
  250. if (si != subst.end()) {
  251. assert((*si).second != NULL);
  252. return (*si).second;
  253. }
  254. if (_incomplete) {
  255. // We haven't finished defining the class yet.
  256. return this;
  257. }
  258. if (_subst_decl_recursive_protect) {
  259. // We're already executing this block; we'll have to return a
  260. // proxy to the type which we'll define later.
  261. CPPTypeProxy *proxy = new CPPTypeProxy;
  262. _proxies.push_back(proxy);
  263. assert(proxy != NULL);
  264. return proxy;
  265. }
  266. _subst_decl_recursive_protect = true;
  267. CPPStructType *rep = new CPPStructType(*this);
  268. if (_ident != NULL) {
  269. rep->_ident =
  270. _ident->substitute_decl(subst, current_scope, global_scope);
  271. }
  272. if (_scope != NULL) {
  273. rep->_scope =
  274. _scope->substitute_decl(subst, current_scope, global_scope);
  275. if (rep->_scope != _scope) {
  276. rep->_scope->set_struct_type(rep);
  277. // If we just instantiated a template scope, write the template
  278. // parameters into our identifier.
  279. CPPScope *pscope = rep->_scope->get_parent_scope();
  280. if (pscope != (CPPScope *)NULL &&
  281. pscope->_name.has_templ()) {
  282. // If the struct name didn't have an explicit template
  283. // reference before, now it does.
  284. if (!_ident->_names.empty() && !_ident->_names.back().has_templ()) {
  285. if (rep->is_template()) {
  286. rep->_template_scope = (CPPTemplateScope *)NULL;
  287. CPPNameComponent nc(get_simple_name());
  288. nc.set_templ(pscope->_name.get_templ());
  289. rep->_ident = new CPPIdentifier(nc);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. bool unchanged =
  296. (rep->_ident == _ident && rep->_scope == _scope);
  297. for (int i = 0; i < (int)_derivation.size(); ++i) {
  298. rep->_derivation[i]._base =
  299. _derivation[i]._base->substitute_decl(subst, current_scope, global_scope)->as_type();
  300. if (rep->_derivation[i]._base != _derivation[i]._base) {
  301. unchanged = false;
  302. }
  303. }
  304. if (unchanged) {
  305. delete rep;
  306. rep = this;
  307. }
  308. subst.insert(SubstDecl::value_type(this, rep));
  309. _subst_decl_recursive_protect = false;
  310. // Now fill in all the proxies we created for our recursive
  311. // references.
  312. Proxies::iterator pi;
  313. for (pi = _proxies.begin(); pi != _proxies.end(); ++pi) {
  314. (*pi)->_actual_type = rep;
  315. }
  316. assert(rep != NULL);
  317. rep = CPPType::new_type(rep)->as_struct_type();
  318. assert(rep != NULL);
  319. if (rep != this) {
  320. _instantiations.insert(rep);
  321. // cerr << "Subst for " << *this << " is " << *rep << "\n";
  322. }
  323. return rep;
  324. }
  325. ////////////////////////////////////////////////////////////////////
  326. // Function: CPPStructType::output
  327. // Access: Public, Virtual
  328. // Description:
  329. ////////////////////////////////////////////////////////////////////
  330. void CPPStructType::
  331. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  332. if (!complete && _ident != NULL) {
  333. // If we have a name, use it.
  334. if (cppparser_output_class_keyword) {
  335. out << _type << " ";
  336. }
  337. out << _ident->get_local_name(scope);
  338. if (is_template()) {
  339. CPPTemplateScope *tscope = get_template_scope();
  340. out << "< ";
  341. tscope->_parameters.output(out, scope);
  342. out << " >";
  343. }
  344. } else if (!complete && !_typedefs.empty()) {
  345. // If we have a typedef name, use it.
  346. out << _typedefs.front()->get_local_name(scope);
  347. } else {
  348. if (is_template()) {
  349. get_template_scope()->_parameters.write_formal(out, scope);
  350. indent(out, indent_level);
  351. }
  352. if (_ident != NULL) {
  353. out << _type << " " << _ident->get_local_name(scope);
  354. } else {
  355. out << _type;
  356. }
  357. // Show any derivation we may have
  358. if (!_derivation.empty()) {
  359. Derivation::const_iterator di = _derivation.begin();
  360. out << ": " << *di;
  361. ++di;
  362. while (di != _derivation.end()) {
  363. out << ", " << *di;
  364. ++di;
  365. }
  366. }
  367. out << " {\n";
  368. _scope->write(out, indent_level + 2, _scope);
  369. indent(out, indent_level) << "}";
  370. }
  371. }
  372. ////////////////////////////////////////////////////////////////////
  373. // Function: CPPStructType::get_subtype
  374. // Access: Public, Virtual
  375. // Description:
  376. ////////////////////////////////////////////////////////////////////
  377. CPPDeclaration::SubType CPPStructType::
  378. get_subtype() const {
  379. return ST_struct;
  380. }
  381. ////////////////////////////////////////////////////////////////////
  382. // Function: CPPStructType::as_struct_type
  383. // Access: Public, Virtual
  384. // Description:
  385. ////////////////////////////////////////////////////////////////////
  386. CPPStructType *CPPStructType::
  387. as_struct_type() {
  388. return this;
  389. }
  390. ////////////////////////////////////////////////////////////////////
  391. // Function: CPPStructType::get_virtual_funcs
  392. // Access: Public
  393. // Description: Fills funcs up with a list of all the virtual
  394. // function declarations (pure-virtual or otherwise)
  395. // defined at or above this class. This is used to
  396. // determine which functions in a given class are
  397. // actually virtual, since a function is virtual whose
  398. // parent class holds a virtual function by the same
  399. // name, whether or not it is actually declared virtual
  400. // in the derived class.
  401. ////////////////////////////////////////////////////////////////////
  402. void CPPStructType::
  403. get_virtual_funcs(VFunctions &funcs) const {
  404. // First, get all the virtual funcs from our parents.
  405. Derivation::const_iterator di;
  406. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  407. VFunctions vf;
  408. CPPStructType *base = (*di)._base->as_struct_type();
  409. if (base != NULL) {
  410. base->get_virtual_funcs(vf);
  411. funcs.splice(funcs.end(), vf);
  412. }
  413. }
  414. // Now look for matching functions in this class that we can now
  415. // infer are virtual.
  416. VFunctions::iterator vfi, vfnext;
  417. vfi = funcs.begin();
  418. while (vfi != funcs.end()) {
  419. vfnext = vfi;
  420. ++vfnext;
  421. CPPInstance *inst = (*vfi);
  422. assert(inst->_type != (CPPType *)NULL);
  423. CPPFunctionType *base_ftype = inst->_type->as_function_type();
  424. assert(base_ftype != (CPPFunctionType *)NULL);
  425. if ((base_ftype->_flags & CPPFunctionType::F_destructor) != 0) {
  426. // Match destructor-for-destructor; don't try to match
  427. // destructors up by name.
  428. CPPInstance *destructor = get_destructor();
  429. if (destructor != (CPPInstance *)NULL) {
  430. // It's a match! This destructor is virtual.
  431. funcs.erase(vfi);
  432. destructor->_storage_class |=
  433. (CPPInstance::SC_virtual | CPPInstance::SC_inherited_virtual);
  434. }
  435. } else {
  436. // Non-destructors we can try to match up by name.
  437. string fname = inst->get_local_name();
  438. CPPScope::Functions::const_iterator fi;
  439. fi = _scope->_functions.find(fname);
  440. if (fi != _scope->_functions.end()) {
  441. CPPFunctionGroup *fgroup = (*fi).second;
  442. // Look for a matching function amid this group.
  443. bool match_found = false;
  444. CPPFunctionGroup::Instances::const_iterator ii;
  445. for (ii = fgroup->_instances.begin();
  446. ii != fgroup->_instances.end() && !match_found;
  447. ++ii) {
  448. CPPInstance *new_inst = (*ii);
  449. assert(new_inst->_type != (CPPType *)NULL);
  450. CPPFunctionType *new_ftype = new_inst->_type->as_function_type();
  451. assert(new_ftype != (CPPFunctionType *)NULL);
  452. if (new_ftype->is_equivalent_function(*base_ftype)) {
  453. // It's a match! We now know it's virtual. Erase this
  454. // function from the list, so we can add it back in below.
  455. funcs.erase(vfi);
  456. match_found = true;
  457. // In fact, it's not only definitely virtual, but it's
  458. // *inherited* virtual, which means only that the
  459. // interface is defined in some parent class. Sometimes
  460. // this is useful to know.
  461. new_inst->_storage_class |=
  462. (CPPInstance::SC_virtual | CPPInstance::SC_inherited_virtual);
  463. }
  464. }
  465. }
  466. }
  467. vfi = vfnext;
  468. }
  469. // Finally, look for more virtual function definitions.
  470. CPPScope::Functions::const_iterator fi;
  471. for (fi = _scope->_functions.begin();
  472. fi != _scope->_functions.end();
  473. ++fi) {
  474. CPPFunctionGroup *fgroup = (*fi).second;
  475. CPPFunctionGroup::Instances::const_iterator ii;
  476. for (ii = fgroup->_instances.begin();
  477. ii != fgroup->_instances.end();
  478. ++ii) {
  479. CPPInstance *inst = (*ii);
  480. if ((inst->_storage_class & CPPInstance::SC_virtual) != 0) {
  481. // Here's a virtual function.
  482. funcs.push_back(inst);
  483. }
  484. }
  485. }
  486. }
  487. ////////////////////////////////////////////////////////////////////
  488. // Function: CPPStructType::count_pure_virtual_funcs
  489. // Access: Public
  490. // Description: Fills funcs up with a list of all the pure virtual
  491. // function declarations defined at or above this class
  492. // that have not been given definitions.
  493. ////////////////////////////////////////////////////////////////////
  494. void CPPStructType::
  495. get_pure_virtual_funcs(VFunctions &funcs) const {
  496. // First, get all the virtual functions.
  497. VFunctions vfuncs;
  498. get_virtual_funcs(vfuncs);
  499. // Now traverse the list, getting out those functions that are pure
  500. // virtual.
  501. VFunctions::iterator vfi;
  502. for (vfi = vfuncs.begin(); vfi != vfuncs.end(); ++vfi) {
  503. CPPInstance *inst = (*vfi);
  504. if ((inst->_storage_class & CPPInstance::SC_pure_virtual) != 0) {
  505. funcs.push_back(inst);
  506. }
  507. }
  508. }
  509. ////////////////////////////////////////////////////////////////////
  510. // Function: CPPStructType::is_equal
  511. // Access: Protected, Virtual
  512. // Description: Called by CPPDeclaration to determine whether this
  513. // type is equivalent to another type of the same type.
  514. ////////////////////////////////////////////////////////////////////
  515. bool CPPStructType::
  516. is_equal(const CPPDeclaration *other) const {
  517. return CPPDeclaration::is_equal(other);
  518. /*
  519. const CPPStructType *ot = ((CPPDeclaration *)other)->as_struct_type();
  520. assert(ot != NULL);
  521. return this == ot ||
  522. (get_fully_scoped_name() == ot->get_fully_scoped_name());
  523. */
  524. }
  525. ////////////////////////////////////////////////////////////////////
  526. // Function: CPPStructType::is_less
  527. // Access: Protected, Virtual
  528. // Description: Called by CPPDeclaration to determine whether this
  529. // type should be ordered before another type of the
  530. // same type, in an arbitrary but fixed ordering.
  531. ////////////////////////////////////////////////////////////////////
  532. bool CPPStructType::
  533. is_less(const CPPDeclaration *other) const {
  534. return CPPDeclaration::is_less(other);
  535. /*
  536. const CPPStructType *ot = ((CPPDeclaration *)other)->as_struct_type();
  537. assert(ot != NULL);
  538. if (this == ot) {
  539. return false;
  540. }
  541. return
  542. (get_fully_scoped_name() < ot->get_fully_scoped_name());
  543. */
  544. }