cppExpression.cxx 54 KB

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