expressions.h 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  1. // expressions.h -- Go frontend expression handling. -*- C++ -*-
  2. // Copyright 2009 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. #ifndef GO_EXPRESSIONS_H
  6. #define GO_EXPRESSIONS_H
  7. #include <mpfr.h>
  8. #include <mpc.h>
  9. #include "operator.h"
  10. class Gogo;
  11. class Translate_context;
  12. class Traverse;
  13. class Statement_inserter;
  14. class Type;
  15. class Method;
  16. struct Type_context;
  17. class Integer_type;
  18. class Float_type;
  19. class Complex_type;
  20. class Function_type;
  21. class Map_type;
  22. class Struct_type;
  23. class Struct_field;
  24. class Expression_list;
  25. class Var_expression;
  26. class Temporary_reference_expression;
  27. class Set_and_use_temporary_expression;
  28. class String_expression;
  29. class Unary_expression;
  30. class Binary_expression;
  31. class Call_expression;
  32. class Func_expression;
  33. class Func_descriptor_expression;
  34. class Unknown_expression;
  35. class Index_expression;
  36. class Map_index_expression;
  37. class Bound_method_expression;
  38. class Field_reference_expression;
  39. class Interface_field_reference_expression;
  40. class Type_guard_expression;
  41. class Receive_expression;
  42. class Numeric_constant;
  43. class Named_object;
  44. class Export;
  45. class Import;
  46. class Temporary_statement;
  47. class Label;
  48. class Ast_dump_context;
  49. class String_dump;
  50. // The precision to use for complex values represented as an mpc_t.
  51. const int mpc_precision = 256;
  52. // The base class for all expressions.
  53. class Expression
  54. {
  55. public:
  56. // The types of expressions.
  57. enum Expression_classification
  58. {
  59. EXPRESSION_ERROR,
  60. EXPRESSION_TYPE,
  61. EXPRESSION_UNARY,
  62. EXPRESSION_BINARY,
  63. EXPRESSION_CONST_REFERENCE,
  64. EXPRESSION_VAR_REFERENCE,
  65. EXPRESSION_TEMPORARY_REFERENCE,
  66. EXPRESSION_SET_AND_USE_TEMPORARY,
  67. EXPRESSION_SINK,
  68. EXPRESSION_FUNC_REFERENCE,
  69. EXPRESSION_FUNC_DESCRIPTOR,
  70. EXPRESSION_FUNC_CODE_REFERENCE,
  71. EXPRESSION_UNKNOWN_REFERENCE,
  72. EXPRESSION_BOOLEAN,
  73. EXPRESSION_STRING,
  74. EXPRESSION_STRING_INFO,
  75. EXPRESSION_INTEGER,
  76. EXPRESSION_FLOAT,
  77. EXPRESSION_COMPLEX,
  78. EXPRESSION_NIL,
  79. EXPRESSION_IOTA,
  80. EXPRESSION_CALL,
  81. EXPRESSION_CALL_RESULT,
  82. EXPRESSION_BOUND_METHOD,
  83. EXPRESSION_INDEX,
  84. EXPRESSION_ARRAY_INDEX,
  85. EXPRESSION_STRING_INDEX,
  86. EXPRESSION_MAP_INDEX,
  87. EXPRESSION_SELECTOR,
  88. EXPRESSION_FIELD_REFERENCE,
  89. EXPRESSION_INTERFACE_FIELD_REFERENCE,
  90. EXPRESSION_ALLOCATION,
  91. EXPRESSION_TYPE_GUARD,
  92. EXPRESSION_CONVERSION,
  93. EXPRESSION_UNSAFE_CONVERSION,
  94. EXPRESSION_STRUCT_CONSTRUCTION,
  95. EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
  96. EXPRESSION_SLICE_CONSTRUCTION,
  97. EXPRESSION_MAP_CONSTRUCTION,
  98. EXPRESSION_COMPOSITE_LITERAL,
  99. EXPRESSION_HEAP,
  100. EXPRESSION_RECEIVE,
  101. EXPRESSION_TYPE_DESCRIPTOR,
  102. EXPRESSION_GC_SYMBOL,
  103. EXPRESSION_TYPE_INFO,
  104. EXPRESSION_SLICE_INFO,
  105. EXPRESSION_SLICE_VALUE,
  106. EXPRESSION_INTERFACE_INFO,
  107. EXPRESSION_INTERFACE_VALUE,
  108. EXPRESSION_INTERFACE_MTABLE,
  109. EXPRESSION_STRUCT_FIELD_OFFSET,
  110. EXPRESSION_MAP_DESCRIPTOR,
  111. EXPRESSION_LABEL_ADDR,
  112. EXPRESSION_CONDITIONAL,
  113. EXPRESSION_COMPOUND
  114. };
  115. Expression(Expression_classification, Location);
  116. virtual ~Expression();
  117. // Make an error expression. This is used when a parse error occurs
  118. // to prevent cascading errors.
  119. static Expression*
  120. make_error(Location);
  121. // Make an expression which is really a type. This is used during
  122. // parsing.
  123. static Expression*
  124. make_type(Type*, Location);
  125. // Make a unary expression.
  126. static Expression*
  127. make_unary(Operator, Expression*, Location);
  128. // Make a binary expression.
  129. static Expression*
  130. make_binary(Operator, Expression*, Expression*, Location);
  131. // Make a reference to a constant in an expression.
  132. static Expression*
  133. make_const_reference(Named_object*, Location);
  134. // Make a reference to a variable in an expression.
  135. static Expression*
  136. make_var_reference(Named_object*, Location);
  137. // Make a reference to a temporary variable. Temporary variables
  138. // are always created by a single statement, which is what we use to
  139. // refer to them.
  140. static Temporary_reference_expression*
  141. make_temporary_reference(Temporary_statement*, Location);
  142. // Make an expressions which sets a temporary variable and then
  143. // evaluates to a reference to that temporary variable. This is
  144. // used to set a temporary variable while retaining the order of
  145. // evaluation.
  146. static Set_and_use_temporary_expression*
  147. make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
  148. // Make a sink expression--a reference to the blank identifier _.
  149. static Expression*
  150. make_sink(Location);
  151. // Make a reference to a function in an expression. This returns a
  152. // pointer to the struct holding the address of the function
  153. // followed by any closed-over variables.
  154. static Expression*
  155. make_func_reference(Named_object*, Expression* closure, Location);
  156. // Make a function descriptor, an immutable struct with a single
  157. // field that points to the function code. This may only be used
  158. // with functions that do not have closures. FN is the function for
  159. // which we are making the descriptor.
  160. static Func_descriptor_expression*
  161. make_func_descriptor(Named_object* fn);
  162. // Make a reference to the code of a function. This is used to set
  163. // descriptor and closure fields.
  164. static Expression*
  165. make_func_code_reference(Named_object*, Location);
  166. // Make a reference to an unknown name. In a correct program this
  167. // will always be lowered to a real const/var/func reference.
  168. static Unknown_expression*
  169. make_unknown_reference(Named_object*, Location);
  170. // Make a constant bool expression.
  171. static Expression*
  172. make_boolean(bool val, Location);
  173. // Make a constant string expression.
  174. static Expression*
  175. make_string(const std::string&, Location);
  176. // Make an expression that evaluates to some characteristic of an string.
  177. // For simplicity, the enum values must match the field indexes in the
  178. // underlying struct.
  179. enum String_info
  180. {
  181. // The underlying data in the string.
  182. STRING_INFO_DATA,
  183. // The length of the string.
  184. STRING_INFO_LENGTH
  185. };
  186. static Expression*
  187. make_string_info(Expression* string, String_info, Location);
  188. // Make a character constant expression. TYPE should be NULL for an
  189. // abstract type.
  190. static Expression*
  191. make_character(const mpz_t*, Type*, Location);
  192. // Make a constant integer expression from a multi-precision
  193. // integer. TYPE should be NULL for an abstract type.
  194. static Expression*
  195. make_integer_z(const mpz_t*, Type*, Location);
  196. // Make a constant integer expression from an unsigned long. TYPE
  197. // should be NULL for an abstract type.
  198. static Expression*
  199. make_integer_ul(unsigned long, Type*, Location);
  200. // Make a constant integer expression from a signed long. TYPE
  201. // should be NULL for an abstract type.
  202. static Expression*
  203. make_integer_sl(long, Type*, Location);
  204. // Make a constant integer expression from an int64_t. TYPE should
  205. // be NULL for an abstract type.
  206. static Expression*
  207. make_integer_int64(int64_t, Type*, Location);
  208. // Make a constant float expression. TYPE should be NULL for an
  209. // abstract type.
  210. static Expression*
  211. make_float(const mpfr_t*, Type*, Location);
  212. // Make a constant complex expression. TYPE should be NULL for an
  213. // abstract type.
  214. static Expression*
  215. make_complex(const mpc_t*, Type*, Location);
  216. // Make a nil expression.
  217. static Expression*
  218. make_nil(Location);
  219. // Make an iota expression. This is used for the predeclared
  220. // constant iota.
  221. static Expression*
  222. make_iota();
  223. // Make a call expression.
  224. static Call_expression*
  225. make_call(Expression* func, Expression_list* args, bool is_varargs,
  226. Location);
  227. // Make a reference to a specific result of a call expression which
  228. // returns a tuple.
  229. static Expression*
  230. make_call_result(Call_expression*, unsigned int index);
  231. // Make an expression which is a method bound to its first
  232. // parameter. METHOD is the method being called, FUNCTION is the
  233. // function to call.
  234. static Bound_method_expression*
  235. make_bound_method(Expression* object, const Method* method,
  236. Named_object* function, Location);
  237. // Make an index or slice expression. This is a parser expression
  238. // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
  239. // index rather than a slice. CAP may be NULL, meaning we use the default
  240. // capacity of LEFT. At parse time we may not know the type of LEFT.
  241. // After parsing this is lowered to an array index, a string index,
  242. // or a map index.
  243. static Expression*
  244. make_index(Expression* left, Expression* start, Expression* end,
  245. Expression* cap, Location);
  246. // Make an array index expression. END may be NULL, in which case
  247. // this is an lvalue. CAP may be NULL, in which case it defaults
  248. // to cap(ARRAY).
  249. static Expression*
  250. make_array_index(Expression* array, Expression* start, Expression* end,
  251. Expression* cap, Location);
  252. // Make a string index expression. END may be NULL. This is never
  253. // an lvalue.
  254. static Expression*
  255. make_string_index(Expression* string, Expression* start, Expression* end,
  256. Location);
  257. // Make a map index expression. This is an lvalue.
  258. static Map_index_expression*
  259. make_map_index(Expression* map, Expression* val, Location);
  260. // Make a selector. This is a parser expression which represents
  261. // LEFT.NAME. At parse time we may not know the type of the left
  262. // hand side.
  263. static Expression*
  264. make_selector(Expression* left, const std::string& name, Location);
  265. // Make a reference to a field in a struct.
  266. static Field_reference_expression*
  267. make_field_reference(Expression*, unsigned int field_index, Location);
  268. // Make a reference to a field of an interface, with an associated
  269. // object.
  270. static Expression*
  271. make_interface_field_reference(Expression*, const std::string&,
  272. Location);
  273. // Make an allocation expression.
  274. static Expression*
  275. make_allocation(Type*, Location);
  276. // Make a type guard expression.
  277. static Expression*
  278. make_type_guard(Expression*, Type*, Location);
  279. // Make a type cast expression.
  280. static Expression*
  281. make_cast(Type*, Expression*, Location);
  282. // Make an unsafe type cast expression. This is only used when
  283. // passing parameter to builtin functions that are part of the Go
  284. // runtime.
  285. static Expression*
  286. make_unsafe_cast(Type*, Expression*, Location);
  287. // Make a composite literal. The DEPTH parameter is how far down we
  288. // are in a list of composite literals with omitted types. HAS_KEYS
  289. // is true if the expression list has keys alternating with values.
  290. // ALL_ARE_NAMES is true if all the keys could be struct field
  291. // names.
  292. static Expression*
  293. make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
  294. bool all_are_names, Location);
  295. // Make a struct composite literal.
  296. static Expression*
  297. make_struct_composite_literal(Type*, Expression_list*, Location);
  298. // Make an array composite literal.
  299. static Expression*
  300. make_array_composite_literal(Type*, Expression_list*, Location);
  301. // Make a slice composite literal.
  302. static Expression*
  303. make_slice_composite_literal(Type*, Expression_list*, Location);
  304. // Take an expression and allocate it on the heap.
  305. static Expression*
  306. make_heap_expression(Expression*, Location);
  307. // Make a receive expression. VAL is NULL for a unary receive.
  308. static Receive_expression*
  309. make_receive(Expression* channel, Location);
  310. // Make an expression which evaluates to the address of the type
  311. // descriptor for TYPE.
  312. static Expression*
  313. make_type_descriptor(Type* type, Location);
  314. // Make an expression which evaluates to the address of the gc
  315. // symbol for TYPE.
  316. static Expression*
  317. make_gc_symbol(Type* type);
  318. // Make an expression which evaluates to some characteristic of a
  319. // type. These are only used for type descriptors, so there is no
  320. // location parameter.
  321. enum Type_info
  322. {
  323. // The size of a value of the type.
  324. TYPE_INFO_SIZE,
  325. // The required alignment of a value of the type.
  326. TYPE_INFO_ALIGNMENT,
  327. // The required alignment of a value of the type when used as a
  328. // field in a struct.
  329. TYPE_INFO_FIELD_ALIGNMENT
  330. };
  331. static Expression*
  332. make_type_info(Type* type, Type_info);
  333. // Make an expression that evaluates to some characteristic of a
  334. // slice. For simplicity, the enum values must match the field indexes
  335. // in the underlying struct.
  336. enum Slice_info
  337. {
  338. // The underlying data of the slice.
  339. SLICE_INFO_VALUE_POINTER,
  340. // The length of the slice.
  341. SLICE_INFO_LENGTH,
  342. // The capacity of the slice.
  343. SLICE_INFO_CAPACITY
  344. };
  345. static Expression*
  346. make_slice_info(Expression* slice, Slice_info, Location);
  347. // Make an expression for a slice value.
  348. static Expression*
  349. make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
  350. Location);
  351. // Make an expression that evaluates to some characteristic of an
  352. // interface. For simplicity, the enum values must match the field indexes
  353. // in the underlying struct.
  354. enum Interface_info
  355. {
  356. // The type descriptor of an empty interface.
  357. INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
  358. // The methods of an interface.
  359. INTERFACE_INFO_METHODS = 0,
  360. // The first argument to pass to an interface method.
  361. INTERFACE_INFO_OBJECT
  362. };
  363. static Expression*
  364. make_interface_info(Expression* iface, Interface_info, Location);
  365. // Make an expression for an interface value.
  366. static Expression*
  367. make_interface_value(Type*, Expression*, Expression*, Location);
  368. // Make an expression that builds a reference to the interface method table
  369. // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
  370. // reference to the interface method table for the pointer receiver type.
  371. static Expression*
  372. make_interface_mtable_ref(Interface_type* itype, Type* type,
  373. bool is_pointer, Location);
  374. // Make an expression which evaluates to the offset of a field in a
  375. // struct. This is only used for type descriptors, so there is no
  376. // location parameter.
  377. static Expression*
  378. make_struct_field_offset(Struct_type*, const Struct_field*);
  379. // Make an expression which evaluates to the address of the map
  380. // descriptor for TYPE.
  381. static Expression*
  382. make_map_descriptor(Map_type* type, Location);
  383. // Make an expression which evaluates to the address of an unnamed
  384. // label.
  385. static Expression*
  386. make_label_addr(Label*, Location);
  387. // Make a conditional expression.
  388. static Expression*
  389. make_conditional(Expression*, Expression*, Expression*, Location);
  390. // Make a compound expression.
  391. static Expression*
  392. make_compound(Expression*, Expression*, Location);
  393. // Return the expression classification.
  394. Expression_classification
  395. classification() const
  396. { return this->classification_; }
  397. // Return the location of the expression.
  398. Location
  399. location() const
  400. { return this->location_; }
  401. // Return whether this is a constant expression.
  402. bool
  403. is_constant() const
  404. { return this->do_is_constant(); }
  405. // Return whether this is an immutable expression.
  406. bool
  407. is_immutable() const
  408. { return this->do_is_immutable(); }
  409. // If this is not a numeric constant, return false. If it is one,
  410. // return true, and set VAL to hold the value.
  411. bool
  412. numeric_constant_value(Numeric_constant* val) const
  413. { return this->do_numeric_constant_value(val); }
  414. // If this is not a constant expression with string type, return
  415. // false. If it is one, return true, and set VAL to the value.
  416. bool
  417. string_constant_value(std::string* val) const
  418. { return this->do_string_constant_value(val); }
  419. // This is called if the value of this expression is being
  420. // discarded. This issues warnings about computed values being
  421. // unused. This returns true if all is well, false if it issued an
  422. // error message.
  423. bool
  424. discarding_value()
  425. { return this->do_discarding_value(); }
  426. // Return whether this is an error expression.
  427. bool
  428. is_error_expression() const
  429. { return this->classification_ == EXPRESSION_ERROR; }
  430. // Return whether this expression really represents a type.
  431. bool
  432. is_type_expression() const
  433. { return this->classification_ == EXPRESSION_TYPE; }
  434. // If this is a variable reference, return the Var_expression
  435. // structure. Otherwise, return NULL. This is a controlled dynamic
  436. // cast.
  437. Var_expression*
  438. var_expression()
  439. { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
  440. const Var_expression*
  441. var_expression() const
  442. { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
  443. // If this is a reference to a temporary variable, return the
  444. // Temporary_reference_expression. Otherwise, return NULL.
  445. Temporary_reference_expression*
  446. temporary_reference_expression()
  447. {
  448. return this->convert<Temporary_reference_expression,
  449. EXPRESSION_TEMPORARY_REFERENCE>();
  450. }
  451. // If this is a set-and-use-temporary, return the
  452. // Set_and_use_temporary_expression. Otherwise, return NULL.
  453. Set_and_use_temporary_expression*
  454. set_and_use_temporary_expression()
  455. {
  456. return this->convert<Set_and_use_temporary_expression,
  457. EXPRESSION_SET_AND_USE_TEMPORARY>();
  458. }
  459. // Return whether this is a sink expression.
  460. bool
  461. is_sink_expression() const
  462. { return this->classification_ == EXPRESSION_SINK; }
  463. // If this is a string expression, return the String_expression
  464. // structure. Otherwise, return NULL.
  465. String_expression*
  466. string_expression()
  467. { return this->convert<String_expression, EXPRESSION_STRING>(); }
  468. // Return whether this is the expression nil.
  469. bool
  470. is_nil_expression() const
  471. { return this->classification_ == EXPRESSION_NIL; }
  472. // If this is an indirection through a pointer, return the
  473. // expression being pointed through. Otherwise return this.
  474. Expression*
  475. deref();
  476. // If this is a unary expression, return the Unary_expression
  477. // structure. Otherwise return NULL.
  478. Unary_expression*
  479. unary_expression()
  480. { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
  481. // If this is a binary expression, return the Binary_expression
  482. // structure. Otherwise return NULL.
  483. Binary_expression*
  484. binary_expression()
  485. { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
  486. // If this is a call expression, return the Call_expression
  487. // structure. Otherwise, return NULL. This is a controlled dynamic
  488. // cast.
  489. Call_expression*
  490. call_expression()
  491. { return this->convert<Call_expression, EXPRESSION_CALL>(); }
  492. // If this is an expression which refers to a function, return the
  493. // Func_expression structure. Otherwise, return NULL.
  494. Func_expression*
  495. func_expression()
  496. { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
  497. const Func_expression*
  498. func_expression() const
  499. { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
  500. // If this is an expression which refers to an unknown name, return
  501. // the Unknown_expression structure. Otherwise, return NULL.
  502. Unknown_expression*
  503. unknown_expression()
  504. { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
  505. const Unknown_expression*
  506. unknown_expression() const
  507. {
  508. return this->convert<const Unknown_expression,
  509. EXPRESSION_UNKNOWN_REFERENCE>();
  510. }
  511. // If this is an index expression, return the Index_expression
  512. // structure. Otherwise, return NULL.
  513. Index_expression*
  514. index_expression()
  515. { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
  516. // If this is an expression which refers to indexing in a map,
  517. // return the Map_index_expression structure. Otherwise, return
  518. // NULL.
  519. Map_index_expression*
  520. map_index_expression()
  521. { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
  522. // If this is a bound method expression, return the
  523. // Bound_method_expression structure. Otherwise, return NULL.
  524. Bound_method_expression*
  525. bound_method_expression()
  526. { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
  527. // If this is a reference to a field in a struct, return the
  528. // Field_reference_expression structure. Otherwise, return NULL.
  529. Field_reference_expression*
  530. field_reference_expression()
  531. {
  532. return this->convert<Field_reference_expression,
  533. EXPRESSION_FIELD_REFERENCE>();
  534. }
  535. // If this is a reference to a field in an interface, return the
  536. // Interface_field_reference_expression structure. Otherwise,
  537. // return NULL.
  538. Interface_field_reference_expression*
  539. interface_field_reference_expression()
  540. {
  541. return this->convert<Interface_field_reference_expression,
  542. EXPRESSION_INTERFACE_FIELD_REFERENCE>();
  543. }
  544. // If this is a type guard expression, return the
  545. // Type_guard_expression structure. Otherwise, return NULL.
  546. Type_guard_expression*
  547. type_guard_expression()
  548. { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
  549. // If this is a receive expression, return the Receive_expression
  550. // structure. Otherwise, return NULL.
  551. Receive_expression*
  552. receive_expression()
  553. { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
  554. // Return true if this is a composite literal.
  555. bool
  556. is_composite_literal() const;
  557. // Return true if this is a composite literal which is not constant.
  558. bool
  559. is_nonconstant_composite_literal() const;
  560. // Return true if this is a variable or temporary variable.
  561. bool
  562. is_variable() const;
  563. // Return true if this is a reference to a local variable.
  564. bool
  565. is_local_variable() const;
  566. // Make the builtin function descriptor type, so that it can be
  567. // converted.
  568. static void
  569. make_func_descriptor_type();
  570. // Traverse an expression.
  571. static int
  572. traverse(Expression**, Traverse*);
  573. // Traverse subexpressions of this expression.
  574. int
  575. traverse_subexpressions(Traverse*);
  576. // Lower an expression. This is called immediately after parsing.
  577. // FUNCTION is the function we are in; it will be NULL for an
  578. // expression initializing a global variable. INSERTER may be used
  579. // to insert statements before the statement or initializer
  580. // containing this expression; it is normally used to create
  581. // temporary variables. IOTA_VALUE is the value that we should give
  582. // to any iota expressions. This function must resolve expressions
  583. // which could not be fully parsed into their final form. It
  584. // returns the same Expression or a new one.
  585. Expression*
  586. lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
  587. int iota_value)
  588. { return this->do_lower(gogo, function, inserter, iota_value); }
  589. // Flatten an expression. This is called after order_evaluation.
  590. // FUNCTION is the function we are in; it will be NULL for an
  591. // expression initializing a global variable. INSERTER may be used
  592. // to insert statements before the statement or initializer
  593. // containing this expression; it is normally used to create
  594. // temporary variables. This function must resolve expressions
  595. // which could not be fully parsed into their final form. It
  596. // returns the same Expression or a new one.
  597. Expression*
  598. flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
  599. { return this->do_flatten(gogo, function, inserter); }
  600. // Determine the real type of an expression with abstract integer,
  601. // floating point, or complex type. TYPE_CONTEXT describes the
  602. // expected type.
  603. void
  604. determine_type(const Type_context*);
  605. // Check types in an expression.
  606. void
  607. check_types(Gogo* gogo)
  608. { this->do_check_types(gogo); }
  609. // Determine the type when there is no context.
  610. void
  611. determine_type_no_context();
  612. // Return the current type of the expression. This may be changed
  613. // by determine_type.
  614. Type*
  615. type()
  616. { return this->do_type(); }
  617. // Return a copy of an expression.
  618. Expression*
  619. copy()
  620. { return this->do_copy(); }
  621. // Return whether the expression is addressable--something which may
  622. // be used as the operand of the unary & operator.
  623. bool
  624. is_addressable() const
  625. { return this->do_is_addressable(); }
  626. // Note that we are taking the address of this expression. ESCAPES
  627. // is true if this address escapes the current function.
  628. void
  629. address_taken(bool escapes)
  630. { this->do_address_taken(escapes); }
  631. // Note that a nil check must be issued for this expression.
  632. void
  633. issue_nil_check()
  634. { this->do_issue_nil_check(); }
  635. // Return whether this expression must be evaluated in order
  636. // according to the order of evaluation rules. This is basically
  637. // true of all expressions with side-effects.
  638. bool
  639. must_eval_in_order() const
  640. { return this->do_must_eval_in_order(); }
  641. // Return whether subexpressions of this expression must be
  642. // evaluated in order. This is true of index expressions and
  643. // pointer indirections. This sets *SKIP to the number of
  644. // subexpressions to skip during traversing, as index expressions
  645. // only requiring moving the index, not the array.
  646. bool
  647. must_eval_subexpressions_in_order(int* skip) const
  648. {
  649. *skip = 0;
  650. return this->do_must_eval_subexpressions_in_order(skip);
  651. }
  652. // Return the backend representation for this expression.
  653. Bexpression*
  654. get_backend(Translate_context*);
  655. // Return an expression handling any conversions which must be done during
  656. // assignment.
  657. static Expression*
  658. convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
  659. Location location);
  660. // Return an expression converting a value of one interface type to another
  661. // interface type. If FOR_TYPE_GUARD is true this is for a type
  662. // assertion.
  663. static Expression*
  664. convert_interface_to_interface(Type* lhs_type,
  665. Expression* rhs, bool for_type_guard,
  666. Location);
  667. // Return a backend expression implementing the comparison LEFT OP RIGHT.
  668. // TYPE is the type of both sides.
  669. static Bexpression*
  670. comparison(Translate_context*, Type* result_type, Operator op,
  671. Expression* left, Expression* right, Location);
  672. // Return the backend expression for the numeric constant VAL.
  673. static Bexpression*
  674. backend_numeric_constant_expression(Translate_context*,
  675. Numeric_constant* val);
  676. // Export the expression. This is only used for constants. It will
  677. // be used for things like values of named constants and sizes of
  678. // arrays.
  679. void
  680. export_expression(Export* exp) const
  681. { this->do_export(exp); }
  682. // Import an expression.
  683. static Expression*
  684. import_expression(Import*);
  685. // Return an expression which checks that VAL, of arbitrary integer type,
  686. // is non-negative and is not more than the maximum integer value.
  687. static Expression*
  688. check_bounds(Expression* val, Location);
  689. // Dump an expression to a dump constext.
  690. void
  691. dump_expression(Ast_dump_context*) const;
  692. protected:
  693. // May be implemented by child class: traverse the expressions.
  694. virtual int
  695. do_traverse(Traverse*);
  696. // Return a lowered expression.
  697. virtual Expression*
  698. do_lower(Gogo*, Named_object*, Statement_inserter*, int)
  699. { return this; }
  700. // Return a flattened expression.
  701. virtual Expression*
  702. do_flatten(Gogo*, Named_object*, Statement_inserter*)
  703. { return this; }
  704. // Return whether this is a constant expression.
  705. virtual bool
  706. do_is_constant() const
  707. { return false; }
  708. // Return whether this is an immutable expression.
  709. virtual bool
  710. do_is_immutable() const
  711. { return false; }
  712. // Return whether this is a constant expression of numeric type, and
  713. // set the Numeric_constant to the value.
  714. virtual bool
  715. do_numeric_constant_value(Numeric_constant*) const
  716. { return false; }
  717. // Return whether this is a constant expression of string type, and
  718. // set VAL to the value.
  719. virtual bool
  720. do_string_constant_value(std::string*) const
  721. { return false; }
  722. // Called by the parser if the value is being discarded.
  723. virtual bool
  724. do_discarding_value();
  725. // Child class holds type.
  726. virtual Type*
  727. do_type() = 0;
  728. // Child class implements determining type information.
  729. virtual void
  730. do_determine_type(const Type_context*) = 0;
  731. // Child class implements type checking if needed.
  732. virtual void
  733. do_check_types(Gogo*)
  734. { }
  735. // Child class implements copying.
  736. virtual Expression*
  737. do_copy() = 0;
  738. // Child class implements whether the expression is addressable.
  739. virtual bool
  740. do_is_addressable() const
  741. { return false; }
  742. // Child class implements taking the address of an expression.
  743. virtual void
  744. do_address_taken(bool)
  745. { }
  746. // Child class implements issuing a nil check if the address is taken.
  747. virtual void
  748. do_issue_nil_check()
  749. { }
  750. // Child class implements whether this expression must be evaluated
  751. // in order.
  752. virtual bool
  753. do_must_eval_in_order() const
  754. { return false; }
  755. // Child class implements whether this expressions requires that
  756. // subexpressions be evaluated in order. The child implementation
  757. // may set *SKIP if it should be non-zero.
  758. virtual bool
  759. do_must_eval_subexpressions_in_order(int* /* skip */) const
  760. { return false; }
  761. // Child class implements conversion to backend representation.
  762. virtual Bexpression*
  763. do_get_backend(Translate_context*) = 0;
  764. // Child class implements export.
  765. virtual void
  766. do_export(Export*) const;
  767. // For children to call to give an error for an unused value.
  768. void
  769. unused_value_error();
  770. // For children to call when they detect that they are in error.
  771. void
  772. set_is_error();
  773. // For children to call to report an error conveniently.
  774. void
  775. report_error(const char*);
  776. // Child class implements dumping to a dump context.
  777. virtual void
  778. do_dump_expression(Ast_dump_context*) const = 0;
  779. private:
  780. // Convert to the desired statement classification, or return NULL.
  781. // This is a controlled dynamic cast.
  782. template<typename Expression_class,
  783. Expression_classification expr_classification>
  784. Expression_class*
  785. convert()
  786. {
  787. return (this->classification_ == expr_classification
  788. ? static_cast<Expression_class*>(this)
  789. : NULL);
  790. }
  791. template<typename Expression_class,
  792. Expression_classification expr_classification>
  793. const Expression_class*
  794. convert() const
  795. {
  796. return (this->classification_ == expr_classification
  797. ? static_cast<const Expression_class*>(this)
  798. : NULL);
  799. }
  800. static Expression*
  801. convert_type_to_interface(Type*, Expression*, Location);
  802. static Expression*
  803. get_interface_type_descriptor(Expression*);
  804. static Expression*
  805. convert_interface_to_type(Type*, Expression*, Location);
  806. // The expression classification.
  807. Expression_classification classification_;
  808. // The location in the input file.
  809. Location location_;
  810. };
  811. // A list of Expressions.
  812. class Expression_list
  813. {
  814. public:
  815. Expression_list()
  816. : entries_()
  817. { }
  818. // Return whether the list is empty.
  819. bool
  820. empty() const
  821. { return this->entries_.empty(); }
  822. // Return the number of entries in the list.
  823. size_t
  824. size() const
  825. { return this->entries_.size(); }
  826. // Add an entry to the end of the list.
  827. void
  828. push_back(Expression* expr)
  829. { this->entries_.push_back(expr); }
  830. void
  831. append(Expression_list* add)
  832. { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
  833. // Reserve space in the list.
  834. void
  835. reserve(size_t size)
  836. { this->entries_.reserve(size); }
  837. // Traverse the expressions in the list.
  838. int
  839. traverse(Traverse*);
  840. // Copy the list.
  841. Expression_list*
  842. copy();
  843. // Return true if the list contains an error expression.
  844. bool
  845. contains_error() const;
  846. // Retrieve an element by index.
  847. Expression*&
  848. at(size_t i)
  849. { return this->entries_.at(i); }
  850. // Return the first and last elements.
  851. Expression*&
  852. front()
  853. { return this->entries_.front(); }
  854. Expression*
  855. front() const
  856. { return this->entries_.front(); }
  857. Expression*&
  858. back()
  859. { return this->entries_.back(); }
  860. Expression*
  861. back() const
  862. { return this->entries_.back(); }
  863. // Iterators.
  864. typedef std::vector<Expression*>::iterator iterator;
  865. typedef std::vector<Expression*>::const_iterator const_iterator;
  866. iterator
  867. begin()
  868. { return this->entries_.begin(); }
  869. const_iterator
  870. begin() const
  871. { return this->entries_.begin(); }
  872. iterator
  873. end()
  874. { return this->entries_.end(); }
  875. const_iterator
  876. end() const
  877. { return this->entries_.end(); }
  878. // Erase an entry.
  879. void
  880. erase(iterator p)
  881. { this->entries_.erase(p); }
  882. private:
  883. std::vector<Expression*> entries_;
  884. };
  885. // An abstract base class for an expression which is only used by the
  886. // parser, and is lowered in the lowering pass.
  887. class Parser_expression : public Expression
  888. {
  889. public:
  890. Parser_expression(Expression_classification classification,
  891. Location location)
  892. : Expression(classification, location)
  893. { }
  894. protected:
  895. virtual Expression*
  896. do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
  897. Type*
  898. do_type();
  899. void
  900. do_determine_type(const Type_context*)
  901. { go_unreachable(); }
  902. void
  903. do_check_types(Gogo*)
  904. { go_unreachable(); }
  905. Bexpression*
  906. do_get_backend(Translate_context*)
  907. { go_unreachable(); }
  908. };
  909. // An expression which is simply a variable.
  910. class Var_expression : public Expression
  911. {
  912. public:
  913. Var_expression(Named_object* variable, Location location)
  914. : Expression(EXPRESSION_VAR_REFERENCE, location),
  915. variable_(variable)
  916. { }
  917. // Return the variable.
  918. Named_object*
  919. named_object() const
  920. { return this->variable_; }
  921. protected:
  922. Expression*
  923. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  924. Type*
  925. do_type();
  926. void
  927. do_determine_type(const Type_context*);
  928. Expression*
  929. do_copy()
  930. { return this; }
  931. bool
  932. do_is_addressable() const
  933. { return true; }
  934. void
  935. do_address_taken(bool);
  936. Bexpression*
  937. do_get_backend(Translate_context*);
  938. void
  939. do_dump_expression(Ast_dump_context*) const;
  940. private:
  941. // The variable we are referencing.
  942. Named_object* variable_;
  943. };
  944. // A reference to a temporary variable.
  945. class Temporary_reference_expression : public Expression
  946. {
  947. public:
  948. Temporary_reference_expression(Temporary_statement* statement,
  949. Location location)
  950. : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
  951. statement_(statement), is_lvalue_(false)
  952. { }
  953. // The temporary that this expression refers to.
  954. Temporary_statement*
  955. statement() const
  956. { return this->statement_; }
  957. // Indicate that this reference appears on the left hand side of an
  958. // assignment statement.
  959. void
  960. set_is_lvalue()
  961. { this->is_lvalue_ = true; }
  962. protected:
  963. Type*
  964. do_type();
  965. void
  966. do_determine_type(const Type_context*)
  967. { }
  968. Expression*
  969. do_copy()
  970. { return make_temporary_reference(this->statement_, this->location()); }
  971. bool
  972. do_is_addressable() const
  973. { return true; }
  974. void
  975. do_address_taken(bool);
  976. Bexpression*
  977. do_get_backend(Translate_context*);
  978. void
  979. do_dump_expression(Ast_dump_context*) const;
  980. private:
  981. // The statement where the temporary variable is defined.
  982. Temporary_statement* statement_;
  983. // Whether this reference appears on the left hand side of an
  984. // assignment statement.
  985. bool is_lvalue_;
  986. };
  987. // Set and use a temporary variable.
  988. class Set_and_use_temporary_expression : public Expression
  989. {
  990. public:
  991. Set_and_use_temporary_expression(Temporary_statement* statement,
  992. Expression* expr, Location location)
  993. : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
  994. statement_(statement), expr_(expr)
  995. { }
  996. // Return the temporary.
  997. Temporary_statement*
  998. temporary() const
  999. { return this->statement_; }
  1000. // Return the expression.
  1001. Expression*
  1002. expression() const
  1003. { return this->expr_; }
  1004. protected:
  1005. int
  1006. do_traverse(Traverse* traverse)
  1007. { return Expression::traverse(&this->expr_, traverse); }
  1008. Type*
  1009. do_type();
  1010. void
  1011. do_determine_type(const Type_context*);
  1012. Expression*
  1013. do_copy()
  1014. {
  1015. return make_set_and_use_temporary(this->statement_, this->expr_,
  1016. this->location());
  1017. }
  1018. bool
  1019. do_is_addressable() const
  1020. { return true; }
  1021. void
  1022. do_address_taken(bool);
  1023. Bexpression*
  1024. do_get_backend(Translate_context*);
  1025. void
  1026. do_dump_expression(Ast_dump_context*) const;
  1027. private:
  1028. // The statement where the temporary variable is defined.
  1029. Temporary_statement* statement_;
  1030. // The expression to assign to the temporary.
  1031. Expression* expr_;
  1032. };
  1033. // A string expression.
  1034. class String_expression : public Expression
  1035. {
  1036. public:
  1037. String_expression(const std::string& val, Location location)
  1038. : Expression(EXPRESSION_STRING, location),
  1039. val_(val), type_(NULL)
  1040. { }
  1041. const std::string&
  1042. val() const
  1043. { return this->val_; }
  1044. static Expression*
  1045. do_import(Import*);
  1046. protected:
  1047. bool
  1048. do_is_constant() const
  1049. { return true; }
  1050. bool
  1051. do_is_immutable() const
  1052. { return true; }
  1053. bool
  1054. do_string_constant_value(std::string* val) const
  1055. {
  1056. *val = this->val_;
  1057. return true;
  1058. }
  1059. Type*
  1060. do_type();
  1061. void
  1062. do_determine_type(const Type_context*);
  1063. Expression*
  1064. do_copy()
  1065. { return this; }
  1066. Bexpression*
  1067. do_get_backend(Translate_context*);
  1068. // Write string literal to a string dump.
  1069. static void
  1070. export_string(String_dump* exp, const String_expression* str);
  1071. void
  1072. do_export(Export*) const;
  1073. void
  1074. do_dump_expression(Ast_dump_context*) const;
  1075. private:
  1076. // The string value. This is immutable.
  1077. const std::string val_;
  1078. // The type as determined by context.
  1079. Type* type_;
  1080. };
  1081. // A Unary expression.
  1082. class Unary_expression : public Expression
  1083. {
  1084. public:
  1085. Unary_expression(Operator op, Expression* expr, Location location)
  1086. : Expression(EXPRESSION_UNARY, location),
  1087. op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
  1088. is_slice_init_(false), expr_(expr), issue_nil_check_(false)
  1089. { }
  1090. // Return the operator.
  1091. Operator
  1092. op() const
  1093. { return this->op_; }
  1094. // Return the operand.
  1095. Expression*
  1096. operand() const
  1097. { return this->expr_; }
  1098. // Record that an address expression does not escape.
  1099. void
  1100. set_does_not_escape()
  1101. {
  1102. go_assert(this->op_ == OPERATOR_AND);
  1103. this->escapes_ = false;
  1104. }
  1105. // Record that this is an address expression which should create a
  1106. // temporary variable if necessary. This is used for method calls.
  1107. void
  1108. set_create_temp()
  1109. {
  1110. go_assert(this->op_ == OPERATOR_AND);
  1111. this->create_temp_ = true;
  1112. }
  1113. // Record that this is an address expression of a GC root, which is a
  1114. // mutable composite literal. This used for registering GC variables.
  1115. void
  1116. set_is_gc_root()
  1117. {
  1118. go_assert(this->op_ == OPERATOR_AND);
  1119. this->is_gc_root_ = true;
  1120. }
  1121. // Record that this is an address expression of a slice value initializer,
  1122. // which is mutable if the values are not copied to the heap.
  1123. void
  1124. set_is_slice_init()
  1125. {
  1126. go_assert(this->op_ == OPERATOR_AND);
  1127. this->is_slice_init_ = true;
  1128. }
  1129. // Apply unary opcode OP to UNC, setting NC. Return true if this
  1130. // could be done, false if not. Issue errors for overflow.
  1131. static bool
  1132. eval_constant(Operator op, const Numeric_constant* unc,
  1133. Location, Numeric_constant* nc);
  1134. static Expression*
  1135. do_import(Import*);
  1136. protected:
  1137. int
  1138. do_traverse(Traverse* traverse)
  1139. { return Expression::traverse(&this->expr_, traverse); }
  1140. Expression*
  1141. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1142. Expression*
  1143. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  1144. bool
  1145. do_is_constant() const;
  1146. bool
  1147. do_is_immutable() const
  1148. {
  1149. return (this->expr_->is_immutable()
  1150. || (this->op_ == OPERATOR_AND && this->expr_->is_variable()));
  1151. }
  1152. bool
  1153. do_numeric_constant_value(Numeric_constant*) const;
  1154. Type*
  1155. do_type();
  1156. void
  1157. do_determine_type(const Type_context*);
  1158. void
  1159. do_check_types(Gogo*);
  1160. Expression*
  1161. do_copy()
  1162. {
  1163. return Expression::make_unary(this->op_, this->expr_->copy(),
  1164. this->location());
  1165. }
  1166. bool
  1167. do_must_eval_subexpressions_in_order(int*) const
  1168. { return this->op_ == OPERATOR_MULT; }
  1169. bool
  1170. do_is_addressable() const
  1171. { return this->op_ == OPERATOR_MULT; }
  1172. Bexpression*
  1173. do_get_backend(Translate_context*);
  1174. void
  1175. do_export(Export*) const;
  1176. void
  1177. do_dump_expression(Ast_dump_context*) const;
  1178. void
  1179. do_issue_nil_check()
  1180. { this->issue_nil_check_ = (this->op_ == OPERATOR_MULT); }
  1181. private:
  1182. // The unary operator to apply.
  1183. Operator op_;
  1184. // Normally true. False if this is an address expression which does
  1185. // not escape the current function.
  1186. bool escapes_;
  1187. // True if this is an address expression which should create a
  1188. // temporary variable if necessary.
  1189. bool create_temp_;
  1190. // True if this is an address expression for a GC root. A GC root is a
  1191. // special struct composite literal that is mutable when addressed, meaning
  1192. // it cannot be represented as an immutable_struct in the backend.
  1193. bool is_gc_root_;
  1194. // True if this is an address expression for a slice value with an immutable
  1195. // initializer. The initializer for a slice's value pointer has an array
  1196. // type, meaning it cannot be represented as an immutable_struct in the
  1197. // backend.
  1198. bool is_slice_init_;
  1199. // The operand.
  1200. Expression* expr_;
  1201. // Whether or not to issue a nil check for this expression if its address
  1202. // is being taken.
  1203. bool issue_nil_check_;
  1204. };
  1205. // A binary expression.
  1206. class Binary_expression : public Expression
  1207. {
  1208. public:
  1209. Binary_expression(Operator op, Expression* left, Expression* right,
  1210. Location location)
  1211. : Expression(EXPRESSION_BINARY, location),
  1212. op_(op), left_(left), right_(right), type_(NULL)
  1213. { }
  1214. // Return the operator.
  1215. Operator
  1216. op()
  1217. { return this->op_; }
  1218. // Return the left hand expression.
  1219. Expression*
  1220. left()
  1221. { return this->left_; }
  1222. // Return the right hand expression.
  1223. Expression*
  1224. right()
  1225. { return this->right_; }
  1226. // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
  1227. // Return true if this could be done, false if not. Issue errors at
  1228. // LOCATION as appropriate.
  1229. static bool
  1230. eval_constant(Operator op, Numeric_constant* left_nc,
  1231. Numeric_constant* right_nc, Location location,
  1232. Numeric_constant* nc);
  1233. // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
  1234. // *RESULT. Return true if this could be done, false if not. Issue
  1235. // errors at LOCATION as appropriate.
  1236. static bool
  1237. compare_constant(Operator op, Numeric_constant* left_nc,
  1238. Numeric_constant* right_nc, Location location,
  1239. bool* result);
  1240. static Expression*
  1241. do_import(Import*);
  1242. // Report an error if OP can not be applied to TYPE. Return whether
  1243. // it can. OTYPE is the type of the other operand.
  1244. static bool
  1245. check_operator_type(Operator op, Type* type, Type* otype, Location);
  1246. protected:
  1247. int
  1248. do_traverse(Traverse* traverse);
  1249. Expression*
  1250. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1251. Expression*
  1252. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  1253. bool
  1254. do_is_constant() const
  1255. { return this->left_->is_constant() && this->right_->is_constant(); }
  1256. bool
  1257. do_is_immutable() const
  1258. { return this->left_->is_immutable() && this->right_->is_immutable(); }
  1259. bool
  1260. do_numeric_constant_value(Numeric_constant*) const;
  1261. bool
  1262. do_discarding_value();
  1263. Type*
  1264. do_type();
  1265. void
  1266. do_determine_type(const Type_context*);
  1267. void
  1268. do_check_types(Gogo*);
  1269. Expression*
  1270. do_copy()
  1271. {
  1272. return Expression::make_binary(this->op_, this->left_->copy(),
  1273. this->right_->copy(), this->location());
  1274. }
  1275. Bexpression*
  1276. do_get_backend(Translate_context*);
  1277. void
  1278. do_export(Export*) const;
  1279. void
  1280. do_dump_expression(Ast_dump_context*) const;
  1281. private:
  1282. static bool
  1283. operation_type(Operator op, Type* left_type, Type* right_type,
  1284. Type** result_type);
  1285. static bool
  1286. cmp_to_bool(Operator op, int cmp);
  1287. static bool
  1288. eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
  1289. Location, Numeric_constant*);
  1290. static bool
  1291. eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
  1292. Location, Numeric_constant*);
  1293. static bool
  1294. eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
  1295. Location, Numeric_constant*);
  1296. static bool
  1297. compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
  1298. static bool
  1299. compare_float(const Numeric_constant*, const Numeric_constant *, int*);
  1300. static bool
  1301. compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
  1302. Expression*
  1303. lower_struct_comparison(Gogo*, Statement_inserter*);
  1304. Expression*
  1305. lower_array_comparison(Gogo*, Statement_inserter*);
  1306. Expression*
  1307. lower_interface_value_comparison(Gogo*, Statement_inserter*);
  1308. Expression*
  1309. lower_compare_to_memcmp(Gogo*, Statement_inserter*);
  1310. Expression*
  1311. operand_address(Statement_inserter*, Expression*);
  1312. // The binary operator to apply.
  1313. Operator op_;
  1314. // The left hand side operand.
  1315. Expression* left_;
  1316. // The right hand side operand.
  1317. Expression* right_;
  1318. // The type of a comparison operation.
  1319. Type* type_;
  1320. };
  1321. // A call expression. The go statement needs to dig inside this.
  1322. class Call_expression : public Expression
  1323. {
  1324. public:
  1325. Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
  1326. Location location)
  1327. : Expression(EXPRESSION_CALL, location),
  1328. fn_(fn), args_(args), type_(NULL), results_(NULL), call_(NULL),
  1329. call_temp_(NULL), expected_result_count_(0), is_varargs_(is_varargs),
  1330. varargs_are_lowered_(false), types_are_determined_(false),
  1331. is_deferred_(false), issued_error_(false), is_multi_value_arg_(false),
  1332. is_flattened_(false)
  1333. { }
  1334. // The function to call.
  1335. Expression*
  1336. fn() const
  1337. { return this->fn_; }
  1338. // The arguments.
  1339. Expression_list*
  1340. args()
  1341. { return this->args_; }
  1342. const Expression_list*
  1343. args() const
  1344. { return this->args_; }
  1345. // Get the function type.
  1346. Function_type*
  1347. get_function_type() const;
  1348. // Return the number of values this call will return.
  1349. size_t
  1350. result_count() const;
  1351. // Return the temporary variable which holds result I. This is only
  1352. // valid after the expression has been lowered, and is only valid
  1353. // for calls which return multiple results.
  1354. Temporary_statement*
  1355. result(size_t i) const;
  1356. // Set the number of results expected from this call. This is used
  1357. // when the call appears in a context that expects multiple results,
  1358. // such as a, b = f().
  1359. void
  1360. set_expected_result_count(size_t);
  1361. // Return whether this is a call to the predeclared function
  1362. // recover.
  1363. bool
  1364. is_recover_call() const;
  1365. // Set the argument for a call to recover.
  1366. void
  1367. set_recover_arg(Expression*);
  1368. // Whether the last argument is a varargs argument (f(a...)).
  1369. bool
  1370. is_varargs() const
  1371. { return this->is_varargs_; }
  1372. // Return whether varargs have already been lowered.
  1373. bool
  1374. varargs_are_lowered() const
  1375. { return this->varargs_are_lowered_; }
  1376. // Note that varargs have already been lowered.
  1377. void
  1378. set_varargs_are_lowered()
  1379. { this->varargs_are_lowered_ = true; }
  1380. // Whether this call is being deferred.
  1381. bool
  1382. is_deferred() const
  1383. { return this->is_deferred_; }
  1384. // Note that the call is being deferred.
  1385. void
  1386. set_is_deferred()
  1387. { this->is_deferred_ = true; }
  1388. // We have found an error with this call expression; return true if
  1389. // we should report it.
  1390. bool
  1391. issue_error();
  1392. // Whether this call returns multiple results that are used as an
  1393. // multi-valued argument.
  1394. bool
  1395. is_multi_value_arg() const
  1396. { return this->is_multi_value_arg_; }
  1397. // Note this call is used as a multi-valued argument.
  1398. void
  1399. set_is_multi_value_arg()
  1400. { this->is_multi_value_arg_ = true; }
  1401. protected:
  1402. int
  1403. do_traverse(Traverse*);
  1404. virtual Expression*
  1405. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1406. virtual Expression*
  1407. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  1408. bool
  1409. do_discarding_value()
  1410. { return true; }
  1411. virtual Type*
  1412. do_type();
  1413. virtual void
  1414. do_determine_type(const Type_context*);
  1415. virtual void
  1416. do_check_types(Gogo*);
  1417. Expression*
  1418. do_copy();
  1419. bool
  1420. do_must_eval_in_order() const;
  1421. virtual Bexpression*
  1422. do_get_backend(Translate_context*);
  1423. virtual bool
  1424. do_is_recover_call() const;
  1425. virtual void
  1426. do_set_recover_arg(Expression*);
  1427. // Let a builtin expression change the argument list.
  1428. void
  1429. set_args(Expression_list* args)
  1430. { this->args_ = args; }
  1431. // Let a builtin expression lower varargs.
  1432. void
  1433. lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
  1434. Type* varargs_type, size_t param_count);
  1435. // Let a builtin expression check whether types have been
  1436. // determined.
  1437. bool
  1438. determining_types();
  1439. void
  1440. do_dump_expression(Ast_dump_context*) const;
  1441. private:
  1442. bool
  1443. check_argument_type(int, const Type*, const Type*, Location, bool);
  1444. Expression*
  1445. interface_method_function(Interface_field_reference_expression*,
  1446. Expression**);
  1447. Bexpression*
  1448. set_results(Translate_context*, Bexpression*);
  1449. // The function to call.
  1450. Expression* fn_;
  1451. // The arguments to pass. This may be NULL if there are no
  1452. // arguments.
  1453. Expression_list* args_;
  1454. // The type of the expression, to avoid recomputing it.
  1455. Type* type_;
  1456. // The list of temporaries which will hold the results if the
  1457. // function returns a tuple.
  1458. std::vector<Temporary_statement*>* results_;
  1459. // The backend expression for the call, used for a call which returns a tuple.
  1460. Bexpression* call_;
  1461. // A temporary variable to store this call if the function returns a tuple.
  1462. Temporary_statement* call_temp_;
  1463. // If not 0, the number of results expected from this call, when
  1464. // used in a context that expects multiple values.
  1465. size_t expected_result_count_;
  1466. // True if the last argument is a varargs argument (f(a...)).
  1467. bool is_varargs_;
  1468. // True if varargs have already been lowered.
  1469. bool varargs_are_lowered_;
  1470. // True if types have been determined.
  1471. bool types_are_determined_;
  1472. // True if the call is an argument to a defer statement.
  1473. bool is_deferred_;
  1474. // True if we reported an error about a mismatch between call
  1475. // results and uses. This is to avoid producing multiple errors
  1476. // when there are multiple Call_result_expressions.
  1477. bool issued_error_;
  1478. // True if this call is used as an argument that returns multiple results.
  1479. bool is_multi_value_arg_;
  1480. // True if this expression has already been flattened.
  1481. bool is_flattened_;
  1482. };
  1483. // An expression which represents a pointer to a function.
  1484. class Func_expression : public Expression
  1485. {
  1486. public:
  1487. Func_expression(Named_object* function, Expression* closure,
  1488. Location location)
  1489. : Expression(EXPRESSION_FUNC_REFERENCE, location),
  1490. function_(function), closure_(closure)
  1491. { }
  1492. // Return the object associated with the function.
  1493. Named_object*
  1494. named_object() const
  1495. { return this->function_; }
  1496. // Return the closure for this function. This will return NULL if
  1497. // the function has no closure, which is the normal case.
  1498. Expression*
  1499. closure()
  1500. { return this->closure_; }
  1501. // Return a backend expression for the code of a function.
  1502. static Bexpression*
  1503. get_code_pointer(Gogo*, Named_object* function, Location loc);
  1504. protected:
  1505. int
  1506. do_traverse(Traverse*);
  1507. Type*
  1508. do_type();
  1509. void
  1510. do_determine_type(const Type_context*)
  1511. {
  1512. if (this->closure_ != NULL)
  1513. this->closure_->determine_type_no_context();
  1514. }
  1515. Expression*
  1516. do_copy()
  1517. {
  1518. return Expression::make_func_reference(this->function_,
  1519. (this->closure_ == NULL
  1520. ? NULL
  1521. : this->closure_->copy()),
  1522. this->location());
  1523. }
  1524. Bexpression*
  1525. do_get_backend(Translate_context*);
  1526. void
  1527. do_dump_expression(Ast_dump_context*) const;
  1528. private:
  1529. // The function itself.
  1530. Named_object* function_;
  1531. // A closure. This is normally NULL. For a nested function, it may
  1532. // be a struct holding pointers to all the variables referenced by
  1533. // this function and defined in enclosing functions.
  1534. Expression* closure_;
  1535. };
  1536. // A function descriptor. A function descriptor is a struct with a
  1537. // single field pointing to the function code. This is used for
  1538. // functions without closures.
  1539. class Func_descriptor_expression : public Expression
  1540. {
  1541. public:
  1542. Func_descriptor_expression(Named_object* fn);
  1543. // Make the function descriptor type, so that it can be converted.
  1544. static void
  1545. make_func_descriptor_type();
  1546. protected:
  1547. int
  1548. do_traverse(Traverse*);
  1549. Type*
  1550. do_type();
  1551. void
  1552. do_determine_type(const Type_context*)
  1553. { }
  1554. Expression*
  1555. do_copy()
  1556. { return Expression::make_func_descriptor(this->fn_); }
  1557. bool
  1558. do_is_addressable() const
  1559. { return true; }
  1560. Bexpression*
  1561. do_get_backend(Translate_context*);
  1562. void
  1563. do_dump_expression(Ast_dump_context* context) const;
  1564. private:
  1565. // The type of all function descriptors.
  1566. static Type* descriptor_type;
  1567. // The function for which this is the descriptor.
  1568. Named_object* fn_;
  1569. // The descriptor variable.
  1570. Bvariable* dvar_;
  1571. };
  1572. // A reference to an unknown name.
  1573. class Unknown_expression : public Parser_expression
  1574. {
  1575. public:
  1576. Unknown_expression(Named_object* named_object, Location location)
  1577. : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
  1578. named_object_(named_object), no_error_message_(false),
  1579. is_composite_literal_key_(false)
  1580. { }
  1581. // The associated named object.
  1582. Named_object*
  1583. named_object() const
  1584. { return this->named_object_; }
  1585. // The name of the identifier which was unknown.
  1586. const std::string&
  1587. name() const;
  1588. // Call this to indicate that we should not give an error if this
  1589. // name is never defined. This is used to avoid knock-on errors
  1590. // during an erroneous parse.
  1591. void
  1592. set_no_error_message()
  1593. { this->no_error_message_ = true; }
  1594. // Note that this expression is being used as the key in a composite
  1595. // literal, so it may be OK if it is not resolved.
  1596. void
  1597. set_is_composite_literal_key()
  1598. { this->is_composite_literal_key_ = true; }
  1599. // Note that this expression should no longer be treated as a
  1600. // composite literal key.
  1601. void
  1602. clear_is_composite_literal_key()
  1603. { this->is_composite_literal_key_ = false; }
  1604. protected:
  1605. Expression*
  1606. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1607. Expression*
  1608. do_copy()
  1609. { return new Unknown_expression(this->named_object_, this->location()); }
  1610. void
  1611. do_dump_expression(Ast_dump_context*) const;
  1612. private:
  1613. // The unknown name.
  1614. Named_object* named_object_;
  1615. // True if we should not give errors if this is undefined. This is
  1616. // used if there was a parse failure.
  1617. bool no_error_message_;
  1618. // True if this is the key in a composite literal.
  1619. bool is_composite_literal_key_;
  1620. };
  1621. // An index expression. This is lowered to an array index, a string
  1622. // index, or a map index.
  1623. class Index_expression : public Parser_expression
  1624. {
  1625. public:
  1626. Index_expression(Expression* left, Expression* start, Expression* end,
  1627. Expression* cap, Location location)
  1628. : Parser_expression(EXPRESSION_INDEX, location),
  1629. left_(left), start_(start), end_(end), cap_(cap), is_lvalue_(false)
  1630. { }
  1631. // Record that this expression is an lvalue.
  1632. void
  1633. set_is_lvalue()
  1634. { this->is_lvalue_ = true; }
  1635. // Dump an index expression, i.e. an expression of the form
  1636. // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
  1637. static void
  1638. dump_index_expression(Ast_dump_context*, const Expression* expr,
  1639. const Expression* start, const Expression* end,
  1640. const Expression* cap);
  1641. protected:
  1642. int
  1643. do_traverse(Traverse*);
  1644. Expression*
  1645. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1646. Expression*
  1647. do_copy()
  1648. {
  1649. return new Index_expression(this->left_->copy(), this->start_->copy(),
  1650. (this->end_ == NULL
  1651. ? NULL
  1652. : this->end_->copy()),
  1653. (this->cap_ == NULL
  1654. ? NULL
  1655. : this->cap_->copy()),
  1656. this->location());
  1657. }
  1658. bool
  1659. do_must_eval_subexpressions_in_order(int* skip) const
  1660. {
  1661. *skip = 1;
  1662. return true;
  1663. }
  1664. void
  1665. do_dump_expression(Ast_dump_context*) const;
  1666. void
  1667. do_issue_nil_check()
  1668. { this->left_->issue_nil_check(); }
  1669. private:
  1670. // The expression being indexed.
  1671. Expression* left_;
  1672. // The first index.
  1673. Expression* start_;
  1674. // The second index. This is NULL for an index, non-NULL for a
  1675. // slice.
  1676. Expression* end_;
  1677. // The capacity argument. This is NULL for indices and slices that use the
  1678. // default capacity, non-NULL for indices and slices that specify the
  1679. // capacity.
  1680. Expression* cap_;
  1681. // Whether this is being used as an l-value. We set this during the
  1682. // parse because map index expressions need to know.
  1683. bool is_lvalue_;
  1684. };
  1685. // An index into a map.
  1686. class Map_index_expression : public Expression
  1687. {
  1688. public:
  1689. Map_index_expression(Expression* map, Expression* index,
  1690. Location location)
  1691. : Expression(EXPRESSION_MAP_INDEX, location),
  1692. map_(map), index_(index), is_lvalue_(false),
  1693. is_in_tuple_assignment_(false), value_pointer_(NULL)
  1694. { }
  1695. // Return the map.
  1696. Expression*
  1697. map()
  1698. { return this->map_; }
  1699. const Expression*
  1700. map() const
  1701. { return this->map_; }
  1702. // Return the index.
  1703. Expression*
  1704. index()
  1705. { return this->index_; }
  1706. const Expression*
  1707. index() const
  1708. { return this->index_; }
  1709. // Get the type of the map being indexed.
  1710. Map_type*
  1711. get_map_type() const;
  1712. // Record that this map expression is an lvalue. The difference is
  1713. // that an lvalue always inserts the key.
  1714. void
  1715. set_is_lvalue()
  1716. { this->is_lvalue_ = true; }
  1717. // Return whether this map expression occurs in an assignment to a
  1718. // pair of values.
  1719. bool
  1720. is_in_tuple_assignment() const
  1721. { return this->is_in_tuple_assignment_; }
  1722. // Record that this map expression occurs in an assignment to a pair
  1723. // of values.
  1724. void
  1725. set_is_in_tuple_assignment()
  1726. { this->is_in_tuple_assignment_ = true; }
  1727. // Return an expression for the map index. This returns an expression which
  1728. // evaluates to a pointer to a value in the map. If INSERT is true,
  1729. // the key will be inserted if not present, and the value pointer
  1730. // will be zero initialized. If INSERT is false, and the key is not
  1731. // present in the map, the pointer will be NULL.
  1732. Expression*
  1733. get_value_pointer(bool insert);
  1734. protected:
  1735. int
  1736. do_traverse(Traverse*);
  1737. Expression*
  1738. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  1739. Type*
  1740. do_type();
  1741. void
  1742. do_determine_type(const Type_context*);
  1743. void
  1744. do_check_types(Gogo*);
  1745. Expression*
  1746. do_copy()
  1747. {
  1748. return Expression::make_map_index(this->map_->copy(),
  1749. this->index_->copy(),
  1750. this->location());
  1751. }
  1752. bool
  1753. do_must_eval_subexpressions_in_order(int* skip) const
  1754. {
  1755. *skip = 1;
  1756. return true;
  1757. }
  1758. // A map index expression is an lvalue but it is not addressable.
  1759. Bexpression*
  1760. do_get_backend(Translate_context*);
  1761. void
  1762. do_dump_expression(Ast_dump_context*) const;
  1763. private:
  1764. // The map we are looking into.
  1765. Expression* map_;
  1766. // The index.
  1767. Expression* index_;
  1768. // Whether this is an lvalue.
  1769. bool is_lvalue_;
  1770. // Whether this is in a tuple assignment to a pair of values.
  1771. bool is_in_tuple_assignment_;
  1772. // A pointer to the value at this index.
  1773. Expression* value_pointer_;
  1774. };
  1775. // An expression which represents a method bound to its first
  1776. // argument.
  1777. class Bound_method_expression : public Expression
  1778. {
  1779. public:
  1780. Bound_method_expression(Expression* expr, const Method *method,
  1781. Named_object* function, Location location)
  1782. : Expression(EXPRESSION_BOUND_METHOD, location),
  1783. expr_(expr), expr_type_(NULL), method_(method), function_(function)
  1784. { }
  1785. // Return the object which is the first argument.
  1786. Expression*
  1787. first_argument()
  1788. { return this->expr_; }
  1789. // Return the implicit type of the first argument. This will be
  1790. // non-NULL when using a method from an anonymous field without
  1791. // using an explicit stub.
  1792. Type*
  1793. first_argument_type() const
  1794. { return this->expr_type_; }
  1795. // Return the method.
  1796. const Method*
  1797. method() const
  1798. { return this->method_; }
  1799. // Return the function to call.
  1800. Named_object*
  1801. function() const
  1802. { return this->function_; }
  1803. // Set the implicit type of the expression.
  1804. void
  1805. set_first_argument_type(Type* type)
  1806. { this->expr_type_ = type; }
  1807. // Create a thunk to call FUNCTION, for METHOD, when it is used as
  1808. // part of a method value.
  1809. static Named_object*
  1810. create_thunk(Gogo*, const Method* method, Named_object* function);
  1811. protected:
  1812. int
  1813. do_traverse(Traverse*);
  1814. Expression*
  1815. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1816. Type*
  1817. do_type();
  1818. void
  1819. do_determine_type(const Type_context*);
  1820. void
  1821. do_check_types(Gogo*);
  1822. Expression*
  1823. do_copy()
  1824. {
  1825. return new Bound_method_expression(this->expr_->copy(), this->method_,
  1826. this->function_, this->location());
  1827. }
  1828. Bexpression*
  1829. do_get_backend(Translate_context*);
  1830. void
  1831. do_dump_expression(Ast_dump_context*) const;
  1832. private:
  1833. // A mapping from method functions to the thunks we have created for
  1834. // them.
  1835. typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
  1836. static Method_value_thunks method_value_thunks;
  1837. // The object used to find the method. This is passed to the method
  1838. // as the first argument.
  1839. Expression* expr_;
  1840. // The implicit type of the object to pass to the method. This is
  1841. // NULL in the normal case, non-NULL when using a method from an
  1842. // anonymous field which does not require a stub.
  1843. Type* expr_type_;
  1844. // The method.
  1845. const Method* method_;
  1846. // The function to call. This is not the same as
  1847. // method_->named_object() when the method has a stub. This will be
  1848. // the real function rather than the stub.
  1849. Named_object* function_;
  1850. };
  1851. // A reference to a field in a struct.
  1852. class Field_reference_expression : public Expression
  1853. {
  1854. public:
  1855. Field_reference_expression(Expression* expr, unsigned int field_index,
  1856. Location location)
  1857. : Expression(EXPRESSION_FIELD_REFERENCE, location),
  1858. expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
  1859. { }
  1860. // Return the struct expression.
  1861. Expression*
  1862. expr() const
  1863. { return this->expr_; }
  1864. // Return the field index.
  1865. unsigned int
  1866. field_index() const
  1867. { return this->field_index_; }
  1868. // Return whether this node was implied by an anonymous field.
  1869. bool
  1870. implicit() const
  1871. { return this->implicit_; }
  1872. void
  1873. set_implicit(bool implicit)
  1874. { this->implicit_ = implicit; }
  1875. // Set the struct expression. This is used when parsing.
  1876. void
  1877. set_struct_expression(Expression* expr)
  1878. {
  1879. go_assert(this->expr_ == NULL);
  1880. this->expr_ = expr;
  1881. }
  1882. protected:
  1883. int
  1884. do_traverse(Traverse* traverse)
  1885. { return Expression::traverse(&this->expr_, traverse); }
  1886. Expression*
  1887. do_lower(Gogo*, Named_object*, Statement_inserter*, int);
  1888. Type*
  1889. do_type();
  1890. void
  1891. do_determine_type(const Type_context*)
  1892. { this->expr_->determine_type_no_context(); }
  1893. void
  1894. do_check_types(Gogo*);
  1895. Expression*
  1896. do_copy()
  1897. {
  1898. return Expression::make_field_reference(this->expr_->copy(),
  1899. this->field_index_,
  1900. this->location());
  1901. }
  1902. bool
  1903. do_is_addressable() const
  1904. { return this->expr_->is_addressable(); }
  1905. void
  1906. do_address_taken(bool escapes)
  1907. { this->expr_->address_taken(escapes); }
  1908. void
  1909. do_issue_nil_check()
  1910. { this->expr_->issue_nil_check(); }
  1911. Bexpression*
  1912. do_get_backend(Translate_context*);
  1913. void
  1914. do_dump_expression(Ast_dump_context*) const;
  1915. private:
  1916. // The expression we are looking into. This should have a type of
  1917. // struct.
  1918. Expression* expr_;
  1919. // The zero-based index of the field we are retrieving.
  1920. unsigned int field_index_;
  1921. // Whether this node was emitted implicitly for an embedded field,
  1922. // that is, expr_ is not the expr_ of the original user node.
  1923. bool implicit_;
  1924. // Whether we have already emitted a fieldtrack call.
  1925. bool called_fieldtrack_;
  1926. };
  1927. // A reference to a field of an interface.
  1928. class Interface_field_reference_expression : public Expression
  1929. {
  1930. public:
  1931. Interface_field_reference_expression(Expression* expr,
  1932. const std::string& name,
  1933. Location location)
  1934. : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
  1935. expr_(expr), name_(name)
  1936. { }
  1937. // Return the expression for the interface object.
  1938. Expression*
  1939. expr()
  1940. { return this->expr_; }
  1941. // Return the name of the method to call.
  1942. const std::string&
  1943. name() const
  1944. { return this->name_; }
  1945. // Create a thunk to call the method NAME in TYPE when it is used as
  1946. // part of a method value.
  1947. static Named_object*
  1948. create_thunk(Gogo*, Interface_type* type, const std::string& name);
  1949. // Return an expression for the pointer to the function to call.
  1950. Expression*
  1951. get_function();
  1952. // Return an expression for the first argument to pass to the interface
  1953. // function. This is the real object associated with the interface object.
  1954. Expression*
  1955. get_underlying_object();
  1956. protected:
  1957. int
  1958. do_traverse(Traverse* traverse);
  1959. Expression*
  1960. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  1961. Type*
  1962. do_type();
  1963. void
  1964. do_determine_type(const Type_context*);
  1965. void
  1966. do_check_types(Gogo*);
  1967. Expression*
  1968. do_copy()
  1969. {
  1970. return Expression::make_interface_field_reference(this->expr_->copy(),
  1971. this->name_,
  1972. this->location());
  1973. }
  1974. Bexpression*
  1975. do_get_backend(Translate_context*);
  1976. void
  1977. do_dump_expression(Ast_dump_context*) const;
  1978. private:
  1979. // A mapping from interface types to a list of thunks we have
  1980. // created for methods.
  1981. typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
  1982. typedef Unordered_map(Interface_type*, Method_thunks*)
  1983. Interface_method_thunks;
  1984. static Interface_method_thunks interface_method_thunks;
  1985. // The expression for the interface object. This should have a type
  1986. // of interface or pointer to interface.
  1987. Expression* expr_;
  1988. // The field we are retrieving--the name of the method.
  1989. std::string name_;
  1990. };
  1991. // A type guard expression.
  1992. class Type_guard_expression : public Expression
  1993. {
  1994. public:
  1995. Type_guard_expression(Expression* expr, Type* type, Location location)
  1996. : Expression(EXPRESSION_TYPE_GUARD, location),
  1997. expr_(expr), type_(type)
  1998. { }
  1999. // Return the expression to convert.
  2000. Expression*
  2001. expr()
  2002. { return this->expr_; }
  2003. // Return the type to which to convert.
  2004. Type*
  2005. type()
  2006. { return this->type_; }
  2007. protected:
  2008. int
  2009. do_traverse(Traverse* traverse);
  2010. Expression*
  2011. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  2012. Type*
  2013. do_type()
  2014. { return this->type_; }
  2015. void
  2016. do_determine_type(const Type_context*)
  2017. { this->expr_->determine_type_no_context(); }
  2018. void
  2019. do_check_types(Gogo*);
  2020. Expression*
  2021. do_copy()
  2022. {
  2023. return new Type_guard_expression(this->expr_->copy(), this->type_,
  2024. this->location());
  2025. }
  2026. Bexpression*
  2027. do_get_backend(Translate_context*);
  2028. void
  2029. do_dump_expression(Ast_dump_context*) const;
  2030. private:
  2031. // The expression to convert.
  2032. Expression* expr_;
  2033. // The type to which to convert.
  2034. Type* type_;
  2035. };
  2036. // A receive expression.
  2037. class Receive_expression : public Expression
  2038. {
  2039. public:
  2040. Receive_expression(Expression* channel, Location location)
  2041. : Expression(EXPRESSION_RECEIVE, location),
  2042. channel_(channel), temp_receiver_(NULL)
  2043. { }
  2044. // Return the channel.
  2045. Expression*
  2046. channel()
  2047. { return this->channel_; }
  2048. protected:
  2049. int
  2050. do_traverse(Traverse* traverse)
  2051. { return Expression::traverse(&this->channel_, traverse); }
  2052. bool
  2053. do_discarding_value()
  2054. { return true; }
  2055. Type*
  2056. do_type();
  2057. Expression*
  2058. do_flatten(Gogo*, Named_object*, Statement_inserter*);
  2059. void
  2060. do_determine_type(const Type_context*)
  2061. { this->channel_->determine_type_no_context(); }
  2062. void
  2063. do_check_types(Gogo*);
  2064. Expression*
  2065. do_copy()
  2066. {
  2067. return Expression::make_receive(this->channel_->copy(), this->location());
  2068. }
  2069. bool
  2070. do_must_eval_in_order() const
  2071. { return true; }
  2072. Bexpression*
  2073. do_get_backend(Translate_context*);
  2074. void
  2075. do_dump_expression(Ast_dump_context*) const;
  2076. private:
  2077. // The channel from which we are receiving.
  2078. Expression* channel_;
  2079. // A temporary reference to the variable storing the received data.
  2080. Temporary_statement* temp_receiver_;
  2081. };
  2082. // A numeric constant. This is used both for untyped constants and
  2083. // for constants that have a type.
  2084. class Numeric_constant
  2085. {
  2086. public:
  2087. Numeric_constant()
  2088. : classification_(NC_INVALID), type_(NULL)
  2089. { }
  2090. ~Numeric_constant();
  2091. Numeric_constant(const Numeric_constant&);
  2092. Numeric_constant& operator=(const Numeric_constant&);
  2093. // Set to an unsigned long value.
  2094. void
  2095. set_unsigned_long(Type*, unsigned long);
  2096. // Set to an integer value.
  2097. void
  2098. set_int(Type*, const mpz_t);
  2099. // Set to a rune value.
  2100. void
  2101. set_rune(Type*, const mpz_t);
  2102. // Set to a floating point value.
  2103. void
  2104. set_float(Type*, const mpfr_t);
  2105. // Set to a complex value.
  2106. void
  2107. set_complex(Type*, const mpc_t);
  2108. // Classifiers.
  2109. bool
  2110. is_int() const
  2111. { return this->classification_ == Numeric_constant::NC_INT; }
  2112. bool
  2113. is_rune() const
  2114. { return this->classification_ == Numeric_constant::NC_RUNE; }
  2115. bool
  2116. is_float() const
  2117. { return this->classification_ == Numeric_constant::NC_FLOAT; }
  2118. bool
  2119. is_complex() const
  2120. { return this->classification_ == Numeric_constant::NC_COMPLEX; }
  2121. // Value retrievers. These will initialize the values as well as
  2122. // set them. GET_INT is only valid if IS_INT returns true, and
  2123. // likewise respectively.
  2124. void
  2125. get_int(mpz_t*) const;
  2126. void
  2127. get_rune(mpz_t*) const;
  2128. void
  2129. get_float(mpfr_t*) const;
  2130. void
  2131. get_complex(mpc_t*) const;
  2132. // Codes returned by to_unsigned_long.
  2133. enum To_unsigned_long
  2134. {
  2135. // Value is integer and fits in unsigned long.
  2136. NC_UL_VALID,
  2137. // Value is not integer.
  2138. NC_UL_NOTINT,
  2139. // Value is integer but is negative.
  2140. NC_UL_NEGATIVE,
  2141. // Value is non-negative integer but does not fit in unsigned
  2142. // long.
  2143. NC_UL_BIG
  2144. };
  2145. // If the value can be expressed as an integer that fits in an
  2146. // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
  2147. // one of the other To_unsigned_long codes.
  2148. To_unsigned_long
  2149. to_unsigned_long(unsigned long* val) const;
  2150. // If the value can be expressed as an int, return true and
  2151. // initialize and set VAL. This will return false for a value with
  2152. // an explicit float or complex type, even if the value is integral.
  2153. bool
  2154. to_int(mpz_t* val) const;
  2155. // If the value can be expressed as a float, return true and
  2156. // initialize and set VAL.
  2157. bool
  2158. to_float(mpfr_t* val) const;
  2159. // If the value can be expressed as a complex, return true and
  2160. // initialize and set VR and VI.
  2161. bool
  2162. to_complex(mpc_t* val) const;
  2163. // Get the type.
  2164. Type*
  2165. type() const;
  2166. // If the constant can be expressed in TYPE, then set the type of
  2167. // the constant to TYPE and return true. Otherwise return false,
  2168. // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
  2169. // the location to use for the error.
  2170. bool
  2171. set_type(Type* type, bool issue_error, Location location);
  2172. // Return an Expression for this value.
  2173. Expression*
  2174. expression(Location) const;
  2175. private:
  2176. void
  2177. clear();
  2178. To_unsigned_long
  2179. mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
  2180. To_unsigned_long
  2181. mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
  2182. bool
  2183. check_int_type(Integer_type*, bool, Location) const;
  2184. bool
  2185. check_float_type(Float_type*, bool, Location);
  2186. bool
  2187. check_complex_type(Complex_type*, bool, Location);
  2188. // The kinds of constants.
  2189. enum Classification
  2190. {
  2191. NC_INVALID,
  2192. NC_RUNE,
  2193. NC_INT,
  2194. NC_FLOAT,
  2195. NC_COMPLEX
  2196. };
  2197. // The kind of constant.
  2198. Classification classification_;
  2199. // The value.
  2200. union
  2201. {
  2202. // If NC_INT or NC_RUNE.
  2203. mpz_t int_val;
  2204. // If NC_FLOAT.
  2205. mpfr_t float_val;
  2206. // If NC_COMPLEX.
  2207. mpc_t complex_val;
  2208. } u_;
  2209. // The type if there is one. This will be NULL for an untyped
  2210. // constant.
  2211. Type* type_;
  2212. };
  2213. #endif // !defined(GO_EXPRESSIONS_H)