cppStructType.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppStructType.cxx
  10. * @author drose
  11. * @date 1999-10-19
  12. */
  13. #include "cppStructType.h"
  14. #include "cppTypedefType.h"
  15. #include "cppScope.h"
  16. #include "cppTypeProxy.h"
  17. #include "cppTemplateScope.h"
  18. #include "cppFunctionGroup.h"
  19. #include "cppFunctionType.h"
  20. #include "cppParameterList.h"
  21. #include "cppTBDType.h"
  22. #include "indent.h"
  23. #include "cppParser.h"
  24. /**
  25. *
  26. */
  27. void CPPStructType::Base::
  28. output(ostream &out) const {
  29. if (_is_virtual) {
  30. out << "virtual ";
  31. }
  32. out << _vis << " " << *_base;
  33. }
  34. /**
  35. *
  36. */
  37. CPPStructType::
  38. CPPStructType(CPPStructType::Type type, CPPIdentifier *ident,
  39. CPPScope *current_scope, CPPScope *scope,
  40. const CPPFile &file) :
  41. CPPExtensionType(type, ident, current_scope, file),
  42. _scope(scope),
  43. _final(false)
  44. {
  45. _subst_decl_recursive_protect = false;
  46. _incomplete = true;
  47. }
  48. /**
  49. *
  50. */
  51. CPPStructType::
  52. CPPStructType(const CPPStructType &copy) :
  53. CPPExtensionType(copy),
  54. _scope(copy._scope),
  55. _incomplete(copy._incomplete),
  56. _derivation(copy._derivation),
  57. _final(copy._final)
  58. {
  59. _subst_decl_recursive_protect = false;
  60. }
  61. /**
  62. *
  63. */
  64. void CPPStructType::
  65. operator = (const CPPStructType &copy) {
  66. CPPExtensionType::operator = (copy);
  67. _scope = copy._scope;
  68. _incomplete = copy._incomplete;
  69. _derivation = copy._derivation;
  70. _final = copy._final;
  71. }
  72. /**
  73. * A handy function used while parsing to add a new base class to the list of
  74. * classes (or structs) this class derives from.
  75. */
  76. void CPPStructType::
  77. append_derivation(CPPType *base, CPPVisibility vis, bool is_virtual) {
  78. if (base != NULL) {
  79. // Unwrap any typedefs, since we can't inherit from a typedef.
  80. CPPTypedefType *def = base->as_typedef_type();
  81. while (def != NULL) {
  82. base = def->_type;
  83. def = base->as_typedef_type();
  84. }
  85. if (vis == V_unknown && base->as_extension_type() != NULL) {
  86. // Default visibility.
  87. if (base->as_extension_type()->_type == T_class) {
  88. vis = V_private;
  89. } else {
  90. vis = V_public;
  91. }
  92. }
  93. Base b;
  94. b._base = base;
  95. b._vis = vis;
  96. b._is_virtual = is_virtual;
  97. _derivation.push_back(b);
  98. }
  99. }
  100. /**
  101. *
  102. */
  103. CPPScope *CPPStructType::
  104. get_scope() const {
  105. return _scope;
  106. }
  107. /**
  108. * Returns true if this struct declaration is abstract, e.g. it contains or
  109. * inherits at least one method that is pure virtual.
  110. */
  111. bool CPPStructType::
  112. is_abstract() const {
  113. VFunctions funcs;
  114. get_pure_virtual_funcs(funcs);
  115. return !funcs.empty();
  116. }
  117. /**
  118. * Returns true if the type is considered a Plain Old Data (POD) type.
  119. */
  120. bool CPPStructType::
  121. is_trivial() const {
  122. // Make sure all base classes are trivial.
  123. Derivation::const_iterator di;
  124. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  125. CPPStructType *base = (*di)._base->as_struct_type();
  126. if (base != NULL && !base->is_trivial()) {
  127. return false;
  128. }
  129. }
  130. assert(_scope != NULL);
  131. // Make sure all members are trivial.
  132. CPPScope::Variables::const_iterator vi;
  133. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  134. CPPInstance *instance = (*vi).second;
  135. assert(instance != NULL);
  136. if (instance->_storage_class & CPPInstance::SC_static) {
  137. // Static members don't count.
  138. continue;
  139. }
  140. if (instance->_initializer != NULL) {
  141. // A member with an initializer means the default constructor would
  142. // assign a value. This means the type can't be trivial.
  143. return false;
  144. }
  145. // Finally, check if the data member itself is non-trivial.
  146. assert(instance->_type != NULL);
  147. if (!instance->_type->is_trivial()) {
  148. return false;
  149. }
  150. }
  151. // Now look for functions that are virtual or condestructors.
  152. bool is_default_constructible = true;
  153. CPPScope::Functions::const_iterator fi;
  154. for (fi = _scope->_functions.begin(); fi != _scope->_functions.end(); ++fi) {
  155. CPPFunctionGroup *fgroup = (*fi).second;
  156. CPPFunctionGroup::Instances::const_iterator ii;
  157. for (ii = fgroup->_instances.begin(); ii != fgroup->_instances.end(); ++ii) {
  158. CPPInstance *inst = (*ii);
  159. if (inst->_storage_class & CPPInstance::SC_virtual) {
  160. // Virtual functions are banned right off the bat.
  161. return false;
  162. }
  163. // The following checks don't apply for defaulted functions.
  164. if (inst->_storage_class & CPPInstance::SC_defaulted) {
  165. continue;
  166. }
  167. assert(inst->_type != (CPPType *)NULL);
  168. CPPFunctionType *ftype = inst->_type->as_function_type();
  169. assert(ftype != (CPPFunctionType *)NULL);
  170. if (ftype->_flags & (CPPFunctionType::F_destructor |
  171. CPPFunctionType::F_move_constructor |
  172. CPPFunctionType::F_copy_constructor)) {
  173. // User-provided destructors and copymove constructors are not trivial
  174. // unless they are defaulted (and not virtual).
  175. return false;
  176. }
  177. if ((ftype->_flags & CPPFunctionType::F_constructor) != 0) {
  178. if (ftype->_parameters->_parameters.size() == 0 &&
  179. !ftype->_parameters->_includes_ellipsis) {
  180. // Same for the default constructor.
  181. return false;
  182. }
  183. // The presence of a non-default constructor makes the class not
  184. // default-constructible.
  185. is_default_constructible = false;
  186. }
  187. if (fgroup->_name == "operator =") {
  188. // Or assignment operators.
  189. return false;
  190. }
  191. }
  192. }
  193. // Finally, the class must be default-constructible.
  194. return is_default_constructible;
  195. }
  196. /**
  197. * Returns true if the type is default-constructible.
  198. */
  199. bool CPPStructType::
  200. is_default_constructible() const {
  201. return is_default_constructible(V_public);
  202. }
  203. /**
  204. * Returns true if the type is copy-constructible.
  205. */
  206. bool CPPStructType::
  207. is_copy_constructible() const {
  208. return is_copy_constructible(V_public);
  209. }
  210. /**
  211. * Returns true if the type is default-constructible.
  212. */
  213. bool CPPStructType::
  214. is_default_constructible(CPPVisibility min_vis) const {
  215. CPPInstance *constructor = get_default_constructor();
  216. if (constructor != (CPPInstance *)NULL) {
  217. // It has a default constructor.
  218. if (constructor->_vis > min_vis) {
  219. // Inaccessible default constructor.
  220. return false;
  221. }
  222. if (constructor->_storage_class & CPPInstance::SC_deleted) {
  223. // Deleted default constructor.
  224. return false;
  225. }
  226. return true;
  227. }
  228. // Does it have constructors at all? If so, no implicit one is generated.
  229. if (get_constructor() != (CPPFunctionGroup *)NULL) {
  230. return false;
  231. }
  232. // Implicit default constructor. Check if the implicit default constructor
  233. // is deleted.
  234. Derivation::const_iterator di;
  235. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  236. CPPStructType *base = (*di)._base->as_struct_type();
  237. if (base != NULL) {
  238. if (!base->is_default_constructible(V_protected)) {
  239. return false;
  240. }
  241. }
  242. }
  243. // Make sure all members are default-constructible or have default values.
  244. CPPScope::Variables::const_iterator vi;
  245. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  246. CPPInstance *instance = (*vi).second;
  247. assert(instance != NULL);
  248. if (instance->_storage_class & CPPInstance::SC_static) {
  249. // Static members don't count.
  250. continue;
  251. }
  252. if (instance->_initializer != (CPPExpression *)NULL) {
  253. // It has a default value.
  254. continue;
  255. }
  256. if (!instance->_type->is_default_constructible()) {
  257. return false;
  258. }
  259. }
  260. // Check that we don't have pure virtual methods.
  261. CPPScope::Functions::const_iterator fi;
  262. for (fi = _scope->_functions.begin();
  263. fi != _scope->_functions.end();
  264. ++fi) {
  265. CPPFunctionGroup *fgroup = (*fi).second;
  266. CPPFunctionGroup::Instances::const_iterator ii;
  267. for (ii = fgroup->_instances.begin();
  268. ii != fgroup->_instances.end();
  269. ++ii) {
  270. CPPInstance *inst = (*ii);
  271. if (inst->_storage_class & CPPInstance::SC_pure_virtual) {
  272. // Here's a pure virtual function.
  273. return false;
  274. }
  275. }
  276. }
  277. return true;
  278. }
  279. /**
  280. * Returns true if the type is copy-constructible.
  281. */
  282. bool CPPStructType::
  283. is_copy_constructible(CPPVisibility min_vis) const {
  284. CPPInstance *constructor = get_copy_constructor();
  285. if (constructor != (CPPInstance *)NULL) {
  286. // It has a copy constructor.
  287. if (constructor->_vis > min_vis) {
  288. // Inaccessible copy constructor.
  289. return false;
  290. }
  291. if (constructor->_storage_class & CPPInstance::SC_deleted) {
  292. // Deleted copy constructor.
  293. return false;
  294. }
  295. return true;
  296. }
  297. CPPInstance *destructor = get_destructor();
  298. if (destructor != (CPPInstance *)NULL) {
  299. if (destructor->_vis > min_vis) {
  300. // Inaccessible destructor.
  301. return false;
  302. }
  303. if (destructor->_storage_class & CPPInstance::SC_deleted) {
  304. // Deleted destructor.
  305. return false;
  306. }
  307. }
  308. // Implicit copy constructor. Check if the implicit copy constructor is
  309. // deleted.
  310. Derivation::const_iterator di;
  311. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  312. CPPStructType *base = (*di)._base->as_struct_type();
  313. if (base != NULL) {
  314. if (!base->is_copy_constructible(V_protected)) {
  315. return false;
  316. }
  317. }
  318. }
  319. // Make sure all members are copy-constructible.
  320. CPPScope::Variables::const_iterator vi;
  321. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  322. CPPInstance *instance = (*vi).second;
  323. assert(instance != NULL);
  324. if (instance->_storage_class & CPPInstance::SC_static) {
  325. // Static members don't count.
  326. continue;
  327. }
  328. if (!instance->_type->is_copy_constructible()) {
  329. return false;
  330. }
  331. }
  332. // Check that we don't have pure virtual methods.
  333. CPPScope::Functions::const_iterator fi;
  334. for (fi = _scope->_functions.begin();
  335. fi != _scope->_functions.end();
  336. ++fi) {
  337. CPPFunctionGroup *fgroup = (*fi).second;
  338. CPPFunctionGroup::Instances::const_iterator ii;
  339. for (ii = fgroup->_instances.begin();
  340. ii != fgroup->_instances.end();
  341. ++ii) {
  342. CPPInstance *inst = (*ii);
  343. if (inst->_storage_class & CPPInstance::SC_pure_virtual) {
  344. // Here's a pure virtual function.
  345. return false;
  346. }
  347. }
  348. }
  349. return true;
  350. }
  351. /**
  352. * Ensures all functions are correctly marked with the "virtual" flag if they
  353. * are truly virtual by virtue of inheritance, rather than simply being
  354. * labeled virtual.
  355. *
  356. * This also sets the CPPInstance::SC_inherited_virtual flags on those virtual
  357. * methods that override a virtual method defined in a parent class (as
  358. * opposed to those that appear for this first time in this class). It is
  359. * sometimes useful to know whether a given virtual method represents the
  360. * first time that particular method appears.
  361. *
  362. * The return value is true if this class defines or inherits any virtual
  363. * methods (and thus requires a virtual function pointer), or false otherwise.
  364. */
  365. bool CPPStructType::
  366. check_virtual() const {
  367. VFunctions funcs;
  368. get_virtual_funcs(funcs);
  369. return !funcs.empty();
  370. }
  371. /**
  372. * Returns true if this declaration is an actual, factual declaration, or
  373. * false if some part of the declaration depends on a template parameter which
  374. * has not yet been instantiated.
  375. */
  376. bool CPPStructType::
  377. is_fully_specified() const {
  378. if (_scope != NULL && !_scope->is_fully_specified()) {
  379. return false;
  380. }
  381. return CPPType::is_fully_specified();
  382. }
  383. /**
  384. * Returns true if the type has not yet been fully specified, false if it has.
  385. */
  386. bool CPPStructType::
  387. is_incomplete() const {
  388. return _incomplete;
  389. }
  390. /**
  391. * Returns the constructor defined for the struct type, if any, or NULL if no
  392. * constructor is found.
  393. */
  394. CPPFunctionGroup *CPPStructType::
  395. get_constructor() const {
  396. // Just look for the function with the same name as the class.
  397. CPPScope::Functions::const_iterator fi;
  398. fi = _scope->_functions.find(get_simple_name());
  399. if (fi != _scope->_functions.end()) {
  400. return fi->second;
  401. } else {
  402. return (CPPFunctionGroup *)NULL;
  403. }
  404. }
  405. /**
  406. * Returns the default constructor defined for the struct type, or NULL if
  407. * there is none.
  408. */
  409. CPPInstance *CPPStructType::
  410. get_default_constructor() const {
  411. CPPFunctionGroup *fgroup = get_constructor();
  412. if (fgroup == (CPPFunctionGroup *)NULL) {
  413. return (CPPInstance *)NULL;
  414. }
  415. CPPFunctionGroup::Instances::const_iterator ii;
  416. for (ii = fgroup->_instances.begin();
  417. ii != fgroup->_instances.end();
  418. ++ii) {
  419. CPPInstance *inst = (*ii);
  420. assert(inst->_type != (CPPType *)NULL);
  421. CPPFunctionType *ftype = inst->_type->as_function_type();
  422. assert(ftype != (CPPFunctionType *)NULL);
  423. if (ftype->_parameters->_parameters.size() == 0 ||
  424. ftype->_parameters->_parameters.front()->_initializer != NULL) {
  425. // It takes 0 parameters (or all parameters have default values).
  426. return inst;
  427. }
  428. }
  429. return (CPPInstance *)NULL;
  430. }
  431. /**
  432. * Returns the copy constructor defined for the struct type, or NULL if no
  433. * copy constructor exists.
  434. */
  435. CPPInstance *CPPStructType::
  436. get_copy_constructor() const {
  437. CPPFunctionGroup *fgroup = get_constructor();
  438. if (fgroup == (CPPFunctionGroup *)NULL) {
  439. return (CPPInstance *)NULL;
  440. }
  441. CPPFunctionGroup::Instances::const_iterator ii;
  442. for (ii = fgroup->_instances.begin();
  443. ii != fgroup->_instances.end();
  444. ++ii) {
  445. CPPInstance *inst = (*ii);
  446. assert(inst->_type != (CPPType *)NULL);
  447. CPPFunctionType *ftype = inst->_type->as_function_type();
  448. assert(ftype != (CPPFunctionType *)NULL);
  449. if ((ftype->_flags & CPPFunctionType::F_copy_constructor) != 0) {
  450. return inst;
  451. }
  452. }
  453. return (CPPInstance *)NULL;
  454. }
  455. /**
  456. * Returns the move constructor defined for the struct type, or NULL if no
  457. * move constructor exists.
  458. */
  459. CPPInstance *CPPStructType::
  460. get_move_constructor() const {
  461. CPPFunctionGroup *fgroup = get_constructor();
  462. if (fgroup == (CPPFunctionGroup *)NULL) {
  463. return (CPPInstance *)NULL;
  464. }
  465. CPPFunctionGroup::Instances::const_iterator ii;
  466. for (ii = fgroup->_instances.begin();
  467. ii != fgroup->_instances.end();
  468. ++ii) {
  469. CPPInstance *inst = (*ii);
  470. assert(inst->_type != (CPPType *)NULL);
  471. CPPFunctionType *ftype = inst->_type->as_function_type();
  472. assert(ftype != (CPPFunctionType *)NULL);
  473. if ((ftype->_flags & CPPFunctionType::F_move_constructor) != 0) {
  474. return inst;
  475. }
  476. }
  477. return (CPPInstance *)NULL;
  478. }
  479. /**
  480. * Returns the destructor defined for the struct type, if any, or NULL if no
  481. * destructor is found.
  482. */
  483. CPPInstance *CPPStructType::
  484. get_destructor() const {
  485. // Iterate through all the functions that begin with '~' until we find one
  486. // that claims to be a destructor. In theory, there should only be one such
  487. // function.
  488. CPPScope::Functions::const_iterator fi;
  489. fi = _scope->_functions.lower_bound("~");
  490. while (fi != _scope->_functions.end() &&
  491. (*fi).first[0] == '~') {
  492. CPPFunctionGroup *fgroup = (*fi).second;
  493. CPPFunctionGroup::Instances::const_iterator ii;
  494. for (ii = fgroup->_instances.begin();
  495. ii != fgroup->_instances.end();
  496. ++ii) {
  497. CPPInstance *inst = (*ii);
  498. assert(inst->_type != (CPPType *)NULL);
  499. CPPFunctionType *ftype = inst->_type->as_function_type();
  500. assert(ftype != (CPPFunctionType *)NULL);
  501. if ((ftype->_flags & CPPFunctionType::F_destructor) != 0) {
  502. return inst;
  503. }
  504. }
  505. ++fi;
  506. }
  507. return (CPPInstance *)NULL;
  508. }
  509. /**
  510. *
  511. */
  512. CPPDeclaration *CPPStructType::
  513. instantiate(const CPPTemplateParameterList *actual_params,
  514. CPPScope *current_scope, CPPScope *global_scope,
  515. CPPPreprocessor *error_sink) const {
  516. // I *think* this assertion is no longer valid. Who knows.
  517. // assert(!_incomplete);
  518. if (_scope == NULL) {
  519. if (error_sink != NULL) {
  520. error_sink->warning("Ignoring template parameters for class " +
  521. get_local_name());
  522. }
  523. return (CPPDeclaration *)this;
  524. }
  525. CPPScope *scope =
  526. _scope->instantiate(actual_params, current_scope, global_scope, error_sink);
  527. if (scope->get_struct_type()->get_scope() != scope) {
  528. // Hmm, this type seems to be not completely defined. We must be in the
  529. // middle of recursively instantiating the scope. Thus, we don't yet know
  530. // what its associated struct type will be.
  531. // Postpone the evaluation of this type.
  532. CPPIdentifier *ident = new CPPIdentifier(get_fully_scoped_name(), _file);
  533. return CPPType::new_type(new CPPTBDType(ident));
  534. }
  535. CPPType *result = scope->get_struct_type();
  536. result = CPPType::new_type(result);
  537. if (result != (CPPType *)this) {
  538. // This really means the method ought to be non-const. But I'm too lazy
  539. // to propagate this change all the way back right now, so this hack is
  540. // here.
  541. ((CPPStructType *)this)->_instantiations.insert(result);
  542. }
  543. return result;
  544. }
  545. /**
  546. *
  547. */
  548. CPPDeclaration *CPPStructType::
  549. substitute_decl(CPPDeclaration::SubstDecl &subst,
  550. CPPScope *current_scope, CPPScope *global_scope) {
  551. SubstDecl::const_iterator si = subst.find(this);
  552. if (si != subst.end()) {
  553. assert((*si).second != NULL);
  554. return (*si).second;
  555. }
  556. if (_incomplete) {
  557. // We haven't finished defining the class yet.
  558. return this;
  559. }
  560. if (_subst_decl_recursive_protect) {
  561. // We're already executing this block; we'll have to return a proxy to the
  562. // type which we'll define later.
  563. CPPTypeProxy *proxy = new CPPTypeProxy;
  564. _proxies.push_back(proxy);
  565. assert(proxy != NULL);
  566. return proxy;
  567. }
  568. _subst_decl_recursive_protect = true;
  569. CPPStructType *rep = new CPPStructType(*this);
  570. if (_ident != NULL) {
  571. rep->_ident =
  572. _ident->substitute_decl(subst, current_scope, global_scope);
  573. }
  574. if (_scope != NULL) {
  575. rep->_scope =
  576. _scope->substitute_decl(subst, current_scope, global_scope);
  577. if (rep->_scope != _scope) {
  578. rep->_scope->set_struct_type(rep);
  579. // If we just instantiated a template scope, write the template
  580. // parameters into our identifier.
  581. CPPScope *pscope = rep->_scope->get_parent_scope();
  582. if (pscope != (CPPScope *)NULL &&
  583. pscope->_name.has_templ()) {
  584. // If the struct name didn't have an explicit template reference
  585. // before, now it does.
  586. if (!_ident->_names.empty() && !_ident->_names.back().has_templ()) {
  587. if (rep->is_template()) {
  588. rep->_template_scope = (CPPTemplateScope *)NULL;
  589. CPPNameComponent nc(get_simple_name());
  590. nc.set_templ(pscope->_name.get_templ());
  591. rep->_ident = new CPPIdentifier(nc, _file);
  592. }
  593. }
  594. }
  595. }
  596. }
  597. bool unchanged =
  598. (rep->_ident == _ident && rep->_scope == _scope);
  599. for (int i = 0; i < (int)_derivation.size(); ++i) {
  600. rep->_derivation[i]._base =
  601. _derivation[i]._base->substitute_decl(subst, current_scope, global_scope)->as_type();
  602. if (rep->_derivation[i]._base != _derivation[i]._base) {
  603. unchanged = false;
  604. }
  605. }
  606. if (unchanged) {
  607. delete rep;
  608. rep = this;
  609. }
  610. subst.insert(SubstDecl::value_type(this, rep));
  611. _subst_decl_recursive_protect = false;
  612. // Now fill in all the proxies we created for our recursive references.
  613. Proxies::iterator pi;
  614. for (pi = _proxies.begin(); pi != _proxies.end(); ++pi) {
  615. (*pi)->_actual_type = rep;
  616. }
  617. assert(rep != NULL);
  618. rep = CPPType::new_type(rep)->as_struct_type();
  619. assert(rep != NULL);
  620. if (rep != this) {
  621. _instantiations.insert(rep);
  622. }
  623. return rep;
  624. }
  625. /**
  626. *
  627. */
  628. void CPPStructType::
  629. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  630. if (!complete && _ident != NULL) {
  631. // If we have a name, use it.
  632. if (cppparser_output_class_keyword) {
  633. out << _type << " ";
  634. }
  635. out << _ident->get_local_name(scope);
  636. if (is_template()) {
  637. CPPTemplateScope *tscope = get_template_scope();
  638. tscope->_parameters.output(out, scope);
  639. }
  640. } else {
  641. if (is_template()) {
  642. get_template_scope()->_parameters.write_formal(out, scope);
  643. indent(out, indent_level);
  644. }
  645. if (_ident != NULL) {
  646. out << _type << " " << _ident->get_local_name(scope);
  647. } else {
  648. out << _type;
  649. }
  650. if (_final) {
  651. out << " final";
  652. }
  653. // Show any derivation we may have
  654. if (!_derivation.empty()) {
  655. Derivation::const_iterator di = _derivation.begin();
  656. out << " : " << *di;
  657. ++di;
  658. while (di != _derivation.end()) {
  659. out << ", " << *di;
  660. ++di;
  661. }
  662. }
  663. out << " {\n";
  664. _scope->write(out, indent_level + 2, _scope);
  665. indent(out, indent_level) << "}";
  666. }
  667. }
  668. /**
  669. *
  670. */
  671. CPPDeclaration::SubType CPPStructType::
  672. get_subtype() const {
  673. return ST_struct;
  674. }
  675. /**
  676. *
  677. */
  678. CPPStructType *CPPStructType::
  679. as_struct_type() {
  680. return this;
  681. }
  682. /**
  683. * Fills funcs up with a list of all the virtual function declarations (pure-
  684. * virtual or otherwise) defined at or above this class. This is used to
  685. * determine which functions in a given class are actually virtual, since a
  686. * function is virtual whose parent class holds a virtual function by the same
  687. * name, whether or not it is actually declared virtual in the derived class.
  688. */
  689. void CPPStructType::
  690. get_virtual_funcs(VFunctions &funcs) const {
  691. // First, get all the virtual funcs from our parents.
  692. Derivation::const_iterator di;
  693. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  694. VFunctions vf;
  695. CPPStructType *base = (*di)._base->as_struct_type();
  696. if (base != NULL) {
  697. base->get_virtual_funcs(vf);
  698. funcs.splice(funcs.end(), vf);
  699. }
  700. }
  701. // Now look for matching functions in this class that we can now infer are
  702. // virtual.
  703. VFunctions::iterator vfi, vfnext;
  704. vfi = funcs.begin();
  705. while (vfi != funcs.end()) {
  706. vfnext = vfi;
  707. ++vfnext;
  708. CPPInstance *inst = (*vfi);
  709. assert(inst->_type != (CPPType *)NULL);
  710. CPPFunctionType *base_ftype = inst->_type->as_function_type();
  711. assert(base_ftype != (CPPFunctionType *)NULL);
  712. if (inst->_storage_class & CPPInstance::SC_deleted) {
  713. // Ignore deleted functions.
  714. } else if ((base_ftype->_flags & CPPFunctionType::F_destructor) != 0) {
  715. // Match destructor-for-destructor; don't try to match destructors up by
  716. // name.
  717. CPPInstance *destructor = get_destructor();
  718. if (destructor != (CPPInstance *)NULL) {
  719. // It's a match! This destructor is virtual.
  720. funcs.erase(vfi);
  721. destructor->_storage_class |=
  722. (CPPInstance::SC_virtual | CPPInstance::SC_inherited_virtual);
  723. }
  724. } else {
  725. // Non-destructors we can try to match up by name.
  726. string fname = inst->get_local_name();
  727. CPPScope::Functions::const_iterator fi;
  728. fi = _scope->_functions.find(fname);
  729. if (fi != _scope->_functions.end()) {
  730. CPPFunctionGroup *fgroup = (*fi).second;
  731. // Look for a matching function amid this group.
  732. bool match_found = false;
  733. CPPFunctionGroup::Instances::const_iterator ii;
  734. for (ii = fgroup->_instances.begin();
  735. ii != fgroup->_instances.end() && !match_found;
  736. ++ii) {
  737. CPPInstance *new_inst = (*ii);
  738. assert(new_inst->_type != (CPPType *)NULL);
  739. CPPFunctionType *new_ftype = new_inst->_type->as_function_type();
  740. assert(new_ftype != (CPPFunctionType *)NULL);
  741. if (new_ftype->is_equivalent_function(*base_ftype)) {
  742. // It's a match! We now know it's virtual. Erase this function
  743. // from the list, so we can add it back in below.
  744. funcs.erase(vfi);
  745. match_found = true;
  746. // In fact, it's not only definitely virtual, but it's *inherited*
  747. // virtual, which means only that the interface is defined in some
  748. // parent class. Sometimes this is useful to know.
  749. new_inst->_storage_class |=
  750. (CPPInstance::SC_virtual | CPPInstance::SC_inherited_virtual);
  751. }
  752. }
  753. }
  754. }
  755. vfi = vfnext;
  756. }
  757. // Finally, look for more virtual function definitions.
  758. CPPScope::Functions::const_iterator fi;
  759. for (fi = _scope->_functions.begin();
  760. fi != _scope->_functions.end();
  761. ++fi) {
  762. CPPFunctionGroup *fgroup = (*fi).second;
  763. CPPFunctionGroup::Instances::const_iterator ii;
  764. for (ii = fgroup->_instances.begin();
  765. ii != fgroup->_instances.end();
  766. ++ii) {
  767. CPPInstance *inst = (*ii);
  768. if ((inst->_storage_class & CPPInstance::SC_virtual) != 0 &&
  769. (inst->_storage_class & CPPInstance::SC_deleted) == 0) {
  770. // Here's a virtual function.
  771. funcs.push_back(inst);
  772. }
  773. }
  774. }
  775. }
  776. /**
  777. * Fills funcs up with a list of all the pure virtual function declarations
  778. * defined at or above this class that have not been given definitions.
  779. */
  780. void CPPStructType::
  781. get_pure_virtual_funcs(VFunctions &funcs) const {
  782. // First, get all the virtual functions.
  783. VFunctions vfuncs;
  784. get_virtual_funcs(vfuncs);
  785. // Now traverse the list, getting out those functions that are pure virtual.
  786. VFunctions::iterator vfi;
  787. for (vfi = vfuncs.begin(); vfi != vfuncs.end(); ++vfi) {
  788. CPPInstance *inst = (*vfi);
  789. if ((inst->_storage_class & CPPInstance::SC_pure_virtual) != 0) {
  790. funcs.push_back(inst);
  791. }
  792. }
  793. }
  794. /**
  795. * Called by CPPDeclaration to determine whether this type is equivalent to
  796. * another type of the same type.
  797. */
  798. bool CPPStructType::
  799. is_equal(const CPPDeclaration *other) const {
  800. return CPPDeclaration::is_equal(other);
  801. /*
  802. const CPPStructType *ot = ((CPPDeclaration *)other)->as_struct_type();
  803. assert(ot != NULL);
  804. return this == ot ||
  805. (get_fully_scoped_name() == ot->get_fully_scoped_name());
  806. */
  807. }
  808. /**
  809. * Called by CPPDeclaration to determine whether this type should be ordered
  810. * before another type of the same type, in an arbitrary but fixed ordering.
  811. */
  812. bool CPPStructType::
  813. is_less(const CPPDeclaration *other) const {
  814. return CPPDeclaration::is_less(other);
  815. /*
  816. const CPPStructType *ot = ((CPPDeclaration *)other)->as_struct_type();
  817. assert(ot != NULL);
  818. if (this == ot) {
  819. return false;
  820. }
  821. return
  822. (get_fully_scoped_name() < ot->get_fully_scoped_name());
  823. */
  824. }