cppInstance.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // Filename: cppInstance.cxx
  2. // Created by: drose (19Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "cppInstance.h"
  15. #include "cppInstanceIdentifier.h"
  16. #include "cppIdentifier.h"
  17. #include "cppTemplateScope.h"
  18. #include "cppFunctionType.h"
  19. #include "cppSimpleType.h"
  20. #include "cppExpression.h"
  21. #include "cppPreprocessor.h"
  22. #include "cppParameterList.h"
  23. #include "cppReferenceType.h"
  24. #include "cppConstType.h"
  25. #include "indent.h"
  26. #include <algorithm>
  27. ////////////////////////////////////////////////////////////////////
  28. // Function: CPPInstance::Constructor
  29. // Access: Public
  30. // Description:
  31. ////////////////////////////////////////////////////////////////////
  32. CPPInstance::
  33. CPPInstance(CPPType *type, const string &name, int storage_class) :
  34. CPPDeclaration(CPPFile()),
  35. _type(type),
  36. _ident(new CPPIdentifier(name)),
  37. _storage_class(storage_class)
  38. {
  39. _initializer = NULL;
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: CPPInstance::Constructor
  43. // Access: Public
  44. // Description:
  45. ////////////////////////////////////////////////////////////////////
  46. CPPInstance::
  47. CPPInstance(CPPType *type, CPPIdentifier *ident, int storage_class) :
  48. CPPDeclaration(CPPFile()),
  49. _type(type),
  50. _ident(ident),
  51. _storage_class(storage_class)
  52. {
  53. _initializer = NULL;
  54. }
  55. ////////////////////////////////////////////////////////////////////
  56. // Function: CPPInstance::Constructor
  57. // Access: Public
  58. // Description: Constructs a new CPPInstance object that defines a
  59. // variable of the indicated type according to the type
  60. // and the InstanceIdentifier. The InstanceIdentifier
  61. // pointer is deallocated.
  62. ////////////////////////////////////////////////////////////////////
  63. CPPInstance::
  64. CPPInstance(CPPType *type, CPPInstanceIdentifier *ii, int storage_class,
  65. const CPPFile &file) :
  66. CPPDeclaration(file)
  67. {
  68. _type = ii->unroll_type(type);
  69. _ident = ii->_ident;
  70. ii->_ident = NULL;
  71. _storage_class = storage_class;
  72. _initializer = NULL;
  73. CPPParameterList *params = ii->get_initializer();
  74. if (params != (CPPParameterList *)NULL) {
  75. // In this case, the instance has a parameter-list initializer, e.g.:
  76. //
  77. // int foo(0);
  78. //
  79. // We really should save this initializer in the instance object.
  80. // But we don't for now, since no one really cares about
  81. // initializers anyway.
  82. }
  83. delete ii;
  84. }
  85. ////////////////////////////////////////////////////////////////////
  86. // Function: CPPInstance::Copy Constructor
  87. // Access: Public
  88. // Description:
  89. ////////////////////////////////////////////////////////////////////
  90. CPPInstance::
  91. CPPInstance(const CPPInstance &copy) :
  92. CPPDeclaration(copy),
  93. _type(copy._type),
  94. _ident(copy._ident),
  95. _initializer(copy._initializer),
  96. _storage_class(copy._storage_class)
  97. {
  98. assert(_type != NULL);
  99. }
  100. ////////////////////////////////////////////////////////////////////
  101. // Function: CPPInstance::Destructor
  102. // Access: Public
  103. // Description:
  104. ////////////////////////////////////////////////////////////////////
  105. CPPInstance::
  106. ~CPPInstance() {
  107. // Can't delete the identifier. Don't try.
  108. }
  109. ////////////////////////////////////////////////////////////////////
  110. // Function: CPPInstance::make_typecast_function
  111. // Access: Public, Static
  112. // Description: Constructs and returns a new CPPInstance object that
  113. // corresponds to a function prototype declaration for a
  114. // typecast method, whose return type is implicit in the
  115. // identifier type.
  116. ////////////////////////////////////////////////////////////////////
  117. CPPInstance *CPPInstance::
  118. make_typecast_function(CPPInstance *inst, CPPIdentifier *ident,
  119. CPPParameterList *parameters, int function_flags) {
  120. CPPType *type = CPPType::new_type(inst->_type);
  121. delete inst;
  122. function_flags |= (int)CPPFunctionType::F_operator_typecast;
  123. CPPType *ft =
  124. CPPType::new_type(new CPPFunctionType(type, parameters, function_flags));
  125. return new CPPInstance(ft, ident);
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: CPPInstance::Equivalence Operator
  129. // Access: Public
  130. // Description:
  131. ////////////////////////////////////////////////////////////////////
  132. bool CPPInstance::
  133. operator == (const CPPInstance &other) const {
  134. if (_type != other._type) {
  135. return false;
  136. }
  137. if (_storage_class != other._storage_class) {
  138. return false;
  139. }
  140. // We *do* care about the identifier. We need to differentiate
  141. // types of function variables, among possibly other things, based
  142. // on the identifier.
  143. if ((_ident == NULL && other._ident != NULL) ||
  144. (_ident != NULL && other._ident == NULL) ||
  145. (_ident != NULL && other._ident != NULL && *_ident != *other._ident))
  146. {
  147. return false;
  148. }
  149. // We similarly care about the initializer.
  150. if ((_initializer == NULL && other._initializer != NULL) ||
  151. (_initializer != NULL && other._initializer == NULL) ||
  152. (_initializer != NULL && other._initializer != NULL &&
  153. *_initializer != *other._initializer))
  154. {
  155. return false;
  156. }
  157. return true;
  158. }
  159. ////////////////////////////////////////////////////////////////////
  160. // Function: CPPInstance::Nonequivalence Operator
  161. // Access: Public
  162. // Description:
  163. ////////////////////////////////////////////////////////////////////
  164. bool CPPInstance::
  165. operator != (const CPPInstance &other) const {
  166. return !operator == (other);
  167. }
  168. ////////////////////////////////////////////////////////////////////
  169. // Function: CPPInstance::Ordering Operator
  170. // Access: Public
  171. // Description:
  172. ////////////////////////////////////////////////////////////////////
  173. bool CPPInstance::
  174. operator < (const CPPInstance &other) const {
  175. if (_type != other._type) {
  176. return _type < other._type;
  177. }
  178. if (_storage_class != other._storage_class) {
  179. return _storage_class < other._storage_class;
  180. }
  181. // We *do* care about the identifier. We need to differentiate
  182. // types of function variables, among possibly other things, based
  183. // on the identifier.
  184. if ((_ident == NULL && other._ident != NULL) ||
  185. (_ident != NULL && other._ident == NULL) ||
  186. (_ident != NULL && other._ident != NULL && *_ident != *other._ident))
  187. {
  188. if (_ident == NULL || other._ident == NULL) {
  189. return _ident < other._ident;
  190. }
  191. return *_ident < *other._ident;
  192. }
  193. // We similarly care about the initializer.
  194. if ((_initializer == NULL && other._initializer != NULL) ||
  195. (_initializer != NULL && other._initializer == NULL) ||
  196. (_initializer != NULL && other._initializer != NULL &&
  197. *_initializer != *other._initializer))
  198. {
  199. if (_initializer == NULL || other._initializer == NULL) {
  200. return _initializer < other._initializer;
  201. }
  202. return *_initializer < *other._initializer;
  203. }
  204. return false;
  205. }
  206. ////////////////////////////////////////////////////////////////////
  207. // Function: CPPInstance::set_initializer
  208. // Access: Public
  209. // Description: Sets the value of the expression that is used to
  210. // initialize the variable, or the default value for a
  211. // parameter. If a non-null expression is set on a
  212. // function declaration, it implies that the function is
  213. // pure virtual.
  214. ////////////////////////////////////////////////////////////////////
  215. void CPPInstance::
  216. set_initializer(CPPExpression *initializer) {
  217. if (_type->as_function_type() != (CPPFunctionType *)NULL) {
  218. // This is a function declaration.
  219. if (initializer == (CPPExpression *)NULL) {
  220. _storage_class &= ~SC_pure_virtual;
  221. } else {
  222. _storage_class |= SC_pure_virtual;
  223. }
  224. _initializer = (CPPExpression *)NULL;
  225. } else {
  226. _initializer = initializer;
  227. }
  228. }
  229. ////////////////////////////////////////////////////////////////////
  230. // Function: CPPInstance::is_scoped
  231. // Access: Public
  232. // Description:
  233. ////////////////////////////////////////////////////////////////////
  234. bool CPPInstance::
  235. is_scoped() const {
  236. if (_ident == NULL) {
  237. return false;
  238. } else {
  239. return _ident->is_scoped();
  240. }
  241. }
  242. ////////////////////////////////////////////////////////////////////
  243. // Function: CPPInstance::get_scope
  244. // Access: Public
  245. // Description:
  246. ////////////////////////////////////////////////////////////////////
  247. CPPScope *CPPInstance::
  248. get_scope(CPPScope *current_scope, CPPScope *global_scope,
  249. CPPPreprocessor *error_sink) const {
  250. if (_ident == NULL) {
  251. return current_scope;
  252. } else {
  253. return _ident->get_scope(current_scope, global_scope, error_sink);
  254. }
  255. }
  256. ////////////////////////////////////////////////////////////////////
  257. // Function: CPPInstance::get_simple_name
  258. // Access: Public
  259. // Description:
  260. ////////////////////////////////////////////////////////////////////
  261. string CPPInstance::
  262. get_simple_name() const {
  263. if (_ident == NULL) {
  264. return "";
  265. } else {
  266. return _ident->get_simple_name();
  267. }
  268. }
  269. ////////////////////////////////////////////////////////////////////
  270. // Function: CPPInstance::get_local_name
  271. // Access: Public
  272. // Description:
  273. ////////////////////////////////////////////////////////////////////
  274. string CPPInstance::
  275. get_local_name(CPPScope *scope) const {
  276. if (_ident == NULL) {
  277. return "";
  278. } else {
  279. return _ident->get_local_name(scope);
  280. }
  281. }
  282. ////////////////////////////////////////////////////////////////////
  283. // Function: CPPInstance::get_fully_scoped_name
  284. // Access: Public
  285. // Description:
  286. ////////////////////////////////////////////////////////////////////
  287. string CPPInstance::
  288. get_fully_scoped_name() const {
  289. if (_ident == NULL) {
  290. return "";
  291. } else {
  292. return _ident->get_fully_scoped_name();
  293. }
  294. }
  295. ////////////////////////////////////////////////////////////////////
  296. // Function: CPPInstance::check_for_constructor
  297. // Access: Public
  298. // Description: If this is a function type instance, checks whether
  299. // the function name matches the class name (or ~name),
  300. // and if so, flags it as a constructor (or destructor).
  301. ////////////////////////////////////////////////////////////////////
  302. void CPPInstance::
  303. check_for_constructor(CPPScope *current_scope, CPPScope *global_scope) {
  304. CPPScope *scope = get_scope(current_scope, global_scope);
  305. if (scope == NULL) {
  306. scope = current_scope;
  307. }
  308. CPPFunctionType *func = _type->as_function_type();
  309. if (func != NULL) {
  310. string method_name = get_local_name(scope);
  311. string class_name = scope->get_local_name();
  312. if (!method_name.empty() && !class_name.empty()) {
  313. if (method_name == class_name) {
  314. CPPType *void_type = CPPType::new_type
  315. (new CPPSimpleType(CPPSimpleType::T_void));
  316. _type = CPPType::new_type
  317. (new CPPFunctionType(void_type, func->_parameters,
  318. func->_flags | CPPFunctionType::F_constructor));
  319. } else if (method_name == "~" + class_name) {
  320. CPPType *void_type = CPPType::new_type
  321. (new CPPSimpleType(CPPSimpleType::T_void));
  322. _type = CPPType::new_type
  323. (new CPPFunctionType(void_type, func->_parameters,
  324. func->_flags | CPPFunctionType::F_destructor));
  325. }
  326. }
  327. }
  328. }
  329. ////////////////////////////////////////////////////////////////////
  330. // Function: CPPInstance::instantiate
  331. // Access: Public
  332. // Description:
  333. ////////////////////////////////////////////////////////////////////
  334. CPPDeclaration *CPPInstance::
  335. instantiate(const CPPTemplateParameterList *actual_params,
  336. CPPScope *current_scope, CPPScope *global_scope,
  337. CPPPreprocessor *error_sink) const {
  338. if (!is_template()) {
  339. if (error_sink != NULL) {
  340. error_sink->warning("Ignoring template parameters for instance " +
  341. _ident->get_local_name());
  342. }
  343. return (CPPInstance *)this;
  344. }
  345. Instantiations::const_iterator ii;
  346. ii = _instantiations.find(actual_params);
  347. if (ii != _instantiations.end()) {
  348. // We've already instantiated this instance with these parameters.
  349. // Return that.
  350. return (*ii).second;
  351. }
  352. CPPTemplateScope *tscope = get_template_scope();
  353. CPPDeclaration::SubstDecl subst;
  354. actual_params->build_subst_decl(tscope->_parameters, subst,
  355. current_scope, global_scope);
  356. CPPInstance *inst =
  357. ((CPPInstance *)this)->substitute_decl(subst, current_scope, global_scope)
  358. ->as_instance();
  359. if (inst == this) {
  360. // Hmm, nothing to substitute. Make a new instance anyway, so we
  361. // can change the name.
  362. inst = new CPPInstance(*this);
  363. }
  364. assert(inst != NULL);
  365. inst->_ident = inst->_ident->substitute_decl(subst, current_scope, global_scope);
  366. if (inst->_ident == _ident) {
  367. inst->_ident = new CPPIdentifier(*inst->_ident);
  368. }
  369. inst->_ident->_names.back().set_templ
  370. (new CPPTemplateParameterList(*actual_params));
  371. inst->_template_scope = NULL;
  372. ((CPPInstance *)this)->_instantiations.insert(Instantiations::value_type(actual_params, inst));
  373. return inst;
  374. }
  375. ////////////////////////////////////////////////////////////////////
  376. // Function: CPPInstance::is_fully_specified
  377. // Access: Public, Virtual
  378. // Description: Returns true if this declaration is an actual,
  379. // factual declaration, or false if some part of the
  380. // declaration depends on a template parameter which has
  381. // not yet been instantiated.
  382. ////////////////////////////////////////////////////////////////////
  383. bool CPPInstance::
  384. is_fully_specified() const {
  385. if (_ident != NULL && !_ident->is_fully_specified()) {
  386. return false;
  387. }
  388. if (_initializer != NULL && !_initializer->is_fully_specified()) {
  389. return false;
  390. }
  391. return CPPDeclaration::is_fully_specified() &&
  392. _type->is_fully_specified();
  393. }
  394. ////////////////////////////////////////////////////////////////////
  395. // Function: CPPInstance::substitute_decl
  396. // Access: Public, Virtual
  397. // Description:
  398. ////////////////////////////////////////////////////////////////////
  399. CPPDeclaration *CPPInstance::
  400. substitute_decl(CPPDeclaration::SubstDecl &subst,
  401. CPPScope *current_scope, CPPScope *global_scope) {
  402. CPPDeclaration *top =
  403. CPPDeclaration::substitute_decl(subst, current_scope, global_scope);
  404. if (top != this) {
  405. return top;
  406. }
  407. CPPInstance *rep = new CPPInstance(*this);
  408. CPPDeclaration *new_type =
  409. _type->substitute_decl(subst, current_scope, global_scope);
  410. rep->_type = new_type->as_type();
  411. if (rep->_type == NULL) {
  412. rep->_type = _type;
  413. }
  414. if (_initializer != NULL) {
  415. rep->_initializer =
  416. _initializer->substitute_decl(subst, current_scope, global_scope)
  417. ->as_expression();
  418. }
  419. if (rep->_type == _type &&
  420. rep->_initializer == _initializer) {
  421. delete rep;
  422. rep = this;
  423. }
  424. subst.insert(SubstDecl::value_type(this, rep));
  425. return rep;
  426. }
  427. ////////////////////////////////////////////////////////////////////
  428. // Function: CPPInstance::output
  429. // Access: Public, Virtual
  430. // Description:
  431. ////////////////////////////////////////////////////////////////////
  432. void CPPInstance::
  433. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  434. output(out, indent_level, scope, complete, -1);
  435. }
  436. ////////////////////////////////////////////////////////////////////
  437. // Function: CPPInstance::output
  438. // Access: Public
  439. // Description: The extra parameter comes into play only when we
  440. // happen to be outputting a function prototype. See
  441. // CPPFunctionType::output().
  442. ////////////////////////////////////////////////////////////////////
  443. void CPPInstance::
  444. output(ostream &out, int indent_level, CPPScope *scope, bool complete,
  445. int num_default_parameters) const {
  446. assert(_type != NULL);
  447. if (_type->is_parameter_expr()) {
  448. // In this case, the whole thing is really an expression, and not
  449. // an instance at all. This can only happen if we parsed an
  450. // instance declaration while we thought we were parsing a
  451. // function prototype.
  452. out << *_initializer;
  453. return;
  454. }
  455. if (is_template()) {
  456. get_template_scope()->_parameters.write_formal(out, scope);
  457. indent(out, indent_level);
  458. }
  459. if (_storage_class & SC_static) {
  460. out << "static ";
  461. }
  462. if (_storage_class & SC_extern) {
  463. out << "extern ";
  464. }
  465. if (_storage_class & SC_c_binding) {
  466. out << "\"C\" ";
  467. }
  468. if (_storage_class & SC_virtual) {
  469. out << "virtual ";
  470. }
  471. if (_storage_class & SC_inline) {
  472. out << "inline ";
  473. }
  474. if (_storage_class & SC_explicit) {
  475. out << "explicit ";
  476. }
  477. if (_storage_class & SC_register) {
  478. out << "register ";
  479. }
  480. if (_storage_class & SC_volatile) {
  481. out << "volatile ";
  482. }
  483. if (_storage_class & SC_mutable) {
  484. out << "mutable ";
  485. }
  486. string name;
  487. if (_ident != NULL) {
  488. name = _ident->get_local_name(scope);
  489. }
  490. if (_type->as_function_type()) {
  491. _type->as_function_type()->
  492. output_instance(out, indent_level, scope, complete, "", name,
  493. num_default_parameters);
  494. } else {
  495. _type->output_instance(out, indent_level, scope, complete, "", name);
  496. }
  497. if (_storage_class & SC_pure_virtual) {
  498. out << " = 0";
  499. }
  500. if (_initializer != NULL) {
  501. out << " = (" << *_initializer << ")";
  502. }
  503. }
  504. ////////////////////////////////////////////////////////////////////
  505. // Function: CPPInstance::get_subtype
  506. // Access: Public, Virtual
  507. // Description:
  508. ////////////////////////////////////////////////////////////////////
  509. CPPDeclaration::SubType CPPInstance::
  510. get_subtype() const {
  511. return ST_instance;
  512. }
  513. ////////////////////////////////////////////////////////////////////
  514. // Function: CPPInstance::as_instance
  515. // Access: Public, Virtual
  516. // Description:
  517. ////////////////////////////////////////////////////////////////////
  518. CPPInstance *CPPInstance::
  519. as_instance() {
  520. return this;
  521. }