cppStructType.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 "cppReferenceType.h"
  16. #include "cppScope.h"
  17. #include "cppTypeProxy.h"
  18. #include "cppTemplateScope.h"
  19. #include "cppFunctionGroup.h"
  20. #include "cppFunctionType.h"
  21. #include "cppParameterList.h"
  22. #include "cppTBDType.h"
  23. #include "indent.h"
  24. #include "cppParser.h"
  25. /**
  26. *
  27. */
  28. void CPPStructType::Base::
  29. output(ostream &out) const {
  30. if (_is_virtual) {
  31. out << "virtual ";
  32. }
  33. out << _vis << " " << *_base;
  34. }
  35. /**
  36. *
  37. */
  38. CPPStructType::
  39. CPPStructType(CPPStructType::Type type, CPPIdentifier *ident,
  40. CPPScope *current_scope, CPPScope *scope,
  41. const CPPFile &file) :
  42. CPPExtensionType(type, ident, current_scope, file),
  43. _scope(scope),
  44. _final(false)
  45. {
  46. _subst_decl_recursive_protect = false;
  47. _incomplete = true;
  48. }
  49. /**
  50. *
  51. */
  52. CPPStructType::
  53. CPPStructType(const CPPStructType &copy) :
  54. CPPExtensionType(copy),
  55. _scope(copy._scope),
  56. _incomplete(copy._incomplete),
  57. _derivation(copy._derivation),
  58. _final(copy._final)
  59. {
  60. _subst_decl_recursive_protect = false;
  61. }
  62. /**
  63. *
  64. */
  65. void CPPStructType::
  66. operator = (const CPPStructType &copy) {
  67. CPPExtensionType::operator = (copy);
  68. _scope = copy._scope;
  69. _incomplete = copy._incomplete;
  70. _derivation = copy._derivation;
  71. _final = copy._final;
  72. }
  73. /**
  74. * A handy function used while parsing to add a new base class to the list of
  75. * classes (or structs) this class derives from.
  76. */
  77. void CPPStructType::
  78. append_derivation(CPPType *base, CPPVisibility vis, bool is_virtual) {
  79. if (base != NULL) {
  80. // Unwrap any typedefs, since we can't inherit from a typedef.
  81. CPPTypedefType *def = base->as_typedef_type();
  82. while (def != NULL) {
  83. base = def->_type;
  84. def = base->as_typedef_type();
  85. }
  86. if (vis == V_unknown && base->as_extension_type() != NULL) {
  87. // Default visibility.
  88. if (base->as_extension_type()->_type == T_class) {
  89. vis = V_private;
  90. } else {
  91. vis = V_public;
  92. }
  93. }
  94. Base b;
  95. b._base = base;
  96. b._vis = vis;
  97. b._is_virtual = is_virtual;
  98. _derivation.push_back(b);
  99. }
  100. }
  101. /**
  102. *
  103. */
  104. CPPScope *CPPStructType::
  105. get_scope() const {
  106. return _scope;
  107. }
  108. /**
  109. * Returns true if this struct declaration is abstract, e.g. it contains or
  110. * inherits at least one method that is pure virtual.
  111. */
  112. bool CPPStructType::
  113. is_abstract() const {
  114. VFunctions funcs;
  115. get_pure_virtual_funcs(funcs);
  116. return !funcs.empty();
  117. }
  118. /**
  119. * Returns true if this struct declaration is a base class of the other given
  120. * class, or the same class.
  121. */
  122. bool CPPStructType::
  123. is_base_of(const CPPStructType *other) const {
  124. if (this == other) {
  125. return true;
  126. }
  127. Derivation::const_iterator di;
  128. for (di = other->_derivation.begin(); di != other->_derivation.end(); ++di) {
  129. const CPPStructType *base = (*di)._base->as_struct_type();
  130. if (base != NULL && is_base_of(base)) {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * Returns true if this struct declaration defines no non-static data members
  138. * other than bit-fields of size 0, no virtual functions, no virtual base
  139. * classes, and no non-empty base classes, and is not a union.
  140. */
  141. bool CPPStructType::
  142. is_empty() const {
  143. if (_type == T_union) {
  144. return false;
  145. }
  146. if (check_virtual()) {
  147. return false;
  148. }
  149. // Make sure all base classes are empty and non-virtual.
  150. Derivation::const_iterator di;
  151. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  152. CPPStructType *base = (*di)._base->as_struct_type();
  153. if ((*di)._is_virtual || (base != NULL && !base->is_empty())) {
  154. return false;
  155. }
  156. }
  157. // Make sure there are no non-static data members.
  158. CPPScope::Variables::const_iterator vi;
  159. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  160. CPPInstance *instance = (*vi).second;
  161. assert(instance != NULL);
  162. if (instance->_storage_class & CPPInstance::SC_static) {
  163. // Static members don't count.
  164. continue;
  165. }
  166. // Only members with a bit width of 0 are okay.
  167. if (instance->_bit_width != 0) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. * Returns true if this class or any of its base classes have virtual methods.
  175. */
  176. bool CPPStructType::
  177. is_polymorphic() const {
  178. if (_type == T_union) {
  179. return false;
  180. }
  181. return check_virtual();
  182. }
  183. /**
  184. * Returns true if the type is considered a standard layout type.
  185. */
  186. bool CPPStructType::
  187. is_standard_layout() const {
  188. assert(_scope != NULL);
  189. CPPVisibility member_vis = V_unknown;
  190. // Make sure all data members have the same vis and are standard layout.
  191. CPPScope::Variables::const_iterator vi;
  192. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  193. CPPInstance *instance = (*vi).second;
  194. assert(instance != NULL);
  195. if (instance->_storage_class & CPPInstance::SC_static) {
  196. // Static members don't count.
  197. continue;
  198. }
  199. // Finally, check if the data member itself is standard layout.
  200. assert(instance->_type != NULL);
  201. if (!instance->_type->is_standard_layout()) {
  202. return false;
  203. }
  204. if (member_vis == V_unknown) {
  205. // The first non-static data member may not be a base class.
  206. CPPStructType *struct_type = instance->_type->remove_cv()->as_struct_type();
  207. if (struct_type != NULL && struct_type->is_base_of(this)) {
  208. return false;
  209. }
  210. member_vis = instance->_vis;
  211. } else if (member_vis != instance->_vis) {
  212. // All members need to have the same access control.
  213. return false;
  214. }
  215. }
  216. // Make sure all base classes are standard-layout and non-virtual.
  217. Derivation::const_iterator di;
  218. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  219. CPPStructType *base = (*di)._base->as_struct_type();
  220. if ((*di)._is_virtual) {
  221. return false;
  222. }
  223. // If this class had instance members, all base classes need to be empty.
  224. if (member_vis != V_unknown) {
  225. if (!base->is_empty()) {
  226. return false;
  227. }
  228. } else {
  229. if (!base->is_standard_layout()) {
  230. return false;
  231. }
  232. }
  233. }
  234. // Make sure we have no virtual functions.
  235. return !check_virtual();
  236. }
  237. /**
  238. * Returns true if the type is considered a Plain Old Data (POD) type.
  239. */
  240. bool CPPStructType::
  241. is_trivial() const {
  242. // Make sure all base classes are trivial and non-virtual.
  243. Derivation::const_iterator di;
  244. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  245. CPPStructType *base = (*di)._base->as_struct_type();
  246. if ((*di)._is_virtual || (base != NULL && !base->is_trivial())) {
  247. return false;
  248. }
  249. }
  250. assert(_scope != NULL);
  251. // Make sure all members are trivial.
  252. CPPScope::Variables::const_iterator vi;
  253. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  254. CPPInstance *instance = (*vi).second;
  255. assert(instance != NULL);
  256. if (instance->_storage_class & CPPInstance::SC_static) {
  257. // Static members don't count.
  258. continue;
  259. }
  260. if (instance->_initializer != NULL) {
  261. // A member with an initializer means the default constructor would
  262. // assign a value. This means the type can't be trivial.
  263. return false;
  264. }
  265. // Finally, check if the data member itself is non-trivial.
  266. assert(instance->_type != NULL);
  267. if (!instance->_type->is_trivial()) {
  268. return false;
  269. }
  270. }
  271. // Now look for functions that are virtual or con/destructors.
  272. bool is_default_constructible = true;
  273. CPPScope::Functions::const_iterator fi;
  274. for (fi = _scope->_functions.begin(); fi != _scope->_functions.end(); ++fi) {
  275. CPPFunctionGroup *fgroup = (*fi).second;
  276. CPPFunctionGroup::Instances::const_iterator ii;
  277. for (ii = fgroup->_instances.begin(); ii != fgroup->_instances.end(); ++ii) {
  278. CPPInstance *inst = (*ii);
  279. if (inst->_storage_class & CPPInstance::SC_virtual) {
  280. // Virtual functions are banned right off the bat.
  281. return false;
  282. }
  283. // The following checks don't apply for defaulted functions.
  284. if (inst->_storage_class & CPPInstance::SC_defaulted) {
  285. continue;
  286. }
  287. assert(inst->_type != (CPPType *)NULL);
  288. CPPFunctionType *ftype = inst->_type->as_function_type();
  289. assert(ftype != (CPPFunctionType *)NULL);
  290. if (ftype->_flags & (CPPFunctionType::F_destructor |
  291. CPPFunctionType::F_move_constructor |
  292. CPPFunctionType::F_copy_constructor)) {
  293. // User-provided destructors and copy/move constructors are not
  294. // trivial unless they are defaulted (and not virtual).
  295. return false;
  296. }
  297. if ((ftype->_flags & CPPFunctionType::F_constructor) != 0) {
  298. if (ftype->_parameters->_parameters.size() == 0 &&
  299. !ftype->_parameters->_includes_ellipsis) {
  300. // Same for the default constructor.
  301. return false;
  302. }
  303. // The presence of a non-default constructor makes the class not
  304. // default-constructible.
  305. is_default_constructible = false;
  306. }
  307. if (fgroup->_name == "operator =") {
  308. // Or assignment operators.
  309. return false;
  310. }
  311. }
  312. }
  313. // Finally, the class must be default-constructible.
  314. return is_default_constructible;
  315. }
  316. /**
  317. * Returns true if the type can be constructed using the given argument.
  318. * This implementation is rudimentary, as it does not attempt to follow all of
  319. * the implicit type conversion rules, but it is still useful.
  320. */
  321. bool CPPStructType::
  322. is_constructible(const CPPType *given_type) const {
  323. // Does the type match the copy constructor or move constructor?
  324. CPPType *base_type = ((CPPType *)given_type)->remove_reference();
  325. if (is_equivalent(*base_type->remove_cv())) {
  326. const CPPReferenceType *ref_type = given_type->as_reference_type();
  327. if (ref_type == NULL ||
  328. ref_type->_value_category == CPPReferenceType::VC_rvalue) {
  329. return is_move_constructible(V_public);
  330. } else {
  331. return is_copy_constructible(V_public);
  332. }
  333. }
  334. if (is_abstract()) {
  335. return false;
  336. }
  337. // Check for a different constructor.
  338. CPPFunctionGroup *fgroup = get_constructor();
  339. if (fgroup != (CPPFunctionGroup *)NULL) {
  340. CPPFunctionGroup::Instances::const_iterator ii;
  341. for (ii = fgroup->_instances.begin();
  342. ii != fgroup->_instances.end();
  343. ++ii) {
  344. CPPInstance *inst = (*ii);
  345. assert(inst->_type != (CPPType *)NULL);
  346. CPPFunctionType *ftype = inst->_type->as_function_type();
  347. assert(ftype != (CPPFunctionType *)NULL);
  348. CPPParameterList *params = ftype->_parameters;
  349. if (params->_parameters.size() == 1 && !params->_includes_ellipsis) {
  350. CPPType *param_type = params->_parameters[0]->_type->remove_reference();
  351. if (!param_type->is_const() && base_type->is_const()) {
  352. // Can't pass a const object to a function taking a non-const.
  353. continue;
  354. }
  355. // It's deleted, anyhow.
  356. if ((inst->_storage_class & CPPInstance::SC_deleted) != 0) {
  357. continue;
  358. }
  359. if (param_type->is_equivalent(*base_type)) {
  360. return true;
  361. }
  362. }
  363. }
  364. }
  365. return false;
  366. }
  367. /**
  368. * Returns true if the type is default-constructible.
  369. */
  370. bool CPPStructType::
  371. is_default_constructible() const {
  372. return is_default_constructible(V_public);
  373. }
  374. /**
  375. * Returns true if the type is copy-constructible.
  376. */
  377. bool CPPStructType::
  378. is_copy_constructible() const {
  379. return is_copy_constructible(V_public);
  380. }
  381. /**
  382. * Returns true if the type is destructible.
  383. */
  384. bool CPPStructType::
  385. is_destructible() const {
  386. return is_destructible(V_public);
  387. }
  388. /**
  389. * Returns true if the type is default-constructible.
  390. */
  391. bool CPPStructType::
  392. is_default_constructible(CPPVisibility min_vis) const {
  393. if (is_abstract()) {
  394. return false;
  395. }
  396. CPPInstance *constructor = get_default_constructor();
  397. if (constructor != (CPPInstance *)NULL) {
  398. // It has a default constructor.
  399. if (constructor->_vis > min_vis) {
  400. // Inaccessible default constructor.
  401. return false;
  402. }
  403. if (constructor->_storage_class & CPPInstance::SC_deleted) {
  404. // Deleted default constructor.
  405. return false;
  406. }
  407. return true;
  408. }
  409. // Does it have constructors at all? If so, no implicit one is generated.
  410. if (get_constructor() != (CPPFunctionGroup *)NULL) {
  411. return false;
  412. }
  413. // Implicit default constructor. Check if the implicit default constructor
  414. // is deleted.
  415. Derivation::const_iterator di;
  416. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  417. CPPStructType *base = (*di)._base->as_struct_type();
  418. if (base != NULL) {
  419. if (!base->is_default_constructible(V_protected)) {
  420. return false;
  421. }
  422. }
  423. }
  424. // Make sure all members are default-constructible or have default values.
  425. CPPScope::Variables::const_iterator vi;
  426. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  427. CPPInstance *instance = (*vi).second;
  428. assert(instance != NULL);
  429. if (instance->_storage_class & CPPInstance::SC_static) {
  430. // Static members don't count.
  431. continue;
  432. }
  433. if (instance->_initializer != (CPPExpression *)NULL) {
  434. // It has a default value.
  435. continue;
  436. }
  437. if (!instance->_type->is_default_constructible()) {
  438. return false;
  439. }
  440. }
  441. return true;
  442. }
  443. /**
  444. * Returns true if the type is copy-constructible.
  445. */
  446. bool CPPStructType::
  447. is_copy_constructible(CPPVisibility min_vis) const {
  448. if (is_abstract()) {
  449. return false;
  450. }
  451. CPPInstance *constructor = get_copy_constructor();
  452. if (constructor != (CPPInstance *)NULL) {
  453. // It has a copy constructor.
  454. if (constructor->_vis > min_vis) {
  455. // Inaccessible copy constructor.
  456. return false;
  457. }
  458. if (constructor->_storage_class & CPPInstance::SC_deleted) {
  459. // Deleted copy constructor.
  460. return false;
  461. }
  462. return true;
  463. }
  464. CPPInstance *destructor = get_destructor();
  465. if (destructor != (CPPInstance *)NULL) {
  466. if (destructor->_vis > min_vis) {
  467. // Inaccessible destructor.
  468. return false;
  469. }
  470. if (destructor->_storage_class & CPPInstance::SC_deleted) {
  471. // Deleted destructor.
  472. return false;
  473. }
  474. }
  475. // Implicit copy constructor. Check if the implicit copy constructor is
  476. // deleted.
  477. Derivation::const_iterator di;
  478. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  479. CPPStructType *base = (*di)._base->as_struct_type();
  480. if (base != NULL) {
  481. if (!base->is_copy_constructible(V_protected)) {
  482. return false;
  483. }
  484. }
  485. }
  486. // Make sure all members are copy-constructible.
  487. CPPScope::Variables::const_iterator vi;
  488. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  489. CPPInstance *instance = (*vi).second;
  490. assert(instance != NULL);
  491. if (instance->_storage_class & CPPInstance::SC_static) {
  492. // Static members don't count.
  493. continue;
  494. }
  495. if (!instance->_type->is_copy_constructible()) {
  496. return false;
  497. }
  498. }
  499. return true;
  500. }
  501. /**
  502. * Returns true if the type is move-constructible.
  503. */
  504. bool CPPStructType::
  505. is_move_constructible(CPPVisibility min_vis) const {
  506. CPPInstance *constructor = get_move_constructor();
  507. if (constructor != (CPPInstance *)NULL) {
  508. // It has a user-declared move constructor.
  509. if (constructor->_vis > min_vis) {
  510. // Inaccessible move constructor.
  511. return false;
  512. }
  513. if (constructor->_storage_class & CPPInstance::SC_deleted) {
  514. // It is deleted.
  515. return false;
  516. }
  517. if (is_abstract()) {
  518. return false;
  519. }
  520. return true;
  521. }
  522. return is_copy_constructible(min_vis);
  523. }
  524. /**
  525. * Returns true if the type is destructible.
  526. */
  527. bool CPPStructType::
  528. is_destructible(CPPVisibility min_vis) const {
  529. // Do we have an explicit destructor?
  530. CPPInstance *destructor = get_destructor();
  531. if (destructor != (CPPInstance *)NULL) {
  532. if (destructor->_vis > min_vis) {
  533. // Yes, but it's inaccessible.
  534. return false;
  535. }
  536. if (destructor->_storage_class & CPPInstance::SC_deleted) {
  537. // Yes, but it's explicitly been deleted.
  538. return false;
  539. }
  540. return true;
  541. }
  542. // Make sure all base classes are destructible.
  543. Derivation::const_iterator di;
  544. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  545. CPPStructType *base = (*di)._base->as_struct_type();
  546. if (base != NULL && !base->is_destructible(V_protected)) {
  547. return false;
  548. }
  549. }
  550. assert(_scope != NULL);
  551. // Make sure all members are destructible.
  552. CPPScope::Variables::const_iterator vi;
  553. for (vi = _scope->_variables.begin(); vi != _scope->_variables.end(); ++vi) {
  554. CPPInstance *instance = (*vi).second;
  555. assert(instance != NULL);
  556. if (instance->_storage_class & CPPInstance::SC_static) {
  557. // Static members don't count.
  558. continue;
  559. }
  560. // If the data member is not destructible, no go.
  561. assert(instance->_type != NULL);
  562. if (!instance->_type->is_destructible()) {
  563. return false;
  564. }
  565. }
  566. return true;
  567. }
  568. /**
  569. * Returns true if variables of this type may be implicitly converted to
  570. * the other type.
  571. */
  572. bool CPPStructType::
  573. is_convertible_to(const CPPType *other) const {
  574. if (CPPType::is_convertible_to(other)) {
  575. return true;
  576. }
  577. // Check all typecast operators to see whether we can cast to a type that is
  578. // convertible to the other type.
  579. CPPScope::Functions::const_iterator fi;
  580. for (fi = _scope->_functions.begin(); fi != _scope->_functions.end(); ++fi) {
  581. CPPFunctionGroup *fgroup = (*fi).second;
  582. CPPFunctionGroup::Instances::const_iterator ii;
  583. for (ii = fgroup->_instances.begin(); ii != fgroup->_instances.end(); ++ii) {
  584. CPPInstance *inst = (*ii);
  585. if (inst->_storage_class & (CPPInstance::SC_deleted | CPPInstance::SC_static | CPPInstance::SC_explicit)) {
  586. // Exclude static/deleted/explicit methods.
  587. continue;
  588. }
  589. // Also, the instance needs to be publicly visible.
  590. if (inst->_vis > V_public) {
  591. continue;
  592. }
  593. assert(inst->_type != (CPPType *)NULL);
  594. CPPFunctionType *ftype = inst->_type->as_function_type();
  595. assert(ftype != (CPPFunctionType *)NULL);
  596. if (ftype->_return_type != NULL &&
  597. (ftype->_flags & CPPFunctionType::F_operator_typecast) != 0) {
  598. // Yes, this is a typecast operator. Test using the return type.
  599. if (ftype->_return_type->is_convertible_to(other)) {
  600. return true;
  601. }
  602. }
  603. }
  604. }
  605. // Check whether any of the base classes are convertible.
  606. Derivation::const_iterator di;
  607. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  608. CPPStructType *base = (*di)._base->as_struct_type();
  609. if (base != NULL && (*di)._vis <= V_public && !base->is_convertible_to(other)) {
  610. return true;
  611. }
  612. }
  613. return false;
  614. }
  615. /**
  616. * Ensures all functions are correctly marked with the "virtual" flag if they
  617. * are truly virtual by virtue of inheritance, rather than simply being
  618. * labeled virtual.
  619. *
  620. * This also sets the CPPInstance::SC_inherited_virtual flags on those virtual
  621. * methods that override a virtual method defined in a parent class (as
  622. * opposed to those that appear for this first time in this class). It is
  623. * sometimes useful to know whether a given virtual method represents the
  624. * first time that particular method appears.
  625. *
  626. * The return value is true if this class defines or inherits any virtual
  627. * methods (and thus requires a virtual function pointer), or false otherwise.
  628. */
  629. bool CPPStructType::
  630. check_virtual() const {
  631. VFunctions funcs;
  632. get_virtual_funcs(funcs);
  633. return !funcs.empty();
  634. }
  635. /**
  636. * Returns true if this class, or any of its base classes, has a virtual
  637. * destructor.
  638. */
  639. bool CPPStructType::
  640. has_virtual_destructor() const {
  641. CPPInstance *destructor = get_destructor();
  642. if (destructor != NULL) {
  643. if (destructor->_storage_class & CPPInstance::SC_virtual) {
  644. return true;
  645. }
  646. }
  647. Derivation::const_iterator di;
  648. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  649. CPPStructType *base = (*di)._base->as_struct_type();
  650. if (base != NULL && base->has_virtual_destructor()) {
  651. return true;
  652. }
  653. }
  654. return false;
  655. }
  656. /**
  657. * Returns true if this declaration is an actual, factual declaration, or
  658. * false if some part of the declaration depends on a template parameter which
  659. * has not yet been instantiated.
  660. */
  661. bool CPPStructType::
  662. is_fully_specified() const {
  663. if (_scope != NULL && !_scope->is_fully_specified()) {
  664. return false;
  665. }
  666. return CPPType::is_fully_specified();
  667. }
  668. /**
  669. * Returns true if the type has not yet been fully specified, false if it has.
  670. */
  671. bool CPPStructType::
  672. is_incomplete() const {
  673. return _incomplete;
  674. }
  675. /**
  676. * Returns the constructor defined for the struct type, if any, or NULL if no
  677. * constructor is found.
  678. */
  679. CPPFunctionGroup *CPPStructType::
  680. get_constructor() const {
  681. // Just look for the function with the same name as the class.
  682. CPPScope::Functions::const_iterator fi;
  683. fi = _scope->_functions.find(get_simple_name());
  684. if (fi != _scope->_functions.end()) {
  685. return fi->second;
  686. } else {
  687. return (CPPFunctionGroup *)NULL;
  688. }
  689. }
  690. /**
  691. * Returns the default constructor defined for the struct type, or NULL if
  692. * there is no user-declared constructor that takes 0 arguments.
  693. */
  694. CPPInstance *CPPStructType::
  695. get_default_constructor() const {
  696. CPPFunctionGroup *fgroup = get_constructor();
  697. if (fgroup == (CPPFunctionGroup *)NULL) {
  698. return (CPPInstance *)NULL;
  699. }
  700. CPPFunctionGroup::Instances::const_iterator ii;
  701. for (ii = fgroup->_instances.begin();
  702. ii != fgroup->_instances.end();
  703. ++ii) {
  704. CPPInstance *inst = (*ii);
  705. assert(inst->_type != (CPPType *)NULL);
  706. CPPFunctionType *ftype = inst->_type->as_function_type();
  707. assert(ftype != (CPPFunctionType *)NULL);
  708. if (ftype->_parameters->_parameters.size() == 0 ||
  709. ftype->_parameters->_parameters.front()->_initializer != NULL) {
  710. // It takes 0 parameters (or all parameters have default values).
  711. return inst;
  712. }
  713. }
  714. return (CPPInstance *)NULL;
  715. }
  716. /**
  717. * Returns the copy constructor defined for the struct type, or NULL if no
  718. * user-declared copy constructor exists.
  719. */
  720. CPPInstance *CPPStructType::
  721. get_copy_constructor() const {
  722. CPPFunctionGroup *fgroup = get_constructor();
  723. if (fgroup == (CPPFunctionGroup *)NULL) {
  724. return (CPPInstance *)NULL;
  725. }
  726. CPPFunctionGroup::Instances::const_iterator ii;
  727. for (ii = fgroup->_instances.begin();
  728. ii != fgroup->_instances.end();
  729. ++ii) {
  730. CPPInstance *inst = (*ii);
  731. assert(inst->_type != (CPPType *)NULL);
  732. CPPFunctionType *ftype = inst->_type->as_function_type();
  733. assert(ftype != (CPPFunctionType *)NULL);
  734. if ((ftype->_flags & CPPFunctionType::F_copy_constructor) != 0) {
  735. return inst;
  736. }
  737. }
  738. return (CPPInstance *)NULL;
  739. }
  740. /**
  741. * Returns the move constructor defined for the struct type, or NULL if no
  742. * user-declared move constructor exists.
  743. */
  744. CPPInstance *CPPStructType::
  745. get_move_constructor() const {
  746. CPPFunctionGroup *fgroup = get_constructor();
  747. if (fgroup == (CPPFunctionGroup *)NULL) {
  748. return (CPPInstance *)NULL;
  749. }
  750. CPPFunctionGroup::Instances::const_iterator ii;
  751. for (ii = fgroup->_instances.begin();
  752. ii != fgroup->_instances.end();
  753. ++ii) {
  754. CPPInstance *inst = (*ii);
  755. assert(inst->_type != (CPPType *)NULL);
  756. CPPFunctionType *ftype = inst->_type->as_function_type();
  757. assert(ftype != (CPPFunctionType *)NULL);
  758. if ((ftype->_flags & CPPFunctionType::F_move_constructor) != 0) {
  759. return inst;
  760. }
  761. }
  762. return (CPPInstance *)NULL;
  763. }
  764. /**
  765. * Returns the destructor defined for the struct type, if any, or NULL if no
  766. * user-declared destructor is found.
  767. */
  768. CPPInstance *CPPStructType::
  769. get_destructor() const {
  770. // Iterate through all the functions that begin with '~' until we find one
  771. // that claims to be a destructor. In theory, there should only be one such
  772. // function.
  773. CPPScope::Functions::const_iterator fi;
  774. fi = _scope->_functions.lower_bound("~");
  775. while (fi != _scope->_functions.end() &&
  776. (*fi).first[0] == '~') {
  777. CPPFunctionGroup *fgroup = (*fi).second;
  778. CPPFunctionGroup::Instances::const_iterator ii;
  779. for (ii = fgroup->_instances.begin();
  780. ii != fgroup->_instances.end();
  781. ++ii) {
  782. CPPInstance *inst = (*ii);
  783. assert(inst->_type != (CPPType *)NULL);
  784. CPPFunctionType *ftype = inst->_type->as_function_type();
  785. assert(ftype != (CPPFunctionType *)NULL);
  786. if ((ftype->_flags & CPPFunctionType::F_destructor) != 0) {
  787. return inst;
  788. }
  789. }
  790. ++fi;
  791. }
  792. return (CPPInstance *)NULL;
  793. }
  794. /**
  795. *
  796. */
  797. CPPDeclaration *CPPStructType::
  798. instantiate(const CPPTemplateParameterList *actual_params,
  799. CPPScope *current_scope, CPPScope *global_scope,
  800. CPPPreprocessor *error_sink) const {
  801. // I *think* this assertion is no longer valid. Who knows.
  802. // assert(!_incomplete);
  803. if (_scope == NULL) {
  804. if (error_sink != NULL) {
  805. error_sink->warning("Ignoring template parameters for class " +
  806. get_local_name());
  807. }
  808. return (CPPDeclaration *)this;
  809. }
  810. CPPScope *scope =
  811. _scope->instantiate(actual_params, current_scope, global_scope, error_sink);
  812. if (scope->get_struct_type()->get_scope() != scope) {
  813. // Hmm, this type seems to be not completely defined. We must be in the
  814. // middle of recursively instantiating the scope. Thus, we don't yet know
  815. // what its associated struct type will be.
  816. // Postpone the evaluation of this type.
  817. CPPIdentifier *ident = new CPPIdentifier(get_fully_scoped_name(), _file);
  818. return CPPType::new_type(new CPPTBDType(ident));
  819. }
  820. CPPType *result = scope->get_struct_type();
  821. result = CPPType::new_type(result);
  822. if (result != (CPPType *)this) {
  823. // This really means the method ought to be non-const. But I'm too lazy
  824. // to propagate this change all the way back right now, so this hack is
  825. // here.
  826. ((CPPStructType *)this)->_instantiations.insert(result);
  827. }
  828. return result;
  829. }
  830. /**
  831. *
  832. */
  833. CPPDeclaration *CPPStructType::
  834. substitute_decl(CPPDeclaration::SubstDecl &subst,
  835. CPPScope *current_scope, CPPScope *global_scope) {
  836. SubstDecl::const_iterator si = subst.find(this);
  837. if (si != subst.end()) {
  838. assert((*si).second != NULL);
  839. return (*si).second;
  840. }
  841. if (_incomplete) {
  842. // We haven't finished defining the class yet.
  843. return this;
  844. }
  845. if (_subst_decl_recursive_protect) {
  846. // We're already executing this block; we'll have to return a proxy to the
  847. // type which we'll define later.
  848. CPPTypeProxy *proxy = new CPPTypeProxy;
  849. _proxies.push_back(proxy);
  850. assert(proxy != NULL);
  851. return proxy;
  852. }
  853. _subst_decl_recursive_protect = true;
  854. CPPStructType *rep = new CPPStructType(*this);
  855. if (_ident != NULL) {
  856. rep->_ident =
  857. _ident->substitute_decl(subst, current_scope, global_scope);
  858. }
  859. if (_scope != NULL) {
  860. rep->_scope =
  861. _scope->substitute_decl(subst, current_scope, global_scope);
  862. if (rep->_scope != _scope) {
  863. rep->_scope->set_struct_type(rep);
  864. // If we just instantiated a template scope, write the template
  865. // parameters into our identifier.
  866. CPPScope *pscope = rep->_scope->get_parent_scope();
  867. if (pscope != (CPPScope *)NULL &&
  868. pscope->_name.has_templ()) {
  869. // If the struct name didn't have an explicit template reference
  870. // before, now it does.
  871. if (_ident != NULL && !_ident->_names.empty() && !_ident->_names.back().has_templ()) {
  872. if (rep->is_template()) {
  873. rep->_template_scope = (CPPTemplateScope *)NULL;
  874. CPPNameComponent nc(get_simple_name());
  875. nc.set_templ(pscope->_name.get_templ());
  876. rep->_ident = new CPPIdentifier(nc, _file);
  877. }
  878. }
  879. }
  880. }
  881. }
  882. bool unchanged =
  883. (rep->_ident == _ident && rep->_scope == _scope);
  884. for (int i = 0; i < (int)_derivation.size(); ++i) {
  885. rep->_derivation[i]._base =
  886. _derivation[i]._base->substitute_decl(subst, current_scope, global_scope)->as_type();
  887. if (rep->_derivation[i]._base != _derivation[i]._base) {
  888. unchanged = false;
  889. }
  890. }
  891. if (unchanged) {
  892. delete rep;
  893. rep = this;
  894. }
  895. subst.insert(SubstDecl::value_type(this, rep));
  896. _subst_decl_recursive_protect = false;
  897. // Now fill in all the proxies we created for our recursive references.
  898. Proxies::iterator pi;
  899. for (pi = _proxies.begin(); pi != _proxies.end(); ++pi) {
  900. (*pi)->_actual_type = rep;
  901. }
  902. assert(rep != NULL);
  903. rep = CPPType::new_type(rep)->as_struct_type();
  904. assert(rep != NULL);
  905. if (rep != this) {
  906. _instantiations.insert(rep);
  907. }
  908. return rep;
  909. }
  910. /**
  911. *
  912. */
  913. void CPPStructType::
  914. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  915. if (!complete && _ident != NULL) {
  916. // If we have a name, use it.
  917. if (cppparser_output_class_keyword) {
  918. out << _type << " ";
  919. }
  920. out << _ident->get_local_name(scope);
  921. if (is_template()) {
  922. CPPTemplateScope *tscope = get_template_scope();
  923. tscope->_parameters.output(out, scope);
  924. }
  925. } else {
  926. if (is_template()) {
  927. get_template_scope()->_parameters.write_formal(out, scope);
  928. indent(out, indent_level);
  929. }
  930. if (_ident != NULL) {
  931. out << _type << " " << _ident->get_local_name(scope);
  932. } else {
  933. out << _type;
  934. }
  935. if (_final) {
  936. out << " final";
  937. }
  938. // Show any derivation we may have
  939. if (!_derivation.empty()) {
  940. Derivation::const_iterator di = _derivation.begin();
  941. out << " : " << *di;
  942. ++di;
  943. while (di != _derivation.end()) {
  944. out << ", " << *di;
  945. ++di;
  946. }
  947. }
  948. out << " {\n";
  949. _scope->write(out, indent_level + 2, _scope);
  950. indent(out, indent_level) << "}";
  951. }
  952. }
  953. /**
  954. *
  955. */
  956. CPPDeclaration::SubType CPPStructType::
  957. get_subtype() const {
  958. return ST_struct;
  959. }
  960. /**
  961. *
  962. */
  963. CPPStructType *CPPStructType::
  964. as_struct_type() {
  965. return this;
  966. }
  967. /**
  968. * Fills funcs up with a list of all the virtual function declarations (pure-
  969. * virtual or otherwise) defined at or above this class. This is used to
  970. * determine which functions in a given class are actually virtual, since a
  971. * function is virtual whose parent class holds a virtual function by the same
  972. * name, whether or not it is actually declared virtual in the derived class.
  973. */
  974. void CPPStructType::
  975. get_virtual_funcs(VFunctions &funcs) const {
  976. // First, get all the virtual funcs from our parents.
  977. Derivation::const_iterator di;
  978. for (di = _derivation.begin(); di != _derivation.end(); ++di) {
  979. VFunctions vf;
  980. CPPStructType *base = (*di)._base->as_struct_type();
  981. if (base != NULL) {
  982. base->get_virtual_funcs(vf);
  983. funcs.splice(funcs.end(), vf);
  984. }
  985. }
  986. // Now look for matching functions in this class that we can now infer are
  987. // virtual.
  988. VFunctions::iterator vfi, vfnext;
  989. vfi = funcs.begin();
  990. while (vfi != funcs.end()) {
  991. vfnext = vfi;
  992. ++vfnext;
  993. CPPInstance *inst = (*vfi);
  994. assert(inst->_type != (CPPType *)NULL);
  995. CPPFunctionType *base_ftype = inst->_type->as_function_type();
  996. assert(base_ftype != (CPPFunctionType *)NULL);
  997. if (inst->_storage_class & CPPInstance::SC_deleted) {
  998. // Ignore deleted functions.
  999. } else if ((base_ftype->_flags & CPPFunctionType::F_destructor) != 0) {
  1000. // Match destructor-for-destructor; don't try to match destructors up by
  1001. // name.
  1002. CPPInstance *destructor = get_destructor();
  1003. if (destructor != (CPPInstance *)NULL) {
  1004. // It's a match! This destructor is virtual.
  1005. funcs.erase(vfi);
  1006. destructor->_storage_class |=
  1007. (CPPInstance::SC_virtual | CPPInstance::SC_inherited_virtual);
  1008. }
  1009. } else {
  1010. // Non-destructors we can try to match up by name.
  1011. string fname = inst->get_local_name();
  1012. CPPScope::Functions::const_iterator fi;
  1013. fi = _scope->_functions.find(fname);
  1014. if (fi != _scope->_functions.end()) {
  1015. CPPFunctionGroup *fgroup = (*fi).second;
  1016. // Look for a matching function amid this group.
  1017. bool match_found = false;
  1018. CPPFunctionGroup::Instances::const_iterator ii;
  1019. for (ii = fgroup->_instances.begin();
  1020. ii != fgroup->_instances.end() && !match_found;
  1021. ++ii) {
  1022. CPPInstance *new_inst = (*ii);
  1023. assert(new_inst->_type != (CPPType *)NULL);
  1024. CPPFunctionType *new_ftype = new_inst->_type->as_function_type();
  1025. assert(new_ftype != (CPPFunctionType *)NULL);
  1026. if (new_ftype->match_virtual_override(*base_ftype)) {
  1027. // It's a match! We now know it's virtual. Erase this function
  1028. // from the list, so we can add it back in below.
  1029. funcs.erase(vfi);
  1030. match_found = true;
  1031. // In fact, it's not only definitely virtual, but it's *inherited*
  1032. // virtual, which means only that the interface is defined in some
  1033. // parent class. Sometimes this is useful to know.
  1034. new_inst->_storage_class |=
  1035. (CPPInstance::SC_virtual | CPPInstance::SC_inherited_virtual);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. vfi = vfnext;
  1041. }
  1042. // Finally, look for more virtual function definitions.
  1043. CPPScope::Functions::const_iterator fi;
  1044. for (fi = _scope->_functions.begin();
  1045. fi != _scope->_functions.end();
  1046. ++fi) {
  1047. CPPFunctionGroup *fgroup = (*fi).second;
  1048. CPPFunctionGroup::Instances::const_iterator ii;
  1049. for (ii = fgroup->_instances.begin();
  1050. ii != fgroup->_instances.end();
  1051. ++ii) {
  1052. CPPInstance *inst = (*ii);
  1053. if ((inst->_storage_class & CPPInstance::SC_virtual) != 0 &&
  1054. (inst->_storage_class & CPPInstance::SC_deleted) == 0) {
  1055. // Here's a virtual function.
  1056. funcs.push_back(inst);
  1057. }
  1058. }
  1059. }
  1060. }
  1061. /**
  1062. * Fills funcs up with a list of all the pure virtual function declarations
  1063. * defined at or above this class that have not been given definitions.
  1064. */
  1065. void CPPStructType::
  1066. get_pure_virtual_funcs(VFunctions &funcs) const {
  1067. // First, get all the virtual functions.
  1068. VFunctions vfuncs;
  1069. get_virtual_funcs(vfuncs);
  1070. // Now traverse the list, getting out those functions that are pure virtual.
  1071. VFunctions::iterator vfi;
  1072. for (vfi = vfuncs.begin(); vfi != vfuncs.end(); ++vfi) {
  1073. CPPInstance *inst = (*vfi);
  1074. if ((inst->_storage_class & CPPInstance::SC_pure_virtual) != 0) {
  1075. funcs.push_back(inst);
  1076. }
  1077. }
  1078. }
  1079. /**
  1080. * Called by CPPDeclaration to determine whether this type is equivalent to
  1081. * another type of the same type.
  1082. */
  1083. bool CPPStructType::
  1084. is_equal(const CPPDeclaration *other) const {
  1085. return CPPDeclaration::is_equal(other);
  1086. /*
  1087. const CPPStructType *ot = ((CPPDeclaration *)other)->as_struct_type();
  1088. assert(ot != NULL);
  1089. return this == ot ||
  1090. (get_fully_scoped_name() == ot->get_fully_scoped_name());
  1091. */
  1092. }
  1093. /**
  1094. * Called by CPPDeclaration to determine whether this type should be ordered
  1095. * before another type of the same type, in an arbitrary but fixed ordering.
  1096. */
  1097. bool CPPStructType::
  1098. is_less(const CPPDeclaration *other) const {
  1099. return CPPDeclaration::is_less(other);
  1100. /*
  1101. const CPPStructType *ot = ((CPPDeclaration *)other)->as_struct_type();
  1102. assert(ot != NULL);
  1103. if (this == ot) {
  1104. return false;
  1105. }
  1106. return
  1107. (get_fully_scoped_name() < ot->get_fully_scoped_name());
  1108. */
  1109. }