cppExpression.cxx 41 KB

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