cppType.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 cppType.cxx
  10. * @author drose
  11. * @date 1999-10-19
  12. */
  13. #include "cppType.h"
  14. #include "cppConstType.h"
  15. #include "cppPointerType.h"
  16. #include "cppReferenceType.h"
  17. #include "cppStructType.h"
  18. #include "cppTypedefType.h"
  19. #include "cppExtensionType.h"
  20. #include <algorithm>
  21. using std::string;
  22. CPPType::Types CPPType::_types;
  23. CPPType::PreferredNames CPPType::_preferred_names;
  24. CPPType::AltNames CPPType::_alt_names;
  25. bool CPPTypeCompare::
  26. operator () (CPPType *a, CPPType *b) const {
  27. return (*a) < (*b);
  28. }
  29. /**
  30. *
  31. */
  32. CPPType::
  33. CPPType(const CPPFile &file) :
  34. CPPDeclaration(file)
  35. {
  36. _declaration = nullptr;
  37. // This is set true by interrogate when the "forcetype" keyword is used.
  38. _forcetype = false;
  39. }
  40. /**
  41. * If this CPPType object is a forward reference or other nonspecified
  42. * reference to a type that might now be known a real type, returns the real
  43. * type. Otherwise returns the type itself.
  44. */
  45. CPPType *CPPType::
  46. resolve_type(CPPScope *, CPPScope *) {
  47. return this;
  48. }
  49. /**
  50. * Returns true if the type, or any nested type within the type, is a
  51. * CPPTBDType and thus isn't fully determined right now. In this case,
  52. * calling resolve_type() may or may not resolve the type.
  53. */
  54. bool CPPType::
  55. is_tbd() const {
  56. return false;
  57. }
  58. /**
  59. * Returns true if the type is considered a fundamental type.
  60. */
  61. bool CPPType::
  62. is_fundamental() const {
  63. return false;
  64. }
  65. /**
  66. * Returns true if the type is considered a standard layout type.
  67. */
  68. bool CPPType::
  69. is_standard_layout() const {
  70. return false;
  71. }
  72. /**
  73. * Returns true if the type is considered a Plain Old Data (POD) type.
  74. */
  75. bool CPPType::
  76. is_trivial() const {
  77. return false;
  78. }
  79. /**
  80. * Returns true if the type can be safely copied by memcpy or memmove.
  81. */
  82. bool CPPType::
  83. is_trivially_copyable() const {
  84. return false;
  85. }
  86. /**
  87. * Returns true if the type can be constructed using the given argument.
  88. */
  89. bool CPPType::
  90. is_constructible(const CPPType *given_type) const {
  91. return false;
  92. }
  93. /**
  94. * Returns true if the type is default-constructible.
  95. */
  96. bool CPPType::
  97. is_default_constructible() const {
  98. return false;
  99. }
  100. /**
  101. * Returns true if the type is copy-constructible.
  102. */
  103. bool CPPType::
  104. is_copy_constructible() const {
  105. return false;
  106. }
  107. /**
  108. * Returns true if the type is copy-assignable.
  109. */
  110. bool CPPType::
  111. is_copy_assignable() const {
  112. return false;
  113. }
  114. /**
  115. * Returns true if the type is destructible.
  116. */
  117. bool CPPType::
  118. is_destructible() const {
  119. return !is_incomplete();
  120. }
  121. /**
  122. * Returns true if the type is a special parameter expression type.
  123. *
  124. * This sort of type is created to handle instance declarations that initially
  125. * look like function prototypes.
  126. */
  127. bool CPPType::
  128. is_parameter_expr() const {
  129. return false;
  130. }
  131. /**
  132. * Returns true if this is an enum type, or a typedef to an enum type.
  133. */
  134. bool CPPType::
  135. is_enum() const {
  136. const CPPTypedefType *td_type = as_typedef_type();
  137. if (td_type != nullptr) {
  138. return td_type->_type->is_enum();
  139. }
  140. const CPPExtensionType *ext_type = as_extension_type();
  141. if (ext_type != nullptr) {
  142. return ext_type->_type == CPPExtensionType::T_enum ||
  143. ext_type->_type == CPPExtensionType::T_enum_struct ||
  144. ext_type->_type == CPPExtensionType::T_enum_class;
  145. }
  146. return false;
  147. }
  148. /**
  149. * Returns true if this is a const type, or a typedef to a const type.
  150. */
  151. bool CPPType::
  152. is_const() const {
  153. const CPPTypedefType *td_type = as_typedef_type();
  154. if (td_type != nullptr) {
  155. return td_type->_type->is_const();
  156. }
  157. return get_subtype() == ST_const;
  158. }
  159. /**
  160. * Returns true if this is a reference type, or a typedef to a reference type.
  161. */
  162. bool CPPType::
  163. is_reference() const {
  164. const CPPTypedefType *td_type = as_typedef_type();
  165. if (td_type != nullptr) {
  166. return td_type->_type->is_reference();
  167. }
  168. return get_subtype() == ST_reference;
  169. }
  170. /**
  171. * Returns true if this is an unqualified or cv-qualified pointer type, or a
  172. * typedef to one.
  173. */
  174. bool CPPType::
  175. is_pointer() const {
  176. const CPPTypedefType *td_type = as_typedef_type();
  177. if (td_type != nullptr) {
  178. return td_type->_type->is_pointer();
  179. }
  180. const CPPConstType *const_type = as_const_type();
  181. if (const_type != nullptr) {
  182. return const_type->_wrapped_around->is_pointer();
  183. }
  184. return get_subtype() == ST_pointer;
  185. }
  186. /**
  187. * Returns the type with any const qualifier stripped off. Will follow
  188. * typedefs, but only if necessary.
  189. */
  190. CPPType *CPPType::
  191. remove_const() {
  192. const CPPTypedefType *td_type = as_typedef_type();
  193. if (td_type != nullptr) {
  194. CPPType *unwrapped = td_type->_type->remove_const();
  195. if (unwrapped != td_type->_type) {
  196. return unwrapped;
  197. } else {
  198. return this;
  199. }
  200. }
  201. const CPPConstType *const_type = as_const_type();
  202. if (const_type != nullptr) {
  203. return const_type->_wrapped_around->remove_const();
  204. }
  205. return this;
  206. }
  207. /**
  208. * Returns the type with any reference stripped off.
  209. */
  210. CPPType *CPPType::
  211. remove_reference() {
  212. const CPPTypedefType *td_type = as_typedef_type();
  213. if (td_type != nullptr) {
  214. CPPType *unwrapped = td_type->_type->remove_reference();
  215. if (unwrapped != td_type->_type) {
  216. return unwrapped;
  217. } else {
  218. return this;
  219. }
  220. }
  221. const CPPReferenceType *ref_type = as_reference_type();
  222. if (ref_type != nullptr) {
  223. return ref_type->_pointing_at;
  224. }
  225. return this;
  226. }
  227. /**
  228. * Returns the type with any pointer and cv-qualifiers stripped off.
  229. */
  230. CPPType *CPPType::
  231. remove_pointer() {
  232. switch (get_subtype()) {
  233. case ST_typedef:
  234. {
  235. const CPPTypedefType *td_type = as_typedef_type();
  236. CPPType *unwrapped = td_type->_type->remove_pointer();
  237. if (unwrapped != td_type->_type) {
  238. return unwrapped;
  239. } else {
  240. return this;
  241. }
  242. }
  243. case ST_pointer:
  244. return ((const CPPPointerType *)this)->_pointing_at;
  245. case ST_const:
  246. return ((const CPPConstType *)this)->_wrapped_around->remove_pointer();
  247. default:
  248. return this;
  249. }
  250. }
  251. /**
  252. * Returns true if the type has even been typedef'ed and therefore has a
  253. * simple name available to stand for it. Extension types are all implicitly
  254. * typedef'ed on declaration.
  255. */
  256. bool CPPType::
  257. has_typedef_name() const {
  258. return !_typedefs.empty();
  259. }
  260. /**
  261. * Returns a string that can be used to name the type, if has_typedef_name()
  262. * returned true. This will be the first typedef name applied to the type.
  263. */
  264. string CPPType::
  265. get_typedef_name(CPPScope *scope) const {
  266. if (_typedefs.empty()) {
  267. return string();
  268. } else {
  269. return _typedefs.front()->get_local_name(scope);
  270. }
  271. }
  272. /**
  273. * Returns a fundametal one-word name for the type. This name will not
  274. * include any scoping operators or template parameters, so it may not be a
  275. * compilable reference to the type.
  276. */
  277. string CPPType::
  278. get_simple_name() const {
  279. return get_local_name();
  280. }
  281. /**
  282. * Returns the compilable, correct name for this type within the indicated
  283. * scope. If the scope is NULL, within the scope the type is declared in.
  284. */
  285. string CPPType::
  286. get_local_name(CPPScope *scope) const {
  287. std::ostringstream ostrm;
  288. output(ostrm, 0, scope, false);
  289. return ostrm.str();
  290. }
  291. /**
  292. * Returns the compilable, correct name for the type, with completely explicit
  293. * scoping.
  294. */
  295. string CPPType::
  296. get_fully_scoped_name() const {
  297. return get_local_name();
  298. }
  299. /**
  300. * Returns the best name to use for the type from a programmer's point of
  301. * view. This will typically be a typedef name if one is available, or the
  302. * full C++ name if it is not. The typedef may or may not be visible within
  303. * the current scope, so this type name may not be compilable.
  304. */
  305. string CPPType::
  306. get_preferred_name() const {
  307. string preferred_name = get_preferred_name_for(this);
  308. if (!preferred_name.empty()) {
  309. return preferred_name;
  310. }
  311. return get_local_name();
  312. }
  313. /**
  314. * Returns the number of "alternate" names for this type. The alternate names
  315. * are alternate typedef names. This list might be empty, or it might be
  316. * long. One of these names may or may not be the same as the "preferred"
  317. * name.
  318. */
  319. int CPPType::
  320. get_num_alt_names() const {
  321. // We do a lookup based on the type's name, instead of its pointer, so we
  322. // can resolve different expansions of the same type.
  323. string tname = this->get_fully_scoped_name();
  324. if (!tname.empty()) {
  325. AltNames::const_iterator ai;
  326. ai = _alt_names.find(tname);
  327. if (ai != _alt_names.end()) {
  328. const Names &names = (*ai).second;
  329. return names.size();
  330. }
  331. }
  332. return 0;
  333. }
  334. /**
  335. * Returns the nth "alternate" name for this type. See get_num_alt_names().
  336. */
  337. string CPPType::
  338. get_alt_name(int n) const {
  339. // We do a lookup based on the type's name, instead of its pointer, so we
  340. // can resolve different expansions of the same type.
  341. string tname = this->get_fully_scoped_name();
  342. if (!tname.empty()) {
  343. AltNames::const_iterator ai;
  344. ai = _alt_names.find(tname);
  345. if (ai != _alt_names.end()) {
  346. const Names &names = (*ai).second;
  347. if (n >= 0 && n < (int)names.size()) {
  348. return names[n];
  349. }
  350. }
  351. }
  352. return string();
  353. }
  354. /**
  355. * Returns true if the type has not yet been fully specified, false if it has.
  356. */
  357. bool CPPType::
  358. is_incomplete() const {
  359. return false;
  360. }
  361. /**
  362. * This is a little more forgiving than is_equal(): it returns true if the
  363. * types appear to be referring to the same thing, even if they may have
  364. * different pointers or somewhat different definitions. It's useful for
  365. * parameter matching, etc.
  366. */
  367. bool CPPType::
  368. is_equivalent(const CPPType &other) const {
  369. if (get_subtype() != other.get_subtype()) {
  370. return false;
  371. }
  372. return is_equal(&other);
  373. }
  374. /**
  375. * Returns true if variables of this type may be implicitly converted to
  376. * the other type.
  377. */
  378. bool CPPType::
  379. is_convertible_to(const CPPType *other) const {
  380. return other->is_constructible(this);
  381. }
  382. /**
  383. * Formats a C++-looking line that defines an instance of the given type, with
  384. * the indicated name. In most cases this will be "type name", but some types
  385. * have special exceptions.
  386. */
  387. void CPPType::
  388. output_instance(std::ostream &out, const string &name, CPPScope *scope) const {
  389. output_instance(out, 0, scope, false, "", name);
  390. }
  391. /**
  392. * Formats a C++-looking line that defines an instance of the given type, with
  393. * the indicated name. In most cases this will be "type name", but some types
  394. * have special exceptions.
  395. */
  396. void CPPType::
  397. output_instance(std::ostream &out, int indent_level, CPPScope *scope,
  398. bool complete, const string &prename,
  399. const string &name) const {
  400. output(out, indent_level, scope, complete);
  401. out << " " << prename << name;
  402. }
  403. /**
  404. *
  405. */
  406. CPPType *CPPType::
  407. as_type() {
  408. return this;
  409. }
  410. /**
  411. * This should be called whenever a new CPPType object is created. It will
  412. * uniquify the type pointers by checking to see if some equivalent CPPType
  413. * object has previously been created; if it has, it returns the old object
  414. * and deletes the new one. Otherwise, it stores the new one and returns it.
  415. */
  416. CPPType *CPPType::
  417. new_type(CPPType *type) {
  418. std::pair<Types::iterator, bool> result = _types.insert(type);
  419. if (result.second) {
  420. // The insertion has taken place; thus, this is the first time this type
  421. // has been declared.
  422. assert(*result.first == type);
  423. return type;
  424. }
  425. // If this triggers, we probably messed up by defining is_less()
  426. // incorrectly; they provide a relative ordering even though they are equal
  427. // to each other. Or, we provided an is_equal() that gives false negatives.
  428. assert(**result.first == *type);
  429. // The insertion has not taken place; thus, there was previously another
  430. // equivalent type declared.
  431. if (*result.first != type) {
  432. // *** Something wrong here. Deleting this should always be safe;
  433. // however, it's not. Thus, someone failed to call new_type() on a type
  434. // pointer before saving it somewhere. Fix me soon. ****
  435. delete type;
  436. }
  437. return *result.first;
  438. }
  439. /**
  440. * Records a global typedef name associated with the indicated Type. This
  441. * will be an "alt" name, and it may also become the "preferred" name.
  442. */
  443. void CPPType::
  444. record_alt_name_for(const CPPType *type, const string &name) {
  445. if (!name.empty()) {
  446. string tname = type->get_fully_scoped_name();
  447. if (!tname.empty()) {
  448. if (tname.find('<') != string::npos) {
  449. // If the name contains a funny character like a template name, then
  450. // we implicitly take the first typedef as the preferred name.
  451. _preferred_names.insert(PreferredNames::value_type(tname, name));
  452. }
  453. Names &names = _alt_names[tname];
  454. if (find(names.begin(), names.end(), name) == names.end()) {
  455. // It's not already recorded as an alt name, so record it now.
  456. names.push_back(name);
  457. }
  458. }
  459. }
  460. }
  461. /**
  462. * Returns the previously-stored "preferred" name associated with the type, if
  463. * any, or empty string if no name is associated.
  464. */
  465. string CPPType::
  466. get_preferred_name_for(const CPPType *type) {
  467. // We do a lookup based on the type's name, instead of its pointer, so we
  468. // can resolve different expansions of the same type.
  469. string tname = type->get_fully_scoped_name();
  470. if (!tname.empty()) {
  471. PreferredNames::const_iterator pi;
  472. pi = _preferred_names.find(tname);
  473. if (pi != _preferred_names.end()) {
  474. return (*pi).second;
  475. }
  476. }
  477. return string();
  478. }