cppExpression.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. // Filename: cppExpression.cxx
  2. // Created by: drose (25Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "cppExpression.h"
  19. #include "cppToken.h"
  20. #include "cppIdentifier.h"
  21. #include "cppType.h"
  22. #include "cppSimpleType.h"
  23. #include "cppPointerType.h"
  24. #include "cppConstType.h"
  25. #include "cppArrayType.h"
  26. #include "cppPreprocessor.h"
  27. #include "cppInstance.h"
  28. #include "cppFunctionGroup.h"
  29. #include "cppFunctionType.h"
  30. #include "cppBison.h"
  31. #include <assert.h>
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: CPPExpresion::Result::Constructor
  34. // Access: Public
  35. // Description:
  36. ////////////////////////////////////////////////////////////////////
  37. CPPExpression::Result::
  38. Result() {
  39. _type = RT_error;
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: CPPExpresion::Result::Constructor
  43. // Access: Public
  44. // Description:
  45. ////////////////////////////////////////////////////////////////////
  46. CPPExpression::Result::
  47. Result(int value) {
  48. _type = RT_integer;
  49. _u._integer = value;
  50. }
  51. ////////////////////////////////////////////////////////////////////
  52. // Function: CPPExpresion::Result::Constructor
  53. // Access: Public
  54. // Description:
  55. ////////////////////////////////////////////////////////////////////
  56. CPPExpression::Result::
  57. Result(double value) {
  58. _type = RT_real;
  59. _u._real = value;
  60. }
  61. ////////////////////////////////////////////////////////////////////
  62. // Function: CPPExpresion::Result::Constructor
  63. // Access: Public
  64. // Description:
  65. ////////////////////////////////////////////////////////////////////
  66. CPPExpression::Result::
  67. Result(void *value) {
  68. _type = RT_pointer;
  69. _u._pointer = value;
  70. }
  71. ////////////////////////////////////////////////////////////////////
  72. // Function: CPPExpresion::Result::as_integer
  73. // Access: Public
  74. // Description:
  75. ////////////////////////////////////////////////////////////////////
  76. int CPPExpression::Result::
  77. as_integer() const {
  78. switch (_type) {
  79. case RT_integer:
  80. return _u._integer;
  81. case RT_real:
  82. return (int)_u._real;
  83. case RT_pointer:
  84. return (int)_u._pointer;
  85. default:
  86. cerr << "Invalid type\n";
  87. assert(false);
  88. return 0;
  89. }
  90. }
  91. ////////////////////////////////////////////////////////////////////
  92. // Function: CPPExpresion::Result::as_real
  93. // Access: Public
  94. // Description:
  95. ////////////////////////////////////////////////////////////////////
  96. double CPPExpression::Result::
  97. as_real() const {
  98. switch (_type) {
  99. case RT_integer:
  100. return (double)_u._integer;
  101. case RT_real:
  102. return _u._real;
  103. case RT_pointer:
  104. return (double)(int)_u._pointer;
  105. default:
  106. cerr << "Invalid type\n";
  107. assert(false);
  108. return 0.0;
  109. }
  110. }
  111. ////////////////////////////////////////////////////////////////////
  112. // Function: CPPExpresion::Result::as_pointer
  113. // Access: Public
  114. // Description:
  115. ////////////////////////////////////////////////////////////////////
  116. void *CPPExpression::Result::
  117. as_pointer() const {
  118. switch (_type) {
  119. case RT_integer:
  120. return (void *)_u._integer;
  121. case RT_real:
  122. return (void *)(int)_u._real;
  123. case RT_pointer:
  124. return _u._pointer;
  125. default:
  126. cerr << "Invalid type\n";
  127. assert(false);
  128. return (void *)NULL;
  129. }
  130. }
  131. ////////////////////////////////////////////////////////////////////
  132. // Function: CPPExpresion::Result::output
  133. // Access: Public
  134. // Description:
  135. ////////////////////////////////////////////////////////////////////
  136. void CPPExpression::Result::
  137. output(ostream &out) const {
  138. switch (_type) {
  139. case RT_integer:
  140. out << _u._integer;
  141. break;
  142. case RT_real:
  143. out << _u._real;
  144. break;
  145. case RT_pointer:
  146. out << _u._pointer;
  147. break;
  148. case RT_error:
  149. out << "(error)";
  150. break;
  151. default:
  152. out << "(**invalid type**)\n";
  153. }
  154. }
  155. ////////////////////////////////////////////////////////////////////
  156. // Function: CPPExpresion::Constructor
  157. // Access: Public
  158. // Description:
  159. ////////////////////////////////////////////////////////////////////
  160. CPPExpression::
  161. CPPExpression(int value) :
  162. CPPDeclaration(CPPFile())
  163. {
  164. _type = T_integer;
  165. _u._integer = value;
  166. }
  167. ////////////////////////////////////////////////////////////////////
  168. // Function: CPPExpression::Constructor
  169. // Access: Public
  170. // Description:
  171. ////////////////////////////////////////////////////////////////////
  172. CPPExpression::
  173. CPPExpression(double value) :
  174. CPPDeclaration(CPPFile())
  175. {
  176. _type = T_real;
  177. _u._real = value;
  178. }
  179. ////////////////////////////////////////////////////////////////////
  180. // Function: CPPExpression::Constructor
  181. // Access: Public
  182. // Description:
  183. ////////////////////////////////////////////////////////////////////
  184. CPPExpression::
  185. CPPExpression(const string &value) :
  186. CPPDeclaration(CPPFile())
  187. {
  188. _type = T_string;
  189. _str = value;
  190. }
  191. ////////////////////////////////////////////////////////////////////
  192. // Function: CPPExpression::Constructor
  193. // Access: Public
  194. // Description:
  195. ////////////////////////////////////////////////////////////////////
  196. CPPExpression::
  197. CPPExpression(CPPIdentifier *ident, CPPScope *current_scope,
  198. CPPScope *global_scope, CPPPreprocessor *error_sink) :
  199. CPPDeclaration(CPPFile())
  200. {
  201. CPPDeclaration *decl =
  202. ident->find_symbol(current_scope, global_scope, error_sink);
  203. if (decl != NULL) {
  204. CPPInstance *inst = decl->as_instance();
  205. if (inst != NULL) {
  206. _type = T_variable;
  207. _u._variable = inst;
  208. return;
  209. }
  210. CPPFunctionGroup *fgroup = decl->as_function_group();
  211. if (fgroup != NULL) {
  212. _type = T_function;
  213. _u._fgroup = fgroup;
  214. return;
  215. }
  216. }
  217. _type = T_unknown_ident;
  218. _u._ident = ident;
  219. }
  220. ////////////////////////////////////////////////////////////////////
  221. // Function: CPPExpression::Constructor
  222. // Access: Public
  223. // Description:
  224. ////////////////////////////////////////////////////////////////////
  225. CPPExpression::
  226. CPPExpression(int unary_operator, CPPExpression *op1) :
  227. CPPDeclaration(CPPFile())
  228. {
  229. _type = T_unary_operation;
  230. _u._op._operator = unary_operator;
  231. _u._op._op1 = op1;
  232. _u._op._op2 = NULL;
  233. _u._op._op3 = NULL;
  234. }
  235. ////////////////////////////////////////////////////////////////////
  236. // Function: CPPExpression::Constructor
  237. // Access: Public
  238. // Description:
  239. ////////////////////////////////////////////////////////////////////
  240. CPPExpression::
  241. CPPExpression(int binary_operator, CPPExpression *op1, CPPExpression *op2) :
  242. CPPDeclaration(CPPFile())
  243. {
  244. _type = T_binary_operation;
  245. _u._op._operator = binary_operator;
  246. _u._op._op1 = op1;
  247. _u._op._op2 = op2;
  248. _u._op._op3 = NULL;
  249. }
  250. ////////////////////////////////////////////////////////////////////
  251. // Function: CPPExpression::Constructor
  252. // Access: Public
  253. // Description:
  254. ////////////////////////////////////////////////////////////////////
  255. CPPExpression::
  256. CPPExpression(int trinary_operator, CPPExpression *op1, CPPExpression *op2,
  257. CPPExpression *op3) :
  258. CPPDeclaration(CPPFile())
  259. {
  260. _type = T_trinary_operation;
  261. _u._op._operator = trinary_operator;
  262. _u._op._op1 = op1;
  263. _u._op._op2 = op2;
  264. _u._op._op3 = op3;
  265. }
  266. ////////////////////////////////////////////////////////////////////
  267. // Function: CPPExpression::named typecast_op constructor
  268. // Access: Public, Static
  269. // Description: Creates an expression that represents a typecast
  270. // operation.
  271. ////////////////////////////////////////////////////////////////////
  272. CPPExpression CPPExpression::
  273. typecast_op(CPPType *type, CPPExpression *op1) {
  274. CPPExpression expr(0);
  275. expr._type = T_typecast;
  276. expr._u._typecast._to = type;
  277. expr._u._typecast._op1 = op1;
  278. return expr;
  279. }
  280. ////////////////////////////////////////////////////////////////////
  281. // Function: CPPExpression::named construct_op constructor
  282. // Access: Public, Static
  283. // Description: Creates an expression that represents a constructor
  284. // call.
  285. ////////////////////////////////////////////////////////////////////
  286. CPPExpression CPPExpression::
  287. construct_op(CPPType *type, CPPExpression *op1) {
  288. CPPExpression expr(0);
  289. if (op1 == NULL) {
  290. // A default constructor call--no parameters.
  291. expr._type = T_default_construct;
  292. expr._u._typecast._to = type;
  293. expr._u._typecast._op1 = NULL;
  294. } else {
  295. // A normal constructor call, with parameters.
  296. expr._type = T_construct;
  297. expr._u._typecast._to = type;
  298. expr._u._typecast._op1 = op1;
  299. }
  300. return expr;
  301. }
  302. ////////////////////////////////////////////////////////////////////
  303. // Function: CPPExpression::named new_op constructor
  304. // Access: Public, Static
  305. // Description: Creates an expression that represents a use of the
  306. // new operator.
  307. ////////////////////////////////////////////////////////////////////
  308. CPPExpression CPPExpression::
  309. new_op(CPPType *type, CPPExpression *op1) {
  310. CPPExpression expr(0);
  311. if (op1 == NULL) {
  312. // A default new operation--no parameters.
  313. expr._type = T_default_new;
  314. expr._u._typecast._to = type;
  315. expr._u._typecast._op1 = NULL;
  316. } else {
  317. // A normal new operation, with parameters.
  318. expr._type = T_new;
  319. expr._u._typecast._to = type;
  320. expr._u._typecast._op1 = op1;
  321. }
  322. return expr;
  323. }
  324. ////////////////////////////////////////////////////////////////////
  325. // Function: CPPExpression::named sizeof_func constructor
  326. // Access: Public, Static
  327. // Description:
  328. ////////////////////////////////////////////////////////////////////
  329. CPPExpression CPPExpression::
  330. sizeof_func(CPPType *type) {
  331. CPPExpression expr(0);
  332. expr._type = T_sizeof;
  333. expr._u._typecast._to = type;
  334. expr._u._typecast._op1 = NULL;
  335. return expr;
  336. }
  337. ////////////////////////////////////////////////////////////////////
  338. // Function: CPPExpression::Destructor
  339. // Access: Public
  340. // Description:
  341. ////////////////////////////////////////////////////////////////////
  342. CPPExpression::
  343. ~CPPExpression() {
  344. }
  345. ////////////////////////////////////////////////////////////////////
  346. // Function: CPPExpression::evaluate
  347. // Access: Public
  348. // Description:
  349. ////////////////////////////////////////////////////////////////////
  350. CPPExpression::Result CPPExpression::
  351. evaluate() const {
  352. Result r1, r2;
  353. switch (_type) {
  354. case T_integer:
  355. return Result(_u._integer);
  356. case T_real:
  357. return Result(_u._real);
  358. case T_string:
  359. return Result();
  360. case T_variable:
  361. case T_function:
  362. return Result();
  363. case T_unknown_ident:
  364. return Result();
  365. case T_typecast:
  366. assert(_u._typecast._op1 != NULL);
  367. r1 = _u._typecast._op1->evaluate();
  368. if (r1._type != RT_error) {
  369. CPPSimpleType *stype = _u._typecast._to->as_simple_type();
  370. if (stype != NULL) {
  371. if (stype->_type == CPPSimpleType::T_int) {
  372. return Result(r1.as_integer());
  373. } else if (stype->_type == CPPSimpleType::T_float ||
  374. stype->_type == CPPSimpleType::T_double) {
  375. return Result(r1.as_real());
  376. }
  377. }
  378. if (_u._typecast._to->as_pointer_type()) {
  379. return Result(r1.as_pointer());
  380. }
  381. }
  382. return Result();
  383. case T_construct:
  384. case T_default_construct:
  385. case T_new:
  386. case T_default_new:
  387. case T_sizeof:
  388. return Result();
  389. case T_binary_operation:
  390. assert(_u._op._op2 != NULL);
  391. r2 = _u._op._op2->evaluate();
  392. // The operators && and || are special cases: these are
  393. // shirt-circuiting operators. Thus, if we are using either of
  394. // these it might be acceptable for the second operand to be
  395. // invalid, since we might never evaluate it.
  396. // In all other cases, both operands must be valid in order for
  397. // the operation to be valid.
  398. if (r2._type == RT_error &&
  399. (_u._op._operator != OROR && _u._op._operator != ANDAND)) {
  400. return r2;
  401. }
  402. // Fall through
  403. case T_trinary_operation:
  404. // The trinary operator is also a short-circuiting operator: we
  405. // don't test the second or third operands until we need them.
  406. // The only critical one is the first operand.
  407. // Fall through
  408. case T_unary_operation:
  409. assert(_u._op._op1 != NULL);
  410. r1 = _u._op._op1->evaluate();
  411. if (r1._type == RT_error) {
  412. return r1;
  413. }
  414. switch (_u._op._operator) {
  415. case UNARY_NOT:
  416. return Result(!r1.as_integer());
  417. case UNARY_NEGATE:
  418. return Result(~r1.as_integer());
  419. case UNARY_MINUS:
  420. return (r1._type == RT_real) ? Result(-r1.as_real()) : Result(-r1.as_integer());
  421. case UNARY_STAR:
  422. case UNARY_REF:
  423. return Result();
  424. case '*':
  425. if (r1._type == RT_real || r2._type == RT_real) {
  426. return Result(r1.as_real() * r2.as_real());
  427. } else {
  428. return Result(r1.as_integer() * r2.as_integer());
  429. }
  430. case '/':
  431. if (r1._type == RT_real || r2._type == RT_real) {
  432. return Result(r1.as_real() / r2.as_real());
  433. } else {
  434. return Result(r1.as_integer() / r2.as_integer());
  435. }
  436. case '%':
  437. return Result(r1.as_integer() % r2.as_integer());
  438. case '+':
  439. if (r1._type == RT_real || r2._type == RT_real) {
  440. return Result(r1.as_real() + r2.as_real());
  441. } else {
  442. return Result(r1.as_integer() + r2.as_integer());
  443. }
  444. case '-':
  445. if (r1._type == RT_real || r2._type == RT_real) {
  446. return Result(r1.as_real() - r2.as_real());
  447. } else {
  448. return Result(r1.as_integer() - r2.as_integer());
  449. }
  450. case '|':
  451. return Result(r1.as_integer() | r2.as_integer());
  452. case '&':
  453. return Result(r1.as_integer() & r2.as_integer());
  454. case OROR:
  455. if (r1.as_integer()) {
  456. return r1;
  457. } else {
  458. return r2;
  459. }
  460. case ANDAND:
  461. if (r1.as_integer()) {
  462. return r2;
  463. } else {
  464. return r1;
  465. }
  466. case EQCOMPARE:
  467. if (r1._type == RT_real || r2._type == RT_real) {
  468. return Result(r1.as_real() == r2.as_real());
  469. } else {
  470. return Result(r1.as_integer() == r2.as_integer());
  471. }
  472. case NECOMPARE:
  473. if (r1._type == RT_real || r2._type == RT_real) {
  474. return Result(r1.as_real() != r2.as_real());
  475. } else {
  476. return Result(r1.as_integer() != r2.as_integer());
  477. }
  478. case LECOMPARE:
  479. if (r1._type == RT_real || r2._type == RT_real) {
  480. return Result(r1.as_real() <= r2.as_real());
  481. } else {
  482. return Result(r1.as_integer() <= r2.as_integer());
  483. }
  484. case GECOMPARE:
  485. if (r1._type == RT_real || r2._type == RT_real) {
  486. return Result(r1.as_real() >= r2.as_real());
  487. } else {
  488. return Result(r1.as_integer() >= r2.as_integer());
  489. }
  490. case '<':
  491. if (r1._type == RT_real || r2._type == RT_real) {
  492. return Result(r1.as_real() < r2.as_real());
  493. } else {
  494. return Result(r1.as_integer() < r2.as_integer());
  495. }
  496. case '>':
  497. if (r1._type == RT_real || r2._type == RT_real) {
  498. return Result(r1.as_real() > r2.as_real());
  499. } else {
  500. return Result(r1.as_integer() > r2.as_integer());
  501. }
  502. case LSHIFT:
  503. return Result(r1.as_integer() << r2.as_integer());
  504. case RSHIFT:
  505. return Result(r1.as_integer() >> r2.as_integer());
  506. case '?':
  507. return r1.as_integer() ?
  508. _u._op._op2->evaluate() : _u._op._op3->evaluate();
  509. case '.':
  510. case POINTSAT:
  511. return Result();
  512. case '[': // Array element reference
  513. return Result();
  514. case 'f': // Function evaluation
  515. return Result();
  516. case ',':
  517. return r2;
  518. default:
  519. cerr << "**unexpected operator**\n";
  520. abort();
  521. }
  522. default:
  523. cerr << "**invalid operand**\n";
  524. abort();
  525. }
  526. return Result(); // Compiler kludge; can't get here.
  527. }
  528. ////////////////////////////////////////////////////////////////////
  529. // Function: CPPExpression::determine_type
  530. // Access: Public
  531. // Description: Returns the type of the expression, if it is known,
  532. // or NULL if the type cannot be determined.
  533. ////////////////////////////////////////////////////////////////////
  534. CPPType *CPPExpression::
  535. determine_type() const {
  536. CPPType *t1 = (CPPType *)NULL;
  537. CPPType *t2 = (CPPType *)NULL;
  538. CPPType *int_type =
  539. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
  540. CPPType *bool_type =
  541. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
  542. CPPType *float_type =
  543. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
  544. CPPType *char_type =
  545. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
  546. CPPType *const_char_type =
  547. CPPType::new_type(new CPPConstType(char_type));
  548. CPPType *char_star_type =
  549. CPPType::new_type(new CPPPointerType(const_char_type));
  550. switch (_type) {
  551. case T_integer:
  552. return int_type;
  553. case T_real:
  554. return float_type;
  555. case T_string:
  556. return char_star_type;
  557. case T_variable:
  558. return _u._variable->_type;
  559. case T_function:
  560. if (_u._fgroup->get_return_type() == (CPPType *)NULL) {
  561. // There are multiple functions by this name that have different
  562. // return types. We could attempt to differentiate them based
  563. // on the parameter list, but that's a lot of work. Let's just
  564. // give up.
  565. return (CPPType *)NULL;
  566. }
  567. return _u._fgroup->_instances.front()->_type;
  568. case T_unknown_ident:
  569. return (CPPType *)NULL;
  570. case T_typecast:
  571. case T_construct:
  572. case T_default_construct:
  573. return _u._typecast._to;
  574. case T_new:
  575. case T_default_new:
  576. return CPPType::new_type(new CPPPointerType(_u._typecast._to));
  577. case T_sizeof:
  578. return int_type;
  579. case T_binary_operation:
  580. case T_trinary_operation:
  581. assert(_u._op._op2 != NULL);
  582. t2 = _u._op._op2->determine_type();
  583. // Fall through
  584. case T_unary_operation:
  585. assert(_u._op._op1 != NULL);
  586. t1 = _u._op._op1->determine_type();
  587. switch (_u._op._operator) {
  588. case UNARY_NOT:
  589. return bool_type;
  590. case UNARY_NEGATE:
  591. return int_type;
  592. case UNARY_MINUS:
  593. return t1;
  594. case UNARY_STAR:
  595. case '[': // Array element reference
  596. if (t1 != NULL) {
  597. if (t1->as_pointer_type()) {
  598. return t1->as_pointer_type()->_pointing_at;
  599. }
  600. if (t1->as_array_type()) {
  601. return t1->as_array_type()->_element_type;
  602. }
  603. }
  604. return NULL;
  605. case UNARY_REF:
  606. return t1;
  607. case '*':
  608. case '/':
  609. case '+':
  610. case '-':
  611. if (t1 == NULL) {
  612. return t2;
  613. } else if (t2 == NULL) {
  614. return t1;
  615. } else if (t1->as_pointer_type()) {
  616. if (t2->as_pointer_type()) {
  617. return int_type;
  618. }
  619. return t1;
  620. }
  621. return elevate_type(t1, t2);
  622. case '%':
  623. case '|':
  624. case '&':
  625. case LSHIFT:
  626. case RSHIFT:
  627. return int_type;
  628. case OROR:
  629. case ANDAND:
  630. case EQCOMPARE:
  631. case NECOMPARE:
  632. case LECOMPARE:
  633. case GECOMPARE:
  634. case '<':
  635. case '>':
  636. return bool_type;
  637. case '?':
  638. return t2;
  639. case '.':
  640. case POINTSAT:
  641. return NULL;
  642. case 'f': // Function evaluation
  643. if (t1 != NULL) {
  644. CPPFunctionType *ftype = t1->as_function_type();
  645. if (ftype != (CPPFunctionType *)NULL) {
  646. return ftype->_return_type;
  647. }
  648. }
  649. return NULL;
  650. case ',':
  651. return t2;
  652. default:
  653. cerr << "**unexpected operator**\n";
  654. abort();
  655. }
  656. default:
  657. cerr << "**invalid operand**\n";
  658. abort();
  659. }
  660. return NULL; // Compiler kludge; can't get here.
  661. }
  662. ////////////////////////////////////////////////////////////////////
  663. // Function: CPPExpression::substitute_decl
  664. // Access: Public, Virtual
  665. // Description:
  666. ////////////////////////////////////////////////////////////////////
  667. CPPDeclaration *CPPExpression::
  668. substitute_decl(CPPDeclaration::SubstDecl &subst,
  669. CPPScope *current_scope, CPPScope *global_scope) {
  670. CPPDeclaration *top =
  671. CPPDeclaration::substitute_decl(subst, current_scope, global_scope);
  672. if (top != this) {
  673. return top;
  674. }
  675. CPPExpression *rep = new CPPExpression(*this);
  676. bool any_changed = false;
  677. CPPDeclaration *decl;
  678. switch (_type) {
  679. case T_variable:
  680. decl = _u._variable->substitute_decl(subst, current_scope, global_scope);
  681. if (decl != rep->_u._variable) {
  682. if (decl->as_instance()) {
  683. // Replacing the variable reference with another variable reference.
  684. rep->_u._variable = decl->as_instance();
  685. any_changed = true;
  686. } else if (decl->as_expression()) {
  687. // Replacing the variable reference with an expression.
  688. delete rep;
  689. rep = decl->as_expression();
  690. any_changed = true;
  691. }
  692. }
  693. break;
  694. case T_typecast:
  695. case T_construct:
  696. case T_new:
  697. rep->_u._typecast._op1 =
  698. _u._typecast._op1->substitute_decl(subst, current_scope, global_scope)
  699. ->as_expression();
  700. any_changed = any_changed || (rep->_u._typecast._op1 != _u._typecast._op1);
  701. // fall through
  702. case T_default_construct:
  703. case T_default_new:
  704. case T_sizeof:
  705. rep->_u._typecast._to =
  706. _u._typecast._to->substitute_decl(subst, current_scope, global_scope)
  707. ->as_type();
  708. any_changed = any_changed || (rep->_u._typecast._to != _u._typecast._to);
  709. break;
  710. case T_trinary_operation:
  711. rep->_u._op._op3 =
  712. _u._op._op3->substitute_decl(subst, current_scope, global_scope)
  713. ->as_expression();
  714. any_changed = any_changed || (rep->_u._op._op3 != _u._op._op3);
  715. // fall through
  716. case T_binary_operation:
  717. rep->_u._op._op2 =
  718. _u._op._op2->substitute_decl(subst, current_scope, global_scope)
  719. ->as_expression();
  720. any_changed = any_changed || (rep->_u._op._op2 != _u._op._op2);
  721. // fall through
  722. case T_unary_operation:
  723. rep->_u._op._op1 =
  724. _u._op._op1->substitute_decl(subst, current_scope, global_scope)
  725. ->as_expression();
  726. any_changed = any_changed || (rep->_u._op._op1 != _u._op._op1);
  727. break;
  728. default:
  729. break;
  730. }
  731. if (!any_changed) {
  732. delete rep;
  733. rep = this;
  734. }
  735. subst.insert(SubstDecl::value_type(this, rep));
  736. return rep;
  737. }
  738. ////////////////////////////////////////////////////////////////////
  739. // Function: CPPExpression::is_tbd
  740. // Access: Public
  741. // Description: Returns true if any type within the expression list is
  742. // a CPPTBDType and thus isn't fully determined right
  743. // now.
  744. ////////////////////////////////////////////////////////////////////
  745. bool CPPExpression::
  746. is_tbd() const {
  747. switch (_type) {
  748. case T_variable:
  749. return true;
  750. case T_typecast:
  751. case T_construct:
  752. case T_new:
  753. case T_default_construct:
  754. case T_default_new:
  755. case T_sizeof:
  756. return _u._typecast._to->is_tbd();
  757. case T_trinary_operation:
  758. if (_u._op._op3->is_tbd()) {
  759. return true;
  760. }
  761. // fall through
  762. case T_binary_operation:
  763. if (_u._op._op2->is_tbd()) {
  764. return true;
  765. }
  766. // fall through
  767. case T_unary_operation:
  768. if (_u._op._op1->is_tbd()) {
  769. return true;
  770. }
  771. return false;
  772. default:
  773. return false;
  774. }
  775. }
  776. ////////////////////////////////////////////////////////////////////
  777. // Function: CPPExpression::output
  778. // Access: Public
  779. // Description:
  780. ////////////////////////////////////////////////////////////////////
  781. void CPPExpression::
  782. output(ostream &out, int indent_level, CPPScope *scope, bool) const {
  783. switch (_type) {
  784. case T_integer:
  785. out << _u._integer;
  786. break;
  787. case T_real:
  788. out << _u._real;
  789. break;
  790. case T_string:
  791. out << '"';
  792. {
  793. string::const_iterator si;
  794. for (si = _str.begin(); si != _str.end(); ++si) {
  795. switch (*si) {
  796. case '\n':
  797. out << "\\n";
  798. break;
  799. case '\t':
  800. out << "\\t";
  801. break;
  802. case '\r':
  803. out << "\\r";
  804. break;
  805. case '\a':
  806. out << "\\a";
  807. break;
  808. case '"':
  809. out << "\\\"";
  810. break;
  811. default:
  812. if (isprint(*si)) {
  813. out << *si;
  814. } else {
  815. out << '\\' << oct << setw(3) << setfill('0') << (int)(*si)
  816. << dec << setw(0);
  817. }
  818. }
  819. }
  820. }
  821. out << '"';
  822. break;
  823. case T_variable:
  824. _u._variable->_ident->output(out, scope);
  825. break;
  826. case T_function:
  827. out << _u._fgroup->_name;
  828. break;
  829. case T_unknown_ident:
  830. _u._ident->output(out, scope);
  831. break;
  832. case T_typecast:
  833. out << "(";
  834. _u._typecast._to->output(out, indent_level, scope, false);
  835. out << ")(";
  836. _u._typecast._op1->output(out, indent_level, scope, false);
  837. out << ")";
  838. break;
  839. case T_construct:
  840. out << "(" << _u._typecast._to->get_typedef_name(scope)
  841. << "(";
  842. _u._typecast._op1->output(out, indent_level, scope, false);
  843. out << "))";
  844. break;
  845. case T_default_construct:
  846. out << "(" << _u._typecast._to->get_typedef_name(scope) << "())";
  847. break;
  848. case T_new:
  849. out << "(new ";
  850. _u._typecast._to->output(out, indent_level, scope, false);
  851. out << "(";
  852. _u._typecast._op1->output(out, indent_level, scope, false);
  853. out << "))";
  854. break;
  855. case T_default_new:
  856. out << "(new ";
  857. _u._typecast._to->output(out, indent_level, scope, false);
  858. out << "())";
  859. break;
  860. case T_sizeof:
  861. out << "sizeof(";
  862. _u._typecast._to->output(out, indent_level, scope, false);
  863. out << ")";
  864. break;
  865. case T_unary_operation:
  866. switch (_u._op._operator) {
  867. case UNARY_NOT:
  868. out << "(! ";
  869. _u._op._op1->output(out, indent_level, scope, false);
  870. out << ")";
  871. break;
  872. case UNARY_NEGATE:
  873. out << "(~ ";
  874. _u._op._op1->output(out, indent_level, scope, false);
  875. out << ")";
  876. break;
  877. case UNARY_MINUS:
  878. out << "(- ";
  879. _u._op._op1->output(out, indent_level, scope, false);
  880. out << ")";
  881. break;
  882. case UNARY_STAR:
  883. out << "(* ";
  884. _u._op._op1->output(out, indent_level, scope, false);
  885. out << ")";
  886. break;
  887. case UNARY_REF:
  888. out << "(& ";
  889. _u._op._op1->output(out, indent_level, scope, false);
  890. out << ")";
  891. break;
  892. case 'f': // Function evaluation, no parameters.
  893. out << "(";
  894. _u._op._op1->output(out, indent_level, scope, false);
  895. out << "())";
  896. break;
  897. default:
  898. out << "(" << (char)_u._op._operator << " ";
  899. _u._op._op1->output(out, indent_level, scope, false);
  900. out << ")";
  901. break;
  902. }
  903. break;
  904. case T_binary_operation:
  905. switch (_u._op._operator) {
  906. case OROR:
  907. out << "(";
  908. _u._op._op1->output(out, indent_level, scope, false);
  909. out << " || ";
  910. _u._op._op2->output(out, indent_level, scope, false);
  911. out << ")";
  912. break;
  913. case ANDAND:
  914. out << "(";
  915. _u._op._op1->output(out, indent_level, scope, false);
  916. out << " && ";
  917. _u._op._op2->output(out, indent_level, scope, false);
  918. out << ")";
  919. break;
  920. case EQCOMPARE:
  921. out << "(";
  922. _u._op._op1->output(out, indent_level, scope, false);
  923. out << " == ";
  924. _u._op._op2->output(out, indent_level, scope, false);
  925. out << ")";
  926. break;
  927. case NECOMPARE:
  928. out << "(";
  929. _u._op._op1->output(out, indent_level, scope, false);
  930. out << " != ";
  931. _u._op._op2->output(out, indent_level, scope, false);
  932. out << ")";
  933. break;
  934. case LECOMPARE:
  935. out << "(";
  936. _u._op._op1->output(out, indent_level, scope, false);
  937. out << " <= ";
  938. _u._op._op2->output(out, indent_level, scope, false);
  939. out << ")";
  940. break;
  941. case GECOMPARE:
  942. out << "(";
  943. _u._op._op1->output(out, indent_level, scope, false);
  944. out << " >= ";
  945. _u._op._op2->output(out, indent_level, scope, false);
  946. out << ")";
  947. break;
  948. case LSHIFT:
  949. out << "(";
  950. _u._op._op1->output(out, indent_level, scope, false);
  951. out << " << ";
  952. _u._op._op2->output(out, indent_level, scope, false);
  953. out << ")";
  954. break;
  955. case RSHIFT:
  956. out << "(";
  957. _u._op._op1->output(out, indent_level, scope, false);
  958. out << " >> ";
  959. _u._op._op2->output(out, indent_level, scope, false);
  960. out << ")";
  961. break;
  962. case '.':
  963. out << "(";
  964. _u._op._op1->output(out, indent_level, scope, false);
  965. out << ".";
  966. _u._op._op2->output(out, indent_level, scope, false);
  967. out << ")";
  968. break;
  969. case POINTSAT:
  970. out << "(";
  971. _u._op._op1->output(out, indent_level, scope, false);
  972. out << "->";
  973. _u._op._op2->output(out, indent_level, scope, false);
  974. out << ")";
  975. break;
  976. case '[': // Array element reference
  977. out << "(";
  978. _u._op._op1->output(out, indent_level, scope, false);
  979. out << "[";
  980. _u._op._op2->output(out, indent_level, scope, false);
  981. out << "])";
  982. break;
  983. case 'f': // Function evaluation
  984. out << "(";
  985. _u._op._op1->output(out, indent_level, scope, false);
  986. out << "(";
  987. _u._op._op2->output(out, indent_level, scope, false);
  988. out << "))";
  989. break;
  990. case ',': // Comma, no parens are used
  991. _u._op._op1->output(out, indent_level, scope, false);
  992. out << ", ";
  993. _u._op._op2->output(out, indent_level, scope, false);
  994. break;
  995. default:
  996. out << "(";
  997. _u._op._op1->output(out, indent_level, scope, false);
  998. out << " " << (char)_u._op._operator << " ";
  999. _u._op._op2->output(out, indent_level, scope, false);
  1000. out << ")";
  1001. }
  1002. break;
  1003. case T_trinary_operation:
  1004. out << "(";
  1005. _u._op._op1->output(out, indent_level, scope, false);
  1006. out << " ? ";
  1007. _u._op._op2->output(out, indent_level, scope, false);
  1008. out << " : ";
  1009. _u._op._op3->output(out, indent_level, scope, false);
  1010. out << ")";
  1011. break;
  1012. default:
  1013. out << "(** invalid operand type " << (int)_type << " **)";
  1014. }
  1015. }
  1016. ////////////////////////////////////////////////////////////////////
  1017. // Function: CPPExpression::get_subtype
  1018. // Access: Public, Virtual
  1019. // Description:
  1020. ////////////////////////////////////////////////////////////////////
  1021. CPPDeclaration::SubType CPPExpression::
  1022. get_subtype() const {
  1023. return ST_expression;
  1024. }
  1025. ////////////////////////////////////////////////////////////////////
  1026. // Function: CPPExpression::as_expression
  1027. // Access: Public, Virtual
  1028. // Description:
  1029. ////////////////////////////////////////////////////////////////////
  1030. CPPExpression *CPPExpression::
  1031. as_expression() {
  1032. return this;
  1033. }
  1034. ////////////////////////////////////////////////////////////////////
  1035. // Function: CPPExpression::elevate_type
  1036. // Access: Public, Static
  1037. // Description: Returns the most general of the two given types.
  1038. ////////////////////////////////////////////////////////////////////
  1039. CPPType *CPPExpression::
  1040. elevate_type(CPPType *t1, CPPType *t2) {
  1041. CPPSimpleType *st1 = t1->as_simple_type();
  1042. CPPSimpleType *st2 = t2->as_simple_type();
  1043. if (st1 == NULL || st2 == NULL) {
  1044. // Nothing we can do about this. Who knows?
  1045. return NULL;
  1046. }
  1047. if (st1->_type == st2->_type) {
  1048. // They have the same type, so return the one with the largest
  1049. // flag bits.
  1050. if (st1->_flags & CPPSimpleType::F_longlong) {
  1051. return st1;
  1052. } else if (st2->_flags & CPPSimpleType::F_longlong) {
  1053. return st2;
  1054. } else if (st1->_flags & CPPSimpleType::F_long) {
  1055. return st1;
  1056. } else if (st2->_flags & CPPSimpleType::F_long) {
  1057. return st2;
  1058. } else if (st1->_flags & CPPSimpleType::F_short) {
  1059. return st2;
  1060. } else if (st2->_flags & CPPSimpleType::F_short) {
  1061. return st1;
  1062. }
  1063. return st1;
  1064. }
  1065. // They have different types.
  1066. if (st1->_type == CPPSimpleType::T_float ||
  1067. st1->_type == CPPSimpleType::T_double) {
  1068. return st1;
  1069. } else if (st2->_type == CPPSimpleType::T_float ||
  1070. st2->_type == CPPSimpleType::T_double) {
  1071. return st2;
  1072. } else if (st1->_type == CPPSimpleType::T_int) {
  1073. return st1;
  1074. } else if (st2->_type == CPPSimpleType::T_int) {
  1075. return st2;
  1076. } else if (st1->_type == CPPSimpleType::T_bool) {
  1077. return st1;
  1078. } else if (st2->_type == CPPSimpleType::T_bool) {
  1079. return st2;
  1080. }
  1081. return st1;
  1082. }
  1083. ////////////////////////////////////////////////////////////////////
  1084. // Function: CPPExpression::is_equal
  1085. // Access: Protected, Virtual
  1086. // Description: Called by CPPDeclaration to determine whether this
  1087. // expr is equivalent to another expr.
  1088. ////////////////////////////////////////////////////////////////////
  1089. bool CPPExpression::
  1090. is_equal(const CPPDeclaration *other) const {
  1091. const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
  1092. assert(ot != NULL);
  1093. if (_type != ot->_type) {
  1094. return false;
  1095. }
  1096. switch (_type) {
  1097. case T_integer:
  1098. return _u._integer == ot->_u._integer;
  1099. case T_real:
  1100. return _u._real == ot->_u._real;
  1101. case T_string:
  1102. return _str == ot->_str;
  1103. case T_variable:
  1104. return _u._variable == ot->_u._variable;
  1105. case T_function:
  1106. return _u._fgroup == ot->_u._fgroup;
  1107. case T_unknown_ident:
  1108. return *_u._ident == *ot->_u._ident;
  1109. case T_typecast:
  1110. case T_construct:
  1111. case T_new:
  1112. return _u._typecast._to == ot->_u._typecast._to &&
  1113. *_u._typecast._op1 == *ot->_u._typecast._op1;
  1114. case T_default_construct:
  1115. case T_default_new:
  1116. case T_sizeof:
  1117. return _u._typecast._to == ot->_u._typecast._to;
  1118. case T_unary_operation:
  1119. return *_u._op._op1 == *ot->_u._op._op1;
  1120. case T_binary_operation:
  1121. return *_u._op._op1 == *ot->_u._op._op1 &&
  1122. *_u._op._op2 == *ot->_u._op._op2;
  1123. case T_trinary_operation:
  1124. return *_u._op._op1 == *ot->_u._op._op1 &&
  1125. *_u._op._op2 == *ot->_u._op._op2;
  1126. default:
  1127. cerr << "(** invalid operand type " << (int)_type << " **)";
  1128. }
  1129. return true;
  1130. }
  1131. ////////////////////////////////////////////////////////////////////
  1132. // Function: CPPExpression::is_less
  1133. // Access: Protected, Virtual
  1134. // Description: Called by CPPDeclaration to determine whether this
  1135. // expr should be ordered before another expr of the
  1136. // same type, in an arbitrary but fixed ordering.
  1137. ////////////////////////////////////////////////////////////////////
  1138. bool CPPExpression::
  1139. is_less(const CPPDeclaration *other) const {
  1140. const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
  1141. assert(ot != NULL);
  1142. if (_type != ot->_type) {
  1143. return (int)_type < (int)ot->_type;
  1144. }
  1145. switch (_type) {
  1146. case T_integer:
  1147. return _u._integer < ot->_u._integer;
  1148. case T_real:
  1149. return _u._real < ot->_u._real;
  1150. case T_string:
  1151. return _str < ot->_str;
  1152. case T_variable:
  1153. return _u._variable < ot->_u._variable;
  1154. case T_function:
  1155. return *_u._fgroup < *ot->_u._fgroup;
  1156. case T_unknown_ident:
  1157. return *_u._ident < *ot->_u._ident;
  1158. case T_typecast:
  1159. case T_construct:
  1160. case T_new:
  1161. if (_u._typecast._to != ot->_u._typecast._to) {
  1162. return _u._typecast._to < ot->_u._typecast._to;
  1163. }
  1164. return *_u._typecast._op1 < *ot->_u._typecast._op1;
  1165. case T_default_construct:
  1166. case T_default_new:
  1167. case T_sizeof:
  1168. return _u._typecast._to < ot->_u._typecast._to;
  1169. case T_trinary_operation:
  1170. if (*_u._op._op3 != *ot->_u._op._op3) {
  1171. return *_u._op._op3 < *ot->_u._op._op3;
  1172. }
  1173. // Fall through
  1174. case T_binary_operation:
  1175. if (*_u._op._op2 != *ot->_u._op._op2) {
  1176. return *_u._op._op2 < *ot->_u._op._op2;
  1177. }
  1178. // Fall through
  1179. case T_unary_operation:
  1180. return *_u._op._op1 < *ot->_u._op._op1;
  1181. default:
  1182. cerr << "(** invalid operand type " << (int)_type << " **)";
  1183. }
  1184. return false;
  1185. }