cppExpression.cxx 40 KB

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