1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262 |
- // types.h -- Go frontend types. -*- C++ -*-
- // Copyright 2009 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- #ifndef GO_TYPES_H
- #define GO_TYPES_H
- #include "go-linemap.h"
- class Gogo;
- class Package;
- class Traverse;
- class Typed_identifier;
- class Typed_identifier_list;
- class Integer_type;
- class Float_type;
- class Complex_type;
- class String_type;
- class Function_type;
- class Backend_function_type;
- class Struct_field;
- class Struct_field_list;
- class Struct_type;
- class Pointer_type;
- class Array_type;
- class Map_type;
- class Channel_type;
- class Interface_type;
- class Named_type;
- class Forward_declaration_type;
- class Method;
- class Methods;
- class Type_hash_identical;
- class Type_identical;
- class Expression;
- class Expression_list;
- class Call_expression;
- class Field_reference_expression;
- class Bound_method_expression;
- class Bindings;
- class Named_object;
- class Function;
- class Translate_context;
- class Export;
- class Import;
- class Btype;
- class Bexpression;
- class Bvariable;
- // Type codes used in type descriptors. These must match the values
- // in libgo/runtime/go-type.h. They also match the values in the gc
- // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
- // although this is not required.
- static const int RUNTIME_TYPE_KIND_BOOL = 1;
- static const int RUNTIME_TYPE_KIND_INT = 2;
- static const int RUNTIME_TYPE_KIND_INT8 = 3;
- static const int RUNTIME_TYPE_KIND_INT16 = 4;
- static const int RUNTIME_TYPE_KIND_INT32 = 5;
- static const int RUNTIME_TYPE_KIND_INT64 = 6;
- static const int RUNTIME_TYPE_KIND_UINT = 7;
- static const int RUNTIME_TYPE_KIND_UINT8 = 8;
- static const int RUNTIME_TYPE_KIND_UINT16 = 9;
- static const int RUNTIME_TYPE_KIND_UINT32 = 10;
- static const int RUNTIME_TYPE_KIND_UINT64 = 11;
- static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
- static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
- static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
- static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
- static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
- static const int RUNTIME_TYPE_KIND_ARRAY = 17;
- static const int RUNTIME_TYPE_KIND_CHAN = 18;
- static const int RUNTIME_TYPE_KIND_FUNC = 19;
- static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
- static const int RUNTIME_TYPE_KIND_MAP = 21;
- static const int RUNTIME_TYPE_KIND_PTR = 22;
- static const int RUNTIME_TYPE_KIND_SLICE = 23;
- static const int RUNTIME_TYPE_KIND_STRING = 24;
- static const int RUNTIME_TYPE_KIND_STRUCT = 25;
- static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
- static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5);
- static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6);
- static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
- // GC instruction opcodes. These must match the values in libgo/runtime/mgc0.h.
- enum GC_Opcode
- {
- GC_END = 0, // End of object, loop or subroutine.
- GC_PTR, // A typed pointer.
- GC_APTR, // Pointer to an arbitrary object.
- GC_ARRAY_START, // Start an array with a fixed length.
- GC_ARRAY_NEXT, // The next element of an array.
- GC_CALL, // Call a subroutine.
- GC_CHAN_PTR, // Go channel.
- GC_STRING, // Go string.
- GC_EFACE, // interface{}.
- GC_IFACE, // interface{...}.
- GC_SLICE, // Go slice.
- GC_REGION, // A region/part of the current object.
- GC_NUM_INSTR // Number of instruction opcodes
- };
- // The GC Stack Capacity must match the value in libgo/runtime/mgc0.h.
- static const int GC_STACK_CAPACITY = 8;
- // To build the complete list of methods for a named type we need to
- // gather all methods from anonymous fields. Those methods may
- // require an arbitrary set of indirections and field offsets. There
- // is also the possibility of ambiguous methods, which we could ignore
- // except that we want to give a better error message for that case.
- // This is a base class. There are two types of methods: named
- // methods, and methods which are inherited from an anonymous field of
- // interface type.
- class Method
- {
- public:
- // For methods in anonymous types we need to know the sequence of
- // field references used to extract the pointer to pass to the
- // method. Since each method for a particular anonymous field will
- // have the sequence of field indexes, and since the indexes can be
- // shared going down the chain, we use a manually managed linked
- // list. The first entry in the list is the field index for the
- // last field, the one passed to the method.
- struct Field_indexes
- {
- const Field_indexes* next;
- unsigned int field_index;
- };
- virtual ~Method()
- { }
- // Get the list of field indexes.
- const Field_indexes*
- field_indexes() const
- { return this->field_indexes_; }
- // Get the depth.
- unsigned int
- depth() const
- { return this->depth_; }
- // Return whether this is a value method--a method which does not
- // require a pointer expression.
- bool
- is_value_method() const
- { return this->is_value_method_; }
- // Return whether we need a stub method--this is true if we can't
- // just pass the main object to the method.
- bool
- needs_stub_method() const
- { return this->needs_stub_method_; }
- // Return whether this is an ambiguous method name.
- bool
- is_ambiguous() const
- { return this->is_ambiguous_; }
- // Note that this method is ambiguous.
- void
- set_is_ambiguous()
- { this->is_ambiguous_ = true; }
- // Return the type of the method.
- Function_type*
- type() const
- { return this->do_type(); }
- // Return the location of the method receiver.
- Location
- receiver_location() const
- { return this->do_receiver_location(); }
- // Return an expression which binds this method to EXPR. This is
- // something which can be used with a function call.
- Expression*
- bind_method(Expression* expr, Location location) const;
- // Return the named object for this method. This may only be called
- // after methods are finalized.
- Named_object*
- named_object() const;
- // Get the stub object.
- Named_object*
- stub_object() const
- {
- go_assert(this->stub_ != NULL);
- return this->stub_;
- }
- // Set the stub object.
- void
- set_stub_object(Named_object* no)
- {
- go_assert(this->stub_ == NULL);
- this->stub_ = no;
- }
- // Return true if this method should not participate in any
- // interfaces.
- bool
- nointerface() const
- { return this->do_nointerface(); }
- protected:
- // These objects are only built by the child classes.
- Method(const Field_indexes* field_indexes, unsigned int depth,
- bool is_value_method, bool needs_stub_method)
- : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
- is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
- is_ambiguous_(false)
- { }
- // The named object for this method.
- virtual Named_object*
- do_named_object() const = 0;
- // The type of the method.
- virtual Function_type*
- do_type() const = 0;
- // Return the location of the method receiver.
- virtual Location
- do_receiver_location() const = 0;
- // Bind a method to an object.
- virtual Expression*
- do_bind_method(Expression* expr, Location location) const = 0;
- // Return whether this method should not participate in interfaces.
- virtual bool
- do_nointerface() const = 0;
- private:
- // The sequence of field indexes used for this method. If this is
- // NULL, then the method is defined for the current type.
- const Field_indexes* field_indexes_;
- // The depth at which this method was found.
- unsigned int depth_;
- // If a stub method is required, this is its object. This is only
- // set after stub methods are built in finalize_methods.
- Named_object* stub_;
- // Whether this is a value method--a method that does not require a
- // pointer.
- bool is_value_method_;
- // Whether a stub method is required.
- bool needs_stub_method_;
- // Whether this method is ambiguous.
- bool is_ambiguous_;
- };
- // A named method. This is what you get with a method declaration,
- // either directly on the type, or inherited from some anonymous
- // embedded field.
- class Named_method : public Method
- {
- public:
- Named_method(Named_object* named_object, const Field_indexes* field_indexes,
- unsigned int depth, bool is_value_method,
- bool needs_stub_method)
- : Method(field_indexes, depth, is_value_method, needs_stub_method),
- named_object_(named_object)
- { }
- protected:
- // Get the Named_object for the method.
- Named_object*
- do_named_object() const
- { return this->named_object_; }
- // The type of the method.
- Function_type*
- do_type() const;
- // Return the location of the method receiver.
- Location
- do_receiver_location() const;
- // Bind a method to an object.
- Expression*
- do_bind_method(Expression* expr, Location location) const;
- // Return whether this method should not participate in interfaces.
- bool
- do_nointerface() const;
- private:
- // The method itself. For a method which needs a stub, this starts
- // out as the underlying method, and is later replaced with the stub
- // method.
- Named_object* named_object_;
- };
- // An interface method. This is used when an interface appears as an
- // anonymous field in a named struct.
- class Interface_method : public Method
- {
- public:
- Interface_method(const std::string& name, Location location,
- Function_type* fntype, const Field_indexes* field_indexes,
- unsigned int depth)
- : Method(field_indexes, depth, true, true),
- name_(name), location_(location), fntype_(fntype)
- { }
- protected:
- // Get the Named_object for the method. This should never be
- // called, as we always create a stub.
- Named_object*
- do_named_object() const
- { go_unreachable(); }
- // The type of the method.
- Function_type*
- do_type() const
- { return this->fntype_; }
- // Return the location of the method receiver.
- Location
- do_receiver_location() const
- { return this->location_; }
- // Bind a method to an object.
- Expression*
- do_bind_method(Expression* expr, Location location) const;
- // Return whether this method should not participate in interfaces.
- bool
- do_nointerface() const
- { return false; }
- private:
- // The name of the interface method to call.
- std::string name_;
- // The location of the definition of the interface method.
- Location location_;
- // The type of the interface method.
- Function_type* fntype_;
- };
- // A mapping from method name to Method. This is a wrapper around a
- // hash table.
- class Methods
- {
- private:
- typedef Unordered_map(std::string, Method*) Method_map;
- public:
- typedef Method_map::const_iterator const_iterator;
- Methods()
- : methods_()
- { }
- // Insert a new method. Returns true if it was inserted, false if
- // it was overidden or ambiguous.
- bool
- insert(const std::string& name, Method* m);
- // The number of (unambiguous) methods.
- size_t
- count() const;
- // Iterate.
- const_iterator
- begin() const
- { return this->methods_.begin(); }
- const_iterator
- end() const
- { return this->methods_.end(); }
- // Lookup.
- const_iterator
- find(const std::string& name) const
- { return this->methods_.find(name); }
- bool
- empty() const
- { return this->methods_.empty(); }
- private:
- Method_map methods_;
- };
- // The base class for all types.
- class Type
- {
- public:
- // The types of types.
- enum Type_classification
- {
- TYPE_ERROR,
- TYPE_VOID,
- TYPE_BOOLEAN,
- TYPE_INTEGER,
- TYPE_FLOAT,
- TYPE_COMPLEX,
- TYPE_STRING,
- TYPE_SINK,
- TYPE_FUNCTION,
- TYPE_POINTER,
- TYPE_NIL,
- TYPE_CALL_MULTIPLE_RESULT,
- TYPE_STRUCT,
- TYPE_ARRAY,
- TYPE_MAP,
- TYPE_CHANNEL,
- TYPE_INTERFACE,
- TYPE_NAMED,
- TYPE_FORWARD
- };
- virtual ~Type();
- // Creators.
- static Type*
- make_error_type();
- static Type*
- make_void_type();
- // Get the unnamed bool type.
- static Type*
- make_boolean_type();
- // Get the named type "bool".
- static Named_type*
- lookup_bool_type();
- // Make the named type "bool".
- static Named_type*
- make_named_bool_type();
- // Make an abstract integer type.
- static Integer_type*
- make_abstract_integer_type();
- // Make an abstract type for a character constant.
- static Integer_type*
- make_abstract_character_type();
- // Make a named integer type with a specified size.
- // RUNTIME_TYPE_KIND is the code to use in reflection information,
- // to distinguish int and int32.
- static Named_type*
- make_integer_type(const char* name, bool is_unsigned, int bits,
- int runtime_type_kind);
- // Look up a named integer type.
- static Named_type*
- lookup_integer_type(const char* name);
- // Make an abstract floating point type.
- static Float_type*
- make_abstract_float_type();
- // Make a named floating point type with a specific size.
- // RUNTIME_TYPE_KIND is the code to use in reflection information,
- // to distinguish float and float32.
- static Named_type*
- make_float_type(const char* name, int bits, int runtime_type_kind);
- // Look up a named float type.
- static Named_type*
- lookup_float_type(const char* name);
- // Make an abstract complex type.
- static Complex_type*
- make_abstract_complex_type();
- // Make a named complex type with a specific size.
- // RUNTIME_TYPE_KIND is the code to use in reflection information,
- // to distinguish complex and complex64.
- static Named_type*
- make_complex_type(const char* name, int bits, int runtime_type_kind);
- // Look up a named complex type.
- static Named_type*
- lookup_complex_type(const char* name);
- // Get the unnamed string type.
- static Type*
- make_string_type();
- // Get the named type "string".
- static Named_type*
- lookup_string_type();
- // Make the named type "string".
- static Named_type*
- make_named_string_type();
- static Type*
- make_sink_type();
- static Function_type*
- make_function_type(Typed_identifier* receiver,
- Typed_identifier_list* parameters,
- Typed_identifier_list* results,
- Location);
- static Backend_function_type*
- make_backend_function_type(Typed_identifier* receiver,
- Typed_identifier_list* parameters,
- Typed_identifier_list* results,
- Location);
- static Pointer_type*
- make_pointer_type(Type*);
- static Type*
- make_nil_type();
- static Type*
- make_call_multiple_result_type(Call_expression*);
- static Struct_type*
- make_struct_type(Struct_field_list* fields, Location);
- static Array_type*
- make_array_type(Type* element_type, Expression* length);
- static Map_type*
- make_map_type(Type* key_type, Type* value_type, Location);
- static Channel_type*
- make_channel_type(bool send, bool receive, Type*);
- static Interface_type*
- make_interface_type(Typed_identifier_list* methods, Location);
- static Interface_type*
- make_empty_interface_type(Location);
- static Type*
- make_type_descriptor_type();
- static Type*
- make_type_descriptor_ptr_type();
- static Named_type*
- make_named_type(Named_object*, Type*, Location);
- static Type*
- make_forward_declaration(Named_object*);
- // Make a builtin struct type from a list of fields.
- static Struct_type*
- make_builtin_struct_type(int nfields, ...);
- // Make a builtin named type.
- static Named_type*
- make_builtin_named_type(const char* name, Type* type);
- // Traverse a type.
- static int
- traverse(Type*, Traverse*);
- // Verify the type. This is called after parsing, and verifies that
- // types are complete and meet the language requirements. This
- // returns false if the type is invalid and we should not continue
- // traversing it.
- bool
- verify()
- { return this->do_verify(); }
- // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
- // returns that an erroneous type is identical to any other type;
- // this is used to avoid cascading errors. If this returns false,
- // and REASON is not NULL, it may set *REASON.
- static bool
- are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
- std::string* reason);
- // Return true if two types are compatible for use in a binary
- // operation, other than a shift, comparison, or channel send. This
- // is an equivalence relation.
- static bool
- are_compatible_for_binop(const Type* t1, const Type* t2);
- // Return true if two types are compatible for use with the
- // comparison operator. IS_EQUALITY_OP is true if this is an
- // equality comparison, false if it is an ordered comparison. This
- // is an equivalence relation. If this returns false, and REASON is
- // not NULL, it sets *REASON.
- static bool
- are_compatible_for_comparison(bool is_equality_op, const Type *t1,
- const Type *t2, std::string* reason);
- // Return true if a type is comparable with itself. This is true of
- // most types, but false for, e.g., function types.
- bool
- is_comparable() const
- { return Type::are_compatible_for_comparison(true, this, this, NULL); }
- // Return true if a value with type RHS is assignable to a variable
- // with type LHS. This is not an equivalence relation. If this
- // returns false, and REASON is not NULL, it sets *REASON.
- static bool
- are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
- // Return true if a value with type RHS may be converted to type
- // LHS. If this returns false, and REASON is not NULL, it sets
- // *REASON.
- static bool
- are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
- // Return true if values of this type can be compared using an
- // identity function which gets nothing but a pointer to the value
- // and a size.
- bool
- compare_is_identity(Gogo* gogo)
- { return this->do_compare_is_identity(gogo); }
- // Return a hash code for this type for the method hash table.
- // Types which are equivalent according to are_identical will have
- // the same hash code.
- unsigned int
- hash_for_method(Gogo*) const;
- // Return the type classification.
- Type_classification
- classification() const
- { return this->classification_; }
- // Return the base type for this type. This looks through forward
- // declarations and names. Using this with a forward declaration
- // which has not been defined will return an error type.
- Type*
- base();
- const Type*
- base() const;
- // Return the type skipping defined forward declarations. If this
- // type is a forward declaration which has not been defined, it will
- // return the Forward_declaration_type. This differs from base() in
- // that it will return a Named_type, and for a
- // Forward_declaration_type which is not defined it will return that
- // type rather than an error type.
- Type*
- forwarded();
- const Type*
- forwarded() const;
- // Return true if this is a basic type: a type which is not composed
- // of other types, and is not void.
- bool
- is_basic_type() const;
- // Return true if this is an abstract type--an integer, floating
- // point, or complex type whose size has not been determined.
- bool
- is_abstract() const;
- // Return a non-abstract version of an abstract type.
- Type*
- make_non_abstract_type();
- // Return true if this type is or contains a pointer. This
- // determines whether the garbage collector needs to look at a value
- // of this type.
- bool
- has_pointer() const
- { return this->do_has_pointer(); }
- // Return true if this is the error type. This returns false for a
- // type which is not defined, as it is called by the parser before
- // all types are defined.
- bool
- is_error_type() const;
- // Return true if this is the error type or if the type is
- // undefined. If the type is undefined, this will give an error.
- // This should only be called after parsing is complete.
- bool
- is_error() const
- { return this->base()->is_error_type(); }
- // Return true if this is a void type.
- bool
- is_void_type() const
- { return this->classification_ == TYPE_VOID; }
- // If this is an integer type, return the Integer_type. Otherwise,
- // return NULL. This is a controlled dynamic_cast.
- Integer_type*
- integer_type()
- { return this->convert<Integer_type, TYPE_INTEGER>(); }
- const Integer_type*
- integer_type() const
- { return this->convert<const Integer_type, TYPE_INTEGER>(); }
- // If this is a floating point type, return the Float_type.
- // Otherwise, return NULL. This is a controlled dynamic_cast.
- Float_type*
- float_type()
- { return this->convert<Float_type, TYPE_FLOAT>(); }
- const Float_type*
- float_type() const
- { return this->convert<const Float_type, TYPE_FLOAT>(); }
- // If this is a complex type, return the Complex_type. Otherwise,
- // return NULL.
- Complex_type*
- complex_type()
- { return this->convert<Complex_type, TYPE_COMPLEX>(); }
- const Complex_type*
- complex_type() const
- { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
- // Return whether this is a numeric type.
- bool
- is_numeric_type() const
- {
- Type_classification tc = this->base()->classification_;
- return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
- }
- // Return true if this is a boolean type.
- bool
- is_boolean_type() const
- { return this->base()->classification_ == TYPE_BOOLEAN; }
- // Return true if this is an abstract boolean type.
- bool
- is_abstract_boolean_type() const
- { return this->classification_ == TYPE_BOOLEAN; }
- // Return true if this is a string type.
- bool
- is_string_type() const
- { return this->base()->classification_ == TYPE_STRING; }
- // Return true if this is an abstract string type.
- bool
- is_abstract_string_type() const
- { return this->classification_ == TYPE_STRING; }
- // Return true if this is the sink type. This is the type of the
- // blank identifier _.
- bool
- is_sink_type() const
- { return this->base()->classification_ == TYPE_SINK; }
- // If this is a function type, return it. Otherwise, return NULL.
- Function_type*
- function_type()
- { return this->convert<Function_type, TYPE_FUNCTION>(); }
- const Function_type*
- function_type() const
- { return this->convert<const Function_type, TYPE_FUNCTION>(); }
- // If this is a pointer type, return the type to which it points.
- // Otherwise, return NULL.
- Type*
- points_to() const;
- // If this is a pointer type, return the type to which it points.
- // Otherwise, return the type itself.
- Type*
- deref()
- {
- Type* pt = this->points_to();
- return pt != NULL ? pt : this;
- }
- const Type*
- deref() const
- {
- const Type* pt = this->points_to();
- return pt != NULL ? pt : this;
- }
- // Return true if this is the nil type. We don't use base() here,
- // because this can be called during parse, and there is no way to
- // name the nil type anyhow.
- bool
- is_nil_type() const
- { return this->classification_ == TYPE_NIL; }
- // Return true if this is the predeclared constant nil being used as
- // a type. This is what the parser produces for type switches which
- // use "case nil".
- bool
- is_nil_constant_as_type() const;
- // Return true if this is the return type of a function which
- // returns multiple values.
- bool
- is_call_multiple_result_type() const
- { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
- // If this is a struct type, return it. Otherwise, return NULL.
- Struct_type*
- struct_type()
- { return this->convert<Struct_type, TYPE_STRUCT>(); }
- const Struct_type*
- struct_type() const
- { return this->convert<const Struct_type, TYPE_STRUCT>(); }
- // If this is an array type, return it. Otherwise, return NULL.
- Array_type*
- array_type()
- { return this->convert<Array_type, TYPE_ARRAY>(); }
- const Array_type*
- array_type() const
- { return this->convert<const Array_type, TYPE_ARRAY>(); }
- // Return whether if this is a slice type.
- bool
- is_slice_type() const;
- // If this is a map type, return it. Otherwise, return NULL.
- Map_type*
- map_type()
- { return this->convert<Map_type, TYPE_MAP>(); }
- const Map_type*
- map_type() const
- { return this->convert<const Map_type, TYPE_MAP>(); }
- // If this is a channel type, return it. Otherwise, return NULL.
- Channel_type*
- channel_type()
- { return this->convert<Channel_type, TYPE_CHANNEL>(); }
- const Channel_type*
- channel_type() const
- { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
- // If this is an interface type, return it. Otherwise, return NULL.
- Interface_type*
- interface_type()
- { return this->convert<Interface_type, TYPE_INTERFACE>(); }
- const Interface_type*
- interface_type() const
- { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
- // If this is a named type, return it. Otherwise, return NULL.
- Named_type*
- named_type();
- const Named_type*
- named_type() const;
- // If this is a forward declaration, return it. Otherwise, return
- // NULL.
- Forward_declaration_type*
- forward_declaration_type()
- { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
- const Forward_declaration_type*
- forward_declaration_type() const
- {
- return this->convert_no_base<const Forward_declaration_type,
- TYPE_FORWARD>();
- }
- // Return true if this type is not yet defined.
- bool
- is_undefined() const;
- // Return true if this is the unsafe.pointer type. We currently
- // represent that as pointer-to-void.
- bool
- is_unsafe_pointer_type() const
- { return this->points_to() != NULL && this->points_to()->is_void_type(); }
- // Look for field or method NAME for TYPE. Return an expression for
- // it, bound to EXPR.
- static Expression*
- bind_field_or_method(Gogo*, const Type* type, Expression* expr,
- const std::string& name, Location);
- // Return true if NAME is an unexported field or method of TYPE.
- static bool
- is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
- std::vector<const Named_type*>*);
- // Convert the builtin named types.
- static void
- convert_builtin_named_types(Gogo*);
- // Return the backend representation of this type.
- Btype*
- get_backend(Gogo*);
- // Return a placeholder for the backend representation of the type.
- // This will return a type of the correct size, but for which some
- // of the fields may still need to be completed.
- Btype*
- get_backend_placeholder(Gogo*);
- // Finish the backend representation of a placeholder.
- void
- finish_backend(Gogo*, Btype*);
- // Build a type descriptor entry for this type. Return a pointer to
- // it. The location is the location which causes us to need the
- // entry.
- Bexpression*
- type_descriptor_pointer(Gogo* gogo, Location);
- // Build the Garbage Collection symbol for this type. Return a pointer to it.
- Bexpression*
- gc_symbol_pointer(Gogo* gogo);
- // Return the type reflection string for this type.
- std::string
- reflection(Gogo*) const;
- // Return a mangled name for the type. This is a name which can be
- // used in assembler code. Identical types should have the same
- // manged name.
- std::string
- mangled_name(Gogo*) const;
- // If the size of the type can be determined, set *PSIZE to the size
- // in bytes and return true. Otherwise, return false. This queries
- // the backend.
- bool
- backend_type_size(Gogo*, int64_t* psize);
- // If the alignment of the type can be determined, set *PALIGN to
- // the alignment in bytes and return true. Otherwise, return false.
- bool
- backend_type_align(Gogo*, int64_t* palign);
- // If the alignment of a struct field of this type can be
- // determined, set *PALIGN to the alignment in bytes and return
- // true. Otherwise, return false.
- bool
- backend_type_field_align(Gogo*, int64_t* palign);
- // Whether the backend size is known.
- bool
- is_backend_type_size_known(Gogo*);
- // Get the hash and equality functions for a type.
- void
- type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
- Function_type* equal_fntype, Named_object** hash_fn,
- Named_object** equal_fn);
- // Write the hash and equality type functions.
- void
- write_specific_type_functions(Gogo*, Named_type*,
- const std::string& hash_name,
- Function_type* hash_fntype,
- const std::string& equal_name,
- Function_type* equal_fntype);
- // Export the type.
- void
- export_type(Export* exp) const
- { this->do_export(exp); }
- // Import a type.
- static Type*
- import_type(Import*);
- protected:
- Type(Type_classification);
- // Functions implemented by the child class.
- // Traverse the subtypes.
- virtual int
- do_traverse(Traverse*);
- // Verify the type.
- virtual bool
- do_verify()
- { return true; }
- virtual bool
- do_has_pointer() const
- { return false; }
- virtual bool
- do_compare_is_identity(Gogo*) = 0;
- virtual unsigned int
- do_hash_for_method(Gogo*) const;
- virtual Btype*
- do_get_backend(Gogo*) = 0;
- virtual Expression*
- do_type_descriptor(Gogo*, Named_type* name) = 0;
- virtual void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int) = 0;
- virtual void
- do_reflection(Gogo*, std::string*) const = 0;
- virtual void
- do_mangled_name(Gogo*, std::string*) const = 0;
- virtual void
- do_export(Export*) const;
- // Return whether a method expects a pointer as the receiver.
- static bool
- method_expects_pointer(const Named_object*);
- // Finalize the methods for a type.
- static void
- finalize_methods(Gogo*, const Type*, Location, Methods**);
- // Return a method from a set of methods.
- static Method*
- method_function(const Methods*, const std::string& name,
- bool* is_ambiguous);
- // A mapping from interfaces to the associated interface method
- // tables for this type. This maps to a decl.
- typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
- Type_identical) Interface_method_tables;
- // Return a pointer to the interface method table for TYPE for the
- // interface INTERFACE.
- static Expression*
- interface_method_table(Type* type,
- Interface_type *interface, bool is_pointer,
- Interface_method_tables** method_tables,
- Interface_method_tables** pointer_tables);
- // Return a composite literal for the type descriptor entry for a
- // type.
- static Expression*
- type_descriptor(Gogo*, Type*);
- // Return a composite literal for the type descriptor entry for
- // TYPE, using NAME as the name of the type.
- static Expression*
- named_type_descriptor(Gogo*, Type* type, Named_type* name);
- // Return a composite literal for a plain type descriptor for this
- // type with the given kind and name.
- Expression*
- plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
- // Build a composite literal for the basic type descriptor.
- Expression*
- type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
- const Methods*, bool only_value_methods);
- // Generate the GC symbol for this TYPE. VALS is the data so far in this
- // symbol; extra values will be appended in do_gc_symbol. OFFSET is the
- // offset into the symbol where the GC data is located. STACK_SIZE is the
- // size of the GC stack when dealing with array types.
- static void
- gc_symbol(Gogo*, Type* type, Expression_list** vals, Expression** offset,
- int stack_size);
- // Build a composite literal for the GC symbol of this type.
- Expression*
- gc_symbol_constructor(Gogo*);
- // Advance the OFFSET of the GC symbol by the size of this type.
- void
- advance_gc_offset(Expression** offset);
- // For the benefit of child class reflection string generation.
- void
- append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
- { type->do_reflection(gogo, ret); }
- // For the benefit of child class mangling.
- void
- append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
- { type->do_mangled_name(gogo, ret); }
- // Incorporate a string into a hash code.
- static unsigned int
- hash_string(const std::string&, unsigned int);
- // Return the backend representation for the underlying type of a
- // named type.
- static Btype*
- get_named_base_btype(Gogo* gogo, Type* base_type)
- { return base_type->get_btype_without_hash(gogo); }
- private:
- // Convert to the desired type classification, or return NULL. This
- // is a controlled dynamic_cast.
- template<typename Type_class, Type_classification type_classification>
- Type_class*
- convert()
- {
- Type* base = this->base();
- return (base->classification_ == type_classification
- ? static_cast<Type_class*>(base)
- : NULL);
- }
- template<typename Type_class, Type_classification type_classification>
- const Type_class*
- convert() const
- {
- const Type* base = this->base();
- return (base->classification_ == type_classification
- ? static_cast<Type_class*>(base)
- : NULL);
- }
- template<typename Type_class, Type_classification type_classification>
- Type_class*
- convert_no_base()
- {
- return (this->classification_ == type_classification
- ? static_cast<Type_class*>(this)
- : NULL);
- }
- template<typename Type_class, Type_classification type_classification>
- const Type_class*
- convert_no_base() const
- {
- return (this->classification_ == type_classification
- ? static_cast<Type_class*>(this)
- : NULL);
- }
- // Map unnamed types to type descriptor decls.
- typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
- Type_identical) Type_descriptor_vars;
- static Type_descriptor_vars type_descriptor_vars;
- // Build the type descriptor variable for this type.
- void
- make_type_descriptor_var(Gogo*);
- // Map unnamed types to type descriptor decls.
- typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
- Type_identical) GC_symbol_vars;
- static GC_symbol_vars gc_symbol_vars;
- // Build the GC symbol for this type.
- void
- make_gc_symbol_var(Gogo*);
- // Return the name of the type descriptor variable. If NAME is not
- // NULL, it is the name to use.
- std::string
- type_descriptor_var_name(Gogo*, Named_type* name);
- // Return true if the type descriptor for this type should be
- // defined in some other package. If NAME is not NULL, it is the
- // name of this type. If this returns true it sets *PACKAGE to the
- // package where the type descriptor is defined.
- bool
- type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
- // Build the hash and equality type functions for a type which needs
- // specific functions.
- void
- specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
- Function_type* equal_fntype, Named_object** hash_fn,
- Named_object** equal_fn);
- void
- write_named_hash(Gogo*, Named_type*, Function_type* hash_fntype,
- Function_type* equal_fntype);
- void
- write_named_equal(Gogo*, Named_type*);
- // Build a composite literal for the uncommon type information.
- Expression*
- uncommon_type_constructor(Gogo*, Type* uncommon_type,
- Named_type*, const Methods*,
- bool only_value_methods) const;
- // Build a composite literal for the methods.
- Expression*
- methods_constructor(Gogo*, Type* methods_type, const Methods*,
- bool only_value_methods) const;
- // Build a composite literal for one method.
- Expression*
- method_constructor(Gogo*, Type* method_type, const std::string& name,
- const Method*, bool only_value_methods) const;
- // Add all methods for TYPE to the list of methods for THIS.
- static void
- add_methods_for_type(const Type* type, const Method::Field_indexes*,
- unsigned int depth, bool, bool,
- std::vector<const Named_type*>*,
- Methods*);
- static void
- add_local_methods_for_type(const Named_type* type,
- const Method::Field_indexes*,
- unsigned int depth, bool, bool, Methods*);
- static void
- add_embedded_methods_for_type(const Type* type,
- const Method::Field_indexes*,
- unsigned int depth, bool, bool,
- std::vector<const Named_type*>*,
- Methods*);
- static void
- add_interface_methods_for_type(const Type* type,
- const Method::Field_indexes*,
- unsigned int depth, Methods*);
- // Build stub methods for a type.
- static void
- build_stub_methods(Gogo*, const Type* type, const Methods* methods,
- Location);
- static void
- build_one_stub_method(Gogo*, Method*, const char* receiver_name,
- const Typed_identifier_list*, bool is_varargs,
- Location);
- static Expression*
- apply_field_indexes(Expression*, const Method::Field_indexes*,
- Location);
- // Look for a field or method named NAME in TYPE.
- static bool
- find_field_or_method(const Type* type, const std::string& name,
- bool receiver_can_be_pointer,
- std::vector<const Named_type*>*, int* level,
- bool* is_method, bool* found_pointer_method,
- std::string* ambig1, std::string* ambig2);
- // Get the backend representation for a type without looking in the
- // hash table for identical types.
- Btype*
- get_btype_without_hash(Gogo*);
- // A backend type that may be a placeholder.
- struct Type_btype_entry
- {
- Btype *btype;
- bool is_placeholder;
- };
- // A mapping from Type to Btype*, used to ensure that the backend
- // representation of identical types is identical. This is only
- // used for unnamed types.
- typedef Unordered_map_hash(const Type*, Type_btype_entry,
- Type_hash_identical, Type_identical) Type_btypes;
- static Type_btypes type_btypes;
- // A list of builtin named types.
- static std::vector<Named_type*> named_builtin_types;
- // A map from types which need specific type functions to the type
- // functions themselves.
- typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
- typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
- Type_identical) Type_functions;
- static Type_functions type_functions_table;
- // The type classification.
- Type_classification classification_;
- // The backend representation of the type, once it has been
- // determined.
- Btype* btype_;
- // The type descriptor for this type. This starts out as NULL and
- // is filled in as needed.
- Bvariable* type_descriptor_var_;
- // The GC symbol for this type. This starts out as NULL and
- // is filled in as needed.
- Bvariable* gc_symbol_var_;
- };
- // Type hash table operations.
- class Type_hash_identical
- {
- public:
- unsigned int
- operator()(const Type* type) const
- { return type->hash_for_method(NULL); }
- };
- class Type_identical
- {
- public:
- bool
- operator()(const Type* t1, const Type* t2) const
- { return Type::are_identical(t1, t2, false, NULL); }
- };
- // An identifier with a type.
- class Typed_identifier
- {
- public:
- Typed_identifier(const std::string& name, Type* type,
- Location location)
- : name_(name), type_(type), location_(location)
- { }
- // Get the name.
- const std::string&
- name() const
- { return this->name_; }
- // Get the type.
- Type*
- type() const
- { return this->type_; }
- // Return the location where the name was seen. This is not always
- // meaningful.
- Location
- location() const
- { return this->location_; }
- // Set the type--sometimes we see the identifier before the type.
- void
- set_type(Type* type)
- {
- go_assert(this->type_ == NULL || type->is_error_type());
- this->type_ = type;
- }
- private:
- // Identifier name.
- std::string name_;
- // Type.
- Type* type_;
- // The location where the name was seen.
- Location location_;
- };
- // A list of Typed_identifiers.
- class Typed_identifier_list
- {
- public:
- Typed_identifier_list()
- : entries_()
- { }
- // Whether the list is empty.
- bool
- empty() const
- { return this->entries_.empty(); }
- // Return the number of entries in the list.
- size_t
- size() const
- { return this->entries_.size(); }
- // Add an entry to the end of the list.
- void
- push_back(const Typed_identifier& td)
- { this->entries_.push_back(td); }
- // Remove an entry from the end of the list.
- void
- pop_back()
- { this->entries_.pop_back(); }
- // Set the type of entry I to TYPE.
- void
- set_type(size_t i, Type* type)
- {
- go_assert(i < this->entries_.size());
- this->entries_[i].set_type(type);
- }
- // Sort the entries by name.
- void
- sort_by_name();
- // Traverse types.
- int
- traverse(Traverse*);
- // Return the first and last elements.
- Typed_identifier&
- front()
- { return this->entries_.front(); }
- const Typed_identifier&
- front() const
- { return this->entries_.front(); }
- Typed_identifier&
- back()
- { return this->entries_.back(); }
- const Typed_identifier&
- back() const
- { return this->entries_.back(); }
- const Typed_identifier&
- at(size_t i) const
- { return this->entries_.at(i); }
- void
- set(size_t i, const Typed_identifier& t)
- { this->entries_.at(i) = t; }
- void
- resize(size_t c)
- {
- go_assert(c <= this->entries_.size());
- this->entries_.resize(c, Typed_identifier("", NULL,
- Linemap::unknown_location()));
- }
- void
- reserve(size_t c)
- { this->entries_.reserve(c); }
- // Iterators.
- typedef std::vector<Typed_identifier>::iterator iterator;
- typedef std::vector<Typed_identifier>::const_iterator const_iterator;
- iterator
- begin()
- { return this->entries_.begin(); }
- const_iterator
- begin() const
- { return this->entries_.begin(); }
- iterator
- end()
- { return this->entries_.end(); }
- const_iterator
- end() const
- { return this->entries_.end(); }
- // Return a copy of this list. This returns an independent copy of
- // the vector, but does not copy the types.
- Typed_identifier_list*
- copy() const;
- private:
- std::vector<Typed_identifier> entries_;
- };
- // The type of an integer.
- class Integer_type : public Type
- {
- public:
- // Create a new integer type.
- static Named_type*
- create_integer_type(const char* name, bool is_unsigned, int bits,
- int runtime_type_kind);
- // Look up an existing integer type.
- static Named_type*
- lookup_integer_type(const char* name);
- // Create an abstract integer type.
- static Integer_type*
- create_abstract_integer_type();
- // Create an abstract character type.
- static Integer_type*
- create_abstract_character_type();
- // Whether this is an abstract integer type.
- bool
- is_abstract() const
- { return this->is_abstract_; }
- // Whether this is an unsigned type.
- bool
- is_unsigned() const
- { return this->is_unsigned_; }
- // The number of bits.
- int
- bits() const
- { return this->bits_; }
- // Whether this type is the same as T.
- bool
- is_identical(const Integer_type* t) const;
- // Whether this is the type "byte" or another name for "byte".
- bool
- is_byte() const
- { return this->is_byte_; }
- // Mark this as the "byte" type.
- void
- set_is_byte()
- { this->is_byte_ = true; }
- // Whether this is the type "rune" or another name for "rune".
- bool
- is_rune() const
- { return this->is_rune_; }
- // Mark this as the "rune" type.
- void
- set_is_rune()
- { this->is_rune_ = true; }
- protected:
- bool
- do_compare_is_identity(Gogo*)
- { return true; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression** offset, int)
- { this->advance_gc_offset(offset); }
- void
- do_mangled_name(Gogo*, std::string*) const;
- private:
- Integer_type(bool is_abstract, bool is_unsigned, int bits,
- int runtime_type_kind)
- : Type(TYPE_INTEGER),
- is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
- is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
- { }
- // Map names of integer types to the types themselves.
- typedef std::map<std::string, Named_type*> Named_integer_types;
- static Named_integer_types named_integer_types;
- // True if this is an abstract type.
- bool is_abstract_;
- // True if this is an unsigned type.
- bool is_unsigned_;
- // True if this is the byte type.
- bool is_byte_;
- // True if this is the rune type.
- bool is_rune_;
- // The number of bits.
- int bits_;
- // The runtime type code used in the type descriptor for this type.
- int runtime_type_kind_;
- };
- // The type of a floating point number.
- class Float_type : public Type
- {
- public:
- // Create a new float type.
- static Named_type*
- create_float_type(const char* name, int bits, int runtime_type_kind);
- // Look up an existing float type.
- static Named_type*
- lookup_float_type(const char* name);
- // Create an abstract float type.
- static Float_type*
- create_abstract_float_type();
- // Whether this is an abstract float type.
- bool
- is_abstract() const
- { return this->is_abstract_; }
- // The number of bits.
- int
- bits() const
- { return this->bits_; }
- // Whether this type is the same as T.
- bool
- is_identical(const Float_type* t) const;
- protected:
- bool
- do_compare_is_identity(Gogo*)
- { return false; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression** offset, int)
- { this->advance_gc_offset(offset); }
- void
- do_mangled_name(Gogo*, std::string*) const;
- private:
- Float_type(bool is_abstract, int bits, int runtime_type_kind)
- : Type(TYPE_FLOAT),
- is_abstract_(is_abstract), bits_(bits),
- runtime_type_kind_(runtime_type_kind)
- { }
- // Map names of float types to the types themselves.
- typedef std::map<std::string, Named_type*> Named_float_types;
- static Named_float_types named_float_types;
- // True if this is an abstract type.
- bool is_abstract_;
- // The number of bits in the floating point value.
- int bits_;
- // The runtime type code used in the type descriptor for this type.
- int runtime_type_kind_;
- };
- // The type of a complex number.
- class Complex_type : public Type
- {
- public:
- // Create a new complex type.
- static Named_type*
- create_complex_type(const char* name, int bits, int runtime_type_kind);
- // Look up an existing complex type.
- static Named_type*
- lookup_complex_type(const char* name);
- // Create an abstract complex type.
- static Complex_type*
- create_abstract_complex_type();
- // Whether this is an abstract complex type.
- bool
- is_abstract() const
- { return this->is_abstract_; }
- // The number of bits: 64 or 128.
- int bits() const
- { return this->bits_; }
- // Whether this type is the same as T.
- bool
- is_identical(const Complex_type* t) const;
- protected:
- bool
- do_compare_is_identity(Gogo*)
- { return false; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression** offset, int)
- { this->advance_gc_offset(offset); }
- void
- do_mangled_name(Gogo*, std::string*) const;
- private:
- Complex_type(bool is_abstract, int bits, int runtime_type_kind)
- : Type(TYPE_COMPLEX),
- is_abstract_(is_abstract), bits_(bits),
- runtime_type_kind_(runtime_type_kind)
- { }
- // Map names of complex types to the types themselves.
- typedef std::map<std::string, Named_type*> Named_complex_types;
- static Named_complex_types named_complex_types;
- // True if this is an abstract type.
- bool is_abstract_;
- // The number of bits in the complex value--64 or 128.
- int bits_;
- // The runtime type code used in the type descriptor for this type.
- int runtime_type_kind_;
- };
- // The type of a string.
- class String_type : public Type
- {
- public:
- String_type()
- : Type(TYPE_STRING)
- { }
- protected:
- bool
- do_has_pointer() const
- { return true; }
- bool
- do_compare_is_identity(Gogo*)
- { return false; }
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string* ret) const;
- private:
- // The named string type.
- static Named_type* string_type_;
- };
- // The type of a function.
- class Function_type : public Type
- {
- public:
- Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
- Typed_identifier_list* results, Location location)
- : Type(TYPE_FUNCTION),
- receiver_(receiver), parameters_(parameters), results_(results),
- location_(location), is_varargs_(false), is_builtin_(false),
- fnbtype_(NULL)
- { }
- // Get the receiver.
- const Typed_identifier*
- receiver() const
- { return this->receiver_; }
- // Get the return names and types.
- const Typed_identifier_list*
- results() const
- { return this->results_; }
- // Get the parameter names and types.
- const Typed_identifier_list*
- parameters() const
- { return this->parameters_; }
- // Whether this is a varargs function.
- bool
- is_varargs() const
- { return this->is_varargs_; }
- // Whether this is a builtin function.
- bool
- is_builtin() const
- { return this->is_builtin_; }
- // The location where this type was defined.
- Location
- location() const
- { return this->location_; }
- // Return whether this is a method type.
- bool
- is_method() const
- { return this->receiver_ != NULL; }
- // Whether T is a valid redeclaration of this type. This is called
- // when a function is declared more than once.
- bool
- is_valid_redeclaration(const Function_type* t, std::string*) const;
- // Whether this type is the same as T.
- bool
- is_identical(const Function_type* t, bool ignore_receiver,
- bool errors_are_identical, std::string*) const;
- // Record that this is a varargs function.
- void
- set_is_varargs()
- { this->is_varargs_ = true; }
- // Record that this is a builtin function.
- void
- set_is_builtin()
- { this->is_builtin_ = true; }
- // Import a function type.
- static Function_type*
- do_import(Import*);
- // Return a copy of this type without a receiver. This is only
- // valid for a method type.
- Function_type*
- copy_without_receiver() const;
- // Return a copy of this type with a receiver. This is used when an
- // interface method is attached to a named or struct type.
- Function_type*
- copy_with_receiver(Type*) const;
- // Return a copy of this type with the receiver treated as the first
- // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
- // forced to be a pointer.
- Function_type*
- copy_with_receiver_as_param(bool want_pointer_receiver) const;
- // Return a copy of this type ignoring any receiver and using dummy
- // names for all parameters. This is used for thunks for method
- // values.
- Function_type*
- copy_with_names() const;
- static Type*
- make_function_type_descriptor_type();
- // Return the backend representation of this function type. This is used
- // as the real type of a backend function declaration or defintion.
- Btype*
- get_backend_fntype(Gogo*);
- protected:
- int
- do_traverse(Traverse*);
- // A function descriptor may be allocated on the heap.
- bool
- do_has_pointer() const
- { return true; }
- bool
- do_compare_is_identity(Gogo*)
- { return false; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- Expression*
- type_descriptor_params(Type*, const Typed_identifier*,
- const Typed_identifier_list*);
- // A mapping from a list of result types to a backend struct type.
- class Results_hash
- {
- public:
- unsigned int
- operator()(const Typed_identifier_list*) const;
- };
- class Results_equal
- {
- public:
- bool
- operator()(const Typed_identifier_list*,
- const Typed_identifier_list*) const;
- };
- typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
- Results_hash, Results_equal) Results_structs;
- static Results_structs results_structs;
- // The receiver name and type. This will be NULL for a normal
- // function, non-NULL for a method.
- Typed_identifier* receiver_;
- // The parameter names and types.
- Typed_identifier_list* parameters_;
- // The result names and types. This will be NULL if no result was
- // specified.
- Typed_identifier_list* results_;
- // The location where this type was defined. This exists solely to
- // give a location for the fields of the struct if this function
- // returns multiple values.
- Location location_;
- // Whether this function takes a variable number of arguments.
- bool is_varargs_;
- // Whether this is a special builtin function which can not simply
- // be called. This is used for len, cap, etc.
- bool is_builtin_;
- // The backend representation of this type for backend function
- // declarations and definitions.
- Btype* fnbtype_;
- };
- // The type of a function's backend representation.
- class Backend_function_type : public Function_type
- {
- public:
- Backend_function_type(Typed_identifier* receiver,
- Typed_identifier_list* parameters,
- Typed_identifier_list* results, Location location)
- : Function_type(receiver, parameters, results, location)
- { }
- protected:
- Btype*
- do_get_backend(Gogo* gogo)
- { return this->get_backend_fntype(gogo); }
- };
- // The type of a pointer.
- class Pointer_type : public Type
- {
- public:
- Pointer_type(Type* to_type)
- : Type(TYPE_POINTER),
- to_type_(to_type)
- {}
- Type*
- points_to() const
- { return this->to_type_; }
- // Import a pointer type.
- static Pointer_type*
- do_import(Import*);
- static Type*
- make_pointer_type_descriptor_type();
- protected:
- int
- do_traverse(Traverse*);
- bool
- do_has_pointer() const
- { return true; }
- bool
- do_compare_is_identity(Gogo*)
- { return true; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- // The type to which this type points.
- Type* to_type_;
- };
- // The type of a field in a struct.
- class Struct_field
- {
- public:
- explicit Struct_field(const Typed_identifier& typed_identifier)
- : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
- { }
- // The field name.
- const std::string&
- field_name() const;
- // Return whether this struct field is named NAME.
- bool
- is_field_name(const std::string& name) const;
- // Return whether this struct field is an unexported field named NAME.
- bool
- is_unexported_field_name(Gogo*, const std::string& name) const;
- // Return whether this struct field is an embedded built-in type.
- bool
- is_embedded_builtin(Gogo*) const;
- // The field type.
- Type*
- type() const
- { return this->typed_identifier_.type(); }
- // The field location.
- Location
- location() const
- { return this->typed_identifier_.location(); }
- // Whether the field has a tag.
- bool
- has_tag() const
- { return this->tag_ != NULL; }
- // The tag.
- const std::string&
- tag() const
- {
- go_assert(this->tag_ != NULL);
- return *this->tag_;
- }
- // Whether this is an anonymous field.
- bool
- is_anonymous() const
- { return this->typed_identifier_.name().empty(); }
- // Set the tag. FIXME: This is never freed.
- void
- set_tag(const std::string& tag)
- { this->tag_ = new std::string(tag); }
- // Record that this field is defined in an imported struct.
- void
- set_is_imported()
- { this->is_imported_ = true; }
- // Set the type. This is only used in error cases.
- void
- set_type(Type* type)
- { this->typed_identifier_.set_type(type); }
- private:
- // The field name, type, and location.
- Typed_identifier typed_identifier_;
- // The field tag. This is NULL if the field has no tag.
- std::string* tag_;
- // Whether this field is defined in an imported struct.
- bool is_imported_;
- };
- // A list of struct fields.
- class Struct_field_list
- {
- public:
- Struct_field_list()
- : entries_()
- { }
- // Whether the list is empty.
- bool
- empty() const
- { return this->entries_.empty(); }
- // Return the number of entries.
- size_t
- size() const
- { return this->entries_.size(); }
- // Add an entry to the end of the list.
- void
- push_back(const Struct_field& sf)
- { this->entries_.push_back(sf); }
- // Index into the list.
- const Struct_field&
- at(size_t i) const
- { return this->entries_.at(i); }
- // Last entry in list.
- Struct_field&
- back()
- { return this->entries_.back(); }
- // Iterators.
- typedef std::vector<Struct_field>::iterator iterator;
- typedef std::vector<Struct_field>::const_iterator const_iterator;
- iterator
- begin()
- { return this->entries_.begin(); }
- const_iterator
- begin() const
- { return this->entries_.begin(); }
- iterator
- end()
- { return this->entries_.end(); }
- const_iterator
- end() const
- { return this->entries_.end(); }
- private:
- std::vector<Struct_field> entries_;
- };
- // The type of a struct.
- class Struct_type : public Type
- {
- public:
- Struct_type(Struct_field_list* fields, Location location)
- : Type(TYPE_STRUCT),
- fields_(fields), location_(location), all_methods_(NULL)
- { }
- // Return the field NAME. This only looks at local fields, not at
- // embedded types. If the field is found, and PINDEX is not NULL,
- // this sets *PINDEX to the field index. If the field is not found,
- // this returns NULL.
- const Struct_field*
- find_local_field(const std::string& name, unsigned int *pindex) const;
- // Return the field number INDEX.
- const Struct_field*
- field(unsigned int index) const
- { return &this->fields_->at(index); }
- // Get the struct fields.
- const Struct_field_list*
- fields() const
- { return this->fields_; }
- // Return the number of fields.
- size_t
- field_count() const
- { return this->fields_->size(); }
- // Push a new field onto the end of the struct. This is used when
- // building a closure variable.
- void
- push_field(const Struct_field& sf)
- { this->fields_->push_back(sf); }
- // Return an expression referring to field NAME in STRUCT_EXPR, or
- // NULL if there is no field with that name.
- Field_reference_expression*
- field_reference(Expression* struct_expr, const std::string& name,
- Location) const;
- // Return the total number of fields, including embedded fields.
- // This is the number of values that can appear in a conversion to
- // this type.
- unsigned int
- total_field_count() const;
- // Whether this type is identical with T.
- bool
- is_identical(const Struct_type* t, bool errors_are_identical) const;
- // Return whether NAME is a local field which is not exported. This
- // is only used for better error reporting.
- bool
- is_unexported_local_field(Gogo*, const std::string& name) const;
- // If this is an unnamed struct, build the complete list of methods,
- // including those from anonymous fields, and build methods stubs if
- // needed.
- void
- finalize_methods(Gogo*);
- // Return whether this type has any methods. This should only be
- // called after the finalize_methods pass.
- bool
- has_any_methods() const
- { return this->all_methods_ != NULL; }
- // Return the methods for tihs type. This should only be called
- // after the finalize_methods pass.
- const Methods*
- methods() const
- { return this->all_methods_; }
- // Return the method to use for NAME. This returns NULL if there is
- // no such method or if the method is ambiguous. When it returns
- // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
- Method*
- method_function(const std::string& name, bool* is_ambiguous) const;
- // Return a pointer to the interface method table for this type for
- // the interface INTERFACE. If IS_POINTER is true, set the type
- // descriptor to a pointer to this type, otherwise set it to this
- // type.
- Expression*
- interface_method_table(Interface_type* interface, bool is_pointer);
- // Traverse just the field types of a struct type.
- int
- traverse_field_types(Traverse* traverse)
- { return this->do_traverse(traverse); }
- // If the offset of field INDEX in the backend implementation can be
- // determined, set *POFFSET to the offset in bytes and return true.
- // Otherwise, return false.
- bool
- backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
- // Finish the backend representation of all the fields.
- void
- finish_backend_fields(Gogo*);
- // Import a struct type.
- static Struct_type*
- do_import(Import*);
- static Type*
- make_struct_type_descriptor_type();
- // Write the hash function for this type.
- void
- write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
- // Write the equality function for this type.
- void
- write_equal_function(Gogo*, Named_type*);
- protected:
- int
- do_traverse(Traverse*);
- bool
- do_verify();
- bool
- do_has_pointer() const;
- bool
- do_compare_is_identity(Gogo*);
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- // Used to merge method sets of identical unnamed structs.
- typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
- Type_identical) Identical_structs;
- static Identical_structs identical_structs;
- // Used to manage method tables for identical unnamed structs.
- typedef std::pair<Interface_method_tables*, Interface_method_tables*>
- Struct_method_table_pair;
- typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
- Type_hash_identical, Type_identical)
- Struct_method_tables;
- static Struct_method_tables struct_method_tables;
- // Used to avoid infinite loops in field_reference_depth.
- struct Saw_named_type
- {
- Saw_named_type* next;
- Named_type* nt;
- };
- Field_reference_expression*
- field_reference_depth(Expression* struct_expr, const std::string& name,
- Location, Saw_named_type*,
- unsigned int* depth) const;
- // The fields of the struct.
- Struct_field_list* fields_;
- // The place where the struct was declared.
- Location location_;
- // If this struct is unnamed, a list of methods.
- Methods* all_methods_;
- };
- // The type of an array.
- class Array_type : public Type
- {
- public:
- Array_type(Type* element_type, Expression* length)
- : Type(TYPE_ARRAY),
- element_type_(element_type), length_(length), blength_(NULL),
- issued_length_error_(false)
- { }
- // Return the element type.
- Type*
- element_type() const
- { return this->element_type_; }
- // Return the length. This will return NULL for a slice.
- Expression*
- length() const
- { return this->length_; }
- // Whether this type is identical with T.
- bool
- is_identical(const Array_type* t, bool errors_are_identical) const;
- // Return an expression for the pointer to the values in an array.
- Expression*
- get_value_pointer(Gogo*, Expression* array) const;
- // Return an expression for the length of an array with this type.
- Expression*
- get_length(Gogo*, Expression* array) const;
- // Return an expression for the capacity of an array with this type.
- Expression*
- get_capacity(Gogo*, Expression* array) const;
- // Import an array type.
- static Array_type*
- do_import(Import*);
- // Return the backend representation of the element type.
- Btype*
- get_backend_element(Gogo*, bool use_placeholder);
- // Return the backend representation of the length.
- Bexpression*
- get_backend_length(Gogo*);
- // Finish the backend representation of the element type.
- void
- finish_backend_element(Gogo*);
- static Type*
- make_array_type_descriptor_type();
- static Type*
- make_slice_type_descriptor_type();
- // Write the hash function for this type.
- void
- write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
- // Write the equality function for this type.
- void
- write_equal_function(Gogo*, Named_type*);
- protected:
- int
- do_traverse(Traverse* traverse);
- bool
- do_verify();
- bool
- do_has_pointer() const
- {
- return this->length_ == NULL || this->element_type_->has_pointer();
- }
- bool
- do_compare_is_identity(Gogo*);
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- bool
- verify_length();
- Expression*
- array_type_descriptor(Gogo*, Named_type*);
- Expression*
- slice_type_descriptor(Gogo*, Named_type*);
- void
- slice_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- array_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- // The type of elements of the array.
- Type* element_type_;
- // The number of elements. This may be NULL.
- Expression* length_;
- // The backend representation of the length.
- // We only want to compute this once.
- Bexpression* blength_;
- // Whether or not an invalid length error has been issued for this type,
- // to avoid knock-on errors.
- mutable bool issued_length_error_;
- };
- // The type of a map.
- class Map_type : public Type
- {
- public:
- Map_type(Type* key_type, Type* val_type, Location location)
- : Type(TYPE_MAP),
- key_type_(key_type), val_type_(val_type), location_(location)
- { }
- // Return the key type.
- Type*
- key_type() const
- { return this->key_type_; }
- // Return the value type.
- Type*
- val_type() const
- { return this->val_type_; }
- // Whether this type is identical with T.
- bool
- is_identical(const Map_type* t, bool errors_are_identical) const;
- // Import a map type.
- static Map_type*
- do_import(Import*);
- static Type*
- make_map_type_descriptor_type();
- static Type*
- make_map_descriptor_type();
- // Build a map descriptor for this type. Return a pointer to it.
- // The location is the location which causes us to need the
- // descriptor.
- Bexpression*
- map_descriptor_pointer(Gogo* gogo, Location);
- protected:
- int
- do_traverse(Traverse*);
- bool
- do_verify();
- bool
- do_has_pointer() const
- { return true; }
- bool
- do_compare_is_identity(Gogo*)
- { return false; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- // Mapping from map types to map descriptors.
- typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
- Type_identical) Map_descriptors;
- static Map_descriptors map_descriptors;
- Bvariable*
- map_descriptor(Gogo*);
- // The key type.
- Type* key_type_;
- // The value type.
- Type* val_type_;
- // Where the type was defined.
- Location location_;
- };
- // The type of a channel.
- class Channel_type : public Type
- {
- public:
- Channel_type(bool may_send, bool may_receive, Type* element_type)
- : Type(TYPE_CHANNEL),
- may_send_(may_send), may_receive_(may_receive),
- element_type_(element_type)
- { go_assert(may_send || may_receive); }
- // Whether this channel can send data.
- bool
- may_send() const
- { return this->may_send_; }
- // Whether this channel can receive data.
- bool
- may_receive() const
- { return this->may_receive_; }
- // The type of the values that may be sent on this channel. This is
- // NULL if any type may be sent.
- Type*
- element_type() const
- { return this->element_type_; }
- // Whether this type is identical with T.
- bool
- is_identical(const Channel_type* t, bool errors_are_identical) const;
- // Import a channel type.
- static Channel_type*
- do_import(Import*);
- static Type*
- make_chan_type_descriptor_type();
- protected:
- int
- do_traverse(Traverse* traverse)
- { return Type::traverse(this->element_type_, traverse); }
- bool
- do_has_pointer() const
- { return true; }
- bool
- do_compare_is_identity(Gogo*)
- { return true; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- // Whether this channel can send data.
- bool may_send_;
- // Whether this channel can receive data.
- bool may_receive_;
- // The types of elements which may be sent on this channel. If this
- // is NULL, it means that any type may be sent.
- Type* element_type_;
- };
- // An interface type.
- class Interface_type : public Type
- {
- public:
- Interface_type(Typed_identifier_list* methods, Location location)
- : Type(TYPE_INTERFACE),
- parse_methods_(methods), all_methods_(NULL), location_(location),
- interface_btype_(NULL), bmethods_(NULL), assume_identical_(NULL),
- methods_are_finalized_(false), bmethods_is_placeholder_(false),
- seen_(false)
- { go_assert(methods == NULL || !methods->empty()); }
- // The location where the interface type was defined.
- Location
- location() const
- { return this->location_; }
- // Return whether this is an empty interface.
- bool
- is_empty() const
- {
- go_assert(this->methods_are_finalized_);
- return this->all_methods_ == NULL;
- }
- // Return the list of methods. This will return NULL for an empty
- // interface.
- const Typed_identifier_list*
- methods() const;
- // Return the number of methods.
- size_t
- method_count() const;
- // Return the method NAME, or NULL.
- const Typed_identifier*
- find_method(const std::string& name) const;
- // Return the zero-based index of method NAME.
- size_t
- method_index(const std::string& name) const;
- // Finalize the methods. This sets all_methods_. This handles
- // interface inheritance.
- void
- finalize_methods();
- // Return true if T implements this interface. If this returns
- // false, and REASON is not NULL, it sets *REASON to the reason that
- // it fails.
- bool
- implements_interface(const Type* t, std::string* reason) const;
- // Whether this type is identical with T. REASON is as in
- // implements_interface.
- bool
- is_identical(const Interface_type* t, bool errors_are_identical) const;
- // Whether we can assign T to this type. is_identical is known to
- // be false.
- bool
- is_compatible_for_assign(const Interface_type*, std::string* reason) const;
- // Return whether NAME is a method which is not exported. This is
- // only used for better error reporting.
- bool
- is_unexported_method(Gogo*, const std::string& name) const;
- // Import an interface type.
- static Interface_type*
- do_import(Import*);
- // Make a struct for an empty interface type.
- static Btype*
- get_backend_empty_interface_type(Gogo*);
- // Get a pointer to the backend representation of the method table.
- Btype*
- get_backend_methods(Gogo*);
- // Return a placeholder for the backend representation of the
- // pointer to the method table.
- Btype*
- get_backend_methods_placeholder(Gogo*);
- // Finish the backend representation of the method types.
- void
- finish_backend_methods(Gogo*);
- static Type*
- make_interface_type_descriptor_type();
- protected:
- int
- do_traverse(Traverse*);
- bool
- do_has_pointer() const
- { return true; }
- bool
- do_compare_is_identity(Gogo*)
- { return false; }
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo*, Expression_list**, Expression**, int);
- void
- do_mangled_name(Gogo*, std::string*) const;
- void
- do_export(Export*) const;
- private:
- // This type guards against infinite recursion when comparing
- // interface types. We keep a list of interface types assumed to be
- // identical during comparison. We just keep the list on the stack.
- // This permits us to compare cases like
- // type I1 interface { F() interface{I1} }
- // type I2 interface { F() interface{I2} }
- struct Assume_identical
- {
- Assume_identical* next;
- const Interface_type* t1;
- const Interface_type* t2;
- };
- bool
- assume_identical(const Interface_type*, const Interface_type*) const;
- // The list of methods associated with the interface from the
- // parser. This will be NULL for the empty interface. This may
- // include unnamed interface types.
- Typed_identifier_list* parse_methods_;
- // The list of all methods associated with the interface. This
- // expands any interface types listed in methods_. It is set by
- // finalize_methods. This will be NULL for the empty interface.
- Typed_identifier_list* all_methods_;
- // The location where the interface was defined.
- Location location_;
- // The backend representation of this type during backend conversion.
- Btype* interface_btype_;
- // The backend representation of the pointer to the method table.
- Btype* bmethods_;
- // A list of interface types assumed to be identical during
- // interface comparison.
- mutable Assume_identical* assume_identical_;
- // Whether the methods have been finalized.
- bool methods_are_finalized_;
- // Whether the bmethods_ field is a placeholder.
- bool bmethods_is_placeholder_;
- // Used to avoid endless recursion in do_mangled_name.
- mutable bool seen_;
- };
- // The value we keep for a named type. This lets us get the right
- // name when we convert to backend. Note that we don't actually keep
- // the name here; the name is in the Named_object which points to
- // this. This object exists to hold a unique backend representation for
- // the type.
- class Named_type : public Type
- {
- public:
- Named_type(Named_object* named_object, Type* type, Location location)
- : Type(TYPE_NAMED),
- named_object_(named_object), in_function_(NULL), in_function_index_(0),
- type_(type), local_methods_(NULL), all_methods_(NULL),
- interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
- location_(location), named_btype_(NULL), dependencies_(),
- is_visible_(true), is_error_(false), is_placeholder_(false),
- is_converted_(false), is_circular_(false), is_verified_(false),
- seen_(false), seen_in_compare_is_identity_(false),
- seen_in_get_backend_(false)
- { }
- // Return the associated Named_object. This holds the actual name.
- Named_object*
- named_object()
- { return this->named_object_; }
- const Named_object*
- named_object() const
- { return this->named_object_; }
- // Set the Named_object. This is used when we see a type
- // declaration followed by a type.
- void
- set_named_object(Named_object* no)
- { this->named_object_ = no; }
- // Return the function in which this type is defined. This will
- // return NULL for a type defined in global scope.
- const Named_object*
- in_function(unsigned int *pindex) const
- {
- *pindex = this->in_function_index_;
- return this->in_function_;
- }
- // Set the function in which this type is defined.
- void
- set_in_function(Named_object* f, unsigned int index)
- {
- this->in_function_ = f;
- this->in_function_index_ = index;
- }
- // Return the name of the type.
- const std::string&
- name() const;
- // Return the name of the type for an error message. The difference
- // is that if the type is defined in a different package, this will
- // return PACKAGE.NAME.
- std::string
- message_name() const;
- // Return the underlying type.
- Type*
- real_type()
- { return this->type_; }
- const Type*
- real_type() const
- { return this->type_; }
- // Return the location.
- Location
- location() const
- { return this->location_; }
- // Whether this type is visible. This only matters when parsing.
- bool
- is_visible() const
- { return this->is_visible_; }
- // Mark this type as visible.
- void
- set_is_visible()
- { this->is_visible_ = true; }
- // Mark this type as invisible.
- void
- clear_is_visible()
- { this->is_visible_ = false; }
- // Whether this is a builtin type.
- bool
- is_builtin() const
- { return Linemap::is_predeclared_location(this->location_); }
- // Whether this is an alias. There are currently two aliases: byte
- // and rune.
- bool
- is_alias() const;
- // Whether this named type is valid. A recursive named type is invalid.
- bool
- is_valid() const
- { return !this->is_error_; }
- // Whether this is a circular type: a pointer or function type that
- // refers to itself, which is not possible in C.
- bool
- is_circular() const
- { return this->is_circular_; }
- // Return the base type for this type.
- Type*
- named_base();
- const Type*
- named_base() const;
- // Return whether this is an error type.
- bool
- is_named_error_type() const;
- // Return whether this type is comparable. If REASON is not NULL,
- // set *REASON when returning false.
- bool
- named_type_is_comparable(std::string* reason) const;
- // Add a method to this type.
- Named_object*
- add_method(const std::string& name, Function*);
- // Add a method declaration to this type.
- Named_object*
- add_method_declaration(const std::string& name, Package* package,
- Function_type* type, Location location);
- // Add an existing method--one defined before the type itself was
- // defined--to a type.
- void
- add_existing_method(Named_object*);
- // Look up a local method.
- Named_object*
- find_local_method(const std::string& name) const;
- // Return the list of local methods.
- const Bindings*
- local_methods() const
- { return this->local_methods_; }
- // Build the complete list of methods, including those from
- // anonymous fields, and build method stubs if needed.
- void
- finalize_methods(Gogo*);
- // Return whether this type has any methods. This should only be
- // called after the finalize_methods pass.
- bool
- has_any_methods() const
- { return this->all_methods_ != NULL; }
- // Return the methods for this type. This should only be called
- // after the finalized_methods pass.
- const Methods*
- methods() const
- { return this->all_methods_; }
- // Return the method to use for NAME. This returns NULL if there is
- // no such method or if the method is ambiguous. When it returns
- // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
- Method*
- method_function(const std::string& name, bool *is_ambiguous) const;
- // Return whether NAME is a known field or method which is not
- // exported. This is only used for better error reporting.
- bool
- is_unexported_local_method(Gogo*, const std::string& name) const;
- // Return a pointer to the interface method table for this type for
- // the interface INTERFACE. If IS_POINTER is true, set the type
- // descriptor to a pointer to this type, otherwise set it to this
- // type.
- Expression*
- interface_method_table(Interface_type* interface, bool is_pointer);
- // Note that a type must be converted to the backend representation
- // before we convert this type.
- void
- add_dependency(Named_type* nt)
- { this->dependencies_.push_back(nt); }
- // Return true if the size and alignment of the backend
- // representation of this type is known. This is always true after
- // types have been converted, but may be false beforehand.
- bool
- is_named_backend_type_size_known() const
- { return this->named_btype_ != NULL && !this->is_placeholder_; }
- // Export the type.
- void
- export_named_type(Export*, const std::string& name) const;
- // Import a named type.
- static void
- import_named_type(Import*, Named_type**);
- // Initial conversion to backend representation.
- void
- convert(Gogo*);
- protected:
- int
- do_traverse(Traverse* traverse)
- { return Type::traverse(this->type_, traverse); }
- bool
- do_verify();
- bool
- do_has_pointer() const;
- bool
- do_compare_is_identity(Gogo*);
- unsigned int
- do_hash_for_method(Gogo*) const;
- Btype*
- do_get_backend(Gogo*);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo* gogo, Expression_list** vals, Expression** offset,
- int stack);
- void
- do_mangled_name(Gogo*, std::string* ret) const;
- void
- do_export(Export*) const;
- private:
- // Create the placeholder during conversion.
- void
- create_placeholder(Gogo*);
- // A pointer back to the Named_object for this type.
- Named_object* named_object_;
- // If this type is defined in a function, a pointer back to the
- // function in which it is defined.
- Named_object* in_function_;
- // The index of this type in IN_FUNCTION_.
- unsigned int in_function_index_;
- // The actual type.
- Type* type_;
- // The list of methods defined for this type. Any named type can
- // have methods.
- Bindings* local_methods_;
- // The full list of methods for this type, including methods
- // declared for anonymous fields.
- Methods* all_methods_;
- // A mapping from interfaces to the associated interface method
- // tables for this type.
- Interface_method_tables* interface_method_tables_;
- // A mapping from interfaces to the associated interface method
- // tables for pointers to this type.
- Interface_method_tables* pointer_interface_method_tables_;
- // The location where this type was defined.
- Location location_;
- // The backend representation of this type during backend
- // conversion. This is used to avoid endless recursion when a named
- // type refers to itself.
- Btype* named_btype_;
- // A list of types which must be converted to the backend
- // representation before this type can be converted. This is for
- // cases like
- // type S1 { p *S2 }
- // type S2 { s S1 }
- // where we can't convert S2 to the backend representation unless we
- // have converted S1.
- std::vector<Named_type*> dependencies_;
- // Whether this type is visible. This is false if this type was
- // created because it was referenced by an imported object, but the
- // type itself was not exported. This will always be true for types
- // created in the current package.
- bool is_visible_;
- // Whether this type is erroneous.
- bool is_error_;
- // Whether the current value of named_btype_ is a placeholder for
- // which the final size of the type is not known.
- bool is_placeholder_;
- // Whether this type has been converted to the backend
- // representation. Implies that is_placeholder_ is false.
- bool is_converted_;
- // Whether this is a pointer or function type which refers to the
- // type itself.
- bool is_circular_;
- // Whether this type has been verified.
- bool is_verified_;
- // In a recursive operation such as has_pointer, this flag is used
- // to prevent infinite recursion when a type refers to itself. This
- // is mutable because it is always reset to false when the function
- // exits.
- mutable bool seen_;
- // Like seen_, but used only by do_compare_is_identity.
- bool seen_in_compare_is_identity_;
- // Like seen_, but used only by do_get_backend.
- bool seen_in_get_backend_;
- };
- // A forward declaration. This handles a type which has been declared
- // but not defined.
- class Forward_declaration_type : public Type
- {
- public:
- Forward_declaration_type(Named_object* named_object);
- // The named object associated with this type declaration. This
- // will be resolved.
- Named_object*
- named_object();
- const Named_object*
- named_object() const;
- // Return the name of the type.
- const std::string&
- name() const;
- // Return the type to which this points. Give an error if the type
- // has not yet been defined.
- Type*
- real_type();
- const Type*
- real_type() const;
- // Whether the base type has been defined.
- bool
- is_defined() const;
- // Add a method to this type.
- Named_object*
- add_method(const std::string& name, Function*);
- // Add a method declaration to this type.
- Named_object*
- add_method_declaration(const std::string& name, Package*, Function_type*,
- Location);
- protected:
- int
- do_traverse(Traverse* traverse);
- bool
- do_verify();
- bool
- do_has_pointer() const
- { return this->real_type()->has_pointer(); }
- bool
- do_compare_is_identity(Gogo* gogo)
- { return this->real_type()->compare_is_identity(gogo); }
- unsigned int
- do_hash_for_method(Gogo* gogo) const
- { return this->real_type()->hash_for_method(gogo); }
- Btype*
- do_get_backend(Gogo* gogo);
- Expression*
- do_type_descriptor(Gogo*, Named_type*);
- void
- do_reflection(Gogo*, std::string*) const;
- void
- do_gc_symbol(Gogo* gogo, Expression_list** vals, Expression** offset,
- int stack_size)
- { Type::gc_symbol(gogo, this->real_type(), vals, offset, stack_size); }
- void
- do_mangled_name(Gogo*, std::string* ret) const;
- void
- do_export(Export*) const;
- private:
- // Issue a warning about a use of an undefined type.
- void
- warn() const;
- // The type declaration.
- Named_object* named_object_;
- // Whether we have issued a warning about this type.
- mutable bool warned_;
- };
- // The Type_context struct describes what we expect for the type of an
- // expression.
- struct Type_context
- {
- // The exact type we expect, if known. This may be NULL.
- Type* type;
- // Whether an abstract type is permitted.
- bool may_be_abstract;
- // Constructors.
- Type_context()
- : type(NULL), may_be_abstract(false)
- { }
- Type_context(Type* a_type, bool a_may_be_abstract)
- : type(a_type), may_be_abstract(a_may_be_abstract)
- { }
- };
- #endif // !defined(GO_TYPES_H)
|