cppExpression.cxx 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppExpression.cxx
  10. * @author drose
  11. * @date 1999-10-25
  12. */
  13. #include "cppExpression.h"
  14. #include "cppToken.h"
  15. #include "cppIdentifier.h"
  16. #include "cppType.h"
  17. #include "cppSimpleType.h"
  18. #include "cppPointerType.h"
  19. #include "cppEnumType.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 "cppClosureType.h"
  27. #include "cppReferenceType.h"
  28. #include "cppStructType.h"
  29. #include "cppBison.h"
  30. #include "pdtoa.h"
  31. #include <assert.h>
  32. using std::cerr;
  33. using std::string;
  34. /**
  35. *
  36. */
  37. CPPExpression::Result::
  38. Result() {
  39. _type = RT_error;
  40. }
  41. /**
  42. *
  43. */
  44. CPPExpression::Result::
  45. Result(int value) {
  46. _type = RT_integer;
  47. _u._integer = value;
  48. }
  49. /**
  50. *
  51. */
  52. CPPExpression::Result::
  53. Result(double value) {
  54. _type = RT_real;
  55. _u._real = value;
  56. }
  57. /**
  58. *
  59. */
  60. CPPExpression::Result::
  61. Result(void *value) {
  62. _type = RT_pointer;
  63. _u._pointer = value;
  64. }
  65. /**
  66. *
  67. */
  68. int CPPExpression::Result::
  69. as_integer() const {
  70. switch (_type) {
  71. case RT_integer:
  72. return _u._integer;
  73. case RT_real:
  74. return (int)_u._real;
  75. case RT_pointer:
  76. // We don't mind if this loses precision.
  77. return (int)(intptr_t)(_u._pointer);
  78. default:
  79. cerr << "Invalid type\n";
  80. assert(false);
  81. return 0;
  82. }
  83. }
  84. /**
  85. *
  86. */
  87. double CPPExpression::Result::
  88. as_real() const {
  89. switch (_type) {
  90. case RT_integer:
  91. return (double)_u._integer;
  92. case RT_real:
  93. return _u._real;
  94. case RT_pointer:
  95. // We don't mind if this loses precision.
  96. return (double)(uintptr_t)(_u._pointer);
  97. default:
  98. cerr << "Invalid type\n";
  99. assert(false);
  100. return 0.0;
  101. }
  102. }
  103. /**
  104. *
  105. */
  106. void *CPPExpression::Result::
  107. as_pointer() const {
  108. switch (_type) {
  109. case RT_integer:
  110. return (void *)(intptr_t)_u._integer;
  111. case RT_real:
  112. return (void *)(uintptr_t)_u._real;
  113. case RT_pointer:
  114. return _u._pointer;
  115. default:
  116. cerr << "Invalid type\n";
  117. assert(false);
  118. return nullptr;
  119. }
  120. }
  121. /**
  122. *
  123. */
  124. bool CPPExpression::Result::
  125. as_boolean() const {
  126. switch (_type) {
  127. case RT_integer:
  128. return (_u._integer != 0);
  129. case RT_real:
  130. return (_u._real != 0.0);
  131. case RT_pointer:
  132. return (_u._pointer != nullptr);
  133. default:
  134. cerr << "Invalid type\n";
  135. assert(false);
  136. return false;
  137. }
  138. }
  139. /**
  140. *
  141. */
  142. void CPPExpression::Result::
  143. output(std::ostream &out) const {
  144. switch (_type) {
  145. case RT_integer:
  146. out << _u._integer;
  147. break;
  148. case RT_real:
  149. out << _u._real;
  150. break;
  151. case RT_pointer:
  152. out << _u._pointer;
  153. break;
  154. case RT_error:
  155. out << "(error)";
  156. break;
  157. default:
  158. out << "(**invalid type**)\n";
  159. }
  160. }
  161. /**
  162. *
  163. */
  164. CPPExpression::
  165. CPPExpression(bool value) :
  166. CPPDeclaration(CPPFile())
  167. {
  168. _type = T_boolean;
  169. _u._boolean = value;
  170. }
  171. /**
  172. *
  173. */
  174. CPPExpression::
  175. CPPExpression(unsigned long long value) :
  176. CPPDeclaration(CPPFile())
  177. {
  178. _type = T_integer;
  179. _u._integer = value;
  180. }
  181. /**
  182. *
  183. */
  184. CPPExpression::
  185. CPPExpression(int value) :
  186. CPPDeclaration(CPPFile())
  187. {
  188. _type = T_integer;
  189. _u._integer = value;
  190. }
  191. /**
  192. *
  193. */
  194. CPPExpression::
  195. CPPExpression(long double value) :
  196. CPPDeclaration(CPPFile())
  197. {
  198. _type = T_real;
  199. _u._real = value;
  200. }
  201. /**
  202. *
  203. */
  204. CPPExpression::
  205. CPPExpression(const string &value) :
  206. CPPDeclaration(CPPFile())
  207. {
  208. _type = T_string;
  209. _str = value;
  210. }
  211. /**
  212. *
  213. */
  214. CPPExpression::
  215. CPPExpression(CPPIdentifier *ident, CPPScope *current_scope,
  216. CPPScope *global_scope, CPPPreprocessor *error_sink) :
  217. CPPDeclaration(CPPFile())
  218. {
  219. CPPDeclaration *decl =
  220. ident->find_symbol(current_scope, global_scope);
  221. if (decl != nullptr) {
  222. CPPInstance *inst = decl->as_instance();
  223. if (inst != nullptr) {
  224. _type = T_variable;
  225. _u._variable = inst;
  226. return;
  227. }
  228. /*CPPFunctionGroup *fgroup = decl->as_function_group();
  229. if (fgroup != nullptr) {
  230. _type = T_function;
  231. _u._fgroup = fgroup;
  232. return;
  233. }*/
  234. }
  235. _type = T_unknown_ident;
  236. _u._ident = ident;
  237. // _u._ident->_native_scope = current_scope;
  238. }
  239. /**
  240. *
  241. */
  242. CPPExpression::
  243. CPPExpression(int unary_operator, CPPExpression *op1) :
  244. CPPDeclaration(CPPFile())
  245. {
  246. _type = T_unary_operation;
  247. _u._op._operator = unary_operator;
  248. _u._op._op1 = op1;
  249. _u._op._op2 = nullptr;
  250. _u._op._op3 = nullptr;
  251. }
  252. /**
  253. *
  254. */
  255. CPPExpression::
  256. CPPExpression(int binary_operator, CPPExpression *op1, CPPExpression *op2) :
  257. CPPDeclaration(CPPFile())
  258. {
  259. _type = T_binary_operation;
  260. _u._op._operator = binary_operator;
  261. _u._op._op1 = op1;
  262. _u._op._op2 = op2;
  263. _u._op._op3 = nullptr;
  264. }
  265. /**
  266. *
  267. */
  268. CPPExpression::
  269. CPPExpression(int trinary_operator, CPPExpression *op1, CPPExpression *op2,
  270. CPPExpression *op3) :
  271. CPPDeclaration(CPPFile())
  272. {
  273. _type = T_trinary_operation;
  274. _u._op._operator = trinary_operator;
  275. _u._op._op1 = op1;
  276. _u._op._op2 = op2;
  277. _u._op._op3 = op3;
  278. }
  279. /**
  280. * Creates an expression that represents a typecast operation.
  281. */
  282. CPPExpression CPPExpression::
  283. typecast_op(CPPType *type, CPPExpression *op1, Type cast_type) {
  284. assert(cast_type >= T_typecast && cast_type <= T_reinterpret_cast);
  285. CPPExpression expr(0);
  286. expr._type = cast_type;
  287. expr._u._typecast._to = type;
  288. expr._u._typecast._op1 = op1;
  289. return expr;
  290. }
  291. /**
  292. * Creates an expression that represents a constructor call.
  293. */
  294. CPPExpression CPPExpression::
  295. construct_op(CPPType *type, CPPExpression *op1) {
  296. CPPExpression expr(0);
  297. if (op1 == nullptr) {
  298. // A default constructor call--no parameters.
  299. expr._type = T_default_construct;
  300. expr._u._typecast._to = type;
  301. expr._u._typecast._op1 = nullptr;
  302. } else {
  303. // A normal constructor call, with parameters.
  304. expr._type = T_construct;
  305. expr._u._typecast._to = type;
  306. expr._u._typecast._op1 = op1;
  307. }
  308. return expr;
  309. }
  310. /**
  311. * Creates an expression that represents an aggregate initialization.
  312. */
  313. CPPExpression CPPExpression::
  314. aggregate_init_op(CPPType *type, CPPExpression *op1) {
  315. CPPExpression expr(0);
  316. if (op1 == nullptr) {
  317. expr._type = T_empty_aggregate_init;
  318. } else {
  319. expr._type = T_aggregate_init;
  320. }
  321. expr._u._typecast._to = type;
  322. expr._u._typecast._op1 = op1;
  323. return expr;
  324. }
  325. /**
  326. * Creates an expression that represents a use of the new operator.
  327. */
  328. CPPExpression CPPExpression::
  329. new_op(CPPType *type, CPPExpression *op1) {
  330. CPPExpression expr(0);
  331. if (op1 == nullptr) {
  332. // A default new operation--no parameters.
  333. expr._type = T_default_new;
  334. expr._u._typecast._to = type;
  335. expr._u._typecast._op1 = nullptr;
  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. * Creates an expression that represents a use of the typeid operator.
  346. */
  347. CPPExpression CPPExpression::
  348. typeid_op(CPPType *type, CPPType *std_type_info) {
  349. CPPExpression expr(0);
  350. expr._type = T_typeid_type;
  351. expr._u._typeid._type = type;
  352. expr._u._typeid._std_type_info = std_type_info;
  353. return expr;
  354. }
  355. /**
  356. * Creates an expression that represents a use of the typeid operator.
  357. */
  358. CPPExpression CPPExpression::
  359. typeid_op(CPPExpression *op1, CPPType *std_type_info) {
  360. CPPExpression expr(0);
  361. expr._type = T_typeid_expr;
  362. expr._u._typeid._expr = op1;
  363. expr._u._typeid._std_type_info = std_type_info;
  364. return expr;
  365. }
  366. /**
  367. * Creates an expression that returns a particular type trait.
  368. */
  369. CPPExpression CPPExpression::
  370. type_trait(int trait, CPPType *type, CPPType *arg) {
  371. CPPExpression expr(0);
  372. expr._type = T_type_trait;
  373. expr._u._type_trait._trait = trait;
  374. expr._u._type_trait._type = type;
  375. expr._u._type_trait._arg = arg;
  376. return expr;
  377. }
  378. /**
  379. *
  380. */
  381. CPPExpression CPPExpression::
  382. sizeof_func(CPPType *type) {
  383. CPPExpression expr(0);
  384. expr._type = T_sizeof_type;
  385. expr._u._typecast._to = type;
  386. expr._u._typecast._op1 = nullptr;
  387. return expr;
  388. }
  389. /**
  390. *
  391. */
  392. CPPExpression CPPExpression::
  393. sizeof_func(CPPExpression *op1) {
  394. CPPExpression expr(0);
  395. expr._type = T_sizeof_expr;
  396. expr._u._typecast._to = nullptr;
  397. expr._u._typecast._op1 = op1;
  398. return expr;
  399. }
  400. /**
  401. *
  402. */
  403. CPPExpression CPPExpression::
  404. sizeof_ellipsis_func(CPPIdentifier *ident) {
  405. CPPExpression expr(0);
  406. expr._type = T_sizeof_ellipsis;
  407. expr._u._ident = ident;
  408. return expr;
  409. }
  410. /**
  411. *
  412. */
  413. CPPExpression CPPExpression::
  414. alignof_func(CPPType *type) {
  415. CPPExpression expr(0);
  416. expr._type = T_alignof;
  417. expr._u._typecast._to = type;
  418. expr._u._typecast._op1 = nullptr;
  419. return expr;
  420. }
  421. /**
  422. *
  423. */
  424. CPPExpression CPPExpression::
  425. lambda(CPPClosureType *type) {
  426. CPPExpression expr(0);
  427. expr._type = T_lambda;
  428. expr._u._closure_type = type;
  429. return expr;
  430. }
  431. /**
  432. *
  433. */
  434. CPPExpression CPPExpression::
  435. literal(unsigned long long value, CPPInstance *lit_op) {
  436. CPPExpression expr(0);
  437. expr._type = T_literal;
  438. expr._u._literal._value = new CPPExpression(value);
  439. expr._u._literal._operator = lit_op;
  440. return expr;
  441. }
  442. /**
  443. *
  444. */
  445. CPPExpression CPPExpression::
  446. literal(long double value, CPPInstance *lit_op) {
  447. CPPExpression expr(0);
  448. expr._type = T_literal;
  449. expr._u._literal._value = new CPPExpression(value);
  450. expr._u._literal._operator = lit_op;
  451. return expr;
  452. }
  453. /**
  454. *
  455. */
  456. CPPExpression CPPExpression::
  457. literal(CPPExpression *value, CPPInstance *lit_op) {
  458. CPPExpression expr(0);
  459. expr._type = T_literal;
  460. expr._u._literal._value = value;
  461. expr._u._literal._operator = lit_op;
  462. return expr;
  463. }
  464. /**
  465. *
  466. */
  467. CPPExpression CPPExpression::
  468. raw_literal(const string &raw, CPPInstance *lit_op) {
  469. CPPExpression expr(0);
  470. expr._type = T_raw_literal;
  471. expr._str = raw;
  472. expr._u._literal._value = nullptr;
  473. expr._u._literal._operator = lit_op;
  474. return expr;
  475. }
  476. /**
  477. *
  478. */
  479. const CPPExpression &CPPExpression::
  480. get_nullptr() {
  481. static CPPExpression expr(0);
  482. expr._type = T_nullptr;
  483. return expr;
  484. }
  485. /**
  486. *
  487. */
  488. const CPPExpression &CPPExpression::
  489. get_default() {
  490. static CPPExpression expr(0);
  491. expr._type = T_default;
  492. return expr;
  493. }
  494. /**
  495. *
  496. */
  497. const CPPExpression &CPPExpression::
  498. get_delete() {
  499. static CPPExpression expr(0);
  500. expr._type = T_delete;
  501. return expr;
  502. }
  503. /**
  504. *
  505. */
  506. CPPExpression::Result CPPExpression::
  507. evaluate() const {
  508. Result r1, r2;
  509. switch (_type) {
  510. case T_nullptr:
  511. return Result(nullptr);
  512. case T_boolean:
  513. return Result((int)_u._boolean);
  514. case T_integer:
  515. return Result((int)_u._integer);
  516. case T_real:
  517. return Result((double)_u._real);
  518. case T_string:
  519. case T_wstring:
  520. case T_u8string:
  521. case T_u16string:
  522. case T_u32string:
  523. return Result();
  524. case T_variable:
  525. if (_u._variable->_type != nullptr &&
  526. _u._variable->_initializer != nullptr) {
  527. // A constexpr variable, which is treated as const.
  528. if (_u._variable->_storage_class & CPPInstance::SC_constexpr) {
  529. return _u._variable->_initializer->evaluate();
  530. }
  531. // A const variable. Fetch its assigned value.
  532. CPPConstType *const_type = _u._variable->_type->as_const_type();
  533. if (const_type != nullptr) {
  534. return _u._variable->_initializer->evaluate();
  535. }
  536. }
  537. return Result();
  538. case T_function:
  539. return Result();
  540. case T_unknown_ident:
  541. return Result();
  542. case T_typecast:
  543. case T_static_cast:
  544. case T_dynamic_cast:
  545. case T_const_cast:
  546. case T_reinterpret_cast:
  547. assert(_u._typecast._op1 != nullptr);
  548. r1 = _u._typecast._op1->evaluate();
  549. if (r1._type != RT_error) {
  550. CPPSimpleType *stype = _u._typecast._to->as_simple_type();
  551. if (stype != nullptr) {
  552. if (stype->_type == CPPSimpleType::T_bool) {
  553. return Result(r1.as_boolean());
  554. } else if (stype->_type == CPPSimpleType::T_int) {
  555. return Result(r1.as_integer());
  556. } else if (stype->_type == CPPSimpleType::T_float ||
  557. stype->_type == CPPSimpleType::T_double) {
  558. return Result(r1.as_real());
  559. }
  560. }
  561. if (_u._typecast._to->as_pointer_type()) {
  562. return Result(r1.as_pointer());
  563. }
  564. }
  565. return Result();
  566. case T_construct:
  567. case T_default_construct:
  568. case T_aggregate_init:
  569. case T_empty_aggregate_init:
  570. case T_new:
  571. case T_default_new:
  572. case T_sizeof_type:
  573. case T_sizeof_expr:
  574. case T_sizeof_ellipsis:
  575. return Result();
  576. case T_alignof:
  577. if (_u._typecast._to != nullptr) {
  578. // Check if the type is defined with an alignas. TODO: this should
  579. // probably be moved to a virtual getter on CPPType.
  580. CPPExtensionType *etype = _u._typecast._to->as_extension_type();
  581. if (etype != nullptr && etype->_alignment != nullptr) {
  582. return etype->_alignment->evaluate();
  583. }
  584. }
  585. return Result();
  586. case T_binary_operation:
  587. assert(_u._op._op2 != nullptr);
  588. r2 = _u._op._op2->evaluate();
  589. // The operators && and || are special cases: these are shirt-circuiting
  590. // operators. Thus, if we are using either of these it might be
  591. // acceptable for the second operand to be invalid, since we might never
  592. // evaluate it.
  593. // In all other cases, both operands must be valid in order for the
  594. // operation to be valid.
  595. if (r2._type == RT_error &&
  596. (_u._op._operator != OROR && _u._op._operator != ANDAND)) {
  597. return r2;
  598. }
  599. // Fall through
  600. case T_trinary_operation:
  601. // The trinary operator is also a short-circuiting operator: we don't test
  602. // the second or third operands until we need them. The only critical one
  603. // is the first operand.
  604. // Fall through
  605. case T_unary_operation:
  606. assert(_u._op._op1 != nullptr);
  607. r1 = _u._op._op1->evaluate();
  608. if (r1._type == RT_error) {
  609. // Here's one more special case: if the first operand is invalid, it
  610. // really means we don't know how to evaluate it. However, if the
  611. // operator is ||, then it might not matter as long as we can evaluate
  612. // the second one *and* that comes out to be true.
  613. if (_u._op._operator == OROR && r2._type == RT_integer &&
  614. r2.as_boolean()) {
  615. return r2;
  616. }
  617. // Ditto for the operator being && and the second one coming out false.
  618. if (_u._op._operator == ANDAND && r2._type == RT_integer &&
  619. !r2.as_boolean()) {
  620. return r2;
  621. }
  622. // Also for the operator being [] and the operand being a string.
  623. if (_u._op._operator == '[' && r2._type == RT_integer &&
  624. (_u._op._op1->_type == T_string ||
  625. _u._op._op1->_type == T_u8string)) {
  626. int index = (int)r2.as_integer();
  627. if ((size_t)index == _u._op._op1->_str.size()) {
  628. return Result(0);
  629. } else if (index >= 0 && (size_t)index < _u._op._op1->_str.size()) {
  630. return Result(_u._op._op1->_str[(size_t)index]);
  631. } else {
  632. cerr << "array index " << index << " out of bounds of string literal "
  633. << *_u._op._op1 << "\n";
  634. }
  635. }
  636. return r1;
  637. }
  638. switch (_u._op._operator) {
  639. case UNARY_NOT:
  640. return Result(!r1.as_boolean());
  641. case UNARY_NEGATE:
  642. return Result(~r1.as_integer());
  643. case UNARY_MINUS:
  644. return (r1._type == RT_real) ? Result(-r1.as_real()) : Result(-r1.as_integer());
  645. case UNARY_PLUS:
  646. return r1;
  647. case UNARY_STAR:
  648. case UNARY_REF:
  649. return Result();
  650. case '*':
  651. if (r1._type == RT_real || r2._type == RT_real) {
  652. return Result(r1.as_real() * r2.as_real());
  653. } else {
  654. return Result(r1.as_integer() * r2.as_integer());
  655. }
  656. case '/':
  657. if (r1._type == RT_real || r2._type == RT_real) {
  658. return Result(r1.as_real() / r2.as_real());
  659. } else {
  660. return Result(r1.as_integer() / r2.as_integer());
  661. }
  662. case '%':
  663. return Result(r1.as_integer() % r2.as_integer());
  664. case '+':
  665. if (r1._type == RT_real || r2._type == RT_real) {
  666. return Result(r1.as_real() + r2.as_real());
  667. } else {
  668. return Result(r1.as_integer() + r2.as_integer());
  669. }
  670. case '-':
  671. if (r1._type == RT_real || r2._type == RT_real) {
  672. return Result(r1.as_real() - r2.as_real());
  673. } else {
  674. return Result(r1.as_integer() - r2.as_integer());
  675. }
  676. case '|':
  677. return Result(r1.as_integer() | r2.as_integer());
  678. case '&':
  679. return Result(r1.as_integer() & r2.as_integer());
  680. case OROR:
  681. if (r1.as_boolean()) {
  682. return r1;
  683. } else {
  684. return r2;
  685. }
  686. case ANDAND:
  687. if (r1.as_boolean()) {
  688. return r2;
  689. } else {
  690. return r1;
  691. }
  692. case EQCOMPARE:
  693. if (r1._type == RT_real || r2._type == RT_real) {
  694. return Result(r1.as_real() == r2.as_real());
  695. } else {
  696. return Result(r1.as_integer() == r2.as_integer());
  697. }
  698. case NECOMPARE:
  699. if (r1._type == RT_real || r2._type == RT_real) {
  700. return Result(r1.as_real() != r2.as_real());
  701. } else {
  702. return Result(r1.as_integer() != r2.as_integer());
  703. }
  704. case LECOMPARE:
  705. if (r1._type == RT_real || r2._type == RT_real) {
  706. return Result(r1.as_real() <= r2.as_real());
  707. } else {
  708. return Result(r1.as_integer() <= r2.as_integer());
  709. }
  710. case GECOMPARE:
  711. if (r1._type == RT_real || r2._type == RT_real) {
  712. return Result(r1.as_real() >= r2.as_real());
  713. } else {
  714. return Result(r1.as_integer() >= r2.as_integer());
  715. }
  716. case '<':
  717. if (r1._type == RT_real || r2._type == RT_real) {
  718. return Result(r1.as_real() < r2.as_real());
  719. } else {
  720. return Result(r1.as_integer() < r2.as_integer());
  721. }
  722. case '>':
  723. if (r1._type == RT_real || r2._type == RT_real) {
  724. return Result(r1.as_real() > r2.as_real());
  725. } else {
  726. return Result(r1.as_integer() > r2.as_integer());
  727. }
  728. case LSHIFT:
  729. return Result(r1.as_integer() << r2.as_integer());
  730. case RSHIFT:
  731. return Result(r1.as_integer() >> r2.as_integer());
  732. case '?':
  733. return r1.as_integer() ?
  734. _u._op._op2->evaluate() : _u._op._op3->evaluate();
  735. case '.':
  736. case POINTSAT:
  737. return Result();
  738. case '[': // Array element reference
  739. return Result();
  740. case 'f': // Function evaluation
  741. return Result();
  742. case ',':
  743. return r2;
  744. default:
  745. cerr << "**unexpected operator**\n";
  746. abort();
  747. }
  748. case T_literal:
  749. case T_raw_literal:
  750. return Result();
  751. case T_typeid_type:
  752. case T_typeid_expr:
  753. return Result();
  754. case T_type_trait:
  755. switch (_u._type_trait._trait) {
  756. case KW_HAS_VIRTUAL_DESTRUCTOR:
  757. {
  758. CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
  759. return Result(struct_type != nullptr && struct_type->has_virtual_destructor());
  760. }
  761. case KW_IS_ABSTRACT:
  762. {
  763. CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
  764. return Result(struct_type != nullptr && struct_type->is_abstract());
  765. }
  766. case KW_IS_BASE_OF:
  767. {
  768. CPPStructType *struct_type1 = _u._type_trait._type->as_struct_type();
  769. CPPStructType *struct_type2 = _u._type_trait._arg->as_struct_type();
  770. return Result(struct_type1 != nullptr && struct_type2 != nullptr && struct_type1->is_base_of(struct_type2));
  771. }
  772. case KW_IS_CLASS:
  773. {
  774. CPPExtensionType *ext_type = _u._type_trait._type->as_extension_type();
  775. return Result(ext_type != nullptr && (
  776. ext_type->_type == CPPExtensionType::T_class ||
  777. ext_type->_type == CPPExtensionType::T_struct));
  778. }
  779. case KW_IS_CONSTRUCTIBLE:
  780. if (_u._type_trait._arg == nullptr) {
  781. return Result(_u._type_trait._type->is_default_constructible());
  782. } else {
  783. return Result(_u._type_trait._type->is_constructible(_u._type_trait._arg));
  784. }
  785. case KW_IS_CONVERTIBLE_TO:
  786. assert(_u._type_trait._arg != nullptr);
  787. return Result(_u._type_trait._type->is_convertible_to(_u._type_trait._arg));
  788. case KW_IS_DESTRUCTIBLE:
  789. return Result(_u._type_trait._type->is_destructible());
  790. case KW_IS_EMPTY:
  791. {
  792. CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
  793. return Result(struct_type != nullptr && struct_type->is_empty());
  794. }
  795. case KW_IS_ENUM:
  796. return Result(_u._type_trait._type->is_enum());
  797. case KW_IS_FINAL:
  798. {
  799. CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
  800. return Result(struct_type != nullptr && struct_type->is_final());
  801. }
  802. case KW_IS_FUNDAMENTAL:
  803. return Result(_u._type_trait._type->is_fundamental());
  804. case KW_IS_POD:
  805. return Result(_u._type_trait._type->is_trivial() &&
  806. _u._type_trait._type->is_standard_layout());
  807. case KW_IS_POLYMORPHIC:
  808. {
  809. CPPStructType *struct_type = _u._type_trait._type->as_struct_type();
  810. return Result(struct_type != nullptr && struct_type->is_polymorphic());
  811. }
  812. case KW_IS_STANDARD_LAYOUT:
  813. return Result(_u._type_trait._type->is_standard_layout());
  814. case KW_IS_TRIVIAL:
  815. return Result(_u._type_trait._type->is_trivial());
  816. case KW_IS_UNION:
  817. {
  818. CPPExtensionType *ext_type = _u._type_trait._type->as_extension_type();
  819. return Result(ext_type != nullptr &&
  820. ext_type->_type == CPPExtensionType::T_union);
  821. }
  822. default:
  823. cerr << "**unexpected type trait**\n";
  824. abort();
  825. }
  826. default:
  827. cerr << "**invalid operand**\n";
  828. abort();
  829. }
  830. return Result(); // Compiler kludge; can't get here.
  831. }
  832. /**
  833. * Returns the type of the expression, if it is known, or NULL if the type
  834. * cannot be determined.
  835. */
  836. CPPType *CPPExpression::
  837. determine_type() const {
  838. CPPType *t1 = nullptr;
  839. CPPType *t2 = nullptr;
  840. static CPPType *nullptr_type =
  841. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_nullptr));
  842. static CPPType *int_type =
  843. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
  844. static CPPType *unsigned_long_type =
  845. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  846. CPPSimpleType::F_unsigned |
  847. CPPSimpleType::F_long));
  848. static CPPType *bool_type =
  849. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
  850. static CPPType *float_type =
  851. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
  852. static CPPType *char_type =
  853. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
  854. static CPPType *wchar_type =
  855. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_wchar_t));
  856. static CPPType *char16_type =
  857. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char16_t));
  858. static CPPType *char32_type =
  859. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char32_t));
  860. static CPPType *char_str_type = CPPType::new_type(
  861. new CPPPointerType(CPPType::new_type(new CPPConstType(char_type))));
  862. static CPPType *wchar_str_type = CPPType::new_type(
  863. new CPPPointerType(CPPType::new_type(new CPPConstType(wchar_type))));
  864. static CPPType *char16_str_type = CPPType::new_type(
  865. new CPPPointerType(CPPType::new_type(new CPPConstType(char16_type))));
  866. static CPPType *char32_str_type = CPPType::new_type(
  867. new CPPPointerType(CPPType::new_type(new CPPConstType(char32_type))));
  868. switch (_type) {
  869. case T_nullptr:
  870. return nullptr_type;
  871. case T_boolean:
  872. return bool_type;
  873. case T_integer:
  874. return int_type;
  875. case T_real:
  876. return float_type;
  877. case T_string:
  878. return char_str_type;
  879. case T_wstring:
  880. return wchar_str_type;
  881. case T_u8string:
  882. return char_str_type;
  883. case T_u16string:
  884. return char16_str_type;
  885. case T_u32string:
  886. return char32_str_type;
  887. case T_variable:
  888. return _u._variable->_type;
  889. case T_function:
  890. if (_u._fgroup->get_return_type() == nullptr) {
  891. // There are multiple functions by this name that have different return
  892. // types. We could attempt to differentiate them based on the parameter
  893. // list, but that's a lot of work. Let's just give up.
  894. return nullptr;
  895. }
  896. return _u._fgroup->_instances.front()->_type;
  897. case T_unknown_ident:
  898. return nullptr;
  899. case T_typecast:
  900. case T_static_cast:
  901. case T_dynamic_cast:
  902. case T_const_cast:
  903. case T_reinterpret_cast:
  904. case T_construct:
  905. case T_default_construct:
  906. case T_aggregate_init:
  907. case T_empty_aggregate_init:
  908. return _u._typecast._to;
  909. case T_new:
  910. case T_default_new:
  911. return CPPType::new_type(new CPPPointerType(_u._typecast._to));
  912. case T_sizeof_type:
  913. case T_sizeof_expr:
  914. case T_sizeof_ellipsis:
  915. case T_alignof:
  916. // Note: this should actually be size_t, but that is defined as a typedef
  917. // in parser-inc. We could try to resolve it, but that's hacky. Eh, it's
  918. // probably not worth the effort to get this right.
  919. return unsigned_long_type;
  920. case T_binary_operation:
  921. case T_trinary_operation:
  922. assert(_u._op._op2 != nullptr);
  923. t2 = _u._op._op2->determine_type();
  924. // Fall through
  925. case T_unary_operation:
  926. assert(_u._op._op1 != nullptr);
  927. t1 = _u._op._op1->determine_type();
  928. switch (_u._op._operator) {
  929. case UNARY_NOT:
  930. return bool_type;
  931. case UNARY_NEGATE:
  932. return int_type;
  933. case UNARY_MINUS:
  934. case UNARY_PLUS:
  935. if (t1 != nullptr) {
  936. switch (t1->get_subtype()) {
  937. case CPPDeclaration::ST_array:
  938. // Decay into pointer.
  939. return CPPType::new_type(new CPPPointerType(t1->as_array_type()->_element_type));
  940. case CPPDeclaration::ST_enum:
  941. // Convert into integral type.
  942. return t1->as_enum_type()->get_underlying_type();
  943. case CPPDeclaration::ST_simple:
  944. {
  945. CPPSimpleType *simple_type = t1->as_simple_type();
  946. if ((simple_type->_flags & CPPSimpleType::F_short) != 0 ||
  947. simple_type->_type == CPPSimpleType::T_bool ||
  948. simple_type->_type == CPPSimpleType::T_wchar_t ||
  949. simple_type->_type == CPPSimpleType::T_char16_t) {
  950. // Integer promotion.
  951. return int_type;
  952. }
  953. }
  954. // Fall through.
  955. default:
  956. return t1;
  957. }
  958. }
  959. return nullptr;
  960. case UNARY_STAR:
  961. case '[': // Array element reference
  962. if (t1 != nullptr) {
  963. if (t1->as_pointer_type()) {
  964. return t1->as_pointer_type()->_pointing_at;
  965. }
  966. if (t1->as_array_type()) {
  967. return t1->as_array_type()->_element_type;
  968. }
  969. }
  970. return nullptr;
  971. case UNARY_REF:
  972. return t1;
  973. case '*':
  974. case '/':
  975. case '+':
  976. case '-':
  977. if (t1 == nullptr) {
  978. return t2;
  979. } else if (t2 == nullptr) {
  980. return t1;
  981. } else if (t1->as_pointer_type()) {
  982. if (t2->as_pointer_type()) {
  983. return int_type;
  984. }
  985. return t1;
  986. }
  987. return elevate_type(t1, t2);
  988. case '%':
  989. case '|':
  990. case '&':
  991. case LSHIFT:
  992. case RSHIFT:
  993. return int_type;
  994. case OROR:
  995. case ANDAND:
  996. case EQCOMPARE:
  997. case NECOMPARE:
  998. case LECOMPARE:
  999. case GECOMPARE:
  1000. case '<':
  1001. case '>':
  1002. return bool_type;
  1003. case '?':
  1004. return t2;
  1005. case '.':
  1006. case POINTSAT:
  1007. return nullptr;
  1008. case 'f': // Function evaluation
  1009. if (t1 != nullptr) {
  1010. // Easy case, function with only a single overload.
  1011. CPPFunctionType *ftype = t1->as_function_type();
  1012. if (ftype != nullptr) {
  1013. return ftype->_return_type;
  1014. }
  1015. } else if (_u._op._op1->_type == T_function) {
  1016. CPPFunctionGroup *fgroup = _u._op._op1->_u._fgroup;
  1017. if (_u._op._op2 == nullptr) {
  1018. // If we are passing no args, look for an overload that has takes no
  1019. // args.
  1020. for (auto it = fgroup->_instances.begin(); it != fgroup->_instances.end(); ++it) {
  1021. CPPInstance *inst = *it;
  1022. if (inst != nullptr && inst->_type != nullptr) {
  1023. CPPFunctionType *type = inst->_type->as_function_type();
  1024. if (type != nullptr && type->accepts_num_parameters(0)) {
  1025. return type->_return_type;
  1026. }
  1027. }
  1028. }
  1029. } else {
  1030. //TODO
  1031. }
  1032. }
  1033. return nullptr;
  1034. case ',':
  1035. return t2;
  1036. default:
  1037. cerr << "**unexpected operator**\n";
  1038. abort();
  1039. }
  1040. case T_literal:
  1041. case T_raw_literal:
  1042. if (_u._literal._operator != nullptr) {
  1043. CPPType *type = _u._literal._operator->_type;
  1044. CPPFunctionType *ftype = type->as_function_type();
  1045. if (ftype != nullptr) {
  1046. return ftype->_return_type;
  1047. }
  1048. }
  1049. return nullptr;
  1050. case T_typeid_type:
  1051. case T_typeid_expr:
  1052. return _u._typeid._std_type_info;
  1053. case T_type_trait:
  1054. return bool_type;
  1055. case T_lambda:
  1056. return _u._closure_type;
  1057. default:
  1058. cerr << "**invalid operand**\n";
  1059. abort();
  1060. }
  1061. return nullptr; // Compiler kludge; can't get here.
  1062. }
  1063. /**
  1064. * Returns true if this is an lvalue expression.
  1065. */
  1066. bool CPPExpression::
  1067. is_lvalue() const {
  1068. switch (_type) {
  1069. case T_variable:
  1070. case T_function:
  1071. case T_unknown_ident:
  1072. return true;
  1073. case T_typecast:
  1074. case T_static_cast:
  1075. case T_dynamic_cast:
  1076. case T_const_cast:
  1077. case T_reinterpret_cast:
  1078. {
  1079. CPPReferenceType *ref_type = _u._typecast._to->as_reference_type();
  1080. return ref_type != nullptr && ref_type->_value_category == CPPReferenceType::VC_lvalue;
  1081. }
  1082. case T_unary_operation:
  1083. if (_u._op._operator == 'f') {
  1084. // A function returning an lvalue reference.
  1085. CPPType *return_type = determine_type();
  1086. if (return_type != nullptr) {
  1087. CPPReferenceType *ref_type = return_type->as_reference_type();
  1088. return ref_type != nullptr && ref_type->_value_category == CPPReferenceType::VC_lvalue;
  1089. }
  1090. }
  1091. return _u._op._operator == PLUSPLUS
  1092. || _u._op._operator == MINUSMINUS
  1093. || _u._op._operator == '*';
  1094. case T_binary_operation:
  1095. if (_u._op._operator == ',') {
  1096. CPPReferenceType *ref_type = _u._op._op2->as_reference_type();
  1097. return ref_type != nullptr && ref_type->_value_category == CPPReferenceType::VC_lvalue;
  1098. }
  1099. return (_u._op._operator == POINTSAT || _u._op._operator == ',');
  1100. case T_trinary_operation:
  1101. return _u._op._op2->is_lvalue() && _u._op._op3->is_lvalue();
  1102. case T_literal:
  1103. case T_raw_literal:
  1104. return true;
  1105. default:
  1106. break;
  1107. }
  1108. return false;
  1109. }
  1110. /**
  1111. * Returns true if this declaration is an actual, factual declaration, or
  1112. * false if some part of the declaration depends on a template parameter which
  1113. * has not yet been instantiated.
  1114. */
  1115. bool CPPExpression::
  1116. is_fully_specified() const {
  1117. if (!CPPDeclaration::is_fully_specified()) {
  1118. return false;
  1119. }
  1120. switch (_type) {
  1121. case T_nullptr:
  1122. case T_boolean:
  1123. case T_integer:
  1124. case T_real:
  1125. case T_string:
  1126. case T_wstring:
  1127. case T_u8string:
  1128. case T_u16string:
  1129. case T_u32string:
  1130. return false;
  1131. case T_variable:
  1132. return _u._variable->is_fully_specified();
  1133. case T_function:
  1134. return _u._fgroup->is_fully_specified();
  1135. case T_unknown_ident:
  1136. return _u._ident->is_fully_specified();
  1137. case T_typecast:
  1138. case T_static_cast:
  1139. case T_dynamic_cast:
  1140. case T_const_cast:
  1141. case T_reinterpret_cast:
  1142. case T_construct:
  1143. case T_aggregate_init:
  1144. case T_new:
  1145. return (_u._typecast._to->is_fully_specified() &&
  1146. _u._typecast._op1->is_fully_specified());
  1147. case T_default_construct:
  1148. case T_empty_aggregate_init:
  1149. case T_default_new:
  1150. case T_sizeof_type:
  1151. case T_alignof:
  1152. return _u._typecast._to->is_fully_specified();
  1153. case T_sizeof_expr:
  1154. return _u._typecast._op1->is_fully_specified();
  1155. case T_sizeof_ellipsis:
  1156. return _u._ident->is_fully_specified();
  1157. case T_trinary_operation:
  1158. if (!_u._op._op3->is_fully_specified()) {
  1159. return false;
  1160. }
  1161. // Fall through
  1162. case T_binary_operation:
  1163. if (!_u._op._op2->is_fully_specified()) {
  1164. return false;
  1165. }
  1166. // Fall through
  1167. case T_unary_operation:
  1168. return _u._op._op1->is_fully_specified();
  1169. case T_literal:
  1170. return _u._literal._value->is_fully_specified() &&
  1171. _u._literal._operator->is_fully_specified();
  1172. case T_raw_literal:
  1173. return _u._literal._value->is_fully_specified();
  1174. case T_typeid_type:
  1175. return _u._typeid._type->is_fully_specified();
  1176. case T_typeid_expr:
  1177. return _u._typeid._expr->is_fully_specified();
  1178. case T_type_trait:
  1179. return _u._type_trait._type->is_fully_specified();
  1180. case T_lambda:
  1181. return _u._closure_type->is_fully_specified();
  1182. default:
  1183. return true;
  1184. }
  1185. }
  1186. /**
  1187. *
  1188. */
  1189. CPPDeclaration *CPPExpression::
  1190. substitute_decl(CPPDeclaration::SubstDecl &subst,
  1191. CPPScope *current_scope, CPPScope *global_scope) {
  1192. CPPDeclaration *top =
  1193. CPPDeclaration::substitute_decl(subst, current_scope, global_scope);
  1194. if (top != this) {
  1195. return top;
  1196. }
  1197. CPPExpression *rep = new CPPExpression(*this);
  1198. bool any_changed = false;
  1199. CPPDeclaration *decl;
  1200. switch (_type) {
  1201. case T_variable:
  1202. decl = _u._variable->substitute_decl(subst, current_scope, global_scope);
  1203. if (decl != rep->_u._variable) {
  1204. if (decl->as_instance()) {
  1205. // Replacing the variable reference with another variable reference.
  1206. rep->_u._variable = decl->as_instance();
  1207. any_changed = true;
  1208. } else if (decl->as_expression()) {
  1209. // Replacing the variable reference with an expression.
  1210. delete rep;
  1211. rep = decl->as_expression();
  1212. any_changed = true;
  1213. }
  1214. }
  1215. break;
  1216. case T_unknown_ident:
  1217. rep->_u._ident = _u._ident->substitute_decl(subst, current_scope, global_scope);
  1218. any_changed = any_changed || (rep->_u._ident != _u._ident);
  1219. // See if we can define it now.
  1220. decl = rep->_u._ident->find_symbol(current_scope, global_scope, subst);
  1221. if (decl != nullptr) {
  1222. CPPInstance *inst = decl->as_instance();
  1223. if (inst != nullptr) {
  1224. rep->_type = T_variable;
  1225. rep->_u._variable = inst;
  1226. any_changed = true;
  1227. decl = inst->substitute_decl(subst, current_scope, global_scope);
  1228. if (decl != inst) {
  1229. if (decl->as_instance()) {
  1230. // Replacing the variable reference with another variable
  1231. // reference.
  1232. rep->_u._variable = decl->as_instance();
  1233. } else if (decl->as_expression()) {
  1234. // Replacing the variable reference with an expression.
  1235. delete rep;
  1236. rep = decl->as_expression();
  1237. }
  1238. }
  1239. break;
  1240. }
  1241. CPPFunctionGroup *fgroup = decl->as_function_group();
  1242. if (fgroup != nullptr) {
  1243. rep->_type = T_function;
  1244. rep->_u._fgroup = fgroup;
  1245. any_changed = true;
  1246. }
  1247. }
  1248. break;
  1249. case T_typecast:
  1250. case T_static_cast:
  1251. case T_dynamic_cast:
  1252. case T_const_cast:
  1253. case T_reinterpret_cast:
  1254. case T_construct:
  1255. case T_aggregate_init:
  1256. case T_new:
  1257. rep->_u._typecast._op1 =
  1258. _u._typecast._op1->substitute_decl(subst, current_scope, global_scope)
  1259. ->as_expression();
  1260. any_changed = any_changed || (rep->_u._typecast._op1 != _u._typecast._op1);
  1261. // fall through
  1262. case T_default_construct:
  1263. case T_empty_aggregate_init:
  1264. case T_default_new:
  1265. case T_sizeof_type:
  1266. case T_alignof:
  1267. rep->_u._typecast._to =
  1268. _u._typecast._to->substitute_decl(subst, current_scope, global_scope)
  1269. ->as_type();
  1270. any_changed = any_changed || (rep->_u._typecast._to != _u._typecast._to);
  1271. break;
  1272. case T_sizeof_expr:
  1273. rep->_u._typecast._op1 =
  1274. _u._typecast._op1->substitute_decl(subst, current_scope, global_scope)
  1275. ->as_expression();
  1276. any_changed = any_changed || (rep->_u._typecast._op1 != _u._typecast._op1);
  1277. break;
  1278. case T_trinary_operation:
  1279. rep->_u._op._op3 =
  1280. _u._op._op3->substitute_decl(subst, current_scope, global_scope)
  1281. ->as_expression();
  1282. any_changed = any_changed || (rep->_u._op._op3 != _u._op._op3);
  1283. // fall through
  1284. case T_binary_operation:
  1285. rep->_u._op._op2 =
  1286. _u._op._op2->substitute_decl(subst, current_scope, global_scope)
  1287. ->as_expression();
  1288. any_changed = any_changed || (rep->_u._op._op2 != _u._op._op2);
  1289. // fall through
  1290. case T_unary_operation:
  1291. rep->_u._op._op1 =
  1292. _u._op._op1->substitute_decl(subst, current_scope, global_scope)
  1293. ->as_expression();
  1294. any_changed = any_changed || (rep->_u._op._op1 != _u._op._op1);
  1295. break;
  1296. case T_typeid_type:
  1297. rep->_u._typeid._type =
  1298. _u._typeid._type->substitute_decl(subst, current_scope, global_scope)
  1299. ->as_type();
  1300. any_changed = any_changed || (rep->_u._typeid._type != _u._typeid._type);
  1301. break;
  1302. case T_typeid_expr:
  1303. rep->_u._typeid._expr =
  1304. _u._typeid._expr->substitute_decl(subst, current_scope, global_scope)
  1305. ->as_expression();
  1306. any_changed = any_changed || (rep->_u._typeid._expr != _u._typeid._expr);
  1307. break;
  1308. case T_type_trait:
  1309. rep->_u._type_trait._type =
  1310. _u._type_trait._type->substitute_decl(subst, current_scope, global_scope)
  1311. ->as_type();
  1312. any_changed = any_changed || (rep->_u._type_trait._type != _u._type_trait._type);
  1313. break;
  1314. default:
  1315. break;
  1316. }
  1317. if (!any_changed) {
  1318. delete rep;
  1319. rep = this;
  1320. }
  1321. subst.insert(SubstDecl::value_type(this, rep));
  1322. return rep;
  1323. }
  1324. /**
  1325. * Returns true if any type within the expression list is a CPPTBDType and
  1326. * thus isn't fully determined right now.
  1327. */
  1328. bool CPPExpression::
  1329. is_tbd() const {
  1330. switch (_type) {
  1331. case T_variable:
  1332. if (_u._variable->_type != nullptr &&
  1333. _u._variable->_initializer != nullptr) {
  1334. if (_u._variable->_storage_class & CPPInstance::SC_constexpr) {
  1335. return false;
  1336. }
  1337. CPPConstType *const_type = _u._variable->_type->as_const_type();
  1338. if (const_type != nullptr) {
  1339. return false;
  1340. }
  1341. }
  1342. return true;
  1343. case T_unknown_ident:
  1344. return true;
  1345. case T_typecast:
  1346. case T_static_cast:
  1347. case T_dynamic_cast:
  1348. case T_const_cast:
  1349. case T_reinterpret_cast:
  1350. case T_construct:
  1351. case T_aggregate_init:
  1352. case T_empty_aggregate_init:
  1353. case T_new:
  1354. case T_default_construct:
  1355. case T_default_new:
  1356. case T_sizeof_type:
  1357. case T_alignof:
  1358. return _u._typecast._to->is_tbd();
  1359. case T_sizeof_expr:
  1360. return _u._typecast._op1->is_tbd();
  1361. case T_trinary_operation:
  1362. if (_u._op._op3->is_tbd()) {
  1363. return true;
  1364. }
  1365. // fall through
  1366. case T_binary_operation:
  1367. if (_u._op._op2->is_tbd()) {
  1368. return true;
  1369. }
  1370. // fall through
  1371. case T_unary_operation:
  1372. if (_u._op._op1->is_tbd()) {
  1373. return true;
  1374. }
  1375. return false;
  1376. case T_typeid_type:
  1377. return _u._typeid._type->is_tbd();
  1378. case T_typeid_expr:
  1379. return _u._typeid._expr->is_tbd();
  1380. case T_type_trait:
  1381. return _u._type_trait._type->is_tbd();
  1382. case T_lambda:
  1383. return _u._closure_type->is_tbd();
  1384. default:
  1385. return false;
  1386. }
  1387. }
  1388. /**
  1389. *
  1390. */
  1391. void CPPExpression::
  1392. output(std::ostream &out, int indent_level, CPPScope *scope, bool) const {
  1393. switch (_type) {
  1394. case T_nullptr:
  1395. out << "nullptr";
  1396. break;
  1397. case T_boolean:
  1398. out << (_u._boolean ? "true" : "false");
  1399. break;
  1400. case T_integer:
  1401. out << _u._integer;
  1402. break;
  1403. case T_real:
  1404. {
  1405. // We use our own dtoa implementation here because it guarantees to
  1406. // never format the number as an integer.
  1407. char buffer[32];
  1408. pdtoa(_u._real, buffer);
  1409. out << buffer;
  1410. }
  1411. break;
  1412. case T_string:
  1413. case T_wstring:
  1414. case T_u8string:
  1415. case T_u16string:
  1416. case T_u32string:
  1417. {
  1418. switch (_type) {
  1419. case T_wstring:
  1420. out << 'L';
  1421. break;
  1422. case T_u8string:
  1423. out << "u8";
  1424. break;
  1425. case T_u16string:
  1426. out << "u";
  1427. break;
  1428. case T_u32string:
  1429. out << "U";
  1430. break;
  1431. default:
  1432. break;
  1433. }
  1434. // We don't really care about preserving the encoding for now.
  1435. out << '"';
  1436. string::const_iterator si;
  1437. for (si = _str.begin(); si != _str.end(); ++si) {
  1438. switch (*si) {
  1439. case '\n':
  1440. out << "\\n";
  1441. break;
  1442. case '\t':
  1443. out << "\\t";
  1444. break;
  1445. case '\r':
  1446. out << "\\r";
  1447. break;
  1448. case '\a':
  1449. out << "\\a";
  1450. break;
  1451. case '"':
  1452. out << "\\\"";
  1453. break;
  1454. case '\\':
  1455. out << "\\\\";
  1456. break;
  1457. default:
  1458. if (isprint(*si)) {
  1459. out << *si;
  1460. } else {
  1461. out << '\\' << std::oct << std::setw(3) << std::setfill('0') << (int)(*si)
  1462. << std::dec << std::setw(0);
  1463. }
  1464. }
  1465. }
  1466. }
  1467. out << '"';
  1468. break;
  1469. case T_variable:
  1470. // We can just refer to the variable by name, except if it's a private
  1471. // constant, in which case we have to compute the value, since we may have
  1472. // to use it in generated code.
  1473. if (_u._variable->_type != nullptr &&
  1474. _u._variable->_initializer != nullptr &&
  1475. _u._variable->_vis > V_public) {
  1476. // A constexpr or const variable. Fetch its assigned value.
  1477. CPPConstType *const_type = _u._variable->_type->as_const_type();
  1478. if ((_u._variable->_storage_class & CPPInstance::SC_constexpr) != 0 ||
  1479. const_type != nullptr) {
  1480. _u._variable->_initializer->output(out, indent_level, scope, false);
  1481. break;
  1482. }
  1483. }
  1484. _u._variable->_ident->output(out, scope);
  1485. break;
  1486. case T_function:
  1487. // Pick any instance; they all have the same name anyway.
  1488. if (!_u._fgroup->_instances.empty() && _u._fgroup->_instances[0]->_ident != nullptr) {
  1489. _u._fgroup->_instances[0]->_ident->output(out, scope);
  1490. } else {
  1491. out << _u._fgroup->_name;
  1492. }
  1493. break;
  1494. case T_unknown_ident:
  1495. _u._ident->output(out, scope);
  1496. break;
  1497. case T_typecast:
  1498. out << "(";
  1499. _u._typecast._to->output(out, indent_level, scope, false);
  1500. out << ")(";
  1501. _u._typecast._op1->output(out, indent_level, scope, false);
  1502. out << ")";
  1503. break;
  1504. case T_static_cast:
  1505. out << "static_cast<";
  1506. _u._typecast._to->output(out, indent_level, scope, false);
  1507. out << ">(";
  1508. _u._typecast._op1->output(out, indent_level, scope, false);
  1509. out << ")";
  1510. break;
  1511. case T_dynamic_cast:
  1512. out << "dynamic_cast<";
  1513. _u._typecast._to->output(out, indent_level, scope, false);
  1514. out << ">(";
  1515. _u._typecast._op1->output(out, indent_level, scope, false);
  1516. out << ")";
  1517. break;
  1518. case T_const_cast:
  1519. out << "const_cast<";
  1520. _u._typecast._to->output(out, indent_level, scope, false);
  1521. out << ">(";
  1522. _u._typecast._op1->output(out, indent_level, scope, false);
  1523. out << ")";
  1524. break;
  1525. case T_reinterpret_cast:
  1526. out << "reinterpret_cast<";
  1527. _u._typecast._to->output(out, indent_level, scope, false);
  1528. out << ">(";
  1529. _u._typecast._op1->output(out, indent_level, scope, false);
  1530. out << ")";
  1531. break;
  1532. case T_construct:
  1533. _u._typecast._to->output(out, indent_level, scope, false);
  1534. out << "(";
  1535. _u._typecast._op1->output(out, indent_level, scope, false);
  1536. out << ")";
  1537. break;
  1538. case T_default_construct:
  1539. _u._typecast._to->output(out, indent_level, scope, false);
  1540. out << "()";
  1541. break;
  1542. case T_aggregate_init:
  1543. _u._typecast._to->output(out, indent_level, scope, false);
  1544. out << "{";
  1545. _u._typecast._op1->output(out, indent_level, scope, false);
  1546. out << "}";
  1547. break;
  1548. case T_empty_aggregate_init:
  1549. _u._typecast._to->output(out, indent_level, scope, false);
  1550. out << "{}";
  1551. break;
  1552. case T_new:
  1553. out << "(new ";
  1554. _u._typecast._to->output(out, indent_level, scope, false);
  1555. out << "(";
  1556. _u._typecast._op1->output(out, indent_level, scope, false);
  1557. out << "))";
  1558. break;
  1559. case T_default_new:
  1560. out << "(new ";
  1561. _u._typecast._to->output(out, indent_level, scope, false);
  1562. out << "())";
  1563. break;
  1564. case T_sizeof_type:
  1565. out << "sizeof(";
  1566. _u._typecast._to->output(out, indent_level, scope, false);
  1567. out << ")";
  1568. break;
  1569. case T_sizeof_expr:
  1570. out << "sizeof ";
  1571. _u._typecast._op1->output(out, indent_level, scope, false);
  1572. break;
  1573. case T_sizeof_ellipsis:
  1574. out << "sizeof...(";
  1575. _u._ident->output(out, scope);
  1576. out << ")";
  1577. break;
  1578. case T_alignof:
  1579. out << "alignof(";
  1580. _u._typecast._to->output(out, indent_level, scope, false);
  1581. out << ")";
  1582. break;
  1583. case T_unary_operation:
  1584. switch (_u._op._operator) {
  1585. case UNARY_NOT:
  1586. out << "(! ";
  1587. _u._op._op1->output(out, indent_level, scope, false);
  1588. out << ")";
  1589. break;
  1590. case UNARY_NEGATE:
  1591. out << "(~ ";
  1592. _u._op._op1->output(out, indent_level, scope, false);
  1593. out << ")";
  1594. break;
  1595. case UNARY_MINUS:
  1596. out << '-';
  1597. _u._op._op1->output(out, indent_level, scope, false);
  1598. break;
  1599. case UNARY_PLUS:
  1600. out << '+';
  1601. _u._op._op1->output(out, indent_level, scope, false);
  1602. break;
  1603. case UNARY_STAR:
  1604. out << "(* ";
  1605. _u._op._op1->output(out, indent_level, scope, false);
  1606. out << ")";
  1607. break;
  1608. case UNARY_REF:
  1609. out << "(& ";
  1610. _u._op._op1->output(out, indent_level, scope, false);
  1611. out << ")";
  1612. break;
  1613. case 'f': // Function evaluation, no parameters.
  1614. _u._op._op1->output(out, indent_level, scope, false);
  1615. out << "()";
  1616. break;
  1617. default:
  1618. out << "(" << (char)_u._op._operator << " ";
  1619. _u._op._op1->output(out, indent_level, scope, false);
  1620. out << ")";
  1621. break;
  1622. }
  1623. break;
  1624. case T_binary_operation:
  1625. switch (_u._op._operator) {
  1626. case OROR:
  1627. out << "(";
  1628. _u._op._op1->output(out, indent_level, scope, false);
  1629. out << " || ";
  1630. _u._op._op2->output(out, indent_level, scope, false);
  1631. out << ")";
  1632. break;
  1633. case ANDAND:
  1634. out << "(";
  1635. _u._op._op1->output(out, indent_level, scope, false);
  1636. out << " && ";
  1637. _u._op._op2->output(out, indent_level, scope, false);
  1638. out << ")";
  1639. break;
  1640. case EQCOMPARE:
  1641. out << "(";
  1642. _u._op._op1->output(out, indent_level, scope, false);
  1643. out << " == ";
  1644. _u._op._op2->output(out, indent_level, scope, false);
  1645. out << ")";
  1646. break;
  1647. case NECOMPARE:
  1648. out << "(";
  1649. _u._op._op1->output(out, indent_level, scope, false);
  1650. out << " != ";
  1651. _u._op._op2->output(out, indent_level, scope, false);
  1652. out << ")";
  1653. break;
  1654. case LECOMPARE:
  1655. out << "(";
  1656. _u._op._op1->output(out, indent_level, scope, false);
  1657. out << " <= ";
  1658. _u._op._op2->output(out, indent_level, scope, false);
  1659. out << ")";
  1660. break;
  1661. case GECOMPARE:
  1662. out << "(";
  1663. _u._op._op1->output(out, indent_level, scope, false);
  1664. out << " >= ";
  1665. _u._op._op2->output(out, indent_level, scope, false);
  1666. out << ")";
  1667. break;
  1668. case LSHIFT:
  1669. out << "(";
  1670. _u._op._op1->output(out, indent_level, scope, false);
  1671. out << " << ";
  1672. _u._op._op2->output(out, indent_level, scope, false);
  1673. out << ")";
  1674. break;
  1675. case RSHIFT:
  1676. out << "(";
  1677. _u._op._op1->output(out, indent_level, scope, false);
  1678. out << " >> ";
  1679. _u._op._op2->output(out, indent_level, scope, false);
  1680. out << ")";
  1681. break;
  1682. case '.':
  1683. _u._op._op1->output(out, indent_level, scope, false);
  1684. out << ".";
  1685. _u._op._op2->output(out, indent_level, scope, false);
  1686. break;
  1687. case POINTSAT:
  1688. _u._op._op1->output(out, indent_level, scope, false);
  1689. out << "->";
  1690. _u._op._op2->output(out, indent_level, scope, false);
  1691. break;
  1692. case '[': // Array element reference
  1693. out << "(";
  1694. _u._op._op1->output(out, indent_level, scope, false);
  1695. out << "[";
  1696. _u._op._op2->output(out, indent_level, scope, false);
  1697. out << "])";
  1698. break;
  1699. case 'f': // Function evaluation
  1700. out << "(";
  1701. _u._op._op1->output(out, indent_level, scope, false);
  1702. out << "(";
  1703. _u._op._op2->output(out, indent_level, scope, false);
  1704. out << "))";
  1705. break;
  1706. case ',': // Comma, no parens are used
  1707. _u._op._op1->output(out, indent_level, scope, false);
  1708. out << ", ";
  1709. _u._op._op2->output(out, indent_level, scope, false);
  1710. break;
  1711. default:
  1712. out << "(";
  1713. _u._op._op1->output(out, indent_level, scope, false);
  1714. out << " " << (char)_u._op._operator << " ";
  1715. _u._op._op2->output(out, indent_level, scope, false);
  1716. out << ")";
  1717. }
  1718. break;
  1719. case T_trinary_operation:
  1720. out << "(";
  1721. _u._op._op1->output(out, indent_level, scope, false);
  1722. out << " ? ";
  1723. _u._op._op2->output(out, indent_level, scope, false);
  1724. out << " : ";
  1725. _u._op._op3->output(out, indent_level, scope, false);
  1726. out << ")";
  1727. break;
  1728. case T_literal:
  1729. _u._literal._value->output(out, indent_level, scope, false);
  1730. if (_u._literal._operator != nullptr) {
  1731. string name = _u._literal._operator->get_simple_name();
  1732. assert(name.substr(0, 12) == "operator \"\" ");
  1733. out << name.substr(12);
  1734. }
  1735. break;
  1736. case T_raw_literal:
  1737. out << _str;
  1738. if (_u._literal._operator != nullptr) {
  1739. string name = _u._literal._operator->get_simple_name();
  1740. assert(name.substr(0, 12) == "operator \"\" ");
  1741. out << name.substr(12);
  1742. }
  1743. break;
  1744. case T_typeid_type:
  1745. out << "typeid(";
  1746. _u._typeid._type->output(out, indent_level, scope, false);
  1747. out << ")";
  1748. break;
  1749. case T_typeid_expr:
  1750. out << "typeid(";
  1751. _u._typeid._expr->output(out, indent_level, scope, false);
  1752. out << ")";
  1753. break;
  1754. case T_default:
  1755. out << "default";
  1756. break;
  1757. case T_delete:
  1758. out << "delete";
  1759. break;
  1760. case T_type_trait:
  1761. switch (_u._type_trait._trait) {
  1762. case KW_HAS_VIRTUAL_DESTRUCTOR:
  1763. out << "__has_virtual_destructor";
  1764. break;
  1765. case KW_IS_ABSTRACT:
  1766. out << "__is_abstract";
  1767. break;
  1768. case KW_IS_BASE_OF:
  1769. out << "__is_base_of";
  1770. break;
  1771. case KW_IS_CLASS:
  1772. out << "__is_class";
  1773. break;
  1774. case KW_IS_CONSTRUCTIBLE:
  1775. out << "__is_constructible";
  1776. break;
  1777. case KW_IS_CONVERTIBLE_TO:
  1778. out << "__is_convertible_to";
  1779. break;
  1780. case KW_IS_DESTRUCTIBLE:
  1781. out << "__is_destructible";
  1782. break;
  1783. case KW_IS_EMPTY:
  1784. out << "__is_empty";
  1785. break;
  1786. case KW_IS_ENUM:
  1787. out << "__is_enum";
  1788. break;
  1789. case KW_IS_FINAL:
  1790. out << "__is_final";
  1791. break;
  1792. case KW_IS_FUNDAMENTAL:
  1793. out << "__is_fundamental";
  1794. break;
  1795. case KW_IS_POD:
  1796. out << "__is_pod";
  1797. break;
  1798. case KW_IS_POLYMORPHIC:
  1799. out << "__is_polymorphic";
  1800. break;
  1801. case KW_IS_STANDARD_LAYOUT:
  1802. out << "__is_standard_layout";
  1803. break;
  1804. case KW_IS_TRIVIAL:
  1805. out << "__is_trivial";
  1806. break;
  1807. case KW_IS_UNION:
  1808. out << "__is_union";
  1809. break;
  1810. default:
  1811. out << (evaluate().as_boolean() ? "true" : "false");
  1812. return;
  1813. }
  1814. out << '(';
  1815. _u._type_trait._type->output(out, indent_level, scope, false);
  1816. out << ')';
  1817. break;
  1818. case T_lambda:
  1819. _u._closure_type->output(out, indent_level, scope, false);
  1820. break;
  1821. default:
  1822. out << "(** invalid operand type " << (int)_type << " **)";
  1823. }
  1824. }
  1825. /**
  1826. *
  1827. */
  1828. CPPDeclaration::SubType CPPExpression::
  1829. get_subtype() const {
  1830. return ST_expression;
  1831. }
  1832. /**
  1833. *
  1834. */
  1835. CPPExpression *CPPExpression::
  1836. as_expression() {
  1837. return this;
  1838. }
  1839. /**
  1840. * Returns the most general of the two given types.
  1841. */
  1842. CPPType *CPPExpression::
  1843. elevate_type(CPPType *t1, CPPType *t2) {
  1844. CPPSimpleType *st1 = t1->as_simple_type();
  1845. CPPSimpleType *st2 = t2->as_simple_type();
  1846. if (st1 == nullptr || st2 == nullptr) {
  1847. // Nothing we can do about this. Who knows?
  1848. return nullptr;
  1849. }
  1850. if (st1->_type == st2->_type) {
  1851. // They have the same type, so return the one with the largest flag bits.
  1852. if (st1->_flags & CPPSimpleType::F_longlong) {
  1853. return st1;
  1854. } else if (st2->_flags & CPPSimpleType::F_longlong) {
  1855. return st2;
  1856. } else if (st1->_flags & CPPSimpleType::F_long) {
  1857. return st1;
  1858. } else if (st2->_flags & CPPSimpleType::F_long) {
  1859. return st2;
  1860. } else if (st1->_flags & CPPSimpleType::F_short) {
  1861. return st2;
  1862. } else if (st2->_flags & CPPSimpleType::F_short) {
  1863. return st1;
  1864. }
  1865. return st1;
  1866. }
  1867. // They have different types.
  1868. if (st1->_type == CPPSimpleType::T_float ||
  1869. st1->_type == CPPSimpleType::T_double) {
  1870. return st1;
  1871. } else if (st2->_type == CPPSimpleType::T_float ||
  1872. st2->_type == CPPSimpleType::T_double) {
  1873. return st2;
  1874. } else if (st1->_type == CPPSimpleType::T_int) {
  1875. return st1;
  1876. } else if (st2->_type == CPPSimpleType::T_int) {
  1877. return st2;
  1878. } else if (st1->_type == CPPSimpleType::T_bool) {
  1879. return st1;
  1880. } else if (st2->_type == CPPSimpleType::T_bool) {
  1881. return st2;
  1882. }
  1883. return st1;
  1884. }
  1885. /**
  1886. * Called by CPPDeclaration to determine whether this expr is equivalent to
  1887. * another expr.
  1888. */
  1889. bool CPPExpression::
  1890. is_equal(const CPPDeclaration *other) const {
  1891. const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
  1892. assert(ot != nullptr);
  1893. if (_type != ot->_type) {
  1894. return false;
  1895. }
  1896. switch (_type) {
  1897. case T_nullptr:
  1898. return true;
  1899. case T_boolean:
  1900. return _u._boolean == ot->_u._boolean;
  1901. case T_integer:
  1902. return _u._integer == ot->_u._integer;
  1903. case T_real:
  1904. return _u._real == ot->_u._real;
  1905. case T_string:
  1906. case T_wstring:
  1907. case T_u8string:
  1908. case T_u16string:
  1909. case T_u32string:
  1910. return _str == ot->_str;
  1911. case T_variable:
  1912. return _u._variable == ot->_u._variable;
  1913. case T_function:
  1914. return _u._fgroup == ot->_u._fgroup;
  1915. case T_unknown_ident:
  1916. case T_sizeof_ellipsis:
  1917. return *_u._ident == *ot->_u._ident;
  1918. case T_typecast:
  1919. case T_static_cast:
  1920. case T_dynamic_cast:
  1921. case T_const_cast:
  1922. case T_reinterpret_cast:
  1923. case T_construct:
  1924. case T_aggregate_init:
  1925. case T_new:
  1926. return _u._typecast._to == ot->_u._typecast._to &&
  1927. *_u._typecast._op1 == *ot->_u._typecast._op1;
  1928. case T_default_construct:
  1929. case T_empty_aggregate_init:
  1930. case T_default_new:
  1931. case T_sizeof_type:
  1932. case T_alignof:
  1933. return _u._typecast._to == ot->_u._typecast._to;
  1934. case T_sizeof_expr:
  1935. return _u._typecast._op1 == ot->_u._typecast._op1;
  1936. case T_unary_operation:
  1937. return *_u._op._op1 == *ot->_u._op._op1;
  1938. case T_binary_operation:
  1939. return *_u._op._op1 == *ot->_u._op._op1 &&
  1940. *_u._op._op2 == *ot->_u._op._op2;
  1941. case T_trinary_operation:
  1942. return *_u._op._op1 == *ot->_u._op._op1 &&
  1943. *_u._op._op2 == *ot->_u._op._op2;
  1944. case T_literal:
  1945. return *_u._literal._value == *ot->_u._literal._value &&
  1946. _u._literal._operator == ot->_u._literal._operator;
  1947. case T_raw_literal:
  1948. return _str == ot->_str &&
  1949. _u._literal._operator == ot->_u._literal._operator;
  1950. case T_typeid_type:
  1951. return _u._typeid._type == ot->_u._typeid._type;
  1952. case T_typeid_expr:
  1953. return _u._typeid._expr == ot->_u._typeid._expr;
  1954. case T_type_trait:
  1955. return _u._type_trait._trait == ot->_u._type_trait._trait &&
  1956. _u._type_trait._type == ot->_u._type_trait._type;
  1957. case T_lambda:
  1958. return _u._closure_type == ot->_u._closure_type;
  1959. default:
  1960. cerr << "(** invalid operand type " << (int)_type << " **)";
  1961. }
  1962. return true;
  1963. }
  1964. /**
  1965. * Called by CPPDeclaration to determine whether this expr should be ordered
  1966. * before another expr of the same type, in an arbitrary but fixed ordering.
  1967. */
  1968. bool CPPExpression::
  1969. is_less(const CPPDeclaration *other) const {
  1970. const CPPExpression *ot = ((CPPDeclaration *)other)->as_expression();
  1971. assert(ot != nullptr);
  1972. if (_type != ot->_type) {
  1973. return (int)_type < (int)ot->_type;
  1974. }
  1975. switch (_type) {
  1976. case T_nullptr:
  1977. return false;
  1978. case T_boolean:
  1979. return _u._boolean < ot->_u._boolean;
  1980. case T_integer:
  1981. return _u._integer < ot->_u._integer;
  1982. case T_real:
  1983. return _u._real < ot->_u._real;
  1984. case T_string:
  1985. case T_wstring:
  1986. case T_u8string:
  1987. case T_u16string:
  1988. case T_u32string:
  1989. return _str < ot->_str;
  1990. case T_variable:
  1991. return _u._variable < ot->_u._variable;
  1992. case T_function:
  1993. return *_u._fgroup < *ot->_u._fgroup;
  1994. case T_unknown_ident:
  1995. case T_sizeof_ellipsis:
  1996. return *_u._ident < *ot->_u._ident;
  1997. case T_typecast:
  1998. case T_static_cast:
  1999. case T_dynamic_cast:
  2000. case T_const_cast:
  2001. case T_reinterpret_cast:
  2002. case T_construct:
  2003. case T_aggregate_init:
  2004. case T_new:
  2005. if (_u._typecast._to != ot->_u._typecast._to) {
  2006. return _u._typecast._to < ot->_u._typecast._to;
  2007. }
  2008. return *_u._typecast._op1 < *ot->_u._typecast._op1;
  2009. case T_default_construct:
  2010. case T_empty_aggregate_init:
  2011. case T_default_new:
  2012. case T_sizeof_type:
  2013. case T_alignof:
  2014. return _u._typecast._to < ot->_u._typecast._to;
  2015. case T_sizeof_expr:
  2016. return _u._typecast._op1 < ot->_u._typecast._op1;
  2017. case T_trinary_operation:
  2018. if (*_u._op._op3 != *ot->_u._op._op3) {
  2019. return *_u._op._op3 < *ot->_u._op._op3;
  2020. }
  2021. // Fall through
  2022. case T_binary_operation:
  2023. if (*_u._op._op2 != *ot->_u._op._op2) {
  2024. return *_u._op._op2 < *ot->_u._op._op2;
  2025. }
  2026. // Fall through
  2027. case T_unary_operation:
  2028. return *_u._op._op1 < *ot->_u._op._op1;
  2029. case T_literal:
  2030. if (_u._literal._operator != ot->_u._literal._operator) {
  2031. return _u._literal._operator < ot->_u._literal._operator;
  2032. }
  2033. return *_u._literal._value < *ot->_u._literal._value;
  2034. case T_raw_literal:
  2035. if (_u._literal._operator != ot->_u._literal._operator) {
  2036. return _u._literal._operator < ot->_u._literal._operator;
  2037. }
  2038. return _str < ot->_str;
  2039. case T_typeid_type:
  2040. return _u._typeid._type < ot->_u._typeid._type;
  2041. case T_typeid_expr:
  2042. return *_u._typeid._expr < *ot->_u._typeid._expr;
  2043. case T_type_trait:
  2044. if (_u._type_trait._trait != ot->_u._type_trait._trait) {
  2045. return _u._type_trait._trait < ot->_u._type_trait._trait;
  2046. }
  2047. return *_u._type_trait._type < *ot->_u._type_trait._type;
  2048. case T_lambda:
  2049. return _u._closure_type < ot->_u._closure_type;
  2050. default:
  2051. cerr << "(** invalid operand type " << (int)_type << " **)";
  2052. }
  2053. return false;
  2054. }