cppExpression.cxx 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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(unsigned long long 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(int value) :
  193. CPPDeclaration(CPPFile())
  194. {
  195. _type = T_integer;
  196. _u._integer = value;
  197. }
  198. ////////////////////////////////////////////////////////////////////
  199. // Function: CPPExpression::Constructor
  200. // Access: Public
  201. // Description:
  202. ////////////////////////////////////////////////////////////////////
  203. CPPExpression::
  204. CPPExpression(long double value) :
  205. CPPDeclaration(CPPFile())
  206. {
  207. _type = T_real;
  208. _u._real = value;
  209. }
  210. ////////////////////////////////////////////////////////////////////
  211. // Function: CPPExpression::Constructor
  212. // Access: Public
  213. // Description:
  214. ////////////////////////////////////////////////////////////////////
  215. CPPExpression::
  216. CPPExpression(const string &value) :
  217. CPPDeclaration(CPPFile())
  218. {
  219. _type = T_string;
  220. _str = value;
  221. }
  222. ////////////////////////////////////////////////////////////////////
  223. // Function: CPPExpression::Constructor
  224. // Access: Public
  225. // Description:
  226. ////////////////////////////////////////////////////////////////////
  227. CPPExpression::
  228. CPPExpression(CPPIdentifier *ident, CPPScope *current_scope,
  229. CPPScope *global_scope, CPPPreprocessor *error_sink) :
  230. CPPDeclaration(CPPFile())
  231. {
  232. CPPDeclaration *decl =
  233. ident->find_symbol(current_scope, global_scope);
  234. if (decl != NULL) {
  235. CPPInstance *inst = decl->as_instance();
  236. if (inst != NULL) {
  237. _type = T_variable;
  238. _u._variable = inst;
  239. return;
  240. }
  241. CPPFunctionGroup *fgroup = decl->as_function_group();
  242. if (fgroup != NULL) {
  243. _type = T_function;
  244. _u._fgroup = fgroup;
  245. return;
  246. }
  247. }
  248. _type = T_unknown_ident;
  249. _u._ident = ident;
  250. _u._ident->_native_scope = current_scope;
  251. }
  252. ////////////////////////////////////////////////////////////////////
  253. // Function: CPPExpression::Constructor
  254. // Access: Public
  255. // Description:
  256. ////////////////////////////////////////////////////////////////////
  257. CPPExpression::
  258. CPPExpression(int unary_operator, CPPExpression *op1) :
  259. CPPDeclaration(CPPFile())
  260. {
  261. _type = T_unary_operation;
  262. _u._op._operator = unary_operator;
  263. _u._op._op1 = op1;
  264. _u._op._op2 = NULL;
  265. _u._op._op3 = NULL;
  266. }
  267. ////////////////////////////////////////////////////////////////////
  268. // Function: CPPExpression::Constructor
  269. // Access: Public
  270. // Description:
  271. ////////////////////////////////////////////////////////////////////
  272. CPPExpression::
  273. CPPExpression(int binary_operator, CPPExpression *op1, CPPExpression *op2) :
  274. CPPDeclaration(CPPFile())
  275. {
  276. _type = T_binary_operation;
  277. _u._op._operator = binary_operator;
  278. _u._op._op1 = op1;
  279. _u._op._op2 = op2;
  280. _u._op._op3 = NULL;
  281. }
  282. ////////////////////////////////////////////////////////////////////
  283. // Function: CPPExpression::Constructor
  284. // Access: Public
  285. // Description:
  286. ////////////////////////////////////////////////////////////////////
  287. CPPExpression::
  288. CPPExpression(int trinary_operator, CPPExpression *op1, CPPExpression *op2,
  289. CPPExpression *op3) :
  290. CPPDeclaration(CPPFile())
  291. {
  292. _type = T_trinary_operation;
  293. _u._op._operator = trinary_operator;
  294. _u._op._op1 = op1;
  295. _u._op._op2 = op2;
  296. _u._op._op3 = op3;
  297. }
  298. ////////////////////////////////////////////////////////////////////
  299. // Function: CPPExpression::named typecast_op constructor
  300. // Access: Public, Static
  301. // Description: Creates an expression that represents a typecast
  302. // operation.
  303. ////////////////////////////////////////////////////////////////////
  304. CPPExpression CPPExpression::
  305. typecast_op(CPPType *type, CPPExpression *op1) {
  306. CPPExpression expr(0);
  307. expr._type = T_typecast;
  308. expr._u._typecast._to = type;
  309. expr._u._typecast._op1 = op1;
  310. return expr;
  311. }
  312. ////////////////////////////////////////////////////////////////////
  313. // Function: CPPExpression::named construct_op constructor
  314. // Access: Public, Static
  315. // Description: Creates an expression that represents a constructor
  316. // call.
  317. ////////////////////////////////////////////////////////////////////
  318. CPPExpression CPPExpression::
  319. construct_op(CPPType *type, CPPExpression *op1) {
  320. CPPExpression expr(0);
  321. if (op1 == NULL) {
  322. // A default constructor call--no parameters.
  323. expr._type = T_default_construct;
  324. expr._u._typecast._to = type;
  325. expr._u._typecast._op1 = NULL;
  326. } else {
  327. // A normal constructor call, with parameters.
  328. expr._type = T_construct;
  329. expr._u._typecast._to = type;
  330. expr._u._typecast._op1 = op1;
  331. }
  332. return expr;
  333. }
  334. ////////////////////////////////////////////////////////////////////
  335. // Function: CPPExpression::named new_op constructor
  336. // Access: Public, Static
  337. // Description: Creates an expression that represents a use of the
  338. // new operator.
  339. ////////////////////////////////////////////////////////////////////
  340. CPPExpression CPPExpression::
  341. new_op(CPPType *type, CPPExpression *op1) {
  342. CPPExpression expr(0);
  343. if (op1 == NULL) {
  344. // A default new operation--no parameters.
  345. expr._type = T_default_new;
  346. expr._u._typecast._to = type;
  347. expr._u._typecast._op1 = NULL;
  348. } else {
  349. // A normal new operation, with parameters.
  350. expr._type = T_new;
  351. expr._u._typecast._to = type;
  352. expr._u._typecast._op1 = op1;
  353. }
  354. return expr;
  355. }
  356. ////////////////////////////////////////////////////////////////////
  357. // Function: CPPExpression::named sizeof_func constructor
  358. // Access: Public, Static
  359. // Description:
  360. ////////////////////////////////////////////////////////////////////
  361. CPPExpression CPPExpression::
  362. sizeof_func(CPPType *type) {
  363. CPPExpression expr(0);
  364. expr._type = T_sizeof;
  365. expr._u._typecast._to = type;
  366. expr._u._typecast._op1 = NULL;
  367. return expr;
  368. }
  369. ////////////////////////////////////////////////////////////////////
  370. // Function: CPPExpression::named alignof_func constructor
  371. // Access: Public, Static
  372. // Description:
  373. ////////////////////////////////////////////////////////////////////
  374. CPPExpression CPPExpression::
  375. alignof_func(CPPType *type) {
  376. CPPExpression expr(0);
  377. expr._type = T_alignof;
  378. expr._u._typecast._to = type;
  379. expr._u._typecast._op1 = NULL;
  380. return expr;
  381. }
  382. ////////////////////////////////////////////////////////////////////
  383. // Function: CPPExpression::named literal constructor
  384. // Access: Public, Static
  385. // Description:
  386. ////////////////////////////////////////////////////////////////////
  387. CPPExpression CPPExpression::
  388. literal(unsigned long long value, CPPInstance *lit_op) {
  389. CPPExpression expr(0);
  390. expr._type = T_literal;
  391. expr._u._literal._value = new CPPExpression(value);
  392. expr._u._literal._operator = lit_op;
  393. return expr;
  394. }
  395. ////////////////////////////////////////////////////////////////////
  396. // Function: CPPExpression::named literal constructor
  397. // Access: Public, Static
  398. // Description:
  399. ////////////////////////////////////////////////////////////////////
  400. CPPExpression CPPExpression::
  401. literal(long double value, CPPInstance *lit_op) {
  402. CPPExpression expr(0);
  403. expr._type = T_literal;
  404. expr._u._literal._value = new CPPExpression(value);
  405. expr._u._literal._operator = lit_op;
  406. return expr;
  407. }
  408. ////////////////////////////////////////////////////////////////////
  409. // Function: CPPExpression::named literal constructor
  410. // Access: Public, Static
  411. // Description:
  412. ////////////////////////////////////////////////////////////////////
  413. CPPExpression CPPExpression::
  414. literal(CPPExpression *value, CPPInstance *lit_op) {
  415. CPPExpression expr(0);
  416. expr._type = T_literal;
  417. expr._u._literal._value = value;
  418. expr._u._literal._operator = lit_op;
  419. return expr;
  420. }
  421. ////////////////////////////////////////////////////////////////////
  422. // Function: CPPExpression::named raw_literal constructor
  423. // Access: Public, Static
  424. // Description:
  425. ////////////////////////////////////////////////////////////////////
  426. CPPExpression CPPExpression::
  427. raw_literal(const string &raw, CPPInstance *lit_op) {
  428. CPPExpression expr(0);
  429. expr._type = T_raw_literal;
  430. expr._str = raw;
  431. expr._u._literal._value = (CPPExpression *)NULL;
  432. expr._u._literal._operator = lit_op;
  433. return expr;
  434. }
  435. ////////////////////////////////////////////////////////////////////
  436. // Function: CPPExpression::get_nullptr
  437. // Access: Public, Static
  438. // Description:
  439. ////////////////////////////////////////////////////////////////////
  440. const CPPExpression &CPPExpression::
  441. get_nullptr() {
  442. static CPPExpression expr(0);
  443. expr._type = T_nullptr;
  444. return expr;
  445. }
  446. ////////////////////////////////////////////////////////////////////
  447. // Function: CPPExpression::Destructor
  448. // Access: Public
  449. // Description:
  450. ////////////////////////////////////////////////////////////////////
  451. CPPExpression::
  452. ~CPPExpression() {
  453. }
  454. ////////////////////////////////////////////////////////////////////
  455. // Function: CPPExpression::evaluate
  456. // Access: Public
  457. // Description:
  458. ////////////////////////////////////////////////////////////////////
  459. CPPExpression::Result CPPExpression::
  460. evaluate() const {
  461. Result r1, r2;
  462. switch (_type) {
  463. case T_nullptr:
  464. return Result((void *)0);
  465. case T_integer:
  466. return Result((int)_u._integer);
  467. case T_real:
  468. return Result((double)_u._real);
  469. case T_string:
  470. case T_wstring:
  471. case T_u8string:
  472. case T_u16string:
  473. case T_u32string:
  474. return Result();
  475. case T_variable:
  476. if (_u._variable->_type != NULL &&
  477. _u._variable->_initializer != NULL) {
  478. // A const variable. Fetch its assigned value.
  479. CPPConstType *const_type = _u._variable->_type->as_const_type();
  480. if (const_type != NULL) {
  481. return _u._variable->_initializer->evaluate();
  482. }
  483. }
  484. return Result();
  485. case T_function:
  486. return Result();
  487. case T_unknown_ident:
  488. return Result();
  489. case T_typecast:
  490. assert(_u._typecast._op1 != NULL);
  491. r1 = _u._typecast._op1->evaluate();
  492. if (r1._type != RT_error) {
  493. CPPSimpleType *stype = _u._typecast._to->as_simple_type();
  494. if (stype != NULL) {
  495. if (stype->_type == CPPSimpleType::T_int) {
  496. return Result(r1.as_integer());
  497. } else if (stype->_type == CPPSimpleType::T_float ||
  498. stype->_type == CPPSimpleType::T_double) {
  499. return Result(r1.as_real());
  500. }
  501. }
  502. if (_u._typecast._to->as_pointer_type()) {
  503. return Result(r1.as_pointer());
  504. }
  505. }
  506. return Result();
  507. case T_construct:
  508. case T_default_construct:
  509. case T_new:
  510. case T_default_new:
  511. case T_sizeof:
  512. return Result();
  513. case T_alignof:
  514. if (_u._typecast._to != NULL) {
  515. // Check if the type is defined with an alignas. TODO: this should
  516. // probably be moved to a virtual getter on CPPType.
  517. CPPExtensionType *etype = _u._typecast._to->as_extension_type();
  518. if (etype != NULL && etype->_alignment != NULL) {
  519. return etype->_alignment->evaluate();
  520. }
  521. }
  522. return Result();
  523. case T_binary_operation:
  524. assert(_u._op._op2 != NULL);
  525. r2 = _u._op._op2->evaluate();
  526. // The operators && and || are special cases: these are
  527. // shirt-circuiting operators. Thus, if we are using either of
  528. // these it might be acceptable for the second operand to be
  529. // invalid, since we might never evaluate it.
  530. // In all other cases, both operands must be valid in order for
  531. // the operation to be valid.
  532. if (r2._type == RT_error &&
  533. (_u._op._operator != OROR && _u._op._operator != ANDAND)) {
  534. return r2;
  535. }
  536. // Fall through
  537. case T_trinary_operation:
  538. // The trinary operator is also a short-circuiting operator: we
  539. // don't test the second or third operands until we need them.
  540. // The only critical one is the first operand.
  541. // Fall through
  542. case T_unary_operation:
  543. assert(_u._op._op1 != NULL);
  544. r1 = _u._op._op1->evaluate();
  545. if (r1._type == RT_error) {
  546. // Here's one more special case: if the first operand is
  547. // invalid, it really means we don't know how to evaluate it.
  548. // However, if the operator is ||, then it might not matter as
  549. // long as we can evaluate the second one *and* that comes out
  550. // to be true.
  551. if (_u._op._operator == OROR && r2._type == RT_integer &&
  552. r2.as_boolean()) {
  553. return r2;
  554. }
  555. // Ditto for the operator being && and the second one coming out
  556. // false.
  557. if (_u._op._operator == ANDAND && r2._type == RT_integer &&
  558. !r2.as_boolean()) {
  559. return r2;
  560. }
  561. // Also for the operator being [] and the operand being a string.
  562. if (_u._op._operator == '[' && r2._type == RT_integer &&
  563. (_u._op._op1->_type == T_string ||
  564. _u._op._op1->_type == T_u8string)) {
  565. int index = r2.as_integer();
  566. if (index == _u._op._op1->_str.size()) {
  567. return Result(0);
  568. } else if (index >= 0 && index < _u._op._op1->_str.size()) {
  569. return Result(_u._op._op1->_str[index]);
  570. } else {
  571. cerr << "array index " << index << " out of bounds of string literal "
  572. << *_u._op._op1 << "\n";
  573. }
  574. }
  575. return r1;
  576. }
  577. switch (_u._op._operator) {
  578. case UNARY_NOT:
  579. return Result(!r1.as_boolean());
  580. case UNARY_NEGATE:
  581. return Result(~r1.as_integer());
  582. case UNARY_MINUS:
  583. return (r1._type == RT_real) ? Result(-r1.as_real()) : Result(-r1.as_integer());
  584. case UNARY_STAR:
  585. case UNARY_REF:
  586. return Result();
  587. case '*':
  588. if (r1._type == RT_real || r2._type == RT_real) {
  589. return Result(r1.as_real() * r2.as_real());
  590. } else {
  591. return Result(r1.as_integer() * r2.as_integer());
  592. }
  593. case '/':
  594. if (r1._type == RT_real || r2._type == RT_real) {
  595. return Result(r1.as_real() / r2.as_real());
  596. } else {
  597. return Result(r1.as_integer() / r2.as_integer());
  598. }
  599. case '%':
  600. return Result(r1.as_integer() % r2.as_integer());
  601. case '+':
  602. if (r1._type == RT_real || r2._type == RT_real) {
  603. return Result(r1.as_real() + r2.as_real());
  604. } else {
  605. return Result(r1.as_integer() + r2.as_integer());
  606. }
  607. case '-':
  608. if (r1._type == RT_real || r2._type == RT_real) {
  609. return Result(r1.as_real() - r2.as_real());
  610. } else {
  611. return Result(r1.as_integer() - r2.as_integer());
  612. }
  613. case '|':
  614. return Result(r1.as_integer() | r2.as_integer());
  615. case '&':
  616. return Result(r1.as_integer() & r2.as_integer());
  617. case OROR:
  618. if (r1.as_boolean()) {
  619. return r1;
  620. } else {
  621. return r2;
  622. }
  623. case ANDAND:
  624. if (r1.as_boolean()) {
  625. return r2;
  626. } else {
  627. return r1;
  628. }
  629. case EQCOMPARE:
  630. if (r1._type == RT_real || r2._type == RT_real) {
  631. return Result(r1.as_real() == r2.as_real());
  632. } else {
  633. return Result(r1.as_integer() == r2.as_integer());
  634. }
  635. case NECOMPARE:
  636. if (r1._type == RT_real || r2._type == RT_real) {
  637. return Result(r1.as_real() != r2.as_real());
  638. } else {
  639. return Result(r1.as_integer() != r2.as_integer());
  640. }
  641. case LECOMPARE:
  642. if (r1._type == RT_real || r2._type == RT_real) {
  643. return Result(r1.as_real() <= r2.as_real());
  644. } else {
  645. return Result(r1.as_integer() <= r2.as_integer());
  646. }
  647. case GECOMPARE:
  648. if (r1._type == RT_real || r2._type == RT_real) {
  649. return Result(r1.as_real() >= r2.as_real());
  650. } else {
  651. return Result(r1.as_integer() >= r2.as_integer());
  652. }
  653. case '<':
  654. if (r1._type == RT_real || r2._type == RT_real) {
  655. return Result(r1.as_real() < r2.as_real());
  656. } else {
  657. return Result(r1.as_integer() < r2.as_integer());
  658. }
  659. case '>':
  660. if (r1._type == RT_real || r2._type == RT_real) {
  661. return Result(r1.as_real() > r2.as_real());
  662. } else {
  663. return Result(r1.as_integer() > r2.as_integer());
  664. }
  665. case LSHIFT:
  666. return Result(r1.as_integer() << r2.as_integer());
  667. case RSHIFT:
  668. return Result(r1.as_integer() >> r2.as_integer());
  669. case '?':
  670. return r1.as_integer() ?
  671. _u._op._op2->evaluate() : _u._op._op3->evaluate();
  672. case '.':
  673. case POINTSAT:
  674. return Result();
  675. case '[': // Array element reference
  676. return Result();
  677. case 'f': // Function evaluation
  678. return Result();
  679. case ',':
  680. return r2;
  681. default:
  682. cerr << "**unexpected operator**\n";
  683. abort();
  684. }
  685. case T_literal:
  686. case T_raw_literal:
  687. return Result();
  688. default:
  689. cerr << "**invalid operand**\n";
  690. abort();
  691. }
  692. return Result(); // Compiler kludge; can't get here.
  693. }
  694. ////////////////////////////////////////////////////////////////////
  695. // Function: CPPExpression::determine_type
  696. // Access: Public
  697. // Description: Returns the type of the expression, if it is known,
  698. // or NULL if the type cannot be determined.
  699. ////////////////////////////////////////////////////////////////////
  700. CPPType *CPPExpression::
  701. determine_type() const {
  702. CPPType *t1 = (CPPType *)NULL;
  703. CPPType *t2 = (CPPType *)NULL;
  704. static CPPType *nullptr_type =
  705. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_nullptr));
  706. static CPPType *int_type =
  707. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
  708. static CPPType *unsigned_long_type =
  709. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  710. CPPSimpleType::F_unsigned |
  711. CPPSimpleType::F_long));
  712. static CPPType *bool_type =
  713. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
  714. static CPPType *float_type =
  715. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
  716. static CPPType *char_type =
  717. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
  718. static CPPType *wchar_type =
  719. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_wchar_t));
  720. static CPPType *char16_type =
  721. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char16_t));
  722. static CPPType *char32_type =
  723. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char32_t));
  724. static CPPType *char_str_type = CPPType::new_type(
  725. new CPPPointerType(CPPType::new_type(new CPPConstType(char_type))));
  726. static CPPType *wchar_str_type = CPPType::new_type(
  727. new CPPPointerType(CPPType::new_type(new CPPConstType(wchar_type))));
  728. static CPPType *char16_str_type = CPPType::new_type(
  729. new CPPPointerType(CPPType::new_type(new CPPConstType(char16_type))));
  730. static CPPType *char32_str_type = CPPType::new_type(
  731. new CPPPointerType(CPPType::new_type(new CPPConstType(char32_type))));
  732. switch (_type) {
  733. case T_nullptr:
  734. return nullptr_type;
  735. case T_integer:
  736. return int_type;
  737. case T_real:
  738. return float_type;
  739. case T_string:
  740. return char_str_type;
  741. case T_wstring:
  742. return wchar_str_type;
  743. case T_u8string:
  744. return char_str_type;
  745. case T_u16string:
  746. return char16_str_type;
  747. case T_u32string:
  748. return char32_str_type;
  749. case T_variable:
  750. return _u._variable->_type;
  751. case T_function:
  752. if (_u._fgroup->get_return_type() == (CPPType *)NULL) {
  753. // There are multiple functions by this name that have different
  754. // return types. We could attempt to differentiate them based
  755. // on the parameter list, but that's a lot of work. Let's just
  756. // give up.
  757. return (CPPType *)NULL;
  758. }
  759. return _u._fgroup->_instances.front()->_type;
  760. case T_unknown_ident:
  761. return (CPPType *)NULL;
  762. case T_typecast:
  763. case T_construct:
  764. case T_default_construct:
  765. return _u._typecast._to;
  766. case T_new:
  767. case T_default_new:
  768. return CPPType::new_type(new CPPPointerType(_u._typecast._to));
  769. case T_sizeof:
  770. case T_alignof:
  771. // Note: this should actually be size_t, but that is defined as a
  772. // typedef in parser-inc. We could try to resolve it, but that's
  773. // hacky. Eh, it's probably not worth the effort to get this right.
  774. return unsigned_long_type;
  775. case T_binary_operation:
  776. case T_trinary_operation:
  777. assert(_u._op._op2 != NULL);
  778. t2 = _u._op._op2->determine_type();
  779. // Fall through
  780. case T_unary_operation:
  781. assert(_u._op._op1 != NULL);
  782. t1 = _u._op._op1->determine_type();
  783. switch (_u._op._operator) {
  784. case UNARY_NOT:
  785. return bool_type;
  786. case UNARY_NEGATE:
  787. return int_type;
  788. case UNARY_MINUS:
  789. return t1;
  790. case UNARY_STAR:
  791. case '[': // Array element reference
  792. if (t1 != NULL) {
  793. if (t1->as_pointer_type()) {
  794. return t1->as_pointer_type()->_pointing_at;
  795. }
  796. if (t1->as_array_type()) {
  797. return t1->as_array_type()->_element_type;
  798. }
  799. }
  800. return NULL;
  801. case UNARY_REF:
  802. return t1;
  803. case '*':
  804. case '/':
  805. case '+':
  806. case '-':
  807. if (t1 == NULL) {
  808. return t2;
  809. } else if (t2 == NULL) {
  810. return t1;
  811. } else if (t1->as_pointer_type()) {
  812. if (t2->as_pointer_type()) {
  813. return int_type;
  814. }
  815. return t1;
  816. }
  817. return elevate_type(t1, t2);
  818. case '%':
  819. case '|':
  820. case '&':
  821. case LSHIFT:
  822. case RSHIFT:
  823. return int_type;
  824. case OROR:
  825. case ANDAND:
  826. case EQCOMPARE:
  827. case NECOMPARE:
  828. case LECOMPARE:
  829. case GECOMPARE:
  830. case '<':
  831. case '>':
  832. return bool_type;
  833. case '?':
  834. return t2;
  835. case '.':
  836. case POINTSAT:
  837. return NULL;
  838. case 'f': // Function evaluation
  839. if (t1 != NULL) {
  840. CPPFunctionType *ftype = t1->as_function_type();
  841. if (ftype != (CPPFunctionType *)NULL) {
  842. return ftype->_return_type;
  843. }
  844. }
  845. return NULL;
  846. case ',':
  847. return t2;
  848. default:
  849. cerr << "**unexpected operator**\n";
  850. abort();
  851. }
  852. case T_literal:
  853. case T_raw_literal:
  854. if (_u._literal._operator != NULL) {
  855. CPPType *type = _u._literal._operator->_type;
  856. CPPFunctionType *ftype = type->as_function_type();
  857. if (ftype != (CPPFunctionType *)NULL) {
  858. return ftype->_return_type;
  859. }
  860. }
  861. return NULL;
  862. default:
  863. cerr << "**invalid operand**\n";
  864. abort();
  865. }
  866. return NULL; // Compiler kludge; can't get here.
  867. }
  868. ////////////////////////////////////////////////////////////////////
  869. // Function: CPPExpression::is_fully_specified
  870. // Access: Public, Virtual
  871. // Description: Returns true if this declaration is an actual,
  872. // factual declaration, or false if some part of the
  873. // declaration depends on a template parameter which has
  874. // not yet been instantiated.
  875. ////////////////////////////////////////////////////////////////////
  876. bool CPPExpression::
  877. is_fully_specified() const {
  878. if (!CPPDeclaration::is_fully_specified()) {
  879. return false;
  880. }
  881. switch (_type) {
  882. case T_nullptr:
  883. case T_integer:
  884. case T_real:
  885. case T_string:
  886. case T_wstring:
  887. case T_u8string:
  888. case T_u16string:
  889. case T_u32string:
  890. return false;
  891. case T_variable:
  892. return _u._variable->is_fully_specified();
  893. case T_function:
  894. return _u._fgroup->is_fully_specified();
  895. case T_unknown_ident:
  896. return _u._ident->is_fully_specified();
  897. case T_typecast:
  898. case T_construct:
  899. case T_new:
  900. return (_u._typecast._to->is_fully_specified() &&
  901. _u._typecast._op1->is_fully_specified());
  902. case T_default_construct:
  903. case T_default_new:
  904. case T_sizeof:
  905. case T_alignof:
  906. return _u._typecast._to->is_fully_specified();
  907. case T_trinary_operation:
  908. if (!_u._op._op3->is_fully_specified()) {
  909. return false;
  910. }
  911. // Fall through
  912. case T_binary_operation:
  913. if (!_u._op._op2->is_fully_specified()) {
  914. return false;
  915. }
  916. // Fall through
  917. case T_unary_operation:
  918. return _u._op._op1->is_fully_specified();
  919. case T_literal:
  920. return _u._literal._value->is_fully_specified() &&
  921. _u._literal._operator->is_fully_specified();
  922. case T_raw_literal:
  923. return _u._literal._value->is_fully_specified();
  924. default:
  925. return true;
  926. }
  927. }
  928. ////////////////////////////////////////////////////////////////////
  929. // Function: CPPExpression::substitute_decl
  930. // Access: Public, Virtual
  931. // Description:
  932. ////////////////////////////////////////////////////////////////////
  933. CPPDeclaration *CPPExpression::
  934. substitute_decl(CPPDeclaration::SubstDecl &subst,
  935. CPPScope *current_scope, CPPScope *global_scope) {
  936. CPPDeclaration *top =
  937. CPPDeclaration::substitute_decl(subst, current_scope, global_scope);
  938. if (top != this) {
  939. return top;
  940. }
  941. CPPExpression *rep = new CPPExpression(*this);
  942. bool any_changed = false;
  943. CPPDeclaration *decl;
  944. switch (_type) {
  945. case T_variable:
  946. decl = _u._variable->substitute_decl(subst, current_scope, global_scope);
  947. if (decl != rep->_u._variable) {
  948. if (decl->as_instance()) {
  949. // Replacing the variable reference with another variable reference.
  950. rep->_u._variable = decl->as_instance();
  951. any_changed = true;
  952. } else if (decl->as_expression()) {
  953. // Replacing the variable reference with an expression.
  954. delete rep;
  955. rep = decl->as_expression();
  956. any_changed = true;
  957. }
  958. }
  959. break;
  960. case T_unknown_ident:
  961. rep->_u._ident = _u._ident->substitute_decl(subst, current_scope, global_scope);
  962. any_changed = any_changed || (rep->_u._ident != _u._ident);
  963. // See if we can define it now.
  964. decl = rep->_u._ident->find_symbol(current_scope, global_scope, subst);
  965. if (decl != NULL) {
  966. CPPInstance *inst = decl->as_instance();
  967. if (inst != NULL) {
  968. rep->_type = T_variable;
  969. rep->_u._variable = inst;
  970. any_changed = true;
  971. decl = inst->substitute_decl(subst, current_scope, global_scope);
  972. if (decl != inst) {
  973. if (decl->as_instance()) {
  974. // Replacing the variable reference with another variable reference.
  975. rep->_u._variable = decl->as_instance();
  976. } else if (decl->as_expression()) {
  977. // Replacing the variable reference with an expression.
  978. delete rep;
  979. rep = decl->as_expression();
  980. }
  981. }
  982. break;
  983. }
  984. CPPFunctionGroup *fgroup = decl->as_function_group();
  985. if (fgroup != NULL) {
  986. rep->_type = T_function;
  987. rep->_u._fgroup = fgroup;
  988. any_changed = true;
  989. }
  990. }
  991. break;
  992. case T_typecast:
  993. case T_construct:
  994. case T_new:
  995. rep->_u._typecast._op1 =
  996. _u._typecast._op1->substitute_decl(subst, current_scope, global_scope)
  997. ->as_expression();
  998. any_changed = any_changed || (rep->_u._typecast._op1 != _u._typecast._op1);
  999. // fall through
  1000. case T_default_construct:
  1001. case T_default_new:
  1002. case T_sizeof:
  1003. case T_alignof:
  1004. rep->_u._typecast._to =
  1005. _u._typecast._to->substitute_decl(subst, current_scope, global_scope)
  1006. ->as_type();
  1007. any_changed = any_changed || (rep->_u._typecast._to != _u._typecast._to);
  1008. break;
  1009. case T_trinary_operation:
  1010. rep->_u._op._op3 =
  1011. _u._op._op3->substitute_decl(subst, current_scope, global_scope)
  1012. ->as_expression();
  1013. any_changed = any_changed || (rep->_u._op._op3 != _u._op._op3);
  1014. // fall through
  1015. case T_binary_operation:
  1016. rep->_u._op._op2 =
  1017. _u._op._op2->substitute_decl(subst, current_scope, global_scope)
  1018. ->as_expression();
  1019. any_changed = any_changed || (rep->_u._op._op2 != _u._op._op2);
  1020. // fall through
  1021. case T_unary_operation:
  1022. rep->_u._op._op1 =
  1023. _u._op._op1->substitute_decl(subst, current_scope, global_scope)
  1024. ->as_expression();
  1025. any_changed = any_changed || (rep->_u._op._op1 != _u._op._op1);
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. if (!any_changed) {
  1031. delete rep;
  1032. rep = this;
  1033. }
  1034. subst.insert(SubstDecl::value_type(this, rep));
  1035. return rep;
  1036. }
  1037. ////////////////////////////////////////////////////////////////////
  1038. // Function: CPPExpression::is_tbd
  1039. // Access: Public
  1040. // Description: Returns true if any type within the expression list is
  1041. // a CPPTBDType and thus isn't fully determined right
  1042. // now.
  1043. ////////////////////////////////////////////////////////////////////
  1044. bool CPPExpression::
  1045. is_tbd() const {
  1046. switch (_type) {
  1047. case T_variable:
  1048. if (_u._variable->_type != NULL &&
  1049. _u._variable->_initializer != NULL) {
  1050. CPPConstType *const_type = _u._variable->_type->as_const_type();
  1051. if (const_type != NULL) {
  1052. return false;
  1053. }
  1054. }
  1055. return true;
  1056. case T_unknown_ident:
  1057. return true;
  1058. case T_typecast:
  1059. case T_construct:
  1060. case T_new:
  1061. case T_default_construct:
  1062. case T_default_new:
  1063. case T_sizeof:
  1064. case T_alignof:
  1065. return _u._typecast._to->is_tbd();
  1066. case T_trinary_operation:
  1067. if (_u._op._op3->is_tbd()) {
  1068. return true;
  1069. }
  1070. // fall through
  1071. case T_binary_operation:
  1072. if (_u._op._op2->is_tbd()) {
  1073. return true;
  1074. }
  1075. // fall through
  1076. case T_unary_operation:
  1077. if (_u._op._op1->is_tbd()) {
  1078. return true;
  1079. }
  1080. return false;
  1081. default:
  1082. return false;
  1083. }
  1084. }
  1085. ////////////////////////////////////////////////////////////////////
  1086. // Function: CPPExpression::output
  1087. // Access: Public
  1088. // Description:
  1089. ////////////////////////////////////////////////////////////////////
  1090. void CPPExpression::
  1091. output(ostream &out, int indent_level, CPPScope *scope, bool) const {
  1092. switch (_type) {
  1093. case T_nullptr:
  1094. out << "nullptr";
  1095. break;
  1096. case T_integer:
  1097. out << _u._integer;
  1098. break;
  1099. case T_real:
  1100. {
  1101. // We use our own dtoa implementation here because it guarantees
  1102. // to never format the number as an integer.
  1103. char buffer[32];
  1104. pdtoa(_u._real, buffer);
  1105. out << buffer;
  1106. }
  1107. break;
  1108. case T_string:
  1109. case T_wstring:
  1110. case T_u8string:
  1111. case T_u16string:
  1112. case T_u32string:
  1113. {
  1114. switch (_type) {
  1115. case T_wstring:
  1116. out << 'L';
  1117. break;
  1118. case T_u8string:
  1119. out << "u8";
  1120. break;
  1121. case T_u16string:
  1122. out << "u";
  1123. break;
  1124. case T_u32string:
  1125. out << "U";
  1126. break;
  1127. default:
  1128. break;
  1129. }
  1130. // We don't really care about preserving the encoding for now.
  1131. out << '"';
  1132. string::const_iterator si;
  1133. for (si = _str.begin(); si != _str.end(); ++si) {
  1134. switch (*si) {
  1135. case '\n':
  1136. out << "\\n";
  1137. break;
  1138. case '\t':
  1139. out << "\\t";
  1140. break;
  1141. case '\r':
  1142. out << "\\r";
  1143. break;
  1144. case '\a':
  1145. out << "\\a";
  1146. break;
  1147. case '"':
  1148. out << "\\\"";
  1149. break;
  1150. default:
  1151. if (isprint(*si)) {
  1152. out << *si;
  1153. } else {
  1154. out << '\\' << oct << setw(3) << setfill('0') << (int)(*si)
  1155. << dec << setw(0);
  1156. }
  1157. }
  1158. }
  1159. }
  1160. out << '"';
  1161. break;
  1162. case T_variable:
  1163. // We can just refer to the variable by name, except if it's a
  1164. // private constant, in which case we have to compute the value,
  1165. // since we may have to use it in generated code.
  1166. if (_u._variable->_type != NULL &&
  1167. _u._variable->_initializer != NULL &&
  1168. _u._variable->_vis > V_public) {
  1169. // A const variable. Fetch its assigned value.
  1170. CPPConstType *const_type = _u._variable->_type->as_const_type();
  1171. if (const_type != NULL) {
  1172. _u._variable->_initializer->output(out, indent_level, scope, false);
  1173. break;
  1174. }
  1175. }
  1176. _u._variable->_ident->output(out, scope);
  1177. break;
  1178. case T_function:
  1179. out << _u._fgroup->_name;
  1180. break;
  1181. case T_unknown_ident:
  1182. _u._ident->output(out, scope);
  1183. break;
  1184. case T_typecast:
  1185. out << "(";
  1186. _u._typecast._to->output(out, indent_level, scope, false);
  1187. out << ")(";
  1188. _u._typecast._op1->output(out, indent_level, scope, false);
  1189. out << ")";
  1190. break;
  1191. case T_construct:
  1192. _u._typecast._to->output(out, indent_level, scope, false);
  1193. out << "(";
  1194. _u._typecast._op1->output(out, indent_level, scope, false);
  1195. out << ")";
  1196. break;
  1197. case T_default_construct:
  1198. _u._typecast._to->output(out, indent_level, scope, false);
  1199. out << "()";
  1200. break;
  1201. case T_new:
  1202. out << "(new ";
  1203. _u._typecast._to->output(out, indent_level, scope, false);
  1204. out << "(";
  1205. _u._typecast._op1->output(out, indent_level, scope, false);
  1206. out << "))";
  1207. break;
  1208. case T_default_new:
  1209. out << "(new ";
  1210. _u._typecast._to->output(out, indent_level, scope, false);
  1211. out << "())";
  1212. break;
  1213. case T_sizeof:
  1214. out << "sizeof(";
  1215. _u._typecast._to->output(out, indent_level, scope, false);
  1216. out << ")";
  1217. break;
  1218. case T_alignof:
  1219. out << "alignof(";
  1220. _u._typecast._to->output(out, indent_level, scope, false);
  1221. out << ")";
  1222. break;
  1223. case T_unary_operation:
  1224. switch (_u._op._operator) {
  1225. case UNARY_NOT:
  1226. out << "(! ";
  1227. _u._op._op1->output(out, indent_level, scope, false);
  1228. out << ")";
  1229. break;
  1230. case UNARY_NEGATE:
  1231. out << "(~ ";
  1232. _u._op._op1->output(out, indent_level, scope, false);
  1233. out << ")";
  1234. break;
  1235. case UNARY_MINUS:
  1236. out << '-';
  1237. _u._op._op1->output(out, indent_level, scope, false);
  1238. break;
  1239. case UNARY_STAR:
  1240. out << "(* ";
  1241. _u._op._op1->output(out, indent_level, scope, false);
  1242. out << ")";
  1243. break;
  1244. case UNARY_REF:
  1245. out << "(& ";
  1246. _u._op._op1->output(out, indent_level, scope, false);
  1247. out << ")";
  1248. break;
  1249. case 'f': // Function evaluation, no parameters.
  1250. out << "(";
  1251. _u._op._op1->output(out, indent_level, scope, false);
  1252. out << "())";
  1253. break;
  1254. default:
  1255. out << "(" << (char)_u._op._operator << " ";
  1256. _u._op._op1->output(out, indent_level, scope, false);
  1257. out << ")";
  1258. break;
  1259. }
  1260. break;
  1261. case T_binary_operation:
  1262. switch (_u._op._operator) {
  1263. case OROR:
  1264. out << "(";
  1265. _u._op._op1->output(out, indent_level, scope, false);
  1266. out << " || ";
  1267. _u._op._op2->output(out, indent_level, scope, false);
  1268. out << ")";
  1269. break;
  1270. case ANDAND:
  1271. out << "(";
  1272. _u._op._op1->output(out, indent_level, scope, false);
  1273. out << " && ";
  1274. _u._op._op2->output(out, indent_level, scope, false);
  1275. out << ")";
  1276. break;
  1277. case EQCOMPARE:
  1278. out << "(";
  1279. _u._op._op1->output(out, indent_level, scope, false);
  1280. out << " == ";
  1281. _u._op._op2->output(out, indent_level, scope, false);
  1282. out << ")";
  1283. break;
  1284. case NECOMPARE:
  1285. out << "(";
  1286. _u._op._op1->output(out, indent_level, scope, false);
  1287. out << " != ";
  1288. _u._op._op2->output(out, indent_level, scope, false);
  1289. out << ")";
  1290. break;
  1291. case LECOMPARE:
  1292. out << "(";
  1293. _u._op._op1->output(out, indent_level, scope, false);
  1294. out << " <= ";
  1295. _u._op._op2->output(out, indent_level, scope, false);
  1296. out << ")";
  1297. break;
  1298. case GECOMPARE:
  1299. out << "(";
  1300. _u._op._op1->output(out, indent_level, scope, false);
  1301. out << " >= ";
  1302. _u._op._op2->output(out, indent_level, scope, false);
  1303. out << ")";
  1304. break;
  1305. case LSHIFT:
  1306. out << "(";
  1307. _u._op._op1->output(out, indent_level, scope, false);
  1308. out << " << ";
  1309. _u._op._op2->output(out, indent_level, scope, false);
  1310. out << ")";
  1311. break;
  1312. case RSHIFT:
  1313. out << "(";
  1314. _u._op._op1->output(out, indent_level, scope, false);
  1315. out << " >> ";
  1316. _u._op._op2->output(out, indent_level, scope, false);
  1317. out << ")";
  1318. break;
  1319. case '.':
  1320. out << "(";
  1321. _u._op._op1->output(out, indent_level, scope, false);
  1322. out << ".";
  1323. _u._op._op2->output(out, indent_level, scope, false);
  1324. out << ")";
  1325. break;
  1326. case POINTSAT:
  1327. out << "(";
  1328. _u._op._op1->output(out, indent_level, scope, false);
  1329. out << "->";
  1330. _u._op._op2->output(out, indent_level, scope, false);
  1331. out << ")";
  1332. break;
  1333. case '[': // Array element reference
  1334. out << "(";
  1335. _u._op._op1->output(out, indent_level, scope, false);
  1336. out << "[";
  1337. _u._op._op2->output(out, indent_level, scope, false);
  1338. out << "])";
  1339. break;
  1340. case 'f': // Function evaluation
  1341. out << "(";
  1342. _u._op._op1->output(out, indent_level, scope, false);
  1343. out << "(";
  1344. _u._op._op2->output(out, indent_level, scope, false);
  1345. out << "))";
  1346. break;
  1347. case ',': // Comma, no parens are used
  1348. _u._op._op1->output(out, indent_level, scope, false);
  1349. out << ", ";
  1350. _u._op._op2->output(out, indent_level, scope, false);
  1351. break;
  1352. default:
  1353. out << "(";
  1354. _u._op._op1->output(out, indent_level, scope, false);
  1355. out << " " << (char)_u._op._operator << " ";
  1356. _u._op._op2->output(out, indent_level, scope, false);
  1357. out << ")";
  1358. }
  1359. break;
  1360. case T_trinary_operation:
  1361. out << "(";
  1362. _u._op._op1->output(out, indent_level, scope, false);
  1363. out << " ? ";
  1364. _u._op._op2->output(out, indent_level, scope, false);
  1365. out << " : ";
  1366. _u._op._op3->output(out, indent_level, scope, false);
  1367. out << ")";
  1368. break;
  1369. case T_literal:
  1370. _u._literal._value->output(out, indent_level, scope, false);
  1371. if (_u._literal._operator != NULL) {
  1372. string name = _u._literal._operator->get_simple_name();
  1373. assert(name.substr(0, 12) == "operator \"\" ");
  1374. out << name.substr(12);
  1375. }
  1376. break;
  1377. case T_raw_literal:
  1378. out << _str;
  1379. if (_u._literal._operator != NULL) {
  1380. string name = _u._literal._operator->get_simple_name();
  1381. assert(name.substr(0, 12) == "operator \"\" ");
  1382. out << name.substr(12);
  1383. }
  1384. break;
  1385. default:
  1386. out << "(** invalid operand type " << (int)_type << " **)";
  1387. }
  1388. }
  1389. ////////////////////////////////////////////////////////////////////
  1390. // Function: CPPExpression::get_subtype
  1391. // Access: Public, Virtual
  1392. // Description:
  1393. ////////////////////////////////////////////////////////////////////
  1394. CPPDeclaration::SubType CPPExpression::
  1395. get_subtype() const {
  1396. return ST_expression;
  1397. }
  1398. ////////////////////////////////////////////////////////////////////
  1399. // Function: CPPExpression::as_expression
  1400. // Access: Public, Virtual
  1401. // Description:
  1402. ////////////////////////////////////////////////////////////////////
  1403. CPPExpression *CPPExpression::
  1404. as_expression() {
  1405. return this;
  1406. }
  1407. ////////////////////////////////////////////////////////////////////
  1408. // Function: CPPExpression::elevate_type
  1409. // Access: Public, Static
  1410. // Description: Returns the most general of the two given types.
  1411. ////////////////////////////////////////////////////////////////////
  1412. CPPType *CPPExpression::
  1413. elevate_type(CPPType *t1, CPPType *t2) {
  1414. CPPSimpleType *st1 = t1->as_simple_type();
  1415. CPPSimpleType *st2 = t2->as_simple_type();
  1416. if (st1 == NULL || st2 == NULL) {
  1417. // Nothing we can do about this. Who knows?
  1418. return NULL;
  1419. }
  1420. if (st1->_type == st2->_type) {
  1421. // They have the same type, so return the one with the largest
  1422. // flag bits.
  1423. if (st1->_flags & CPPSimpleType::F_longlong) {
  1424. return st1;
  1425. } else if (st2->_flags & CPPSimpleType::F_longlong) {
  1426. return st2;
  1427. } else if (st1->_flags & CPPSimpleType::F_long) {
  1428. return st1;
  1429. } else if (st2->_flags & CPPSimpleType::F_long) {
  1430. return st2;
  1431. } else if (st1->_flags & CPPSimpleType::F_short) {
  1432. return st2;
  1433. } else if (st2->_flags & CPPSimpleType::F_short) {
  1434. return st1;
  1435. }
  1436. return st1;
  1437. }
  1438. // They have different types.
  1439. if (st1->_type == CPPSimpleType::T_float ||
  1440. st1->_type == CPPSimpleType::T_double) {
  1441. return st1;
  1442. } else if (st2->_type == CPPSimpleType::T_float ||
  1443. st2->_type == CPPSimpleType::T_double) {
  1444. return st2;
  1445. } else if (st1->_type == CPPSimpleType::T_int) {
  1446. return st1;
  1447. } else if (st2->_type == CPPSimpleType::T_int) {
  1448. return st2;
  1449. } else if (st1->_type == CPPSimpleType::T_bool) {
  1450. return st1;
  1451. } else if (st2->_type == CPPSimpleType::T_bool) {
  1452. return st2;
  1453. }
  1454. return st1;
  1455. }
  1456. ////////////////////////////////////////////////////////////////////
  1457. // Function: CPPExpression::is_equal
  1458. // Access: Protected, Virtual
  1459. // Description: Called by CPPDeclaration to determine whether this
  1460. // expr is equivalent to another expr.
  1461. ////////////////////////////////////////////////////////////////////
  1462. bool CPPExpression::
  1463. is_equal(const CPPDeclaration *other) const {
  1464. const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
  1465. assert(ot != NULL);
  1466. if (_type != ot->_type) {
  1467. return false;
  1468. }
  1469. switch (_type) {
  1470. case T_nullptr:
  1471. return true;
  1472. case T_integer:
  1473. return _u._integer == ot->_u._integer;
  1474. case T_real:
  1475. return _u._real == ot->_u._real;
  1476. case T_string:
  1477. case T_wstring:
  1478. case T_u8string:
  1479. case T_u16string:
  1480. case T_u32string:
  1481. return _str == ot->_str;
  1482. case T_variable:
  1483. return _u._variable == ot->_u._variable;
  1484. case T_function:
  1485. return _u._fgroup == ot->_u._fgroup;
  1486. case T_unknown_ident:
  1487. return *_u._ident == *ot->_u._ident;
  1488. case T_typecast:
  1489. case T_construct:
  1490. case T_new:
  1491. return _u._typecast._to == ot->_u._typecast._to &&
  1492. *_u._typecast._op1 == *ot->_u._typecast._op1;
  1493. case T_default_construct:
  1494. case T_default_new:
  1495. case T_sizeof:
  1496. case T_alignof:
  1497. return _u._typecast._to == ot->_u._typecast._to;
  1498. case T_unary_operation:
  1499. return *_u._op._op1 == *ot->_u._op._op1;
  1500. case T_binary_operation:
  1501. return *_u._op._op1 == *ot->_u._op._op1 &&
  1502. *_u._op._op2 == *ot->_u._op._op2;
  1503. case T_trinary_operation:
  1504. return *_u._op._op1 == *ot->_u._op._op1 &&
  1505. *_u._op._op2 == *ot->_u._op._op2;
  1506. case T_literal:
  1507. return *_u._literal._value == *ot->_u._literal._value &&
  1508. _u._literal._operator == ot->_u._literal._operator;
  1509. case T_raw_literal:
  1510. return _str == ot->_str &&
  1511. _u._literal._operator == ot->_u._literal._operator;
  1512. default:
  1513. cerr << "(** invalid operand type " << (int)_type << " **)";
  1514. }
  1515. return true;
  1516. }
  1517. ////////////////////////////////////////////////////////////////////
  1518. // Function: CPPExpression::is_less
  1519. // Access: Protected, Virtual
  1520. // Description: Called by CPPDeclaration to determine whether this
  1521. // expr should be ordered before another expr of the
  1522. // same type, in an arbitrary but fixed ordering.
  1523. ////////////////////////////////////////////////////////////////////
  1524. bool CPPExpression::
  1525. is_less(const CPPDeclaration *other) const {
  1526. const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
  1527. assert(ot != NULL);
  1528. if (_type != ot->_type) {
  1529. return (int)_type < (int)ot->_type;
  1530. }
  1531. switch (_type) {
  1532. case T_nullptr:
  1533. return false;
  1534. case T_integer:
  1535. return _u._integer < ot->_u._integer;
  1536. case T_real:
  1537. return _u._real < ot->_u._real;
  1538. case T_string:
  1539. case T_wstring:
  1540. case T_u8string:
  1541. case T_u16string:
  1542. case T_u32string:
  1543. return _str < ot->_str;
  1544. case T_variable:
  1545. return _u._variable < ot->_u._variable;
  1546. case T_function:
  1547. return *_u._fgroup < *ot->_u._fgroup;
  1548. case T_unknown_ident:
  1549. return *_u._ident < *ot->_u._ident;
  1550. case T_typecast:
  1551. case T_construct:
  1552. case T_new:
  1553. if (_u._typecast._to != ot->_u._typecast._to) {
  1554. return _u._typecast._to < ot->_u._typecast._to;
  1555. }
  1556. return *_u._typecast._op1 < *ot->_u._typecast._op1;
  1557. case T_default_construct:
  1558. case T_default_new:
  1559. case T_sizeof:
  1560. case T_alignof:
  1561. return _u._typecast._to < ot->_u._typecast._to;
  1562. case T_trinary_operation:
  1563. if (*_u._op._op3 != *ot->_u._op._op3) {
  1564. return *_u._op._op3 < *ot->_u._op._op3;
  1565. }
  1566. // Fall through
  1567. case T_binary_operation:
  1568. if (*_u._op._op2 != *ot->_u._op._op2) {
  1569. return *_u._op._op2 < *ot->_u._op._op2;
  1570. }
  1571. // Fall through
  1572. case T_unary_operation:
  1573. return *_u._op._op1 < *ot->_u._op._op1;
  1574. case T_literal:
  1575. if (_u._literal._operator != ot->_u._literal._operator) {
  1576. return _u._literal._operator < ot->_u._literal._operator;
  1577. }
  1578. return *_u._literal._value < *ot->_u._literal._value;
  1579. case T_raw_literal:
  1580. if (_u._literal._operator != ot->_u._literal._operator) {
  1581. return _u._literal._operator < ot->_u._literal._operator;
  1582. }
  1583. return _str < ot->_str;
  1584. default:
  1585. cerr << "(** invalid operand type " << (int)_type << " **)";
  1586. }
  1587. return false;
  1588. }