types.h 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262
  1. // types.h -- Go frontend types. -*- 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_TYPES_H
  6. #define GO_TYPES_H
  7. #include "go-linemap.h"
  8. class Gogo;
  9. class Package;
  10. class Traverse;
  11. class Typed_identifier;
  12. class Typed_identifier_list;
  13. class Integer_type;
  14. class Float_type;
  15. class Complex_type;
  16. class String_type;
  17. class Function_type;
  18. class Backend_function_type;
  19. class Struct_field;
  20. class Struct_field_list;
  21. class Struct_type;
  22. class Pointer_type;
  23. class Array_type;
  24. class Map_type;
  25. class Channel_type;
  26. class Interface_type;
  27. class Named_type;
  28. class Forward_declaration_type;
  29. class Method;
  30. class Methods;
  31. class Type_hash_identical;
  32. class Type_identical;
  33. class Expression;
  34. class Expression_list;
  35. class Call_expression;
  36. class Field_reference_expression;
  37. class Bound_method_expression;
  38. class Bindings;
  39. class Named_object;
  40. class Function;
  41. class Translate_context;
  42. class Export;
  43. class Import;
  44. class Btype;
  45. class Bexpression;
  46. class Bvariable;
  47. // Type codes used in type descriptors. These must match the values
  48. // in libgo/runtime/go-type.h. They also match the values in the gc
  49. // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
  50. // although this is not required.
  51. static const int RUNTIME_TYPE_KIND_BOOL = 1;
  52. static const int RUNTIME_TYPE_KIND_INT = 2;
  53. static const int RUNTIME_TYPE_KIND_INT8 = 3;
  54. static const int RUNTIME_TYPE_KIND_INT16 = 4;
  55. static const int RUNTIME_TYPE_KIND_INT32 = 5;
  56. static const int RUNTIME_TYPE_KIND_INT64 = 6;
  57. static const int RUNTIME_TYPE_KIND_UINT = 7;
  58. static const int RUNTIME_TYPE_KIND_UINT8 = 8;
  59. static const int RUNTIME_TYPE_KIND_UINT16 = 9;
  60. static const int RUNTIME_TYPE_KIND_UINT32 = 10;
  61. static const int RUNTIME_TYPE_KIND_UINT64 = 11;
  62. static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
  63. static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
  64. static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
  65. static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
  66. static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
  67. static const int RUNTIME_TYPE_KIND_ARRAY = 17;
  68. static const int RUNTIME_TYPE_KIND_CHAN = 18;
  69. static const int RUNTIME_TYPE_KIND_FUNC = 19;
  70. static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
  71. static const int RUNTIME_TYPE_KIND_MAP = 21;
  72. static const int RUNTIME_TYPE_KIND_PTR = 22;
  73. static const int RUNTIME_TYPE_KIND_SLICE = 23;
  74. static const int RUNTIME_TYPE_KIND_STRING = 24;
  75. static const int RUNTIME_TYPE_KIND_STRUCT = 25;
  76. static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
  77. static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5);
  78. static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6);
  79. static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
  80. // GC instruction opcodes. These must match the values in libgo/runtime/mgc0.h.
  81. enum GC_Opcode
  82. {
  83. GC_END = 0, // End of object, loop or subroutine.
  84. GC_PTR, // A typed pointer.
  85. GC_APTR, // Pointer to an arbitrary object.
  86. GC_ARRAY_START, // Start an array with a fixed length.
  87. GC_ARRAY_NEXT, // The next element of an array.
  88. GC_CALL, // Call a subroutine.
  89. GC_CHAN_PTR, // Go channel.
  90. GC_STRING, // Go string.
  91. GC_EFACE, // interface{}.
  92. GC_IFACE, // interface{...}.
  93. GC_SLICE, // Go slice.
  94. GC_REGION, // A region/part of the current object.
  95. GC_NUM_INSTR // Number of instruction opcodes
  96. };
  97. // The GC Stack Capacity must match the value in libgo/runtime/mgc0.h.
  98. static const int GC_STACK_CAPACITY = 8;
  99. // To build the complete list of methods for a named type we need to
  100. // gather all methods from anonymous fields. Those methods may
  101. // require an arbitrary set of indirections and field offsets. There
  102. // is also the possibility of ambiguous methods, which we could ignore
  103. // except that we want to give a better error message for that case.
  104. // This is a base class. There are two types of methods: named
  105. // methods, and methods which are inherited from an anonymous field of
  106. // interface type.
  107. class Method
  108. {
  109. public:
  110. // For methods in anonymous types we need to know the sequence of
  111. // field references used to extract the pointer to pass to the
  112. // method. Since each method for a particular anonymous field will
  113. // have the sequence of field indexes, and since the indexes can be
  114. // shared going down the chain, we use a manually managed linked
  115. // list. The first entry in the list is the field index for the
  116. // last field, the one passed to the method.
  117. struct Field_indexes
  118. {
  119. const Field_indexes* next;
  120. unsigned int field_index;
  121. };
  122. virtual ~Method()
  123. { }
  124. // Get the list of field indexes.
  125. const Field_indexes*
  126. field_indexes() const
  127. { return this->field_indexes_; }
  128. // Get the depth.
  129. unsigned int
  130. depth() const
  131. { return this->depth_; }
  132. // Return whether this is a value method--a method which does not
  133. // require a pointer expression.
  134. bool
  135. is_value_method() const
  136. { return this->is_value_method_; }
  137. // Return whether we need a stub method--this is true if we can't
  138. // just pass the main object to the method.
  139. bool
  140. needs_stub_method() const
  141. { return this->needs_stub_method_; }
  142. // Return whether this is an ambiguous method name.
  143. bool
  144. is_ambiguous() const
  145. { return this->is_ambiguous_; }
  146. // Note that this method is ambiguous.
  147. void
  148. set_is_ambiguous()
  149. { this->is_ambiguous_ = true; }
  150. // Return the type of the method.
  151. Function_type*
  152. type() const
  153. { return this->do_type(); }
  154. // Return the location of the method receiver.
  155. Location
  156. receiver_location() const
  157. { return this->do_receiver_location(); }
  158. // Return an expression which binds this method to EXPR. This is
  159. // something which can be used with a function call.
  160. Expression*
  161. bind_method(Expression* expr, Location location) const;
  162. // Return the named object for this method. This may only be called
  163. // after methods are finalized.
  164. Named_object*
  165. named_object() const;
  166. // Get the stub object.
  167. Named_object*
  168. stub_object() const
  169. {
  170. go_assert(this->stub_ != NULL);
  171. return this->stub_;
  172. }
  173. // Set the stub object.
  174. void
  175. set_stub_object(Named_object* no)
  176. {
  177. go_assert(this->stub_ == NULL);
  178. this->stub_ = no;
  179. }
  180. // Return true if this method should not participate in any
  181. // interfaces.
  182. bool
  183. nointerface() const
  184. { return this->do_nointerface(); }
  185. protected:
  186. // These objects are only built by the child classes.
  187. Method(const Field_indexes* field_indexes, unsigned int depth,
  188. bool is_value_method, bool needs_stub_method)
  189. : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
  190. is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
  191. is_ambiguous_(false)
  192. { }
  193. // The named object for this method.
  194. virtual Named_object*
  195. do_named_object() const = 0;
  196. // The type of the method.
  197. virtual Function_type*
  198. do_type() const = 0;
  199. // Return the location of the method receiver.
  200. virtual Location
  201. do_receiver_location() const = 0;
  202. // Bind a method to an object.
  203. virtual Expression*
  204. do_bind_method(Expression* expr, Location location) const = 0;
  205. // Return whether this method should not participate in interfaces.
  206. virtual bool
  207. do_nointerface() const = 0;
  208. private:
  209. // The sequence of field indexes used for this method. If this is
  210. // NULL, then the method is defined for the current type.
  211. const Field_indexes* field_indexes_;
  212. // The depth at which this method was found.
  213. unsigned int depth_;
  214. // If a stub method is required, this is its object. This is only
  215. // set after stub methods are built in finalize_methods.
  216. Named_object* stub_;
  217. // Whether this is a value method--a method that does not require a
  218. // pointer.
  219. bool is_value_method_;
  220. // Whether a stub method is required.
  221. bool needs_stub_method_;
  222. // Whether this method is ambiguous.
  223. bool is_ambiguous_;
  224. };
  225. // A named method. This is what you get with a method declaration,
  226. // either directly on the type, or inherited from some anonymous
  227. // embedded field.
  228. class Named_method : public Method
  229. {
  230. public:
  231. Named_method(Named_object* named_object, const Field_indexes* field_indexes,
  232. unsigned int depth, bool is_value_method,
  233. bool needs_stub_method)
  234. : Method(field_indexes, depth, is_value_method, needs_stub_method),
  235. named_object_(named_object)
  236. { }
  237. protected:
  238. // Get the Named_object for the method.
  239. Named_object*
  240. do_named_object() const
  241. { return this->named_object_; }
  242. // The type of the method.
  243. Function_type*
  244. do_type() const;
  245. // Return the location of the method receiver.
  246. Location
  247. do_receiver_location() const;
  248. // Bind a method to an object.
  249. Expression*
  250. do_bind_method(Expression* expr, Location location) const;
  251. // Return whether this method should not participate in interfaces.
  252. bool
  253. do_nointerface() const;
  254. private:
  255. // The method itself. For a method which needs a stub, this starts
  256. // out as the underlying method, and is later replaced with the stub
  257. // method.
  258. Named_object* named_object_;
  259. };
  260. // An interface method. This is used when an interface appears as an
  261. // anonymous field in a named struct.
  262. class Interface_method : public Method
  263. {
  264. public:
  265. Interface_method(const std::string& name, Location location,
  266. Function_type* fntype, const Field_indexes* field_indexes,
  267. unsigned int depth)
  268. : Method(field_indexes, depth, true, true),
  269. name_(name), location_(location), fntype_(fntype)
  270. { }
  271. protected:
  272. // Get the Named_object for the method. This should never be
  273. // called, as we always create a stub.
  274. Named_object*
  275. do_named_object() const
  276. { go_unreachable(); }
  277. // The type of the method.
  278. Function_type*
  279. do_type() const
  280. { return this->fntype_; }
  281. // Return the location of the method receiver.
  282. Location
  283. do_receiver_location() const
  284. { return this->location_; }
  285. // Bind a method to an object.
  286. Expression*
  287. do_bind_method(Expression* expr, Location location) const;
  288. // Return whether this method should not participate in interfaces.
  289. bool
  290. do_nointerface() const
  291. { return false; }
  292. private:
  293. // The name of the interface method to call.
  294. std::string name_;
  295. // The location of the definition of the interface method.
  296. Location location_;
  297. // The type of the interface method.
  298. Function_type* fntype_;
  299. };
  300. // A mapping from method name to Method. This is a wrapper around a
  301. // hash table.
  302. class Methods
  303. {
  304. private:
  305. typedef Unordered_map(std::string, Method*) Method_map;
  306. public:
  307. typedef Method_map::const_iterator const_iterator;
  308. Methods()
  309. : methods_()
  310. { }
  311. // Insert a new method. Returns true if it was inserted, false if
  312. // it was overidden or ambiguous.
  313. bool
  314. insert(const std::string& name, Method* m);
  315. // The number of (unambiguous) methods.
  316. size_t
  317. count() const;
  318. // Iterate.
  319. const_iterator
  320. begin() const
  321. { return this->methods_.begin(); }
  322. const_iterator
  323. end() const
  324. { return this->methods_.end(); }
  325. // Lookup.
  326. const_iterator
  327. find(const std::string& name) const
  328. { return this->methods_.find(name); }
  329. bool
  330. empty() const
  331. { return this->methods_.empty(); }
  332. private:
  333. Method_map methods_;
  334. };
  335. // The base class for all types.
  336. class Type
  337. {
  338. public:
  339. // The types of types.
  340. enum Type_classification
  341. {
  342. TYPE_ERROR,
  343. TYPE_VOID,
  344. TYPE_BOOLEAN,
  345. TYPE_INTEGER,
  346. TYPE_FLOAT,
  347. TYPE_COMPLEX,
  348. TYPE_STRING,
  349. TYPE_SINK,
  350. TYPE_FUNCTION,
  351. TYPE_POINTER,
  352. TYPE_NIL,
  353. TYPE_CALL_MULTIPLE_RESULT,
  354. TYPE_STRUCT,
  355. TYPE_ARRAY,
  356. TYPE_MAP,
  357. TYPE_CHANNEL,
  358. TYPE_INTERFACE,
  359. TYPE_NAMED,
  360. TYPE_FORWARD
  361. };
  362. virtual ~Type();
  363. // Creators.
  364. static Type*
  365. make_error_type();
  366. static Type*
  367. make_void_type();
  368. // Get the unnamed bool type.
  369. static Type*
  370. make_boolean_type();
  371. // Get the named type "bool".
  372. static Named_type*
  373. lookup_bool_type();
  374. // Make the named type "bool".
  375. static Named_type*
  376. make_named_bool_type();
  377. // Make an abstract integer type.
  378. static Integer_type*
  379. make_abstract_integer_type();
  380. // Make an abstract type for a character constant.
  381. static Integer_type*
  382. make_abstract_character_type();
  383. // Make a named integer type with a specified size.
  384. // RUNTIME_TYPE_KIND is the code to use in reflection information,
  385. // to distinguish int and int32.
  386. static Named_type*
  387. make_integer_type(const char* name, bool is_unsigned, int bits,
  388. int runtime_type_kind);
  389. // Look up a named integer type.
  390. static Named_type*
  391. lookup_integer_type(const char* name);
  392. // Make an abstract floating point type.
  393. static Float_type*
  394. make_abstract_float_type();
  395. // Make a named floating point type with a specific size.
  396. // RUNTIME_TYPE_KIND is the code to use in reflection information,
  397. // to distinguish float and float32.
  398. static Named_type*
  399. make_float_type(const char* name, int bits, int runtime_type_kind);
  400. // Look up a named float type.
  401. static Named_type*
  402. lookup_float_type(const char* name);
  403. // Make an abstract complex type.
  404. static Complex_type*
  405. make_abstract_complex_type();
  406. // Make a named complex type with a specific size.
  407. // RUNTIME_TYPE_KIND is the code to use in reflection information,
  408. // to distinguish complex and complex64.
  409. static Named_type*
  410. make_complex_type(const char* name, int bits, int runtime_type_kind);
  411. // Look up a named complex type.
  412. static Named_type*
  413. lookup_complex_type(const char* name);
  414. // Get the unnamed string type.
  415. static Type*
  416. make_string_type();
  417. // Get the named type "string".
  418. static Named_type*
  419. lookup_string_type();
  420. // Make the named type "string".
  421. static Named_type*
  422. make_named_string_type();
  423. static Type*
  424. make_sink_type();
  425. static Function_type*
  426. make_function_type(Typed_identifier* receiver,
  427. Typed_identifier_list* parameters,
  428. Typed_identifier_list* results,
  429. Location);
  430. static Backend_function_type*
  431. make_backend_function_type(Typed_identifier* receiver,
  432. Typed_identifier_list* parameters,
  433. Typed_identifier_list* results,
  434. Location);
  435. static Pointer_type*
  436. make_pointer_type(Type*);
  437. static Type*
  438. make_nil_type();
  439. static Type*
  440. make_call_multiple_result_type(Call_expression*);
  441. static Struct_type*
  442. make_struct_type(Struct_field_list* fields, Location);
  443. static Array_type*
  444. make_array_type(Type* element_type, Expression* length);
  445. static Map_type*
  446. make_map_type(Type* key_type, Type* value_type, Location);
  447. static Channel_type*
  448. make_channel_type(bool send, bool receive, Type*);
  449. static Interface_type*
  450. make_interface_type(Typed_identifier_list* methods, Location);
  451. static Interface_type*
  452. make_empty_interface_type(Location);
  453. static Type*
  454. make_type_descriptor_type();
  455. static Type*
  456. make_type_descriptor_ptr_type();
  457. static Named_type*
  458. make_named_type(Named_object*, Type*, Location);
  459. static Type*
  460. make_forward_declaration(Named_object*);
  461. // Make a builtin struct type from a list of fields.
  462. static Struct_type*
  463. make_builtin_struct_type(int nfields, ...);
  464. // Make a builtin named type.
  465. static Named_type*
  466. make_builtin_named_type(const char* name, Type* type);
  467. // Traverse a type.
  468. static int
  469. traverse(Type*, Traverse*);
  470. // Verify the type. This is called after parsing, and verifies that
  471. // types are complete and meet the language requirements. This
  472. // returns false if the type is invalid and we should not continue
  473. // traversing it.
  474. bool
  475. verify()
  476. { return this->do_verify(); }
  477. // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
  478. // returns that an erroneous type is identical to any other type;
  479. // this is used to avoid cascading errors. If this returns false,
  480. // and REASON is not NULL, it may set *REASON.
  481. static bool
  482. are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
  483. std::string* reason);
  484. // Return true if two types are compatible for use in a binary
  485. // operation, other than a shift, comparison, or channel send. This
  486. // is an equivalence relation.
  487. static bool
  488. are_compatible_for_binop(const Type* t1, const Type* t2);
  489. // Return true if two types are compatible for use with the
  490. // comparison operator. IS_EQUALITY_OP is true if this is an
  491. // equality comparison, false if it is an ordered comparison. This
  492. // is an equivalence relation. If this returns false, and REASON is
  493. // not NULL, it sets *REASON.
  494. static bool
  495. are_compatible_for_comparison(bool is_equality_op, const Type *t1,
  496. const Type *t2, std::string* reason);
  497. // Return true if a type is comparable with itself. This is true of
  498. // most types, but false for, e.g., function types.
  499. bool
  500. is_comparable() const
  501. { return Type::are_compatible_for_comparison(true, this, this, NULL); }
  502. // Return true if a value with type RHS is assignable to a variable
  503. // with type LHS. This is not an equivalence relation. If this
  504. // returns false, and REASON is not NULL, it sets *REASON.
  505. static bool
  506. are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
  507. // Return true if a value with type RHS may be converted to type
  508. // LHS. If this returns false, and REASON is not NULL, it sets
  509. // *REASON.
  510. static bool
  511. are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
  512. // Return true if values of this type can be compared using an
  513. // identity function which gets nothing but a pointer to the value
  514. // and a size.
  515. bool
  516. compare_is_identity(Gogo* gogo)
  517. { return this->do_compare_is_identity(gogo); }
  518. // Return a hash code for this type for the method hash table.
  519. // Types which are equivalent according to are_identical will have
  520. // the same hash code.
  521. unsigned int
  522. hash_for_method(Gogo*) const;
  523. // Return the type classification.
  524. Type_classification
  525. classification() const
  526. { return this->classification_; }
  527. // Return the base type for this type. This looks through forward
  528. // declarations and names. Using this with a forward declaration
  529. // which has not been defined will return an error type.
  530. Type*
  531. base();
  532. const Type*
  533. base() const;
  534. // Return the type skipping defined forward declarations. If this
  535. // type is a forward declaration which has not been defined, it will
  536. // return the Forward_declaration_type. This differs from base() in
  537. // that it will return a Named_type, and for a
  538. // Forward_declaration_type which is not defined it will return that
  539. // type rather than an error type.
  540. Type*
  541. forwarded();
  542. const Type*
  543. forwarded() const;
  544. // Return true if this is a basic type: a type which is not composed
  545. // of other types, and is not void.
  546. bool
  547. is_basic_type() const;
  548. // Return true if this is an abstract type--an integer, floating
  549. // point, or complex type whose size has not been determined.
  550. bool
  551. is_abstract() const;
  552. // Return a non-abstract version of an abstract type.
  553. Type*
  554. make_non_abstract_type();
  555. // Return true if this type is or contains a pointer. This
  556. // determines whether the garbage collector needs to look at a value
  557. // of this type.
  558. bool
  559. has_pointer() const
  560. { return this->do_has_pointer(); }
  561. // Return true if this is the error type. This returns false for a
  562. // type which is not defined, as it is called by the parser before
  563. // all types are defined.
  564. bool
  565. is_error_type() const;
  566. // Return true if this is the error type or if the type is
  567. // undefined. If the type is undefined, this will give an error.
  568. // This should only be called after parsing is complete.
  569. bool
  570. is_error() const
  571. { return this->base()->is_error_type(); }
  572. // Return true if this is a void type.
  573. bool
  574. is_void_type() const
  575. { return this->classification_ == TYPE_VOID; }
  576. // If this is an integer type, return the Integer_type. Otherwise,
  577. // return NULL. This is a controlled dynamic_cast.
  578. Integer_type*
  579. integer_type()
  580. { return this->convert<Integer_type, TYPE_INTEGER>(); }
  581. const Integer_type*
  582. integer_type() const
  583. { return this->convert<const Integer_type, TYPE_INTEGER>(); }
  584. // If this is a floating point type, return the Float_type.
  585. // Otherwise, return NULL. This is a controlled dynamic_cast.
  586. Float_type*
  587. float_type()
  588. { return this->convert<Float_type, TYPE_FLOAT>(); }
  589. const Float_type*
  590. float_type() const
  591. { return this->convert<const Float_type, TYPE_FLOAT>(); }
  592. // If this is a complex type, return the Complex_type. Otherwise,
  593. // return NULL.
  594. Complex_type*
  595. complex_type()
  596. { return this->convert<Complex_type, TYPE_COMPLEX>(); }
  597. const Complex_type*
  598. complex_type() const
  599. { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
  600. // Return whether this is a numeric type.
  601. bool
  602. is_numeric_type() const
  603. {
  604. Type_classification tc = this->base()->classification_;
  605. return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
  606. }
  607. // Return true if this is a boolean type.
  608. bool
  609. is_boolean_type() const
  610. { return this->base()->classification_ == TYPE_BOOLEAN; }
  611. // Return true if this is an abstract boolean type.
  612. bool
  613. is_abstract_boolean_type() const
  614. { return this->classification_ == TYPE_BOOLEAN; }
  615. // Return true if this is a string type.
  616. bool
  617. is_string_type() const
  618. { return this->base()->classification_ == TYPE_STRING; }
  619. // Return true if this is an abstract string type.
  620. bool
  621. is_abstract_string_type() const
  622. { return this->classification_ == TYPE_STRING; }
  623. // Return true if this is the sink type. This is the type of the
  624. // blank identifier _.
  625. bool
  626. is_sink_type() const
  627. { return this->base()->classification_ == TYPE_SINK; }
  628. // If this is a function type, return it. Otherwise, return NULL.
  629. Function_type*
  630. function_type()
  631. { return this->convert<Function_type, TYPE_FUNCTION>(); }
  632. const Function_type*
  633. function_type() const
  634. { return this->convert<const Function_type, TYPE_FUNCTION>(); }
  635. // If this is a pointer type, return the type to which it points.
  636. // Otherwise, return NULL.
  637. Type*
  638. points_to() const;
  639. // If this is a pointer type, return the type to which it points.
  640. // Otherwise, return the type itself.
  641. Type*
  642. deref()
  643. {
  644. Type* pt = this->points_to();
  645. return pt != NULL ? pt : this;
  646. }
  647. const Type*
  648. deref() const
  649. {
  650. const Type* pt = this->points_to();
  651. return pt != NULL ? pt : this;
  652. }
  653. // Return true if this is the nil type. We don't use base() here,
  654. // because this can be called during parse, and there is no way to
  655. // name the nil type anyhow.
  656. bool
  657. is_nil_type() const
  658. { return this->classification_ == TYPE_NIL; }
  659. // Return true if this is the predeclared constant nil being used as
  660. // a type. This is what the parser produces for type switches which
  661. // use "case nil".
  662. bool
  663. is_nil_constant_as_type() const;
  664. // Return true if this is the return type of a function which
  665. // returns multiple values.
  666. bool
  667. is_call_multiple_result_type() const
  668. { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
  669. // If this is a struct type, return it. Otherwise, return NULL.
  670. Struct_type*
  671. struct_type()
  672. { return this->convert<Struct_type, TYPE_STRUCT>(); }
  673. const Struct_type*
  674. struct_type() const
  675. { return this->convert<const Struct_type, TYPE_STRUCT>(); }
  676. // If this is an array type, return it. Otherwise, return NULL.
  677. Array_type*
  678. array_type()
  679. { return this->convert<Array_type, TYPE_ARRAY>(); }
  680. const Array_type*
  681. array_type() const
  682. { return this->convert<const Array_type, TYPE_ARRAY>(); }
  683. // Return whether if this is a slice type.
  684. bool
  685. is_slice_type() const;
  686. // If this is a map type, return it. Otherwise, return NULL.
  687. Map_type*
  688. map_type()
  689. { return this->convert<Map_type, TYPE_MAP>(); }
  690. const Map_type*
  691. map_type() const
  692. { return this->convert<const Map_type, TYPE_MAP>(); }
  693. // If this is a channel type, return it. Otherwise, return NULL.
  694. Channel_type*
  695. channel_type()
  696. { return this->convert<Channel_type, TYPE_CHANNEL>(); }
  697. const Channel_type*
  698. channel_type() const
  699. { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
  700. // If this is an interface type, return it. Otherwise, return NULL.
  701. Interface_type*
  702. interface_type()
  703. { return this->convert<Interface_type, TYPE_INTERFACE>(); }
  704. const Interface_type*
  705. interface_type() const
  706. { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
  707. // If this is a named type, return it. Otherwise, return NULL.
  708. Named_type*
  709. named_type();
  710. const Named_type*
  711. named_type() const;
  712. // If this is a forward declaration, return it. Otherwise, return
  713. // NULL.
  714. Forward_declaration_type*
  715. forward_declaration_type()
  716. { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
  717. const Forward_declaration_type*
  718. forward_declaration_type() const
  719. {
  720. return this->convert_no_base<const Forward_declaration_type,
  721. TYPE_FORWARD>();
  722. }
  723. // Return true if this type is not yet defined.
  724. bool
  725. is_undefined() const;
  726. // Return true if this is the unsafe.pointer type. We currently
  727. // represent that as pointer-to-void.
  728. bool
  729. is_unsafe_pointer_type() const
  730. { return this->points_to() != NULL && this->points_to()->is_void_type(); }
  731. // Look for field or method NAME for TYPE. Return an expression for
  732. // it, bound to EXPR.
  733. static Expression*
  734. bind_field_or_method(Gogo*, const Type* type, Expression* expr,
  735. const std::string& name, Location);
  736. // Return true if NAME is an unexported field or method of TYPE.
  737. static bool
  738. is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
  739. std::vector<const Named_type*>*);
  740. // Convert the builtin named types.
  741. static void
  742. convert_builtin_named_types(Gogo*);
  743. // Return the backend representation of this type.
  744. Btype*
  745. get_backend(Gogo*);
  746. // Return a placeholder for the backend representation of the type.
  747. // This will return a type of the correct size, but for which some
  748. // of the fields may still need to be completed.
  749. Btype*
  750. get_backend_placeholder(Gogo*);
  751. // Finish the backend representation of a placeholder.
  752. void
  753. finish_backend(Gogo*, Btype*);
  754. // Build a type descriptor entry for this type. Return a pointer to
  755. // it. The location is the location which causes us to need the
  756. // entry.
  757. Bexpression*
  758. type_descriptor_pointer(Gogo* gogo, Location);
  759. // Build the Garbage Collection symbol for this type. Return a pointer to it.
  760. Bexpression*
  761. gc_symbol_pointer(Gogo* gogo);
  762. // Return the type reflection string for this type.
  763. std::string
  764. reflection(Gogo*) const;
  765. // Return a mangled name for the type. This is a name which can be
  766. // used in assembler code. Identical types should have the same
  767. // manged name.
  768. std::string
  769. mangled_name(Gogo*) const;
  770. // If the size of the type can be determined, set *PSIZE to the size
  771. // in bytes and return true. Otherwise, return false. This queries
  772. // the backend.
  773. bool
  774. backend_type_size(Gogo*, int64_t* psize);
  775. // If the alignment of the type can be determined, set *PALIGN to
  776. // the alignment in bytes and return true. Otherwise, return false.
  777. bool
  778. backend_type_align(Gogo*, int64_t* palign);
  779. // If the alignment of a struct field of this type can be
  780. // determined, set *PALIGN to the alignment in bytes and return
  781. // true. Otherwise, return false.
  782. bool
  783. backend_type_field_align(Gogo*, int64_t* palign);
  784. // Whether the backend size is known.
  785. bool
  786. is_backend_type_size_known(Gogo*);
  787. // Get the hash and equality functions for a type.
  788. void
  789. type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
  790. Function_type* equal_fntype, Named_object** hash_fn,
  791. Named_object** equal_fn);
  792. // Write the hash and equality type functions.
  793. void
  794. write_specific_type_functions(Gogo*, Named_type*,
  795. const std::string& hash_name,
  796. Function_type* hash_fntype,
  797. const std::string& equal_name,
  798. Function_type* equal_fntype);
  799. // Export the type.
  800. void
  801. export_type(Export* exp) const
  802. { this->do_export(exp); }
  803. // Import a type.
  804. static Type*
  805. import_type(Import*);
  806. protected:
  807. Type(Type_classification);
  808. // Functions implemented by the child class.
  809. // Traverse the subtypes.
  810. virtual int
  811. do_traverse(Traverse*);
  812. // Verify the type.
  813. virtual bool
  814. do_verify()
  815. { return true; }
  816. virtual bool
  817. do_has_pointer() const
  818. { return false; }
  819. virtual bool
  820. do_compare_is_identity(Gogo*) = 0;
  821. virtual unsigned int
  822. do_hash_for_method(Gogo*) const;
  823. virtual Btype*
  824. do_get_backend(Gogo*) = 0;
  825. virtual Expression*
  826. do_type_descriptor(Gogo*, Named_type* name) = 0;
  827. virtual void
  828. do_gc_symbol(Gogo*, Expression_list**, Expression**, int) = 0;
  829. virtual void
  830. do_reflection(Gogo*, std::string*) const = 0;
  831. virtual void
  832. do_mangled_name(Gogo*, std::string*) const = 0;
  833. virtual void
  834. do_export(Export*) const;
  835. // Return whether a method expects a pointer as the receiver.
  836. static bool
  837. method_expects_pointer(const Named_object*);
  838. // Finalize the methods for a type.
  839. static void
  840. finalize_methods(Gogo*, const Type*, Location, Methods**);
  841. // Return a method from a set of methods.
  842. static Method*
  843. method_function(const Methods*, const std::string& name,
  844. bool* is_ambiguous);
  845. // A mapping from interfaces to the associated interface method
  846. // tables for this type. This maps to a decl.
  847. typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
  848. Type_identical) Interface_method_tables;
  849. // Return a pointer to the interface method table for TYPE for the
  850. // interface INTERFACE.
  851. static Expression*
  852. interface_method_table(Type* type,
  853. Interface_type *interface, bool is_pointer,
  854. Interface_method_tables** method_tables,
  855. Interface_method_tables** pointer_tables);
  856. // Return a composite literal for the type descriptor entry for a
  857. // type.
  858. static Expression*
  859. type_descriptor(Gogo*, Type*);
  860. // Return a composite literal for the type descriptor entry for
  861. // TYPE, using NAME as the name of the type.
  862. static Expression*
  863. named_type_descriptor(Gogo*, Type* type, Named_type* name);
  864. // Return a composite literal for a plain type descriptor for this
  865. // type with the given kind and name.
  866. Expression*
  867. plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
  868. // Build a composite literal for the basic type descriptor.
  869. Expression*
  870. type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
  871. const Methods*, bool only_value_methods);
  872. // Generate the GC symbol for this TYPE. VALS is the data so far in this
  873. // symbol; extra values will be appended in do_gc_symbol. OFFSET is the
  874. // offset into the symbol where the GC data is located. STACK_SIZE is the
  875. // size of the GC stack when dealing with array types.
  876. static void
  877. gc_symbol(Gogo*, Type* type, Expression_list** vals, Expression** offset,
  878. int stack_size);
  879. // Build a composite literal for the GC symbol of this type.
  880. Expression*
  881. gc_symbol_constructor(Gogo*);
  882. // Advance the OFFSET of the GC symbol by the size of this type.
  883. void
  884. advance_gc_offset(Expression** offset);
  885. // For the benefit of child class reflection string generation.
  886. void
  887. append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
  888. { type->do_reflection(gogo, ret); }
  889. // For the benefit of child class mangling.
  890. void
  891. append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
  892. { type->do_mangled_name(gogo, ret); }
  893. // Incorporate a string into a hash code.
  894. static unsigned int
  895. hash_string(const std::string&, unsigned int);
  896. // Return the backend representation for the underlying type of a
  897. // named type.
  898. static Btype*
  899. get_named_base_btype(Gogo* gogo, Type* base_type)
  900. { return base_type->get_btype_without_hash(gogo); }
  901. private:
  902. // Convert to the desired type classification, or return NULL. This
  903. // is a controlled dynamic_cast.
  904. template<typename Type_class, Type_classification type_classification>
  905. Type_class*
  906. convert()
  907. {
  908. Type* base = this->base();
  909. return (base->classification_ == type_classification
  910. ? static_cast<Type_class*>(base)
  911. : NULL);
  912. }
  913. template<typename Type_class, Type_classification type_classification>
  914. const Type_class*
  915. convert() const
  916. {
  917. const Type* base = this->base();
  918. return (base->classification_ == type_classification
  919. ? static_cast<Type_class*>(base)
  920. : NULL);
  921. }
  922. template<typename Type_class, Type_classification type_classification>
  923. Type_class*
  924. convert_no_base()
  925. {
  926. return (this->classification_ == type_classification
  927. ? static_cast<Type_class*>(this)
  928. : NULL);
  929. }
  930. template<typename Type_class, Type_classification type_classification>
  931. const Type_class*
  932. convert_no_base() const
  933. {
  934. return (this->classification_ == type_classification
  935. ? static_cast<Type_class*>(this)
  936. : NULL);
  937. }
  938. // Map unnamed types to type descriptor decls.
  939. typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
  940. Type_identical) Type_descriptor_vars;
  941. static Type_descriptor_vars type_descriptor_vars;
  942. // Build the type descriptor variable for this type.
  943. void
  944. make_type_descriptor_var(Gogo*);
  945. // Map unnamed types to type descriptor decls.
  946. typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
  947. Type_identical) GC_symbol_vars;
  948. static GC_symbol_vars gc_symbol_vars;
  949. // Build the GC symbol for this type.
  950. void
  951. make_gc_symbol_var(Gogo*);
  952. // Return the name of the type descriptor variable. If NAME is not
  953. // NULL, it is the name to use.
  954. std::string
  955. type_descriptor_var_name(Gogo*, Named_type* name);
  956. // Return true if the type descriptor for this type should be
  957. // defined in some other package. If NAME is not NULL, it is the
  958. // name of this type. If this returns true it sets *PACKAGE to the
  959. // package where the type descriptor is defined.
  960. bool
  961. type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
  962. // Build the hash and equality type functions for a type which needs
  963. // specific functions.
  964. void
  965. specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
  966. Function_type* equal_fntype, Named_object** hash_fn,
  967. Named_object** equal_fn);
  968. void
  969. write_named_hash(Gogo*, Named_type*, Function_type* hash_fntype,
  970. Function_type* equal_fntype);
  971. void
  972. write_named_equal(Gogo*, Named_type*);
  973. // Build a composite literal for the uncommon type information.
  974. Expression*
  975. uncommon_type_constructor(Gogo*, Type* uncommon_type,
  976. Named_type*, const Methods*,
  977. bool only_value_methods) const;
  978. // Build a composite literal for the methods.
  979. Expression*
  980. methods_constructor(Gogo*, Type* methods_type, const Methods*,
  981. bool only_value_methods) const;
  982. // Build a composite literal for one method.
  983. Expression*
  984. method_constructor(Gogo*, Type* method_type, const std::string& name,
  985. const Method*, bool only_value_methods) const;
  986. // Add all methods for TYPE to the list of methods for THIS.
  987. static void
  988. add_methods_for_type(const Type* type, const Method::Field_indexes*,
  989. unsigned int depth, bool, bool,
  990. std::vector<const Named_type*>*,
  991. Methods*);
  992. static void
  993. add_local_methods_for_type(const Named_type* type,
  994. const Method::Field_indexes*,
  995. unsigned int depth, bool, bool, Methods*);
  996. static void
  997. add_embedded_methods_for_type(const Type* type,
  998. const Method::Field_indexes*,
  999. unsigned int depth, bool, bool,
  1000. std::vector<const Named_type*>*,
  1001. Methods*);
  1002. static void
  1003. add_interface_methods_for_type(const Type* type,
  1004. const Method::Field_indexes*,
  1005. unsigned int depth, Methods*);
  1006. // Build stub methods for a type.
  1007. static void
  1008. build_stub_methods(Gogo*, const Type* type, const Methods* methods,
  1009. Location);
  1010. static void
  1011. build_one_stub_method(Gogo*, Method*, const char* receiver_name,
  1012. const Typed_identifier_list*, bool is_varargs,
  1013. Location);
  1014. static Expression*
  1015. apply_field_indexes(Expression*, const Method::Field_indexes*,
  1016. Location);
  1017. // Look for a field or method named NAME in TYPE.
  1018. static bool
  1019. find_field_or_method(const Type* type, const std::string& name,
  1020. bool receiver_can_be_pointer,
  1021. std::vector<const Named_type*>*, int* level,
  1022. bool* is_method, bool* found_pointer_method,
  1023. std::string* ambig1, std::string* ambig2);
  1024. // Get the backend representation for a type without looking in the
  1025. // hash table for identical types.
  1026. Btype*
  1027. get_btype_without_hash(Gogo*);
  1028. // A backend type that may be a placeholder.
  1029. struct Type_btype_entry
  1030. {
  1031. Btype *btype;
  1032. bool is_placeholder;
  1033. };
  1034. // A mapping from Type to Btype*, used to ensure that the backend
  1035. // representation of identical types is identical. This is only
  1036. // used for unnamed types.
  1037. typedef Unordered_map_hash(const Type*, Type_btype_entry,
  1038. Type_hash_identical, Type_identical) Type_btypes;
  1039. static Type_btypes type_btypes;
  1040. // A list of builtin named types.
  1041. static std::vector<Named_type*> named_builtin_types;
  1042. // A map from types which need specific type functions to the type
  1043. // functions themselves.
  1044. typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
  1045. typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
  1046. Type_identical) Type_functions;
  1047. static Type_functions type_functions_table;
  1048. // The type classification.
  1049. Type_classification classification_;
  1050. // The backend representation of the type, once it has been
  1051. // determined.
  1052. Btype* btype_;
  1053. // The type descriptor for this type. This starts out as NULL and
  1054. // is filled in as needed.
  1055. Bvariable* type_descriptor_var_;
  1056. // The GC symbol for this type. This starts out as NULL and
  1057. // is filled in as needed.
  1058. Bvariable* gc_symbol_var_;
  1059. };
  1060. // Type hash table operations.
  1061. class Type_hash_identical
  1062. {
  1063. public:
  1064. unsigned int
  1065. operator()(const Type* type) const
  1066. { return type->hash_for_method(NULL); }
  1067. };
  1068. class Type_identical
  1069. {
  1070. public:
  1071. bool
  1072. operator()(const Type* t1, const Type* t2) const
  1073. { return Type::are_identical(t1, t2, false, NULL); }
  1074. };
  1075. // An identifier with a type.
  1076. class Typed_identifier
  1077. {
  1078. public:
  1079. Typed_identifier(const std::string& name, Type* type,
  1080. Location location)
  1081. : name_(name), type_(type), location_(location)
  1082. { }
  1083. // Get the name.
  1084. const std::string&
  1085. name() const
  1086. { return this->name_; }
  1087. // Get the type.
  1088. Type*
  1089. type() const
  1090. { return this->type_; }
  1091. // Return the location where the name was seen. This is not always
  1092. // meaningful.
  1093. Location
  1094. location() const
  1095. { return this->location_; }
  1096. // Set the type--sometimes we see the identifier before the type.
  1097. void
  1098. set_type(Type* type)
  1099. {
  1100. go_assert(this->type_ == NULL || type->is_error_type());
  1101. this->type_ = type;
  1102. }
  1103. private:
  1104. // Identifier name.
  1105. std::string name_;
  1106. // Type.
  1107. Type* type_;
  1108. // The location where the name was seen.
  1109. Location location_;
  1110. };
  1111. // A list of Typed_identifiers.
  1112. class Typed_identifier_list
  1113. {
  1114. public:
  1115. Typed_identifier_list()
  1116. : entries_()
  1117. { }
  1118. // Whether the list is empty.
  1119. bool
  1120. empty() const
  1121. { return this->entries_.empty(); }
  1122. // Return the number of entries in the list.
  1123. size_t
  1124. size() const
  1125. { return this->entries_.size(); }
  1126. // Add an entry to the end of the list.
  1127. void
  1128. push_back(const Typed_identifier& td)
  1129. { this->entries_.push_back(td); }
  1130. // Remove an entry from the end of the list.
  1131. void
  1132. pop_back()
  1133. { this->entries_.pop_back(); }
  1134. // Set the type of entry I to TYPE.
  1135. void
  1136. set_type(size_t i, Type* type)
  1137. {
  1138. go_assert(i < this->entries_.size());
  1139. this->entries_[i].set_type(type);
  1140. }
  1141. // Sort the entries by name.
  1142. void
  1143. sort_by_name();
  1144. // Traverse types.
  1145. int
  1146. traverse(Traverse*);
  1147. // Return the first and last elements.
  1148. Typed_identifier&
  1149. front()
  1150. { return this->entries_.front(); }
  1151. const Typed_identifier&
  1152. front() const
  1153. { return this->entries_.front(); }
  1154. Typed_identifier&
  1155. back()
  1156. { return this->entries_.back(); }
  1157. const Typed_identifier&
  1158. back() const
  1159. { return this->entries_.back(); }
  1160. const Typed_identifier&
  1161. at(size_t i) const
  1162. { return this->entries_.at(i); }
  1163. void
  1164. set(size_t i, const Typed_identifier& t)
  1165. { this->entries_.at(i) = t; }
  1166. void
  1167. resize(size_t c)
  1168. {
  1169. go_assert(c <= this->entries_.size());
  1170. this->entries_.resize(c, Typed_identifier("", NULL,
  1171. Linemap::unknown_location()));
  1172. }
  1173. void
  1174. reserve(size_t c)
  1175. { this->entries_.reserve(c); }
  1176. // Iterators.
  1177. typedef std::vector<Typed_identifier>::iterator iterator;
  1178. typedef std::vector<Typed_identifier>::const_iterator const_iterator;
  1179. iterator
  1180. begin()
  1181. { return this->entries_.begin(); }
  1182. const_iterator
  1183. begin() const
  1184. { return this->entries_.begin(); }
  1185. iterator
  1186. end()
  1187. { return this->entries_.end(); }
  1188. const_iterator
  1189. end() const
  1190. { return this->entries_.end(); }
  1191. // Return a copy of this list. This returns an independent copy of
  1192. // the vector, but does not copy the types.
  1193. Typed_identifier_list*
  1194. copy() const;
  1195. private:
  1196. std::vector<Typed_identifier> entries_;
  1197. };
  1198. // The type of an integer.
  1199. class Integer_type : public Type
  1200. {
  1201. public:
  1202. // Create a new integer type.
  1203. static Named_type*
  1204. create_integer_type(const char* name, bool is_unsigned, int bits,
  1205. int runtime_type_kind);
  1206. // Look up an existing integer type.
  1207. static Named_type*
  1208. lookup_integer_type(const char* name);
  1209. // Create an abstract integer type.
  1210. static Integer_type*
  1211. create_abstract_integer_type();
  1212. // Create an abstract character type.
  1213. static Integer_type*
  1214. create_abstract_character_type();
  1215. // Whether this is an abstract integer type.
  1216. bool
  1217. is_abstract() const
  1218. { return this->is_abstract_; }
  1219. // Whether this is an unsigned type.
  1220. bool
  1221. is_unsigned() const
  1222. { return this->is_unsigned_; }
  1223. // The number of bits.
  1224. int
  1225. bits() const
  1226. { return this->bits_; }
  1227. // Whether this type is the same as T.
  1228. bool
  1229. is_identical(const Integer_type* t) const;
  1230. // Whether this is the type "byte" or another name for "byte".
  1231. bool
  1232. is_byte() const
  1233. { return this->is_byte_; }
  1234. // Mark this as the "byte" type.
  1235. void
  1236. set_is_byte()
  1237. { this->is_byte_ = true; }
  1238. // Whether this is the type "rune" or another name for "rune".
  1239. bool
  1240. is_rune() const
  1241. { return this->is_rune_; }
  1242. // Mark this as the "rune" type.
  1243. void
  1244. set_is_rune()
  1245. { this->is_rune_ = true; }
  1246. protected:
  1247. bool
  1248. do_compare_is_identity(Gogo*)
  1249. { return true; }
  1250. unsigned int
  1251. do_hash_for_method(Gogo*) const;
  1252. Btype*
  1253. do_get_backend(Gogo*);
  1254. Expression*
  1255. do_type_descriptor(Gogo*, Named_type*);
  1256. void
  1257. do_reflection(Gogo*, std::string*) const;
  1258. void
  1259. do_gc_symbol(Gogo*, Expression_list**, Expression** offset, int)
  1260. { this->advance_gc_offset(offset); }
  1261. void
  1262. do_mangled_name(Gogo*, std::string*) const;
  1263. private:
  1264. Integer_type(bool is_abstract, bool is_unsigned, int bits,
  1265. int runtime_type_kind)
  1266. : Type(TYPE_INTEGER),
  1267. is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
  1268. is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
  1269. { }
  1270. // Map names of integer types to the types themselves.
  1271. typedef std::map<std::string, Named_type*> Named_integer_types;
  1272. static Named_integer_types named_integer_types;
  1273. // True if this is an abstract type.
  1274. bool is_abstract_;
  1275. // True if this is an unsigned type.
  1276. bool is_unsigned_;
  1277. // True if this is the byte type.
  1278. bool is_byte_;
  1279. // True if this is the rune type.
  1280. bool is_rune_;
  1281. // The number of bits.
  1282. int bits_;
  1283. // The runtime type code used in the type descriptor for this type.
  1284. int runtime_type_kind_;
  1285. };
  1286. // The type of a floating point number.
  1287. class Float_type : public Type
  1288. {
  1289. public:
  1290. // Create a new float type.
  1291. static Named_type*
  1292. create_float_type(const char* name, int bits, int runtime_type_kind);
  1293. // Look up an existing float type.
  1294. static Named_type*
  1295. lookup_float_type(const char* name);
  1296. // Create an abstract float type.
  1297. static Float_type*
  1298. create_abstract_float_type();
  1299. // Whether this is an abstract float type.
  1300. bool
  1301. is_abstract() const
  1302. { return this->is_abstract_; }
  1303. // The number of bits.
  1304. int
  1305. bits() const
  1306. { return this->bits_; }
  1307. // Whether this type is the same as T.
  1308. bool
  1309. is_identical(const Float_type* t) const;
  1310. protected:
  1311. bool
  1312. do_compare_is_identity(Gogo*)
  1313. { return false; }
  1314. unsigned int
  1315. do_hash_for_method(Gogo*) const;
  1316. Btype*
  1317. do_get_backend(Gogo*);
  1318. Expression*
  1319. do_type_descriptor(Gogo*, Named_type*);
  1320. void
  1321. do_reflection(Gogo*, std::string*) const;
  1322. void
  1323. do_gc_symbol(Gogo*, Expression_list**, Expression** offset, int)
  1324. { this->advance_gc_offset(offset); }
  1325. void
  1326. do_mangled_name(Gogo*, std::string*) const;
  1327. private:
  1328. Float_type(bool is_abstract, int bits, int runtime_type_kind)
  1329. : Type(TYPE_FLOAT),
  1330. is_abstract_(is_abstract), bits_(bits),
  1331. runtime_type_kind_(runtime_type_kind)
  1332. { }
  1333. // Map names of float types to the types themselves.
  1334. typedef std::map<std::string, Named_type*> Named_float_types;
  1335. static Named_float_types named_float_types;
  1336. // True if this is an abstract type.
  1337. bool is_abstract_;
  1338. // The number of bits in the floating point value.
  1339. int bits_;
  1340. // The runtime type code used in the type descriptor for this type.
  1341. int runtime_type_kind_;
  1342. };
  1343. // The type of a complex number.
  1344. class Complex_type : public Type
  1345. {
  1346. public:
  1347. // Create a new complex type.
  1348. static Named_type*
  1349. create_complex_type(const char* name, int bits, int runtime_type_kind);
  1350. // Look up an existing complex type.
  1351. static Named_type*
  1352. lookup_complex_type(const char* name);
  1353. // Create an abstract complex type.
  1354. static Complex_type*
  1355. create_abstract_complex_type();
  1356. // Whether this is an abstract complex type.
  1357. bool
  1358. is_abstract() const
  1359. { return this->is_abstract_; }
  1360. // The number of bits: 64 or 128.
  1361. int bits() const
  1362. { return this->bits_; }
  1363. // Whether this type is the same as T.
  1364. bool
  1365. is_identical(const Complex_type* t) const;
  1366. protected:
  1367. bool
  1368. do_compare_is_identity(Gogo*)
  1369. { return false; }
  1370. unsigned int
  1371. do_hash_for_method(Gogo*) const;
  1372. Btype*
  1373. do_get_backend(Gogo*);
  1374. Expression*
  1375. do_type_descriptor(Gogo*, Named_type*);
  1376. void
  1377. do_reflection(Gogo*, std::string*) const;
  1378. void
  1379. do_gc_symbol(Gogo*, Expression_list**, Expression** offset, int)
  1380. { this->advance_gc_offset(offset); }
  1381. void
  1382. do_mangled_name(Gogo*, std::string*) const;
  1383. private:
  1384. Complex_type(bool is_abstract, int bits, int runtime_type_kind)
  1385. : Type(TYPE_COMPLEX),
  1386. is_abstract_(is_abstract), bits_(bits),
  1387. runtime_type_kind_(runtime_type_kind)
  1388. { }
  1389. // Map names of complex types to the types themselves.
  1390. typedef std::map<std::string, Named_type*> Named_complex_types;
  1391. static Named_complex_types named_complex_types;
  1392. // True if this is an abstract type.
  1393. bool is_abstract_;
  1394. // The number of bits in the complex value--64 or 128.
  1395. int bits_;
  1396. // The runtime type code used in the type descriptor for this type.
  1397. int runtime_type_kind_;
  1398. };
  1399. // The type of a string.
  1400. class String_type : public Type
  1401. {
  1402. public:
  1403. String_type()
  1404. : Type(TYPE_STRING)
  1405. { }
  1406. protected:
  1407. bool
  1408. do_has_pointer() const
  1409. { return true; }
  1410. bool
  1411. do_compare_is_identity(Gogo*)
  1412. { return false; }
  1413. Btype*
  1414. do_get_backend(Gogo*);
  1415. Expression*
  1416. do_type_descriptor(Gogo*, Named_type*);
  1417. void
  1418. do_reflection(Gogo*, std::string*) const;
  1419. void
  1420. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1421. void
  1422. do_mangled_name(Gogo*, std::string* ret) const;
  1423. private:
  1424. // The named string type.
  1425. static Named_type* string_type_;
  1426. };
  1427. // The type of a function.
  1428. class Function_type : public Type
  1429. {
  1430. public:
  1431. Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
  1432. Typed_identifier_list* results, Location location)
  1433. : Type(TYPE_FUNCTION),
  1434. receiver_(receiver), parameters_(parameters), results_(results),
  1435. location_(location), is_varargs_(false), is_builtin_(false),
  1436. fnbtype_(NULL)
  1437. { }
  1438. // Get the receiver.
  1439. const Typed_identifier*
  1440. receiver() const
  1441. { return this->receiver_; }
  1442. // Get the return names and types.
  1443. const Typed_identifier_list*
  1444. results() const
  1445. { return this->results_; }
  1446. // Get the parameter names and types.
  1447. const Typed_identifier_list*
  1448. parameters() const
  1449. { return this->parameters_; }
  1450. // Whether this is a varargs function.
  1451. bool
  1452. is_varargs() const
  1453. { return this->is_varargs_; }
  1454. // Whether this is a builtin function.
  1455. bool
  1456. is_builtin() const
  1457. { return this->is_builtin_; }
  1458. // The location where this type was defined.
  1459. Location
  1460. location() const
  1461. { return this->location_; }
  1462. // Return whether this is a method type.
  1463. bool
  1464. is_method() const
  1465. { return this->receiver_ != NULL; }
  1466. // Whether T is a valid redeclaration of this type. This is called
  1467. // when a function is declared more than once.
  1468. bool
  1469. is_valid_redeclaration(const Function_type* t, std::string*) const;
  1470. // Whether this type is the same as T.
  1471. bool
  1472. is_identical(const Function_type* t, bool ignore_receiver,
  1473. bool errors_are_identical, std::string*) const;
  1474. // Record that this is a varargs function.
  1475. void
  1476. set_is_varargs()
  1477. { this->is_varargs_ = true; }
  1478. // Record that this is a builtin function.
  1479. void
  1480. set_is_builtin()
  1481. { this->is_builtin_ = true; }
  1482. // Import a function type.
  1483. static Function_type*
  1484. do_import(Import*);
  1485. // Return a copy of this type without a receiver. This is only
  1486. // valid for a method type.
  1487. Function_type*
  1488. copy_without_receiver() const;
  1489. // Return a copy of this type with a receiver. This is used when an
  1490. // interface method is attached to a named or struct type.
  1491. Function_type*
  1492. copy_with_receiver(Type*) const;
  1493. // Return a copy of this type with the receiver treated as the first
  1494. // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
  1495. // forced to be a pointer.
  1496. Function_type*
  1497. copy_with_receiver_as_param(bool want_pointer_receiver) const;
  1498. // Return a copy of this type ignoring any receiver and using dummy
  1499. // names for all parameters. This is used for thunks for method
  1500. // values.
  1501. Function_type*
  1502. copy_with_names() const;
  1503. static Type*
  1504. make_function_type_descriptor_type();
  1505. // Return the backend representation of this function type. This is used
  1506. // as the real type of a backend function declaration or defintion.
  1507. Btype*
  1508. get_backend_fntype(Gogo*);
  1509. protected:
  1510. int
  1511. do_traverse(Traverse*);
  1512. // A function descriptor may be allocated on the heap.
  1513. bool
  1514. do_has_pointer() const
  1515. { return true; }
  1516. bool
  1517. do_compare_is_identity(Gogo*)
  1518. { return false; }
  1519. unsigned int
  1520. do_hash_for_method(Gogo*) const;
  1521. Btype*
  1522. do_get_backend(Gogo*);
  1523. Expression*
  1524. do_type_descriptor(Gogo*, Named_type*);
  1525. void
  1526. do_reflection(Gogo*, std::string*) const;
  1527. void
  1528. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1529. void
  1530. do_mangled_name(Gogo*, std::string*) const;
  1531. void
  1532. do_export(Export*) const;
  1533. private:
  1534. Expression*
  1535. type_descriptor_params(Type*, const Typed_identifier*,
  1536. const Typed_identifier_list*);
  1537. // A mapping from a list of result types to a backend struct type.
  1538. class Results_hash
  1539. {
  1540. public:
  1541. unsigned int
  1542. operator()(const Typed_identifier_list*) const;
  1543. };
  1544. class Results_equal
  1545. {
  1546. public:
  1547. bool
  1548. operator()(const Typed_identifier_list*,
  1549. const Typed_identifier_list*) const;
  1550. };
  1551. typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
  1552. Results_hash, Results_equal) Results_structs;
  1553. static Results_structs results_structs;
  1554. // The receiver name and type. This will be NULL for a normal
  1555. // function, non-NULL for a method.
  1556. Typed_identifier* receiver_;
  1557. // The parameter names and types.
  1558. Typed_identifier_list* parameters_;
  1559. // The result names and types. This will be NULL if no result was
  1560. // specified.
  1561. Typed_identifier_list* results_;
  1562. // The location where this type was defined. This exists solely to
  1563. // give a location for the fields of the struct if this function
  1564. // returns multiple values.
  1565. Location location_;
  1566. // Whether this function takes a variable number of arguments.
  1567. bool is_varargs_;
  1568. // Whether this is a special builtin function which can not simply
  1569. // be called. This is used for len, cap, etc.
  1570. bool is_builtin_;
  1571. // The backend representation of this type for backend function
  1572. // declarations and definitions.
  1573. Btype* fnbtype_;
  1574. };
  1575. // The type of a function's backend representation.
  1576. class Backend_function_type : public Function_type
  1577. {
  1578. public:
  1579. Backend_function_type(Typed_identifier* receiver,
  1580. Typed_identifier_list* parameters,
  1581. Typed_identifier_list* results, Location location)
  1582. : Function_type(receiver, parameters, results, location)
  1583. { }
  1584. protected:
  1585. Btype*
  1586. do_get_backend(Gogo* gogo)
  1587. { return this->get_backend_fntype(gogo); }
  1588. };
  1589. // The type of a pointer.
  1590. class Pointer_type : public Type
  1591. {
  1592. public:
  1593. Pointer_type(Type* to_type)
  1594. : Type(TYPE_POINTER),
  1595. to_type_(to_type)
  1596. {}
  1597. Type*
  1598. points_to() const
  1599. { return this->to_type_; }
  1600. // Import a pointer type.
  1601. static Pointer_type*
  1602. do_import(Import*);
  1603. static Type*
  1604. make_pointer_type_descriptor_type();
  1605. protected:
  1606. int
  1607. do_traverse(Traverse*);
  1608. bool
  1609. do_has_pointer() const
  1610. { return true; }
  1611. bool
  1612. do_compare_is_identity(Gogo*)
  1613. { return true; }
  1614. unsigned int
  1615. do_hash_for_method(Gogo*) const;
  1616. Btype*
  1617. do_get_backend(Gogo*);
  1618. Expression*
  1619. do_type_descriptor(Gogo*, Named_type*);
  1620. void
  1621. do_reflection(Gogo*, std::string*) const;
  1622. void
  1623. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1624. void
  1625. do_mangled_name(Gogo*, std::string*) const;
  1626. void
  1627. do_export(Export*) const;
  1628. private:
  1629. // The type to which this type points.
  1630. Type* to_type_;
  1631. };
  1632. // The type of a field in a struct.
  1633. class Struct_field
  1634. {
  1635. public:
  1636. explicit Struct_field(const Typed_identifier& typed_identifier)
  1637. : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
  1638. { }
  1639. // The field name.
  1640. const std::string&
  1641. field_name() const;
  1642. // Return whether this struct field is named NAME.
  1643. bool
  1644. is_field_name(const std::string& name) const;
  1645. // Return whether this struct field is an unexported field named NAME.
  1646. bool
  1647. is_unexported_field_name(Gogo*, const std::string& name) const;
  1648. // Return whether this struct field is an embedded built-in type.
  1649. bool
  1650. is_embedded_builtin(Gogo*) const;
  1651. // The field type.
  1652. Type*
  1653. type() const
  1654. { return this->typed_identifier_.type(); }
  1655. // The field location.
  1656. Location
  1657. location() const
  1658. { return this->typed_identifier_.location(); }
  1659. // Whether the field has a tag.
  1660. bool
  1661. has_tag() const
  1662. { return this->tag_ != NULL; }
  1663. // The tag.
  1664. const std::string&
  1665. tag() const
  1666. {
  1667. go_assert(this->tag_ != NULL);
  1668. return *this->tag_;
  1669. }
  1670. // Whether this is an anonymous field.
  1671. bool
  1672. is_anonymous() const
  1673. { return this->typed_identifier_.name().empty(); }
  1674. // Set the tag. FIXME: This is never freed.
  1675. void
  1676. set_tag(const std::string& tag)
  1677. { this->tag_ = new std::string(tag); }
  1678. // Record that this field is defined in an imported struct.
  1679. void
  1680. set_is_imported()
  1681. { this->is_imported_ = true; }
  1682. // Set the type. This is only used in error cases.
  1683. void
  1684. set_type(Type* type)
  1685. { this->typed_identifier_.set_type(type); }
  1686. private:
  1687. // The field name, type, and location.
  1688. Typed_identifier typed_identifier_;
  1689. // The field tag. This is NULL if the field has no tag.
  1690. std::string* tag_;
  1691. // Whether this field is defined in an imported struct.
  1692. bool is_imported_;
  1693. };
  1694. // A list of struct fields.
  1695. class Struct_field_list
  1696. {
  1697. public:
  1698. Struct_field_list()
  1699. : entries_()
  1700. { }
  1701. // Whether the list is empty.
  1702. bool
  1703. empty() const
  1704. { return this->entries_.empty(); }
  1705. // Return the number of entries.
  1706. size_t
  1707. size() const
  1708. { return this->entries_.size(); }
  1709. // Add an entry to the end of the list.
  1710. void
  1711. push_back(const Struct_field& sf)
  1712. { this->entries_.push_back(sf); }
  1713. // Index into the list.
  1714. const Struct_field&
  1715. at(size_t i) const
  1716. { return this->entries_.at(i); }
  1717. // Last entry in list.
  1718. Struct_field&
  1719. back()
  1720. { return this->entries_.back(); }
  1721. // Iterators.
  1722. typedef std::vector<Struct_field>::iterator iterator;
  1723. typedef std::vector<Struct_field>::const_iterator const_iterator;
  1724. iterator
  1725. begin()
  1726. { return this->entries_.begin(); }
  1727. const_iterator
  1728. begin() const
  1729. { return this->entries_.begin(); }
  1730. iterator
  1731. end()
  1732. { return this->entries_.end(); }
  1733. const_iterator
  1734. end() const
  1735. { return this->entries_.end(); }
  1736. private:
  1737. std::vector<Struct_field> entries_;
  1738. };
  1739. // The type of a struct.
  1740. class Struct_type : public Type
  1741. {
  1742. public:
  1743. Struct_type(Struct_field_list* fields, Location location)
  1744. : Type(TYPE_STRUCT),
  1745. fields_(fields), location_(location), all_methods_(NULL)
  1746. { }
  1747. // Return the field NAME. This only looks at local fields, not at
  1748. // embedded types. If the field is found, and PINDEX is not NULL,
  1749. // this sets *PINDEX to the field index. If the field is not found,
  1750. // this returns NULL.
  1751. const Struct_field*
  1752. find_local_field(const std::string& name, unsigned int *pindex) const;
  1753. // Return the field number INDEX.
  1754. const Struct_field*
  1755. field(unsigned int index) const
  1756. { return &this->fields_->at(index); }
  1757. // Get the struct fields.
  1758. const Struct_field_list*
  1759. fields() const
  1760. { return this->fields_; }
  1761. // Return the number of fields.
  1762. size_t
  1763. field_count() const
  1764. { return this->fields_->size(); }
  1765. // Push a new field onto the end of the struct. This is used when
  1766. // building a closure variable.
  1767. void
  1768. push_field(const Struct_field& sf)
  1769. { this->fields_->push_back(sf); }
  1770. // Return an expression referring to field NAME in STRUCT_EXPR, or
  1771. // NULL if there is no field with that name.
  1772. Field_reference_expression*
  1773. field_reference(Expression* struct_expr, const std::string& name,
  1774. Location) const;
  1775. // Return the total number of fields, including embedded fields.
  1776. // This is the number of values that can appear in a conversion to
  1777. // this type.
  1778. unsigned int
  1779. total_field_count() const;
  1780. // Whether this type is identical with T.
  1781. bool
  1782. is_identical(const Struct_type* t, bool errors_are_identical) const;
  1783. // Return whether NAME is a local field which is not exported. This
  1784. // is only used for better error reporting.
  1785. bool
  1786. is_unexported_local_field(Gogo*, const std::string& name) const;
  1787. // If this is an unnamed struct, build the complete list of methods,
  1788. // including those from anonymous fields, and build methods stubs if
  1789. // needed.
  1790. void
  1791. finalize_methods(Gogo*);
  1792. // Return whether this type has any methods. This should only be
  1793. // called after the finalize_methods pass.
  1794. bool
  1795. has_any_methods() const
  1796. { return this->all_methods_ != NULL; }
  1797. // Return the methods for tihs type. This should only be called
  1798. // after the finalize_methods pass.
  1799. const Methods*
  1800. methods() const
  1801. { return this->all_methods_; }
  1802. // Return the method to use for NAME. This returns NULL if there is
  1803. // no such method or if the method is ambiguous. When it returns
  1804. // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
  1805. Method*
  1806. method_function(const std::string& name, bool* is_ambiguous) const;
  1807. // Return a pointer to the interface method table for this type for
  1808. // the interface INTERFACE. If IS_POINTER is true, set the type
  1809. // descriptor to a pointer to this type, otherwise set it to this
  1810. // type.
  1811. Expression*
  1812. interface_method_table(Interface_type* interface, bool is_pointer);
  1813. // Traverse just the field types of a struct type.
  1814. int
  1815. traverse_field_types(Traverse* traverse)
  1816. { return this->do_traverse(traverse); }
  1817. // If the offset of field INDEX in the backend implementation can be
  1818. // determined, set *POFFSET to the offset in bytes and return true.
  1819. // Otherwise, return false.
  1820. bool
  1821. backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
  1822. // Finish the backend representation of all the fields.
  1823. void
  1824. finish_backend_fields(Gogo*);
  1825. // Import a struct type.
  1826. static Struct_type*
  1827. do_import(Import*);
  1828. static Type*
  1829. make_struct_type_descriptor_type();
  1830. // Write the hash function for this type.
  1831. void
  1832. write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
  1833. // Write the equality function for this type.
  1834. void
  1835. write_equal_function(Gogo*, Named_type*);
  1836. protected:
  1837. int
  1838. do_traverse(Traverse*);
  1839. bool
  1840. do_verify();
  1841. bool
  1842. do_has_pointer() const;
  1843. bool
  1844. do_compare_is_identity(Gogo*);
  1845. unsigned int
  1846. do_hash_for_method(Gogo*) const;
  1847. Btype*
  1848. do_get_backend(Gogo*);
  1849. Expression*
  1850. do_type_descriptor(Gogo*, Named_type*);
  1851. void
  1852. do_reflection(Gogo*, std::string*) const;
  1853. void
  1854. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1855. void
  1856. do_mangled_name(Gogo*, std::string*) const;
  1857. void
  1858. do_export(Export*) const;
  1859. private:
  1860. // Used to merge method sets of identical unnamed structs.
  1861. typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
  1862. Type_identical) Identical_structs;
  1863. static Identical_structs identical_structs;
  1864. // Used to manage method tables for identical unnamed structs.
  1865. typedef std::pair<Interface_method_tables*, Interface_method_tables*>
  1866. Struct_method_table_pair;
  1867. typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
  1868. Type_hash_identical, Type_identical)
  1869. Struct_method_tables;
  1870. static Struct_method_tables struct_method_tables;
  1871. // Used to avoid infinite loops in field_reference_depth.
  1872. struct Saw_named_type
  1873. {
  1874. Saw_named_type* next;
  1875. Named_type* nt;
  1876. };
  1877. Field_reference_expression*
  1878. field_reference_depth(Expression* struct_expr, const std::string& name,
  1879. Location, Saw_named_type*,
  1880. unsigned int* depth) const;
  1881. // The fields of the struct.
  1882. Struct_field_list* fields_;
  1883. // The place where the struct was declared.
  1884. Location location_;
  1885. // If this struct is unnamed, a list of methods.
  1886. Methods* all_methods_;
  1887. };
  1888. // The type of an array.
  1889. class Array_type : public Type
  1890. {
  1891. public:
  1892. Array_type(Type* element_type, Expression* length)
  1893. : Type(TYPE_ARRAY),
  1894. element_type_(element_type), length_(length), blength_(NULL),
  1895. issued_length_error_(false)
  1896. { }
  1897. // Return the element type.
  1898. Type*
  1899. element_type() const
  1900. { return this->element_type_; }
  1901. // Return the length. This will return NULL for a slice.
  1902. Expression*
  1903. length() const
  1904. { return this->length_; }
  1905. // Whether this type is identical with T.
  1906. bool
  1907. is_identical(const Array_type* t, bool errors_are_identical) const;
  1908. // Return an expression for the pointer to the values in an array.
  1909. Expression*
  1910. get_value_pointer(Gogo*, Expression* array) const;
  1911. // Return an expression for the length of an array with this type.
  1912. Expression*
  1913. get_length(Gogo*, Expression* array) const;
  1914. // Return an expression for the capacity of an array with this type.
  1915. Expression*
  1916. get_capacity(Gogo*, Expression* array) const;
  1917. // Import an array type.
  1918. static Array_type*
  1919. do_import(Import*);
  1920. // Return the backend representation of the element type.
  1921. Btype*
  1922. get_backend_element(Gogo*, bool use_placeholder);
  1923. // Return the backend representation of the length.
  1924. Bexpression*
  1925. get_backend_length(Gogo*);
  1926. // Finish the backend representation of the element type.
  1927. void
  1928. finish_backend_element(Gogo*);
  1929. static Type*
  1930. make_array_type_descriptor_type();
  1931. static Type*
  1932. make_slice_type_descriptor_type();
  1933. // Write the hash function for this type.
  1934. void
  1935. write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
  1936. // Write the equality function for this type.
  1937. void
  1938. write_equal_function(Gogo*, Named_type*);
  1939. protected:
  1940. int
  1941. do_traverse(Traverse* traverse);
  1942. bool
  1943. do_verify();
  1944. bool
  1945. do_has_pointer() const
  1946. {
  1947. return this->length_ == NULL || this->element_type_->has_pointer();
  1948. }
  1949. bool
  1950. do_compare_is_identity(Gogo*);
  1951. unsigned int
  1952. do_hash_for_method(Gogo*) const;
  1953. Btype*
  1954. do_get_backend(Gogo*);
  1955. Expression*
  1956. do_type_descriptor(Gogo*, Named_type*);
  1957. void
  1958. do_reflection(Gogo*, std::string*) const;
  1959. void
  1960. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1961. void
  1962. do_mangled_name(Gogo*, std::string*) const;
  1963. void
  1964. do_export(Export*) const;
  1965. private:
  1966. bool
  1967. verify_length();
  1968. Expression*
  1969. array_type_descriptor(Gogo*, Named_type*);
  1970. Expression*
  1971. slice_type_descriptor(Gogo*, Named_type*);
  1972. void
  1973. slice_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1974. void
  1975. array_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  1976. // The type of elements of the array.
  1977. Type* element_type_;
  1978. // The number of elements. This may be NULL.
  1979. Expression* length_;
  1980. // The backend representation of the length.
  1981. // We only want to compute this once.
  1982. Bexpression* blength_;
  1983. // Whether or not an invalid length error has been issued for this type,
  1984. // to avoid knock-on errors.
  1985. mutable bool issued_length_error_;
  1986. };
  1987. // The type of a map.
  1988. class Map_type : public Type
  1989. {
  1990. public:
  1991. Map_type(Type* key_type, Type* val_type, Location location)
  1992. : Type(TYPE_MAP),
  1993. key_type_(key_type), val_type_(val_type), location_(location)
  1994. { }
  1995. // Return the key type.
  1996. Type*
  1997. key_type() const
  1998. { return this->key_type_; }
  1999. // Return the value type.
  2000. Type*
  2001. val_type() const
  2002. { return this->val_type_; }
  2003. // Whether this type is identical with T.
  2004. bool
  2005. is_identical(const Map_type* t, bool errors_are_identical) const;
  2006. // Import a map type.
  2007. static Map_type*
  2008. do_import(Import*);
  2009. static Type*
  2010. make_map_type_descriptor_type();
  2011. static Type*
  2012. make_map_descriptor_type();
  2013. // Build a map descriptor for this type. Return a pointer to it.
  2014. // The location is the location which causes us to need the
  2015. // descriptor.
  2016. Bexpression*
  2017. map_descriptor_pointer(Gogo* gogo, Location);
  2018. protected:
  2019. int
  2020. do_traverse(Traverse*);
  2021. bool
  2022. do_verify();
  2023. bool
  2024. do_has_pointer() const
  2025. { return true; }
  2026. bool
  2027. do_compare_is_identity(Gogo*)
  2028. { return false; }
  2029. unsigned int
  2030. do_hash_for_method(Gogo*) const;
  2031. Btype*
  2032. do_get_backend(Gogo*);
  2033. Expression*
  2034. do_type_descriptor(Gogo*, Named_type*);
  2035. void
  2036. do_reflection(Gogo*, std::string*) const;
  2037. void
  2038. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  2039. void
  2040. do_mangled_name(Gogo*, std::string*) const;
  2041. void
  2042. do_export(Export*) const;
  2043. private:
  2044. // Mapping from map types to map descriptors.
  2045. typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
  2046. Type_identical) Map_descriptors;
  2047. static Map_descriptors map_descriptors;
  2048. Bvariable*
  2049. map_descriptor(Gogo*);
  2050. // The key type.
  2051. Type* key_type_;
  2052. // The value type.
  2053. Type* val_type_;
  2054. // Where the type was defined.
  2055. Location location_;
  2056. };
  2057. // The type of a channel.
  2058. class Channel_type : public Type
  2059. {
  2060. public:
  2061. Channel_type(bool may_send, bool may_receive, Type* element_type)
  2062. : Type(TYPE_CHANNEL),
  2063. may_send_(may_send), may_receive_(may_receive),
  2064. element_type_(element_type)
  2065. { go_assert(may_send || may_receive); }
  2066. // Whether this channel can send data.
  2067. bool
  2068. may_send() const
  2069. { return this->may_send_; }
  2070. // Whether this channel can receive data.
  2071. bool
  2072. may_receive() const
  2073. { return this->may_receive_; }
  2074. // The type of the values that may be sent on this channel. This is
  2075. // NULL if any type may be sent.
  2076. Type*
  2077. element_type() const
  2078. { return this->element_type_; }
  2079. // Whether this type is identical with T.
  2080. bool
  2081. is_identical(const Channel_type* t, bool errors_are_identical) const;
  2082. // Import a channel type.
  2083. static Channel_type*
  2084. do_import(Import*);
  2085. static Type*
  2086. make_chan_type_descriptor_type();
  2087. protected:
  2088. int
  2089. do_traverse(Traverse* traverse)
  2090. { return Type::traverse(this->element_type_, traverse); }
  2091. bool
  2092. do_has_pointer() const
  2093. { return true; }
  2094. bool
  2095. do_compare_is_identity(Gogo*)
  2096. { return true; }
  2097. unsigned int
  2098. do_hash_for_method(Gogo*) const;
  2099. Btype*
  2100. do_get_backend(Gogo*);
  2101. Expression*
  2102. do_type_descriptor(Gogo*, Named_type*);
  2103. void
  2104. do_reflection(Gogo*, std::string*) const;
  2105. void
  2106. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  2107. void
  2108. do_mangled_name(Gogo*, std::string*) const;
  2109. void
  2110. do_export(Export*) const;
  2111. private:
  2112. // Whether this channel can send data.
  2113. bool may_send_;
  2114. // Whether this channel can receive data.
  2115. bool may_receive_;
  2116. // The types of elements which may be sent on this channel. If this
  2117. // is NULL, it means that any type may be sent.
  2118. Type* element_type_;
  2119. };
  2120. // An interface type.
  2121. class Interface_type : public Type
  2122. {
  2123. public:
  2124. Interface_type(Typed_identifier_list* methods, Location location)
  2125. : Type(TYPE_INTERFACE),
  2126. parse_methods_(methods), all_methods_(NULL), location_(location),
  2127. interface_btype_(NULL), bmethods_(NULL), assume_identical_(NULL),
  2128. methods_are_finalized_(false), bmethods_is_placeholder_(false),
  2129. seen_(false)
  2130. { go_assert(methods == NULL || !methods->empty()); }
  2131. // The location where the interface type was defined.
  2132. Location
  2133. location() const
  2134. { return this->location_; }
  2135. // Return whether this is an empty interface.
  2136. bool
  2137. is_empty() const
  2138. {
  2139. go_assert(this->methods_are_finalized_);
  2140. return this->all_methods_ == NULL;
  2141. }
  2142. // Return the list of methods. This will return NULL for an empty
  2143. // interface.
  2144. const Typed_identifier_list*
  2145. methods() const;
  2146. // Return the number of methods.
  2147. size_t
  2148. method_count() const;
  2149. // Return the method NAME, or NULL.
  2150. const Typed_identifier*
  2151. find_method(const std::string& name) const;
  2152. // Return the zero-based index of method NAME.
  2153. size_t
  2154. method_index(const std::string& name) const;
  2155. // Finalize the methods. This sets all_methods_. This handles
  2156. // interface inheritance.
  2157. void
  2158. finalize_methods();
  2159. // Return true if T implements this interface. If this returns
  2160. // false, and REASON is not NULL, it sets *REASON to the reason that
  2161. // it fails.
  2162. bool
  2163. implements_interface(const Type* t, std::string* reason) const;
  2164. // Whether this type is identical with T. REASON is as in
  2165. // implements_interface.
  2166. bool
  2167. is_identical(const Interface_type* t, bool errors_are_identical) const;
  2168. // Whether we can assign T to this type. is_identical is known to
  2169. // be false.
  2170. bool
  2171. is_compatible_for_assign(const Interface_type*, std::string* reason) const;
  2172. // Return whether NAME is a method which is not exported. This is
  2173. // only used for better error reporting.
  2174. bool
  2175. is_unexported_method(Gogo*, const std::string& name) const;
  2176. // Import an interface type.
  2177. static Interface_type*
  2178. do_import(Import*);
  2179. // Make a struct for an empty interface type.
  2180. static Btype*
  2181. get_backend_empty_interface_type(Gogo*);
  2182. // Get a pointer to the backend representation of the method table.
  2183. Btype*
  2184. get_backend_methods(Gogo*);
  2185. // Return a placeholder for the backend representation of the
  2186. // pointer to the method table.
  2187. Btype*
  2188. get_backend_methods_placeholder(Gogo*);
  2189. // Finish the backend representation of the method types.
  2190. void
  2191. finish_backend_methods(Gogo*);
  2192. static Type*
  2193. make_interface_type_descriptor_type();
  2194. protected:
  2195. int
  2196. do_traverse(Traverse*);
  2197. bool
  2198. do_has_pointer() const
  2199. { return true; }
  2200. bool
  2201. do_compare_is_identity(Gogo*)
  2202. { return false; }
  2203. unsigned int
  2204. do_hash_for_method(Gogo*) const;
  2205. Btype*
  2206. do_get_backend(Gogo*);
  2207. Expression*
  2208. do_type_descriptor(Gogo*, Named_type*);
  2209. void
  2210. do_reflection(Gogo*, std::string*) const;
  2211. void
  2212. do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
  2213. void
  2214. do_mangled_name(Gogo*, std::string*) const;
  2215. void
  2216. do_export(Export*) const;
  2217. private:
  2218. // This type guards against infinite recursion when comparing
  2219. // interface types. We keep a list of interface types assumed to be
  2220. // identical during comparison. We just keep the list on the stack.
  2221. // This permits us to compare cases like
  2222. // type I1 interface { F() interface{I1} }
  2223. // type I2 interface { F() interface{I2} }
  2224. struct Assume_identical
  2225. {
  2226. Assume_identical* next;
  2227. const Interface_type* t1;
  2228. const Interface_type* t2;
  2229. };
  2230. bool
  2231. assume_identical(const Interface_type*, const Interface_type*) const;
  2232. // The list of methods associated with the interface from the
  2233. // parser. This will be NULL for the empty interface. This may
  2234. // include unnamed interface types.
  2235. Typed_identifier_list* parse_methods_;
  2236. // The list of all methods associated with the interface. This
  2237. // expands any interface types listed in methods_. It is set by
  2238. // finalize_methods. This will be NULL for the empty interface.
  2239. Typed_identifier_list* all_methods_;
  2240. // The location where the interface was defined.
  2241. Location location_;
  2242. // The backend representation of this type during backend conversion.
  2243. Btype* interface_btype_;
  2244. // The backend representation of the pointer to the method table.
  2245. Btype* bmethods_;
  2246. // A list of interface types assumed to be identical during
  2247. // interface comparison.
  2248. mutable Assume_identical* assume_identical_;
  2249. // Whether the methods have been finalized.
  2250. bool methods_are_finalized_;
  2251. // Whether the bmethods_ field is a placeholder.
  2252. bool bmethods_is_placeholder_;
  2253. // Used to avoid endless recursion in do_mangled_name.
  2254. mutable bool seen_;
  2255. };
  2256. // The value we keep for a named type. This lets us get the right
  2257. // name when we convert to backend. Note that we don't actually keep
  2258. // the name here; the name is in the Named_object which points to
  2259. // this. This object exists to hold a unique backend representation for
  2260. // the type.
  2261. class Named_type : public Type
  2262. {
  2263. public:
  2264. Named_type(Named_object* named_object, Type* type, Location location)
  2265. : Type(TYPE_NAMED),
  2266. named_object_(named_object), in_function_(NULL), in_function_index_(0),
  2267. type_(type), local_methods_(NULL), all_methods_(NULL),
  2268. interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
  2269. location_(location), named_btype_(NULL), dependencies_(),
  2270. is_visible_(true), is_error_(false), is_placeholder_(false),
  2271. is_converted_(false), is_circular_(false), is_verified_(false),
  2272. seen_(false), seen_in_compare_is_identity_(false),
  2273. seen_in_get_backend_(false)
  2274. { }
  2275. // Return the associated Named_object. This holds the actual name.
  2276. Named_object*
  2277. named_object()
  2278. { return this->named_object_; }
  2279. const Named_object*
  2280. named_object() const
  2281. { return this->named_object_; }
  2282. // Set the Named_object. This is used when we see a type
  2283. // declaration followed by a type.
  2284. void
  2285. set_named_object(Named_object* no)
  2286. { this->named_object_ = no; }
  2287. // Return the function in which this type is defined. This will
  2288. // return NULL for a type defined in global scope.
  2289. const Named_object*
  2290. in_function(unsigned int *pindex) const
  2291. {
  2292. *pindex = this->in_function_index_;
  2293. return this->in_function_;
  2294. }
  2295. // Set the function in which this type is defined.
  2296. void
  2297. set_in_function(Named_object* f, unsigned int index)
  2298. {
  2299. this->in_function_ = f;
  2300. this->in_function_index_ = index;
  2301. }
  2302. // Return the name of the type.
  2303. const std::string&
  2304. name() const;
  2305. // Return the name of the type for an error message. The difference
  2306. // is that if the type is defined in a different package, this will
  2307. // return PACKAGE.NAME.
  2308. std::string
  2309. message_name() const;
  2310. // Return the underlying type.
  2311. Type*
  2312. real_type()
  2313. { return this->type_; }
  2314. const Type*
  2315. real_type() const
  2316. { return this->type_; }
  2317. // Return the location.
  2318. Location
  2319. location() const
  2320. { return this->location_; }
  2321. // Whether this type is visible. This only matters when parsing.
  2322. bool
  2323. is_visible() const
  2324. { return this->is_visible_; }
  2325. // Mark this type as visible.
  2326. void
  2327. set_is_visible()
  2328. { this->is_visible_ = true; }
  2329. // Mark this type as invisible.
  2330. void
  2331. clear_is_visible()
  2332. { this->is_visible_ = false; }
  2333. // Whether this is a builtin type.
  2334. bool
  2335. is_builtin() const
  2336. { return Linemap::is_predeclared_location(this->location_); }
  2337. // Whether this is an alias. There are currently two aliases: byte
  2338. // and rune.
  2339. bool
  2340. is_alias() const;
  2341. // Whether this named type is valid. A recursive named type is invalid.
  2342. bool
  2343. is_valid() const
  2344. { return !this->is_error_; }
  2345. // Whether this is a circular type: a pointer or function type that
  2346. // refers to itself, which is not possible in C.
  2347. bool
  2348. is_circular() const
  2349. { return this->is_circular_; }
  2350. // Return the base type for this type.
  2351. Type*
  2352. named_base();
  2353. const Type*
  2354. named_base() const;
  2355. // Return whether this is an error type.
  2356. bool
  2357. is_named_error_type() const;
  2358. // Return whether this type is comparable. If REASON is not NULL,
  2359. // set *REASON when returning false.
  2360. bool
  2361. named_type_is_comparable(std::string* reason) const;
  2362. // Add a method to this type.
  2363. Named_object*
  2364. add_method(const std::string& name, Function*);
  2365. // Add a method declaration to this type.
  2366. Named_object*
  2367. add_method_declaration(const std::string& name, Package* package,
  2368. Function_type* type, Location location);
  2369. // Add an existing method--one defined before the type itself was
  2370. // defined--to a type.
  2371. void
  2372. add_existing_method(Named_object*);
  2373. // Look up a local method.
  2374. Named_object*
  2375. find_local_method(const std::string& name) const;
  2376. // Return the list of local methods.
  2377. const Bindings*
  2378. local_methods() const
  2379. { return this->local_methods_; }
  2380. // Build the complete list of methods, including those from
  2381. // anonymous fields, and build method stubs if needed.
  2382. void
  2383. finalize_methods(Gogo*);
  2384. // Return whether this type has any methods. This should only be
  2385. // called after the finalize_methods pass.
  2386. bool
  2387. has_any_methods() const
  2388. { return this->all_methods_ != NULL; }
  2389. // Return the methods for this type. This should only be called
  2390. // after the finalized_methods pass.
  2391. const Methods*
  2392. methods() const
  2393. { return this->all_methods_; }
  2394. // Return the method to use for NAME. This returns NULL if there is
  2395. // no such method or if the method is ambiguous. When it returns
  2396. // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
  2397. Method*
  2398. method_function(const std::string& name, bool *is_ambiguous) const;
  2399. // Return whether NAME is a known field or method which is not
  2400. // exported. This is only used for better error reporting.
  2401. bool
  2402. is_unexported_local_method(Gogo*, const std::string& name) const;
  2403. // Return a pointer to the interface method table for this type for
  2404. // the interface INTERFACE. If IS_POINTER is true, set the type
  2405. // descriptor to a pointer to this type, otherwise set it to this
  2406. // type.
  2407. Expression*
  2408. interface_method_table(Interface_type* interface, bool is_pointer);
  2409. // Note that a type must be converted to the backend representation
  2410. // before we convert this type.
  2411. void
  2412. add_dependency(Named_type* nt)
  2413. { this->dependencies_.push_back(nt); }
  2414. // Return true if the size and alignment of the backend
  2415. // representation of this type is known. This is always true after
  2416. // types have been converted, but may be false beforehand.
  2417. bool
  2418. is_named_backend_type_size_known() const
  2419. { return this->named_btype_ != NULL && !this->is_placeholder_; }
  2420. // Export the type.
  2421. void
  2422. export_named_type(Export*, const std::string& name) const;
  2423. // Import a named type.
  2424. static void
  2425. import_named_type(Import*, Named_type**);
  2426. // Initial conversion to backend representation.
  2427. void
  2428. convert(Gogo*);
  2429. protected:
  2430. int
  2431. do_traverse(Traverse* traverse)
  2432. { return Type::traverse(this->type_, traverse); }
  2433. bool
  2434. do_verify();
  2435. bool
  2436. do_has_pointer() const;
  2437. bool
  2438. do_compare_is_identity(Gogo*);
  2439. unsigned int
  2440. do_hash_for_method(Gogo*) const;
  2441. Btype*
  2442. do_get_backend(Gogo*);
  2443. Expression*
  2444. do_type_descriptor(Gogo*, Named_type*);
  2445. void
  2446. do_reflection(Gogo*, std::string*) const;
  2447. void
  2448. do_gc_symbol(Gogo* gogo, Expression_list** vals, Expression** offset,
  2449. int stack);
  2450. void
  2451. do_mangled_name(Gogo*, std::string* ret) const;
  2452. void
  2453. do_export(Export*) const;
  2454. private:
  2455. // Create the placeholder during conversion.
  2456. void
  2457. create_placeholder(Gogo*);
  2458. // A pointer back to the Named_object for this type.
  2459. Named_object* named_object_;
  2460. // If this type is defined in a function, a pointer back to the
  2461. // function in which it is defined.
  2462. Named_object* in_function_;
  2463. // The index of this type in IN_FUNCTION_.
  2464. unsigned int in_function_index_;
  2465. // The actual type.
  2466. Type* type_;
  2467. // The list of methods defined for this type. Any named type can
  2468. // have methods.
  2469. Bindings* local_methods_;
  2470. // The full list of methods for this type, including methods
  2471. // declared for anonymous fields.
  2472. Methods* all_methods_;
  2473. // A mapping from interfaces to the associated interface method
  2474. // tables for this type.
  2475. Interface_method_tables* interface_method_tables_;
  2476. // A mapping from interfaces to the associated interface method
  2477. // tables for pointers to this type.
  2478. Interface_method_tables* pointer_interface_method_tables_;
  2479. // The location where this type was defined.
  2480. Location location_;
  2481. // The backend representation of this type during backend
  2482. // conversion. This is used to avoid endless recursion when a named
  2483. // type refers to itself.
  2484. Btype* named_btype_;
  2485. // A list of types which must be converted to the backend
  2486. // representation before this type can be converted. This is for
  2487. // cases like
  2488. // type S1 { p *S2 }
  2489. // type S2 { s S1 }
  2490. // where we can't convert S2 to the backend representation unless we
  2491. // have converted S1.
  2492. std::vector<Named_type*> dependencies_;
  2493. // Whether this type is visible. This is false if this type was
  2494. // created because it was referenced by an imported object, but the
  2495. // type itself was not exported. This will always be true for types
  2496. // created in the current package.
  2497. bool is_visible_;
  2498. // Whether this type is erroneous.
  2499. bool is_error_;
  2500. // Whether the current value of named_btype_ is a placeholder for
  2501. // which the final size of the type is not known.
  2502. bool is_placeholder_;
  2503. // Whether this type has been converted to the backend
  2504. // representation. Implies that is_placeholder_ is false.
  2505. bool is_converted_;
  2506. // Whether this is a pointer or function type which refers to the
  2507. // type itself.
  2508. bool is_circular_;
  2509. // Whether this type has been verified.
  2510. bool is_verified_;
  2511. // In a recursive operation such as has_pointer, this flag is used
  2512. // to prevent infinite recursion when a type refers to itself. This
  2513. // is mutable because it is always reset to false when the function
  2514. // exits.
  2515. mutable bool seen_;
  2516. // Like seen_, but used only by do_compare_is_identity.
  2517. bool seen_in_compare_is_identity_;
  2518. // Like seen_, but used only by do_get_backend.
  2519. bool seen_in_get_backend_;
  2520. };
  2521. // A forward declaration. This handles a type which has been declared
  2522. // but not defined.
  2523. class Forward_declaration_type : public Type
  2524. {
  2525. public:
  2526. Forward_declaration_type(Named_object* named_object);
  2527. // The named object associated with this type declaration. This
  2528. // will be resolved.
  2529. Named_object*
  2530. named_object();
  2531. const Named_object*
  2532. named_object() const;
  2533. // Return the name of the type.
  2534. const std::string&
  2535. name() const;
  2536. // Return the type to which this points. Give an error if the type
  2537. // has not yet been defined.
  2538. Type*
  2539. real_type();
  2540. const Type*
  2541. real_type() const;
  2542. // Whether the base type has been defined.
  2543. bool
  2544. is_defined() const;
  2545. // Add a method to this type.
  2546. Named_object*
  2547. add_method(const std::string& name, Function*);
  2548. // Add a method declaration to this type.
  2549. Named_object*
  2550. add_method_declaration(const std::string& name, Package*, Function_type*,
  2551. Location);
  2552. protected:
  2553. int
  2554. do_traverse(Traverse* traverse);
  2555. bool
  2556. do_verify();
  2557. bool
  2558. do_has_pointer() const
  2559. { return this->real_type()->has_pointer(); }
  2560. bool
  2561. do_compare_is_identity(Gogo* gogo)
  2562. { return this->real_type()->compare_is_identity(gogo); }
  2563. unsigned int
  2564. do_hash_for_method(Gogo* gogo) const
  2565. { return this->real_type()->hash_for_method(gogo); }
  2566. Btype*
  2567. do_get_backend(Gogo* gogo);
  2568. Expression*
  2569. do_type_descriptor(Gogo*, Named_type*);
  2570. void
  2571. do_reflection(Gogo*, std::string*) const;
  2572. void
  2573. do_gc_symbol(Gogo* gogo, Expression_list** vals, Expression** offset,
  2574. int stack_size)
  2575. { Type::gc_symbol(gogo, this->real_type(), vals, offset, stack_size); }
  2576. void
  2577. do_mangled_name(Gogo*, std::string* ret) const;
  2578. void
  2579. do_export(Export*) const;
  2580. private:
  2581. // Issue a warning about a use of an undefined type.
  2582. void
  2583. warn() const;
  2584. // The type declaration.
  2585. Named_object* named_object_;
  2586. // Whether we have issued a warning about this type.
  2587. mutable bool warned_;
  2588. };
  2589. // The Type_context struct describes what we expect for the type of an
  2590. // expression.
  2591. struct Type_context
  2592. {
  2593. // The exact type we expect, if known. This may be NULL.
  2594. Type* type;
  2595. // Whether an abstract type is permitted.
  2596. bool may_be_abstract;
  2597. // Constructors.
  2598. Type_context()
  2599. : type(NULL), may_be_abstract(false)
  2600. { }
  2601. Type_context(Type* a_type, bool a_may_be_abstract)
  2602. : type(a_type), may_be_abstract(a_may_be_abstract)
  2603. { }
  2604. };
  2605. #endif // !defined(GO_TYPES_H)