statements.cc 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076
  1. // statements.cc -- Go frontend statements.
  2. // Copyright 2009 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. #include "go-system.h"
  6. #include "go-c.h"
  7. #include "types.h"
  8. #include "expressions.h"
  9. #include "gogo.h"
  10. #include "runtime.h"
  11. #include "backend.h"
  12. #include "statements.h"
  13. #include "ast-dump.h"
  14. // Class Statement.
  15. Statement::Statement(Statement_classification classification,
  16. Location location)
  17. : classification_(classification), location_(location)
  18. {
  19. }
  20. Statement::~Statement()
  21. {
  22. }
  23. // Traverse the tree. The work of walking the components is handled
  24. // by the subclasses.
  25. int
  26. Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
  27. {
  28. if (this->classification_ == STATEMENT_ERROR)
  29. return TRAVERSE_CONTINUE;
  30. unsigned int traverse_mask = traverse->traverse_mask();
  31. if ((traverse_mask & Traverse::traverse_statements) != 0)
  32. {
  33. int t = traverse->statement(block, pindex, this);
  34. if (t == TRAVERSE_EXIT)
  35. return TRAVERSE_EXIT;
  36. else if (t == TRAVERSE_SKIP_COMPONENTS)
  37. return TRAVERSE_CONTINUE;
  38. }
  39. // No point in checking traverse_mask here--a statement may contain
  40. // other blocks or statements, and if we got here we always want to
  41. // walk them.
  42. return this->do_traverse(traverse);
  43. }
  44. // Traverse the contents of a statement.
  45. int
  46. Statement::traverse_contents(Traverse* traverse)
  47. {
  48. return this->do_traverse(traverse);
  49. }
  50. // Traverse assignments.
  51. bool
  52. Statement::traverse_assignments(Traverse_assignments* tassign)
  53. {
  54. if (this->classification_ == STATEMENT_ERROR)
  55. return false;
  56. return this->do_traverse_assignments(tassign);
  57. }
  58. // Traverse an expression in a statement. This is a helper function
  59. // for child classes.
  60. int
  61. Statement::traverse_expression(Traverse* traverse, Expression** expr)
  62. {
  63. if ((traverse->traverse_mask()
  64. & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
  65. return TRAVERSE_CONTINUE;
  66. return Expression::traverse(expr, traverse);
  67. }
  68. // Traverse an expression list in a statement. This is a helper
  69. // function for child classes.
  70. int
  71. Statement::traverse_expression_list(Traverse* traverse,
  72. Expression_list* expr_list)
  73. {
  74. if (expr_list == NULL)
  75. return TRAVERSE_CONTINUE;
  76. if ((traverse->traverse_mask()
  77. & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
  78. return TRAVERSE_CONTINUE;
  79. return expr_list->traverse(traverse);
  80. }
  81. // Traverse a type in a statement. This is a helper function for
  82. // child classes.
  83. int
  84. Statement::traverse_type(Traverse* traverse, Type* type)
  85. {
  86. if ((traverse->traverse_mask()
  87. & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
  88. return TRAVERSE_CONTINUE;
  89. return Type::traverse(type, traverse);
  90. }
  91. // Set type information for unnamed constants. This is really done by
  92. // the child class.
  93. void
  94. Statement::determine_types()
  95. {
  96. this->do_determine_types();
  97. }
  98. // If this is a thunk statement, return it.
  99. Thunk_statement*
  100. Statement::thunk_statement()
  101. {
  102. Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
  103. if (ret == NULL)
  104. ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
  105. return ret;
  106. }
  107. // Convert a Statement to the backend representation. This is really
  108. // done by the child class.
  109. Bstatement*
  110. Statement::get_backend(Translate_context* context)
  111. {
  112. if (this->classification_ == STATEMENT_ERROR)
  113. return context->backend()->error_statement();
  114. return this->do_get_backend(context);
  115. }
  116. // Dump AST representation for a statement to a dump context.
  117. void
  118. Statement::dump_statement(Ast_dump_context* ast_dump_context) const
  119. {
  120. this->do_dump_statement(ast_dump_context);
  121. }
  122. // Note that this statement is erroneous. This is called by children
  123. // when they discover an error.
  124. void
  125. Statement::set_is_error()
  126. {
  127. this->classification_ = STATEMENT_ERROR;
  128. }
  129. // For children to call to report an error conveniently.
  130. void
  131. Statement::report_error(const char* msg)
  132. {
  133. error_at(this->location_, "%s", msg);
  134. this->set_is_error();
  135. }
  136. // An error statement, used to avoid crashing after we report an
  137. // error.
  138. class Error_statement : public Statement
  139. {
  140. public:
  141. Error_statement(Location location)
  142. : Statement(STATEMENT_ERROR, location)
  143. { }
  144. protected:
  145. int
  146. do_traverse(Traverse*)
  147. { return TRAVERSE_CONTINUE; }
  148. Bstatement*
  149. do_get_backend(Translate_context*)
  150. { go_unreachable(); }
  151. void
  152. do_dump_statement(Ast_dump_context*) const;
  153. };
  154. // Dump the AST representation for an error statement.
  155. void
  156. Error_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  157. {
  158. ast_dump_context->print_indent();
  159. ast_dump_context->ostream() << "Error statement" << std::endl;
  160. }
  161. // Make an error statement.
  162. Statement*
  163. Statement::make_error_statement(Location location)
  164. {
  165. return new Error_statement(location);
  166. }
  167. // Class Variable_declaration_statement.
  168. Variable_declaration_statement::Variable_declaration_statement(
  169. Named_object* var)
  170. : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
  171. var_(var)
  172. {
  173. }
  174. // We don't actually traverse the variable here; it was traversed
  175. // while traversing the Block.
  176. int
  177. Variable_declaration_statement::do_traverse(Traverse*)
  178. {
  179. return TRAVERSE_CONTINUE;
  180. }
  181. // Traverse the assignments in a variable declaration. Note that this
  182. // traversal is different from the usual traversal.
  183. bool
  184. Variable_declaration_statement::do_traverse_assignments(
  185. Traverse_assignments* tassign)
  186. {
  187. tassign->initialize_variable(this->var_);
  188. return true;
  189. }
  190. // Lower the variable's initialization expression.
  191. Statement*
  192. Variable_declaration_statement::do_lower(Gogo* gogo, Named_object* function,
  193. Block*, Statement_inserter* inserter)
  194. {
  195. this->var_->var_value()->lower_init_expression(gogo, function, inserter);
  196. return this;
  197. }
  198. // Flatten the variable's initialization expression.
  199. Statement*
  200. Variable_declaration_statement::do_flatten(Gogo* gogo, Named_object* function,
  201. Block*, Statement_inserter* inserter)
  202. {
  203. this->var_->var_value()->flatten_init_expression(gogo, function, inserter);
  204. return this;
  205. }
  206. // Convert a variable declaration to the backend representation.
  207. Bstatement*
  208. Variable_declaration_statement::do_get_backend(Translate_context* context)
  209. {
  210. Variable* var = this->var_->var_value();
  211. Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
  212. context->function());
  213. Bexpression* binit = var->get_init(context->gogo(), context->function());
  214. if (!var->is_in_heap())
  215. {
  216. go_assert(binit != NULL);
  217. return context->backend()->init_statement(bvar, binit);
  218. }
  219. // Something takes the address of this variable, so the value is
  220. // stored in the heap. Initialize it to newly allocated memory
  221. // space, and assign the initial value to the new space.
  222. Location loc = this->location();
  223. Named_object* newfn = context->gogo()->lookup_global("new");
  224. go_assert(newfn != NULL && newfn->is_function_declaration());
  225. Expression* func = Expression::make_func_reference(newfn, NULL, loc);
  226. Expression_list* params = new Expression_list();
  227. params->push_back(Expression::make_type(var->type(), loc));
  228. Expression* call = Expression::make_call(func, params, false, loc);
  229. context->gogo()->lower_expression(context->function(), NULL, &call);
  230. Temporary_statement* temp = Statement::make_temporary(NULL, call, loc);
  231. Bstatement* btemp = temp->get_backend(context);
  232. Bstatement* set = NULL;
  233. if (binit != NULL)
  234. {
  235. Expression* e = Expression::make_temporary_reference(temp, loc);
  236. e = Expression::make_unary(OPERATOR_MULT, e, loc);
  237. Bexpression* be = e->get_backend(context);
  238. set = context->backend()->assignment_statement(be, binit, loc);
  239. }
  240. Expression* ref = Expression::make_temporary_reference(temp, loc);
  241. Bexpression* bref = ref->get_backend(context);
  242. Bstatement* sinit = context->backend()->init_statement(bvar, bref);
  243. std::vector<Bstatement*> stats;
  244. stats.reserve(3);
  245. stats.push_back(btemp);
  246. if (set != NULL)
  247. stats.push_back(set);
  248. stats.push_back(sinit);
  249. return context->backend()->statement_list(stats);
  250. }
  251. // Dump the AST representation for a variable declaration.
  252. void
  253. Variable_declaration_statement::do_dump_statement(
  254. Ast_dump_context* ast_dump_context) const
  255. {
  256. ast_dump_context->print_indent();
  257. go_assert(var_->is_variable());
  258. ast_dump_context->ostream() << "var " << this->var_->name() << " ";
  259. Variable* var = this->var_->var_value();
  260. if (var->has_type())
  261. {
  262. ast_dump_context->dump_type(var->type());
  263. ast_dump_context->ostream() << " ";
  264. }
  265. if (var->init() != NULL)
  266. {
  267. ast_dump_context->ostream() << "= ";
  268. ast_dump_context->dump_expression(var->init());
  269. }
  270. ast_dump_context->ostream() << std::endl;
  271. }
  272. // Make a variable declaration.
  273. Statement*
  274. Statement::make_variable_declaration(Named_object* var)
  275. {
  276. return new Variable_declaration_statement(var);
  277. }
  278. // Class Temporary_statement.
  279. // Return the type of the temporary variable.
  280. Type*
  281. Temporary_statement::type() const
  282. {
  283. return this->type_ != NULL ? this->type_ : this->init_->type();
  284. }
  285. // Traversal.
  286. int
  287. Temporary_statement::do_traverse(Traverse* traverse)
  288. {
  289. if (this->type_ != NULL
  290. && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
  291. return TRAVERSE_EXIT;
  292. if (this->init_ == NULL)
  293. return TRAVERSE_CONTINUE;
  294. else
  295. return this->traverse_expression(traverse, &this->init_);
  296. }
  297. // Traverse assignments.
  298. bool
  299. Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
  300. {
  301. if (this->init_ == NULL)
  302. return false;
  303. tassign->value(&this->init_, true, true);
  304. return true;
  305. }
  306. // Determine types.
  307. void
  308. Temporary_statement::do_determine_types()
  309. {
  310. if (this->type_ != NULL && this->type_->is_abstract())
  311. this->type_ = this->type_->make_non_abstract_type();
  312. if (this->init_ != NULL)
  313. {
  314. if (this->type_ == NULL)
  315. this->init_->determine_type_no_context();
  316. else
  317. {
  318. Type_context context(this->type_, false);
  319. this->init_->determine_type(&context);
  320. }
  321. }
  322. if (this->type_ == NULL)
  323. {
  324. this->type_ = this->init_->type();
  325. go_assert(!this->type_->is_abstract());
  326. }
  327. }
  328. // Check types.
  329. void
  330. Temporary_statement::do_check_types(Gogo*)
  331. {
  332. if (this->type_ != NULL && this->init_ != NULL)
  333. {
  334. std::string reason;
  335. if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
  336. {
  337. if (reason.empty())
  338. error_at(this->location(), "incompatible types in assignment");
  339. else
  340. error_at(this->location(), "incompatible types in assignment (%s)",
  341. reason.c_str());
  342. this->set_is_error();
  343. }
  344. }
  345. }
  346. // Flatten a temporary statement: add another temporary when it might
  347. // be needed for interface conversion.
  348. Statement*
  349. Temporary_statement::do_flatten(Gogo*, Named_object*, Block*,
  350. Statement_inserter* inserter)
  351. {
  352. if (this->type_ != NULL
  353. && this->init_ != NULL
  354. && !Type::are_identical(this->type_, this->init_->type(), false, NULL)
  355. && this->init_->type()->interface_type() != NULL
  356. && !this->init_->is_variable())
  357. {
  358. Temporary_statement *temp =
  359. Statement::make_temporary(NULL, this->init_, this->location());
  360. inserter->insert(temp);
  361. this->init_ = Expression::make_temporary_reference(temp,
  362. this->location());
  363. }
  364. return this;
  365. }
  366. // Convert to backend representation.
  367. Bstatement*
  368. Temporary_statement::do_get_backend(Translate_context* context)
  369. {
  370. go_assert(this->bvariable_ == NULL);
  371. Named_object* function = context->function();
  372. go_assert(function != NULL);
  373. Bfunction* bfunction = function->func_value()->get_decl();
  374. Btype* btype = this->type()->get_backend(context->gogo());
  375. Bexpression* binit;
  376. if (this->init_ == NULL)
  377. binit = NULL;
  378. else if (this->type_ == NULL)
  379. binit = this->init_->get_backend(context);
  380. else
  381. {
  382. Expression* init = Expression::convert_for_assignment(context->gogo(),
  383. this->type_,
  384. this->init_,
  385. this->location());
  386. binit = init->get_backend(context);
  387. }
  388. Bstatement* statement;
  389. this->bvariable_ =
  390. context->backend()->temporary_variable(bfunction, context->bblock(),
  391. btype, binit,
  392. this->is_address_taken_,
  393. this->location(), &statement);
  394. return statement;
  395. }
  396. // Return the backend variable.
  397. Bvariable*
  398. Temporary_statement::get_backend_variable(Translate_context* context) const
  399. {
  400. if (this->bvariable_ == NULL)
  401. {
  402. go_assert(saw_errors());
  403. return context->backend()->error_variable();
  404. }
  405. return this->bvariable_;
  406. }
  407. // Dump the AST represemtation for a temporary statement
  408. void
  409. Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  410. {
  411. ast_dump_context->print_indent();
  412. ast_dump_context->dump_temp_variable_name(this);
  413. if (this->type_ != NULL)
  414. {
  415. ast_dump_context->ostream() << " ";
  416. ast_dump_context->dump_type(this->type_);
  417. }
  418. if (this->init_ != NULL)
  419. {
  420. ast_dump_context->ostream() << " = ";
  421. ast_dump_context->dump_expression(this->init_);
  422. }
  423. ast_dump_context->ostream() << std::endl;
  424. }
  425. // Make and initialize a temporary variable in BLOCK.
  426. Temporary_statement*
  427. Statement::make_temporary(Type* type, Expression* init,
  428. Location location)
  429. {
  430. return new Temporary_statement(type, init, location);
  431. }
  432. // An assignment statement.
  433. class Assignment_statement : public Statement
  434. {
  435. public:
  436. Assignment_statement(Expression* lhs, Expression* rhs,
  437. Location location)
  438. : Statement(STATEMENT_ASSIGNMENT, location),
  439. lhs_(lhs), rhs_(rhs)
  440. { }
  441. protected:
  442. int
  443. do_traverse(Traverse* traverse);
  444. bool
  445. do_traverse_assignments(Traverse_assignments*);
  446. void
  447. do_determine_types();
  448. void
  449. do_check_types(Gogo*);
  450. Statement*
  451. do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
  452. Bstatement*
  453. do_get_backend(Translate_context*);
  454. void
  455. do_dump_statement(Ast_dump_context*) const;
  456. private:
  457. // Left hand side--the lvalue.
  458. Expression* lhs_;
  459. // Right hand side--the rvalue.
  460. Expression* rhs_;
  461. };
  462. // Traversal.
  463. int
  464. Assignment_statement::do_traverse(Traverse* traverse)
  465. {
  466. if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
  467. return TRAVERSE_EXIT;
  468. return this->traverse_expression(traverse, &this->rhs_);
  469. }
  470. bool
  471. Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
  472. {
  473. tassign->assignment(&this->lhs_, &this->rhs_);
  474. return true;
  475. }
  476. // Set types for the assignment.
  477. void
  478. Assignment_statement::do_determine_types()
  479. {
  480. this->lhs_->determine_type_no_context();
  481. Type* rhs_context_type = this->lhs_->type();
  482. if (rhs_context_type->is_sink_type())
  483. rhs_context_type = NULL;
  484. Type_context context(rhs_context_type, false);
  485. this->rhs_->determine_type(&context);
  486. }
  487. // Check types for an assignment.
  488. void
  489. Assignment_statement::do_check_types(Gogo*)
  490. {
  491. // The left hand side must be either addressable, a map index
  492. // expression, or the blank identifier.
  493. if (!this->lhs_->is_addressable()
  494. && this->lhs_->map_index_expression() == NULL
  495. && !this->lhs_->is_sink_expression())
  496. {
  497. if (!this->lhs_->type()->is_error())
  498. this->report_error(_("invalid left hand side of assignment"));
  499. return;
  500. }
  501. Type* lhs_type = this->lhs_->type();
  502. Type* rhs_type = this->rhs_->type();
  503. // Invalid assignment of nil to the blank identifier.
  504. if (lhs_type->is_sink_type()
  505. && rhs_type->is_nil_type())
  506. {
  507. this->report_error(_("use of untyped nil"));
  508. return;
  509. }
  510. std::string reason;
  511. if (!Type::are_assignable(lhs_type, rhs_type, &reason))
  512. {
  513. if (reason.empty())
  514. error_at(this->location(), "incompatible types in assignment");
  515. else
  516. error_at(this->location(), "incompatible types in assignment (%s)",
  517. reason.c_str());
  518. this->set_is_error();
  519. }
  520. if (lhs_type->is_error() || rhs_type->is_error())
  521. this->set_is_error();
  522. }
  523. // Flatten an assignment statement. We may need a temporary for
  524. // interface conversion.
  525. Statement*
  526. Assignment_statement::do_flatten(Gogo*, Named_object*, Block*,
  527. Statement_inserter* inserter)
  528. {
  529. if (!this->lhs_->is_sink_expression()
  530. && !Type::are_identical(this->lhs_->type(), this->rhs_->type(),
  531. false, NULL)
  532. && this->rhs_->type()->interface_type() != NULL
  533. && !this->rhs_->is_variable())
  534. {
  535. Temporary_statement* temp =
  536. Statement::make_temporary(NULL, this->rhs_, this->location());
  537. inserter->insert(temp);
  538. this->rhs_ = Expression::make_temporary_reference(temp,
  539. this->location());
  540. }
  541. return this;
  542. }
  543. // Convert an assignment statement to the backend representation.
  544. Bstatement*
  545. Assignment_statement::do_get_backend(Translate_context* context)
  546. {
  547. if (this->lhs_->is_sink_expression())
  548. {
  549. Bexpression* rhs = this->rhs_->get_backend(context);
  550. return context->backend()->expression_statement(rhs);
  551. }
  552. Bexpression* lhs = this->lhs_->get_backend(context);
  553. Expression* conv =
  554. Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),
  555. this->rhs_, this->location());
  556. Bexpression* rhs = conv->get_backend(context);
  557. return context->backend()->assignment_statement(lhs, rhs, this->location());
  558. }
  559. // Dump the AST representation for an assignment statement.
  560. void
  561. Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
  562. const
  563. {
  564. ast_dump_context->print_indent();
  565. ast_dump_context->dump_expression(this->lhs_);
  566. ast_dump_context->ostream() << " = " ;
  567. ast_dump_context->dump_expression(this->rhs_);
  568. ast_dump_context->ostream() << std::endl;
  569. }
  570. // Make an assignment statement.
  571. Statement*
  572. Statement::make_assignment(Expression* lhs, Expression* rhs,
  573. Location location)
  574. {
  575. return new Assignment_statement(lhs, rhs, location);
  576. }
  577. // The Move_subexpressions class is used to move all top-level
  578. // subexpressions of an expression. This is used for things like
  579. // index expressions in which we must evaluate the index value before
  580. // it can be changed by a multiple assignment.
  581. class Move_subexpressions : public Traverse
  582. {
  583. public:
  584. Move_subexpressions(int skip, Block* block)
  585. : Traverse(traverse_expressions),
  586. skip_(skip), block_(block)
  587. { }
  588. protected:
  589. int
  590. expression(Expression**);
  591. private:
  592. // The number of subexpressions to skip moving. This is used to
  593. // avoid moving the array itself, as we only need to move the index.
  594. int skip_;
  595. // The block where new temporary variables should be added.
  596. Block* block_;
  597. };
  598. int
  599. Move_subexpressions::expression(Expression** pexpr)
  600. {
  601. if (this->skip_ > 0)
  602. --this->skip_;
  603. else if ((*pexpr)->temporary_reference_expression() == NULL
  604. && !(*pexpr)->is_nil_expression())
  605. {
  606. Location loc = (*pexpr)->location();
  607. Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
  608. this->block_->add_statement(temp);
  609. *pexpr = Expression::make_temporary_reference(temp, loc);
  610. }
  611. // We only need to move top-level subexpressions.
  612. return TRAVERSE_SKIP_COMPONENTS;
  613. }
  614. // The Move_ordered_evals class is used to find any subexpressions of
  615. // an expression that have an evaluation order dependency. It creates
  616. // temporary variables to hold them.
  617. class Move_ordered_evals : public Traverse
  618. {
  619. public:
  620. Move_ordered_evals(Block* block)
  621. : Traverse(traverse_expressions),
  622. block_(block)
  623. { }
  624. protected:
  625. int
  626. expression(Expression**);
  627. private:
  628. // The block where new temporary variables should be added.
  629. Block* block_;
  630. };
  631. int
  632. Move_ordered_evals::expression(Expression** pexpr)
  633. {
  634. // We have to look at subexpressions first.
  635. if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
  636. return TRAVERSE_EXIT;
  637. int i;
  638. if ((*pexpr)->must_eval_subexpressions_in_order(&i))
  639. {
  640. Move_subexpressions ms(i, this->block_);
  641. if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
  642. return TRAVERSE_EXIT;
  643. }
  644. if ((*pexpr)->must_eval_in_order())
  645. {
  646. Call_expression* call = (*pexpr)->call_expression();
  647. if (call != NULL && call->is_multi_value_arg())
  648. {
  649. // A call expression which returns multiple results as an argument
  650. // to another call must be handled specially. We can't create a
  651. // temporary because there is no type to give it. Instead, group
  652. // the caller and this multi-valued call argument and use a temporary
  653. // variable to hold them.
  654. return TRAVERSE_SKIP_COMPONENTS;
  655. }
  656. Location loc = (*pexpr)->location();
  657. Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
  658. this->block_->add_statement(temp);
  659. *pexpr = Expression::make_temporary_reference(temp, loc);
  660. }
  661. return TRAVERSE_SKIP_COMPONENTS;
  662. }
  663. // An assignment operation statement.
  664. class Assignment_operation_statement : public Statement
  665. {
  666. public:
  667. Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
  668. Location location)
  669. : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
  670. op_(op), lhs_(lhs), rhs_(rhs)
  671. { }
  672. protected:
  673. int
  674. do_traverse(Traverse*);
  675. bool
  676. do_traverse_assignments(Traverse_assignments*)
  677. { go_unreachable(); }
  678. Statement*
  679. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  680. Bstatement*
  681. do_get_backend(Translate_context*)
  682. { go_unreachable(); }
  683. void
  684. do_dump_statement(Ast_dump_context*) const;
  685. private:
  686. // The operator (OPERATOR_PLUSEQ, etc.).
  687. Operator op_;
  688. // Left hand side.
  689. Expression* lhs_;
  690. // Right hand side.
  691. Expression* rhs_;
  692. };
  693. // Traversal.
  694. int
  695. Assignment_operation_statement::do_traverse(Traverse* traverse)
  696. {
  697. if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
  698. return TRAVERSE_EXIT;
  699. return this->traverse_expression(traverse, &this->rhs_);
  700. }
  701. // Lower an assignment operation statement to a regular assignment
  702. // statement.
  703. Statement*
  704. Assignment_operation_statement::do_lower(Gogo*, Named_object*,
  705. Block* enclosing, Statement_inserter*)
  706. {
  707. Location loc = this->location();
  708. // We have to evaluate the left hand side expression only once. We
  709. // do this by moving out any expression with side effects.
  710. Block* b = new Block(enclosing, loc);
  711. Move_ordered_evals moe(b);
  712. this->lhs_->traverse_subexpressions(&moe);
  713. Expression* lval = this->lhs_->copy();
  714. Operator op;
  715. switch (this->op_)
  716. {
  717. case OPERATOR_PLUSEQ:
  718. op = OPERATOR_PLUS;
  719. break;
  720. case OPERATOR_MINUSEQ:
  721. op = OPERATOR_MINUS;
  722. break;
  723. case OPERATOR_OREQ:
  724. op = OPERATOR_OR;
  725. break;
  726. case OPERATOR_XOREQ:
  727. op = OPERATOR_XOR;
  728. break;
  729. case OPERATOR_MULTEQ:
  730. op = OPERATOR_MULT;
  731. break;
  732. case OPERATOR_DIVEQ:
  733. op = OPERATOR_DIV;
  734. break;
  735. case OPERATOR_MODEQ:
  736. op = OPERATOR_MOD;
  737. break;
  738. case OPERATOR_LSHIFTEQ:
  739. op = OPERATOR_LSHIFT;
  740. break;
  741. case OPERATOR_RSHIFTEQ:
  742. op = OPERATOR_RSHIFT;
  743. break;
  744. case OPERATOR_ANDEQ:
  745. op = OPERATOR_AND;
  746. break;
  747. case OPERATOR_BITCLEAREQ:
  748. op = OPERATOR_BITCLEAR;
  749. break;
  750. default:
  751. go_unreachable();
  752. }
  753. Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
  754. Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
  755. if (b->statements()->empty())
  756. {
  757. delete b;
  758. return s;
  759. }
  760. else
  761. {
  762. b->add_statement(s);
  763. return Statement::make_block_statement(b, loc);
  764. }
  765. }
  766. // Dump the AST representation for an assignment operation statement
  767. void
  768. Assignment_operation_statement::do_dump_statement(
  769. Ast_dump_context* ast_dump_context) const
  770. {
  771. ast_dump_context->print_indent();
  772. ast_dump_context->dump_expression(this->lhs_);
  773. ast_dump_context->dump_operator(this->op_);
  774. ast_dump_context->dump_expression(this->rhs_);
  775. ast_dump_context->ostream() << std::endl;
  776. }
  777. // Make an assignment operation statement.
  778. Statement*
  779. Statement::make_assignment_operation(Operator op, Expression* lhs,
  780. Expression* rhs, Location location)
  781. {
  782. return new Assignment_operation_statement(op, lhs, rhs, location);
  783. }
  784. // A tuple assignment statement. This differs from an assignment
  785. // statement in that the right-hand-side expressions are evaluated in
  786. // parallel.
  787. class Tuple_assignment_statement : public Statement
  788. {
  789. public:
  790. Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
  791. Location location)
  792. : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
  793. lhs_(lhs), rhs_(rhs)
  794. { }
  795. protected:
  796. int
  797. do_traverse(Traverse* traverse);
  798. bool
  799. do_traverse_assignments(Traverse_assignments*)
  800. { go_unreachable(); }
  801. Statement*
  802. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  803. Bstatement*
  804. do_get_backend(Translate_context*)
  805. { go_unreachable(); }
  806. void
  807. do_dump_statement(Ast_dump_context*) const;
  808. private:
  809. // Left hand side--a list of lvalues.
  810. Expression_list* lhs_;
  811. // Right hand side--a list of rvalues.
  812. Expression_list* rhs_;
  813. };
  814. // Traversal.
  815. int
  816. Tuple_assignment_statement::do_traverse(Traverse* traverse)
  817. {
  818. if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
  819. return TRAVERSE_EXIT;
  820. return this->traverse_expression_list(traverse, this->rhs_);
  821. }
  822. // Lower a tuple assignment. We use temporary variables to split it
  823. // up into a set of single assignments.
  824. Statement*
  825. Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
  826. Statement_inserter*)
  827. {
  828. Location loc = this->location();
  829. Block* b = new Block(enclosing, loc);
  830. // First move out any subexpressions on the left hand side. The
  831. // right hand side will be evaluated in the required order anyhow.
  832. Move_ordered_evals moe(b);
  833. for (Expression_list::iterator plhs = this->lhs_->begin();
  834. plhs != this->lhs_->end();
  835. ++plhs)
  836. Expression::traverse(&*plhs, &moe);
  837. std::vector<Temporary_statement*> temps;
  838. temps.reserve(this->lhs_->size());
  839. Expression_list::const_iterator prhs = this->rhs_->begin();
  840. for (Expression_list::const_iterator plhs = this->lhs_->begin();
  841. plhs != this->lhs_->end();
  842. ++plhs, ++prhs)
  843. {
  844. go_assert(prhs != this->rhs_->end());
  845. if ((*plhs)->is_error_expression()
  846. || (*plhs)->type()->is_error()
  847. || (*prhs)->is_error_expression()
  848. || (*prhs)->type()->is_error())
  849. continue;
  850. if ((*plhs)->is_sink_expression())
  851. {
  852. if ((*prhs)->type()->is_nil_type())
  853. this->report_error(_("use of untyped nil"));
  854. else
  855. b->add_statement(Statement::make_statement(*prhs, true));
  856. continue;
  857. }
  858. Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
  859. *prhs, loc);
  860. b->add_statement(temp);
  861. temps.push_back(temp);
  862. }
  863. go_assert(prhs == this->rhs_->end());
  864. prhs = this->rhs_->begin();
  865. std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
  866. for (Expression_list::const_iterator plhs = this->lhs_->begin();
  867. plhs != this->lhs_->end();
  868. ++plhs, ++prhs)
  869. {
  870. if ((*plhs)->is_error_expression()
  871. || (*plhs)->type()->is_error()
  872. || (*prhs)->is_error_expression()
  873. || (*prhs)->type()->is_error())
  874. continue;
  875. if ((*plhs)->is_sink_expression())
  876. continue;
  877. Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
  878. b->add_statement(Statement::make_assignment(*plhs, ref, loc));
  879. ++ptemp;
  880. }
  881. go_assert(ptemp == temps.end() || saw_errors());
  882. return Statement::make_block_statement(b, loc);
  883. }
  884. // Dump the AST representation for a tuple assignment statement.
  885. void
  886. Tuple_assignment_statement::do_dump_statement(
  887. Ast_dump_context* ast_dump_context) const
  888. {
  889. ast_dump_context->print_indent();
  890. ast_dump_context->dump_expression_list(this->lhs_);
  891. ast_dump_context->ostream() << " = ";
  892. ast_dump_context->dump_expression_list(this->rhs_);
  893. ast_dump_context->ostream() << std::endl;
  894. }
  895. // Make a tuple assignment statement.
  896. Statement*
  897. Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
  898. Location location)
  899. {
  900. return new Tuple_assignment_statement(lhs, rhs, location);
  901. }
  902. // A tuple assignment from a map index expression.
  903. // v, ok = m[k]
  904. class Tuple_map_assignment_statement : public Statement
  905. {
  906. public:
  907. Tuple_map_assignment_statement(Expression* val, Expression* present,
  908. Expression* map_index,
  909. Location location)
  910. : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
  911. val_(val), present_(present), map_index_(map_index)
  912. { }
  913. protected:
  914. int
  915. do_traverse(Traverse* traverse);
  916. bool
  917. do_traverse_assignments(Traverse_assignments*)
  918. { go_unreachable(); }
  919. Statement*
  920. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  921. Bstatement*
  922. do_get_backend(Translate_context*)
  923. { go_unreachable(); }
  924. void
  925. do_dump_statement(Ast_dump_context*) const;
  926. private:
  927. // Lvalue which receives the value from the map.
  928. Expression* val_;
  929. // Lvalue which receives whether the key value was present.
  930. Expression* present_;
  931. // The map index expression.
  932. Expression* map_index_;
  933. };
  934. // Traversal.
  935. int
  936. Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
  937. {
  938. if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
  939. || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
  940. return TRAVERSE_EXIT;
  941. return this->traverse_expression(traverse, &this->map_index_);
  942. }
  943. // Lower a tuple map assignment.
  944. Statement*
  945. Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
  946. Block* enclosing, Statement_inserter*)
  947. {
  948. Location loc = this->location();
  949. Map_index_expression* map_index = this->map_index_->map_index_expression();
  950. if (map_index == NULL)
  951. {
  952. this->report_error(_("expected map index on right hand side"));
  953. return Statement::make_error_statement(loc);
  954. }
  955. Map_type* map_type = map_index->get_map_type();
  956. if (map_type == NULL)
  957. return Statement::make_error_statement(loc);
  958. Block* b = new Block(enclosing, loc);
  959. // Move out any subexpressions to make sure that functions are
  960. // called in the required order.
  961. Move_ordered_evals moe(b);
  962. this->val_->traverse_subexpressions(&moe);
  963. this->present_->traverse_subexpressions(&moe);
  964. // Copy the key value into a temporary so that we can take its
  965. // address without pushing the value onto the heap.
  966. // var key_temp KEY_TYPE = MAP_INDEX
  967. Temporary_statement* key_temp =
  968. Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
  969. b->add_statement(key_temp);
  970. // var val_temp VAL_TYPE
  971. Temporary_statement* val_temp =
  972. Statement::make_temporary(map_type->val_type(), NULL, loc);
  973. b->add_statement(val_temp);
  974. // var present_temp bool
  975. Temporary_statement* present_temp =
  976. Statement::make_temporary((this->present_->type()->is_sink_type())
  977. ? Type::make_boolean_type()
  978. : this->present_->type(),
  979. NULL, loc);
  980. b->add_statement(present_temp);
  981. // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
  982. Expression* a1 = Expression::make_type_descriptor(map_type, loc);
  983. Expression* a2 = map_index->map();
  984. Temporary_reference_expression* ref =
  985. Expression::make_temporary_reference(key_temp, loc);
  986. Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
  987. ref = Expression::make_temporary_reference(val_temp, loc);
  988. Expression* a4 = Expression::make_unary(OPERATOR_AND, ref, loc);
  989. Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 4,
  990. a1, a2, a3, a4);
  991. ref = Expression::make_temporary_reference(present_temp, loc);
  992. ref->set_is_lvalue();
  993. Statement* s = Statement::make_assignment(ref, call, loc);
  994. b->add_statement(s);
  995. // val = val_temp
  996. ref = Expression::make_temporary_reference(val_temp, loc);
  997. s = Statement::make_assignment(this->val_, ref, loc);
  998. b->add_statement(s);
  999. // present = present_temp
  1000. ref = Expression::make_temporary_reference(present_temp, loc);
  1001. s = Statement::make_assignment(this->present_, ref, loc);
  1002. b->add_statement(s);
  1003. return Statement::make_block_statement(b, loc);
  1004. }
  1005. // Dump the AST representation for a tuple map assignment statement.
  1006. void
  1007. Tuple_map_assignment_statement::do_dump_statement(
  1008. Ast_dump_context* ast_dump_context) const
  1009. {
  1010. ast_dump_context->print_indent();
  1011. ast_dump_context->dump_expression(this->val_);
  1012. ast_dump_context->ostream() << ", ";
  1013. ast_dump_context->dump_expression(this->present_);
  1014. ast_dump_context->ostream() << " = ";
  1015. ast_dump_context->dump_expression(this->map_index_);
  1016. ast_dump_context->ostream() << std::endl;
  1017. }
  1018. // Make a map assignment statement which returns a pair of values.
  1019. Statement*
  1020. Statement::make_tuple_map_assignment(Expression* val, Expression* present,
  1021. Expression* map_index,
  1022. Location location)
  1023. {
  1024. return new Tuple_map_assignment_statement(val, present, map_index, location);
  1025. }
  1026. // Assign a pair of entries to a map.
  1027. // m[k] = v, p
  1028. class Map_assignment_statement : public Statement
  1029. {
  1030. public:
  1031. Map_assignment_statement(Expression* map_index,
  1032. Expression* val, Expression* should_set,
  1033. Location location)
  1034. : Statement(STATEMENT_MAP_ASSIGNMENT, location),
  1035. map_index_(map_index), val_(val), should_set_(should_set)
  1036. { }
  1037. protected:
  1038. int
  1039. do_traverse(Traverse* traverse);
  1040. bool
  1041. do_traverse_assignments(Traverse_assignments*)
  1042. { go_unreachable(); }
  1043. Statement*
  1044. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  1045. Bstatement*
  1046. do_get_backend(Translate_context*)
  1047. { go_unreachable(); }
  1048. void
  1049. do_dump_statement(Ast_dump_context*) const;
  1050. private:
  1051. // A reference to the map index which should be set or deleted.
  1052. Expression* map_index_;
  1053. // The value to add to the map.
  1054. Expression* val_;
  1055. // Whether or not to add the value.
  1056. Expression* should_set_;
  1057. };
  1058. // Traverse a map assignment.
  1059. int
  1060. Map_assignment_statement::do_traverse(Traverse* traverse)
  1061. {
  1062. if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
  1063. || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
  1064. return TRAVERSE_EXIT;
  1065. return this->traverse_expression(traverse, &this->should_set_);
  1066. }
  1067. // Lower a map assignment to a function call.
  1068. Statement*
  1069. Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
  1070. Statement_inserter*)
  1071. {
  1072. Location loc = this->location();
  1073. Map_index_expression* map_index = this->map_index_->map_index_expression();
  1074. if (map_index == NULL)
  1075. {
  1076. this->report_error(_("expected map index on left hand side"));
  1077. return Statement::make_error_statement(loc);
  1078. }
  1079. Map_type* map_type = map_index->get_map_type();
  1080. if (map_type == NULL)
  1081. return Statement::make_error_statement(loc);
  1082. Block* b = new Block(enclosing, loc);
  1083. // Evaluate the map first to get order of evaluation right.
  1084. // map_temp := m // we are evaluating m[k] = v, p
  1085. Temporary_statement* map_temp = Statement::make_temporary(map_type,
  1086. map_index->map(),
  1087. loc);
  1088. b->add_statement(map_temp);
  1089. // var key_temp MAP_KEY_TYPE = k
  1090. Temporary_statement* key_temp =
  1091. Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
  1092. b->add_statement(key_temp);
  1093. // var val_temp MAP_VAL_TYPE = v
  1094. Temporary_statement* val_temp =
  1095. Statement::make_temporary(map_type->val_type(), this->val_, loc);
  1096. b->add_statement(val_temp);
  1097. // var insert_temp bool = p
  1098. Temporary_statement* insert_temp =
  1099. Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
  1100. loc);
  1101. b->add_statement(insert_temp);
  1102. // mapassign2(map_temp, &key_temp, &val_temp, p)
  1103. Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
  1104. Expression* ref = Expression::make_temporary_reference(key_temp, loc);
  1105. Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
  1106. ref = Expression::make_temporary_reference(val_temp, loc);
  1107. Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
  1108. Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
  1109. Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
  1110. p1, p2, p3, p4);
  1111. Statement* s = Statement::make_statement(call, true);
  1112. b->add_statement(s);
  1113. return Statement::make_block_statement(b, loc);
  1114. }
  1115. // Dump the AST representation for a map assignment statement.
  1116. void
  1117. Map_assignment_statement::do_dump_statement(
  1118. Ast_dump_context* ast_dump_context) const
  1119. {
  1120. ast_dump_context->print_indent();
  1121. ast_dump_context->dump_expression(this->map_index_);
  1122. ast_dump_context->ostream() << " = ";
  1123. ast_dump_context->dump_expression(this->val_);
  1124. ast_dump_context->ostream() << ", ";
  1125. ast_dump_context->dump_expression(this->should_set_);
  1126. ast_dump_context->ostream() << std::endl;
  1127. }
  1128. // Make a statement which assigns a pair of entries to a map.
  1129. Statement*
  1130. Statement::make_map_assignment(Expression* map_index,
  1131. Expression* val, Expression* should_set,
  1132. Location location)
  1133. {
  1134. return new Map_assignment_statement(map_index, val, should_set, location);
  1135. }
  1136. // A tuple assignment from a receive statement.
  1137. class Tuple_receive_assignment_statement : public Statement
  1138. {
  1139. public:
  1140. Tuple_receive_assignment_statement(Expression* val, Expression* closed,
  1141. Expression* channel, Location location)
  1142. : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
  1143. val_(val), closed_(closed), channel_(channel)
  1144. { }
  1145. protected:
  1146. int
  1147. do_traverse(Traverse* traverse);
  1148. bool
  1149. do_traverse_assignments(Traverse_assignments*)
  1150. { go_unreachable(); }
  1151. Statement*
  1152. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  1153. Bstatement*
  1154. do_get_backend(Translate_context*)
  1155. { go_unreachable(); }
  1156. void
  1157. do_dump_statement(Ast_dump_context*) const;
  1158. private:
  1159. // Lvalue which receives the value from the channel.
  1160. Expression* val_;
  1161. // Lvalue which receives whether the channel is closed.
  1162. Expression* closed_;
  1163. // The channel on which we receive the value.
  1164. Expression* channel_;
  1165. };
  1166. // Traversal.
  1167. int
  1168. Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
  1169. {
  1170. if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
  1171. || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
  1172. return TRAVERSE_EXIT;
  1173. return this->traverse_expression(traverse, &this->channel_);
  1174. }
  1175. // Lower to a function call.
  1176. Statement*
  1177. Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
  1178. Block* enclosing,
  1179. Statement_inserter*)
  1180. {
  1181. Location loc = this->location();
  1182. Channel_type* channel_type = this->channel_->type()->channel_type();
  1183. if (channel_type == NULL)
  1184. {
  1185. this->report_error(_("expected channel"));
  1186. return Statement::make_error_statement(loc);
  1187. }
  1188. if (!channel_type->may_receive())
  1189. {
  1190. this->report_error(_("invalid receive on send-only channel"));
  1191. return Statement::make_error_statement(loc);
  1192. }
  1193. Block* b = new Block(enclosing, loc);
  1194. // Make sure that any subexpressions on the left hand side are
  1195. // evaluated in the right order.
  1196. Move_ordered_evals moe(b);
  1197. this->val_->traverse_subexpressions(&moe);
  1198. this->closed_->traverse_subexpressions(&moe);
  1199. // var val_temp ELEMENT_TYPE
  1200. Temporary_statement* val_temp =
  1201. Statement::make_temporary(channel_type->element_type(), NULL, loc);
  1202. b->add_statement(val_temp);
  1203. // var closed_temp bool
  1204. Temporary_statement* closed_temp =
  1205. Statement::make_temporary((this->closed_->type()->is_sink_type())
  1206. ? Type::make_boolean_type()
  1207. : this->closed_->type(),
  1208. NULL, loc);
  1209. b->add_statement(closed_temp);
  1210. // closed_temp = chanrecv2(type, channel, &val_temp)
  1211. Expression* td = Expression::make_type_descriptor(this->channel_->type(),
  1212. loc);
  1213. Temporary_reference_expression* ref =
  1214. Expression::make_temporary_reference(val_temp, loc);
  1215. Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
  1216. Expression* call = Runtime::make_call(Runtime::CHANRECV2,
  1217. loc, 3, td, this->channel_, p2);
  1218. ref = Expression::make_temporary_reference(closed_temp, loc);
  1219. ref->set_is_lvalue();
  1220. Statement* s = Statement::make_assignment(ref, call, loc);
  1221. b->add_statement(s);
  1222. // val = val_temp
  1223. ref = Expression::make_temporary_reference(val_temp, loc);
  1224. s = Statement::make_assignment(this->val_, ref, loc);
  1225. b->add_statement(s);
  1226. // closed = closed_temp
  1227. ref = Expression::make_temporary_reference(closed_temp, loc);
  1228. s = Statement::make_assignment(this->closed_, ref, loc);
  1229. b->add_statement(s);
  1230. return Statement::make_block_statement(b, loc);
  1231. }
  1232. // Dump the AST representation for a tuple receive statement.
  1233. void
  1234. Tuple_receive_assignment_statement::do_dump_statement(
  1235. Ast_dump_context* ast_dump_context) const
  1236. {
  1237. ast_dump_context->print_indent();
  1238. ast_dump_context->dump_expression(this->val_);
  1239. ast_dump_context->ostream() << ", ";
  1240. ast_dump_context->dump_expression(this->closed_);
  1241. ast_dump_context->ostream() << " <- ";
  1242. ast_dump_context->dump_expression(this->channel_);
  1243. ast_dump_context->ostream() << std::endl;
  1244. }
  1245. // Make a nonblocking receive statement.
  1246. Statement*
  1247. Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
  1248. Expression* channel,
  1249. Location location)
  1250. {
  1251. return new Tuple_receive_assignment_statement(val, closed, channel,
  1252. location);
  1253. }
  1254. // An assignment to a pair of values from a type guard. This is a
  1255. // conditional type guard. v, ok = i.(type).
  1256. class Tuple_type_guard_assignment_statement : public Statement
  1257. {
  1258. public:
  1259. Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
  1260. Expression* expr, Type* type,
  1261. Location location)
  1262. : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
  1263. val_(val), ok_(ok), expr_(expr), type_(type)
  1264. { }
  1265. protected:
  1266. int
  1267. do_traverse(Traverse*);
  1268. bool
  1269. do_traverse_assignments(Traverse_assignments*)
  1270. { go_unreachable(); }
  1271. Statement*
  1272. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  1273. Bstatement*
  1274. do_get_backend(Translate_context*)
  1275. { go_unreachable(); }
  1276. void
  1277. do_dump_statement(Ast_dump_context*) const;
  1278. private:
  1279. Call_expression*
  1280. lower_to_type(Runtime::Function);
  1281. void
  1282. lower_to_object_type(Block*, Runtime::Function);
  1283. // The variable which recieves the converted value.
  1284. Expression* val_;
  1285. // The variable which receives the indication of success.
  1286. Expression* ok_;
  1287. // The expression being converted.
  1288. Expression* expr_;
  1289. // The type to which the expression is being converted.
  1290. Type* type_;
  1291. };
  1292. // Traverse a type guard tuple assignment.
  1293. int
  1294. Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
  1295. {
  1296. if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
  1297. || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
  1298. || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
  1299. return TRAVERSE_EXIT;
  1300. return this->traverse_expression(traverse, &this->expr_);
  1301. }
  1302. // Lower to a function call.
  1303. Statement*
  1304. Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
  1305. Block* enclosing,
  1306. Statement_inserter*)
  1307. {
  1308. Location loc = this->location();
  1309. Type* expr_type = this->expr_->type();
  1310. if (expr_type->interface_type() == NULL)
  1311. {
  1312. if (!expr_type->is_error() && !this->type_->is_error())
  1313. this->report_error(_("type assertion only valid for interface types"));
  1314. return Statement::make_error_statement(loc);
  1315. }
  1316. Block* b = new Block(enclosing, loc);
  1317. // Make sure that any subexpressions on the left hand side are
  1318. // evaluated in the right order.
  1319. Move_ordered_evals moe(b);
  1320. this->val_->traverse_subexpressions(&moe);
  1321. this->ok_->traverse_subexpressions(&moe);
  1322. bool expr_is_empty = expr_type->interface_type()->is_empty();
  1323. Call_expression* call;
  1324. if (this->type_->interface_type() != NULL)
  1325. {
  1326. if (this->type_->interface_type()->is_empty())
  1327. call = Runtime::make_call((expr_is_empty
  1328. ? Runtime::IFACEE2E2
  1329. : Runtime::IFACEI2E2),
  1330. loc, 1, this->expr_);
  1331. else
  1332. call = this->lower_to_type(expr_is_empty
  1333. ? Runtime::IFACEE2I2
  1334. : Runtime::IFACEI2I2);
  1335. }
  1336. else if (this->type_->points_to() != NULL)
  1337. call = this->lower_to_type(expr_is_empty
  1338. ? Runtime::IFACEE2T2P
  1339. : Runtime::IFACEI2T2P);
  1340. else
  1341. {
  1342. this->lower_to_object_type(b,
  1343. (expr_is_empty
  1344. ? Runtime::IFACEE2T2
  1345. : Runtime::IFACEI2T2));
  1346. call = NULL;
  1347. }
  1348. if (call != NULL)
  1349. {
  1350. Expression* res = Expression::make_call_result(call, 0);
  1351. res = Expression::make_unsafe_cast(this->type_, res, loc);
  1352. Statement* s = Statement::make_assignment(this->val_, res, loc);
  1353. b->add_statement(s);
  1354. res = Expression::make_call_result(call, 1);
  1355. s = Statement::make_assignment(this->ok_, res, loc);
  1356. b->add_statement(s);
  1357. }
  1358. return Statement::make_block_statement(b, loc);
  1359. }
  1360. // Lower a conversion to a non-empty interface type or a pointer type.
  1361. Call_expression*
  1362. Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
  1363. {
  1364. Location loc = this->location();
  1365. return Runtime::make_call(code, loc, 2,
  1366. Expression::make_type_descriptor(this->type_, loc),
  1367. this->expr_);
  1368. }
  1369. // Lower a conversion to a non-interface non-pointer type.
  1370. void
  1371. Tuple_type_guard_assignment_statement::lower_to_object_type(
  1372. Block* b,
  1373. Runtime::Function code)
  1374. {
  1375. Location loc = this->location();
  1376. // var val_temp TYPE
  1377. Temporary_statement* val_temp = Statement::make_temporary(this->type_,
  1378. NULL, loc);
  1379. b->add_statement(val_temp);
  1380. // ok = CODE(type_descriptor, expr, &val_temp)
  1381. Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
  1382. Expression* ref = Expression::make_temporary_reference(val_temp, loc);
  1383. Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
  1384. Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
  1385. Statement* s = Statement::make_assignment(this->ok_, call, loc);
  1386. b->add_statement(s);
  1387. // val = val_temp
  1388. ref = Expression::make_temporary_reference(val_temp, loc);
  1389. s = Statement::make_assignment(this->val_, ref, loc);
  1390. b->add_statement(s);
  1391. }
  1392. // Dump the AST representation for a tuple type guard statement.
  1393. void
  1394. Tuple_type_guard_assignment_statement::do_dump_statement(
  1395. Ast_dump_context* ast_dump_context) const
  1396. {
  1397. ast_dump_context->print_indent();
  1398. ast_dump_context->dump_expression(this->val_);
  1399. ast_dump_context->ostream() << ", ";
  1400. ast_dump_context->dump_expression(this->ok_);
  1401. ast_dump_context->ostream() << " = ";
  1402. ast_dump_context->dump_expression(this->expr_);
  1403. ast_dump_context->ostream() << " . ";
  1404. ast_dump_context->dump_type(this->type_);
  1405. ast_dump_context->ostream() << std::endl;
  1406. }
  1407. // Make an assignment from a type guard to a pair of variables.
  1408. Statement*
  1409. Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
  1410. Expression* expr, Type* type,
  1411. Location location)
  1412. {
  1413. return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
  1414. location);
  1415. }
  1416. // Class Expression_statement.
  1417. // Constructor.
  1418. Expression_statement::Expression_statement(Expression* expr, bool is_ignored)
  1419. : Statement(STATEMENT_EXPRESSION, expr->location()),
  1420. expr_(expr), is_ignored_(is_ignored)
  1421. {
  1422. }
  1423. // Determine types.
  1424. void
  1425. Expression_statement::do_determine_types()
  1426. {
  1427. this->expr_->determine_type_no_context();
  1428. }
  1429. // Check the types of an expression statement. The only check we do
  1430. // is to possibly give an error about discarding the value of the
  1431. // expression.
  1432. void
  1433. Expression_statement::do_check_types(Gogo*)
  1434. {
  1435. if (!this->is_ignored_)
  1436. this->expr_->discarding_value();
  1437. }
  1438. // An expression statement is only a terminating statement if it is
  1439. // a call to panic.
  1440. bool
  1441. Expression_statement::do_may_fall_through() const
  1442. {
  1443. const Call_expression* call = this->expr_->call_expression();
  1444. if (call != NULL)
  1445. {
  1446. const Expression* fn = call->fn();
  1447. // panic is still an unknown named object.
  1448. const Unknown_expression* ue = fn->unknown_expression();
  1449. if (ue != NULL)
  1450. {
  1451. Named_object* no = ue->named_object();
  1452. if (no->is_unknown())
  1453. no = no->unknown_value()->real_named_object();
  1454. if (no != NULL)
  1455. {
  1456. Function_type* fntype;
  1457. if (no->is_function())
  1458. fntype = no->func_value()->type();
  1459. else if (no->is_function_declaration())
  1460. fntype = no->func_declaration_value()->type();
  1461. else
  1462. fntype = NULL;
  1463. // The builtin function panic does not return.
  1464. if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
  1465. return false;
  1466. }
  1467. }
  1468. }
  1469. return true;
  1470. }
  1471. // Convert to backend representation.
  1472. Bstatement*
  1473. Expression_statement::do_get_backend(Translate_context* context)
  1474. {
  1475. Bexpression* bexpr = this->expr_->get_backend(context);
  1476. return context->backend()->expression_statement(bexpr);
  1477. }
  1478. // Dump the AST representation for an expression statement
  1479. void
  1480. Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
  1481. const
  1482. {
  1483. ast_dump_context->print_indent();
  1484. ast_dump_context->dump_expression(expr_);
  1485. ast_dump_context->ostream() << std::endl;
  1486. }
  1487. // Make an expression statement from an Expression.
  1488. Statement*
  1489. Statement::make_statement(Expression* expr, bool is_ignored)
  1490. {
  1491. return new Expression_statement(expr, is_ignored);
  1492. }
  1493. // A block statement--a list of statements which may include variable
  1494. // definitions.
  1495. class Block_statement : public Statement
  1496. {
  1497. public:
  1498. Block_statement(Block* block, Location location)
  1499. : Statement(STATEMENT_BLOCK, location),
  1500. block_(block)
  1501. { }
  1502. protected:
  1503. int
  1504. do_traverse(Traverse* traverse)
  1505. { return this->block_->traverse(traverse); }
  1506. void
  1507. do_determine_types()
  1508. { this->block_->determine_types(); }
  1509. bool
  1510. do_may_fall_through() const
  1511. { return this->block_->may_fall_through(); }
  1512. Bstatement*
  1513. do_get_backend(Translate_context* context);
  1514. void
  1515. do_dump_statement(Ast_dump_context*) const;
  1516. private:
  1517. Block* block_;
  1518. };
  1519. // Convert a block to the backend representation of a statement.
  1520. Bstatement*
  1521. Block_statement::do_get_backend(Translate_context* context)
  1522. {
  1523. Bblock* bblock = this->block_->get_backend(context);
  1524. return context->backend()->block_statement(bblock);
  1525. }
  1526. // Dump the AST for a block statement
  1527. void
  1528. Block_statement::do_dump_statement(Ast_dump_context*) const
  1529. {
  1530. // block statement braces are dumped when traversing.
  1531. }
  1532. // Make a block statement.
  1533. Statement*
  1534. Statement::make_block_statement(Block* block, Location location)
  1535. {
  1536. return new Block_statement(block, location);
  1537. }
  1538. // An increment or decrement statement.
  1539. class Inc_dec_statement : public Statement
  1540. {
  1541. public:
  1542. Inc_dec_statement(bool is_inc, Expression* expr)
  1543. : Statement(STATEMENT_INCDEC, expr->location()),
  1544. expr_(expr), is_inc_(is_inc)
  1545. { }
  1546. protected:
  1547. int
  1548. do_traverse(Traverse* traverse)
  1549. { return this->traverse_expression(traverse, &this->expr_); }
  1550. bool
  1551. do_traverse_assignments(Traverse_assignments*)
  1552. { go_unreachable(); }
  1553. Statement*
  1554. do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
  1555. Bstatement*
  1556. do_get_backend(Translate_context*)
  1557. { go_unreachable(); }
  1558. void
  1559. do_dump_statement(Ast_dump_context*) const;
  1560. private:
  1561. // The l-value to increment or decrement.
  1562. Expression* expr_;
  1563. // Whether to increment or decrement.
  1564. bool is_inc_;
  1565. };
  1566. // Lower to += or -=.
  1567. Statement*
  1568. Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
  1569. {
  1570. Location loc = this->location();
  1571. Expression* oexpr = Expression::make_integer_ul(1, this->expr_->type(), loc);
  1572. Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
  1573. return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
  1574. }
  1575. // Dump the AST representation for a inc/dec statement.
  1576. void
  1577. Inc_dec_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  1578. {
  1579. ast_dump_context->print_indent();
  1580. ast_dump_context->dump_expression(expr_);
  1581. ast_dump_context->ostream() << (is_inc_? "++": "--") << std::endl;
  1582. }
  1583. // Make an increment statement.
  1584. Statement*
  1585. Statement::make_inc_statement(Expression* expr)
  1586. {
  1587. return new Inc_dec_statement(true, expr);
  1588. }
  1589. // Make a decrement statement.
  1590. Statement*
  1591. Statement::make_dec_statement(Expression* expr)
  1592. {
  1593. return new Inc_dec_statement(false, expr);
  1594. }
  1595. // Class Thunk_statement. This is the base class for go and defer
  1596. // statements.
  1597. Unordered_set(const Struct_type*) Thunk_statement::thunk_types;
  1598. // Constructor.
  1599. Thunk_statement::Thunk_statement(Statement_classification classification,
  1600. Call_expression* call,
  1601. Location location)
  1602. : Statement(classification, location),
  1603. call_(call), struct_type_(NULL)
  1604. {
  1605. }
  1606. // Return whether this is a simple statement which does not require a
  1607. // thunk.
  1608. bool
  1609. Thunk_statement::is_simple(Function_type* fntype) const
  1610. {
  1611. // We need a thunk to call a method, or to pass a variable number of
  1612. // arguments.
  1613. if (fntype->is_method() || fntype->is_varargs())
  1614. return false;
  1615. // A defer statement requires a thunk to set up for whether the
  1616. // function can call recover.
  1617. if (this->classification() == STATEMENT_DEFER)
  1618. return false;
  1619. // We can only permit a single parameter of pointer type.
  1620. const Typed_identifier_list* parameters = fntype->parameters();
  1621. if (parameters != NULL
  1622. && (parameters->size() > 1
  1623. || (parameters->size() == 1
  1624. && parameters->begin()->type()->points_to() == NULL)))
  1625. return false;
  1626. // If the function returns multiple values, or returns a type other
  1627. // than integer, floating point, or pointer, then it may get a
  1628. // hidden first parameter, in which case we need the more
  1629. // complicated approach. This is true even though we are going to
  1630. // ignore the return value.
  1631. const Typed_identifier_list* results = fntype->results();
  1632. if (results != NULL
  1633. && (results->size() > 1
  1634. || (results->size() == 1
  1635. && !results->begin()->type()->is_basic_type()
  1636. && results->begin()->type()->points_to() == NULL)))
  1637. return false;
  1638. // If this calls something that is not a simple function, then we
  1639. // need a thunk.
  1640. Expression* fn = this->call_->call_expression()->fn();
  1641. if (fn->func_expression() == NULL)
  1642. return false;
  1643. // If the function uses a closure, then we need a thunk. FIXME: We
  1644. // could accept a zero argument function with a closure.
  1645. if (fn->func_expression()->closure() != NULL)
  1646. return false;
  1647. return true;
  1648. }
  1649. // Traverse a thunk statement.
  1650. int
  1651. Thunk_statement::do_traverse(Traverse* traverse)
  1652. {
  1653. return this->traverse_expression(traverse, &this->call_);
  1654. }
  1655. // We implement traverse_assignment for a thunk statement because it
  1656. // effectively copies the function call.
  1657. bool
  1658. Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
  1659. {
  1660. Expression* fn = this->call_->call_expression()->fn();
  1661. Expression* fn2 = fn;
  1662. tassign->value(&fn2, true, false);
  1663. return true;
  1664. }
  1665. // Determine types in a thunk statement.
  1666. void
  1667. Thunk_statement::do_determine_types()
  1668. {
  1669. this->call_->determine_type_no_context();
  1670. // Now that we know the types of the call, build the struct used to
  1671. // pass parameters.
  1672. Call_expression* ce = this->call_->call_expression();
  1673. if (ce == NULL)
  1674. return;
  1675. Function_type* fntype = ce->get_function_type();
  1676. if (fntype != NULL && !this->is_simple(fntype))
  1677. this->struct_type_ = this->build_struct(fntype);
  1678. }
  1679. // Check types in a thunk statement.
  1680. void
  1681. Thunk_statement::do_check_types(Gogo*)
  1682. {
  1683. if (!this->call_->discarding_value())
  1684. return;
  1685. Call_expression* ce = this->call_->call_expression();
  1686. if (ce == NULL)
  1687. {
  1688. if (!this->call_->is_error_expression())
  1689. this->report_error("expected call expression");
  1690. return;
  1691. }
  1692. }
  1693. // The Traverse class used to find and simplify thunk statements.
  1694. class Simplify_thunk_traverse : public Traverse
  1695. {
  1696. public:
  1697. Simplify_thunk_traverse(Gogo* gogo)
  1698. : Traverse(traverse_functions | traverse_blocks),
  1699. gogo_(gogo), function_(NULL)
  1700. { }
  1701. int
  1702. function(Named_object*);
  1703. int
  1704. block(Block*);
  1705. private:
  1706. // General IR.
  1707. Gogo* gogo_;
  1708. // The function we are traversing.
  1709. Named_object* function_;
  1710. };
  1711. // Keep track of the current function while looking for thunks.
  1712. int
  1713. Simplify_thunk_traverse::function(Named_object* no)
  1714. {
  1715. go_assert(this->function_ == NULL);
  1716. this->function_ = no;
  1717. int t = no->func_value()->traverse(this);
  1718. this->function_ = NULL;
  1719. if (t == TRAVERSE_EXIT)
  1720. return t;
  1721. return TRAVERSE_SKIP_COMPONENTS;
  1722. }
  1723. // Look for thunks in a block.
  1724. int
  1725. Simplify_thunk_traverse::block(Block* b)
  1726. {
  1727. // The parser ensures that thunk statements always appear at the end
  1728. // of a block.
  1729. if (b->statements()->size() < 1)
  1730. return TRAVERSE_CONTINUE;
  1731. Thunk_statement* stat = b->statements()->back()->thunk_statement();
  1732. if (stat == NULL)
  1733. return TRAVERSE_CONTINUE;
  1734. if (stat->simplify_statement(this->gogo_, this->function_, b))
  1735. return TRAVERSE_SKIP_COMPONENTS;
  1736. return TRAVERSE_CONTINUE;
  1737. }
  1738. // Simplify all thunk statements.
  1739. void
  1740. Gogo::simplify_thunk_statements()
  1741. {
  1742. Simplify_thunk_traverse thunk_traverse(this);
  1743. this->traverse(&thunk_traverse);
  1744. }
  1745. // Return true if the thunk function is a constant, which means that
  1746. // it does not need to be passed to the thunk routine.
  1747. bool
  1748. Thunk_statement::is_constant_function() const
  1749. {
  1750. Call_expression* ce = this->call_->call_expression();
  1751. Function_type* fntype = ce->get_function_type();
  1752. if (fntype == NULL)
  1753. {
  1754. go_assert(saw_errors());
  1755. return false;
  1756. }
  1757. if (fntype->is_builtin())
  1758. return true;
  1759. Expression* fn = ce->fn();
  1760. if (fn->func_expression() != NULL)
  1761. return fn->func_expression()->closure() == NULL;
  1762. if (fn->interface_field_reference_expression() != NULL)
  1763. return true;
  1764. return false;
  1765. }
  1766. // Simplify complex thunk statements into simple ones. A complicated
  1767. // thunk statement is one which takes anything other than zero
  1768. // parameters or a single pointer parameter. We rewrite it into code
  1769. // which allocates a struct, stores the parameter values into the
  1770. // struct, and does a simple go or defer statement which passes the
  1771. // struct to a thunk. The thunk does the real call.
  1772. bool
  1773. Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
  1774. Block* block)
  1775. {
  1776. if (this->classification() == STATEMENT_ERROR)
  1777. return false;
  1778. if (this->call_->is_error_expression())
  1779. return false;
  1780. if (this->classification() == STATEMENT_DEFER)
  1781. {
  1782. // Make sure that the defer stack exists for the function. We
  1783. // will use when converting this statement to the backend
  1784. // representation, but we want it to exist when we start
  1785. // converting the function.
  1786. function->func_value()->defer_stack(this->location());
  1787. }
  1788. Call_expression* ce = this->call_->call_expression();
  1789. Function_type* fntype = ce->get_function_type();
  1790. if (fntype == NULL)
  1791. {
  1792. go_assert(saw_errors());
  1793. this->set_is_error();
  1794. return false;
  1795. }
  1796. if (this->is_simple(fntype))
  1797. return false;
  1798. Expression* fn = ce->fn();
  1799. Interface_field_reference_expression* interface_method =
  1800. fn->interface_field_reference_expression();
  1801. Location location = this->location();
  1802. std::string thunk_name = Gogo::thunk_name();
  1803. // Build the thunk.
  1804. this->build_thunk(gogo, thunk_name);
  1805. // Generate code to call the thunk.
  1806. // Get the values to store into the struct which is the single
  1807. // argument to the thunk.
  1808. Expression_list* vals = new Expression_list();
  1809. if (!this->is_constant_function())
  1810. vals->push_back(fn);
  1811. if (interface_method != NULL)
  1812. vals->push_back(interface_method->expr());
  1813. if (ce->args() != NULL)
  1814. {
  1815. for (Expression_list::const_iterator p = ce->args()->begin();
  1816. p != ce->args()->end();
  1817. ++p)
  1818. {
  1819. if ((*p)->is_constant())
  1820. continue;
  1821. vals->push_back(*p);
  1822. }
  1823. }
  1824. // Build the struct.
  1825. Expression* constructor =
  1826. Expression::make_struct_composite_literal(this->struct_type_, vals,
  1827. location);
  1828. // Allocate the initialized struct on the heap.
  1829. constructor = Expression::make_heap_expression(constructor, location);
  1830. // Look up the thunk.
  1831. Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
  1832. go_assert(named_thunk != NULL && named_thunk->is_function());
  1833. // Build the call.
  1834. Expression* func = Expression::make_func_reference(named_thunk, NULL,
  1835. location);
  1836. Expression_list* params = new Expression_list();
  1837. params->push_back(constructor);
  1838. Call_expression* call = Expression::make_call(func, params, false, location);
  1839. // Build the simple go or defer statement.
  1840. Statement* s;
  1841. if (this->classification() == STATEMENT_GO)
  1842. s = Statement::make_go_statement(call, location);
  1843. else if (this->classification() == STATEMENT_DEFER)
  1844. s = Statement::make_defer_statement(call, location);
  1845. else
  1846. go_unreachable();
  1847. // The current block should end with the go statement.
  1848. go_assert(block->statements()->size() >= 1);
  1849. go_assert(block->statements()->back() == this);
  1850. block->replace_statement(block->statements()->size() - 1, s);
  1851. // We already ran the determine_types pass, so we need to run it now
  1852. // for the new statement.
  1853. s->determine_types();
  1854. // Sanity check.
  1855. gogo->check_types_in_block(block);
  1856. // Return true to tell the block not to keep looking at statements.
  1857. return true;
  1858. }
  1859. // Set the name to use for thunk parameter N.
  1860. void
  1861. Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
  1862. {
  1863. snprintf(buf, buflen, "a%d", n);
  1864. }
  1865. // Build a new struct type to hold the parameters for a complicated
  1866. // thunk statement. FNTYPE is the type of the function call.
  1867. Struct_type*
  1868. Thunk_statement::build_struct(Function_type* fntype)
  1869. {
  1870. Location location = this->location();
  1871. Struct_field_list* fields = new Struct_field_list();
  1872. Call_expression* ce = this->call_->call_expression();
  1873. Expression* fn = ce->fn();
  1874. if (!this->is_constant_function())
  1875. {
  1876. // The function to call.
  1877. fields->push_back(Struct_field(Typed_identifier("fn", fntype,
  1878. location)));
  1879. }
  1880. // If this thunk statement calls a method on an interface, we pass
  1881. // the interface object to the thunk.
  1882. Interface_field_reference_expression* interface_method =
  1883. fn->interface_field_reference_expression();
  1884. if (interface_method != NULL)
  1885. {
  1886. Typed_identifier tid("object", interface_method->expr()->type(),
  1887. location);
  1888. fields->push_back(Struct_field(tid));
  1889. }
  1890. // The predeclared recover function has no argument. However, we
  1891. // add an argument when building recover thunks. Handle that here.
  1892. if (ce->is_recover_call())
  1893. {
  1894. fields->push_back(Struct_field(Typed_identifier("can_recover",
  1895. Type::lookup_bool_type(),
  1896. location)));
  1897. }
  1898. const Expression_list* args = ce->args();
  1899. if (args != NULL)
  1900. {
  1901. int i = 0;
  1902. for (Expression_list::const_iterator p = args->begin();
  1903. p != args->end();
  1904. ++p, ++i)
  1905. {
  1906. if ((*p)->is_constant())
  1907. continue;
  1908. char buf[50];
  1909. this->thunk_field_param(i, buf, sizeof buf);
  1910. fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
  1911. location)));
  1912. }
  1913. }
  1914. Struct_type *st = Type::make_struct_type(fields, location);
  1915. Thunk_statement::thunk_types.insert(st);
  1916. return st;
  1917. }
  1918. // Return whether ST is a type created to hold thunk parameters.
  1919. bool
  1920. Thunk_statement::is_thunk_struct(const Struct_type* st)
  1921. {
  1922. return (Thunk_statement::thunk_types.find(st)
  1923. != Thunk_statement::thunk_types.end());
  1924. }
  1925. // Build the thunk we are going to call. This is a brand new, albeit
  1926. // artificial, function.
  1927. void
  1928. Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
  1929. {
  1930. Location location = this->location();
  1931. Call_expression* ce = this->call_->call_expression();
  1932. bool may_call_recover = false;
  1933. if (this->classification() == STATEMENT_DEFER)
  1934. {
  1935. Func_expression* fn = ce->fn()->func_expression();
  1936. if (fn == NULL)
  1937. may_call_recover = true;
  1938. else
  1939. {
  1940. const Named_object* no = fn->named_object();
  1941. if (!no->is_function())
  1942. may_call_recover = true;
  1943. else
  1944. may_call_recover = no->func_value()->calls_recover();
  1945. }
  1946. }
  1947. // Build the type of the thunk. The thunk takes a single parameter,
  1948. // which is a pointer to the special structure we build.
  1949. const char* const parameter_name = "__go_thunk_parameter";
  1950. Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
  1951. Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
  1952. thunk_parameters->push_back(Typed_identifier(parameter_name,
  1953. pointer_to_struct_type,
  1954. location));
  1955. Typed_identifier_list* thunk_results = NULL;
  1956. if (may_call_recover)
  1957. {
  1958. // When deferring a function which may call recover, add a
  1959. // return value, to disable tail call optimizations which will
  1960. // break the way we check whether recover is permitted.
  1961. thunk_results = new Typed_identifier_list();
  1962. thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
  1963. location));
  1964. }
  1965. Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
  1966. thunk_results,
  1967. location);
  1968. // Start building the thunk.
  1969. Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
  1970. location);
  1971. gogo->start_block(location);
  1972. // For a defer statement, start with a call to
  1973. // __go_set_defer_retaddr. */
  1974. Label* retaddr_label = NULL;
  1975. if (may_call_recover)
  1976. {
  1977. retaddr_label = gogo->add_label_reference("retaddr", location, false);
  1978. Expression* arg = Expression::make_label_addr(retaddr_label, location);
  1979. Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
  1980. location, 1, arg);
  1981. // This is a hack to prevent the middle-end from deleting the
  1982. // label.
  1983. gogo->start_block(location);
  1984. gogo->add_statement(Statement::make_goto_statement(retaddr_label,
  1985. location));
  1986. Block* then_block = gogo->finish_block(location);
  1987. then_block->determine_types();
  1988. Statement* s = Statement::make_if_statement(call, then_block, NULL,
  1989. location);
  1990. s->determine_types();
  1991. gogo->add_statement(s);
  1992. function->func_value()->set_calls_defer_retaddr();
  1993. }
  1994. // Get a reference to the parameter.
  1995. Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
  1996. go_assert(named_parameter != NULL && named_parameter->is_variable());
  1997. // Build the call. Note that the field names are the same as the
  1998. // ones used in build_struct.
  1999. Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
  2000. location);
  2001. thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
  2002. location);
  2003. Interface_field_reference_expression* interface_method =
  2004. ce->fn()->interface_field_reference_expression();
  2005. Expression* func_to_call;
  2006. unsigned int next_index;
  2007. if (this->is_constant_function())
  2008. {
  2009. func_to_call = ce->fn();
  2010. next_index = 0;
  2011. }
  2012. else
  2013. {
  2014. func_to_call = Expression::make_field_reference(thunk_parameter,
  2015. 0, location);
  2016. next_index = 1;
  2017. }
  2018. if (interface_method != NULL)
  2019. {
  2020. // The main program passes the interface object.
  2021. go_assert(next_index == 0);
  2022. Expression* r = Expression::make_field_reference(thunk_parameter, 0,
  2023. location);
  2024. const std::string& name(interface_method->name());
  2025. func_to_call = Expression::make_interface_field_reference(r, name,
  2026. location);
  2027. next_index = 1;
  2028. }
  2029. Expression_list* call_params = new Expression_list();
  2030. const Struct_field_list* fields = this->struct_type_->fields();
  2031. Struct_field_list::const_iterator p = fields->begin();
  2032. for (unsigned int i = 0; i < next_index; ++i)
  2033. ++p;
  2034. bool is_recover_call = ce->is_recover_call();
  2035. Expression* recover_arg = NULL;
  2036. const Expression_list* args = ce->args();
  2037. if (args != NULL)
  2038. {
  2039. for (Expression_list::const_iterator arg = args->begin();
  2040. arg != args->end();
  2041. ++arg)
  2042. {
  2043. Expression* param;
  2044. if ((*arg)->is_constant())
  2045. param = *arg;
  2046. else
  2047. {
  2048. Expression* thunk_param =
  2049. Expression::make_var_reference(named_parameter, location);
  2050. thunk_param =
  2051. Expression::make_unary(OPERATOR_MULT, thunk_param, location);
  2052. param = Expression::make_field_reference(thunk_param,
  2053. next_index,
  2054. location);
  2055. ++next_index;
  2056. }
  2057. if (!is_recover_call)
  2058. call_params->push_back(param);
  2059. else
  2060. {
  2061. go_assert(call_params->empty());
  2062. recover_arg = param;
  2063. }
  2064. }
  2065. }
  2066. if (call_params->empty())
  2067. {
  2068. delete call_params;
  2069. call_params = NULL;
  2070. }
  2071. Call_expression* call = Expression::make_call(func_to_call, call_params,
  2072. false, location);
  2073. // This call expression was already lowered before entering the
  2074. // thunk statement. Don't try to lower varargs again, as that will
  2075. // cause confusion for, e.g., method calls which already have a
  2076. // receiver parameter.
  2077. call->set_varargs_are_lowered();
  2078. Statement* call_statement = Statement::make_statement(call, true);
  2079. gogo->add_statement(call_statement);
  2080. // If this is a defer statement, the label comes immediately after
  2081. // the call.
  2082. if (may_call_recover)
  2083. {
  2084. gogo->add_label_definition("retaddr", location);
  2085. Expression_list* vals = new Expression_list();
  2086. vals->push_back(Expression::make_boolean(false, location));
  2087. gogo->add_statement(Statement::make_return_statement(vals, location));
  2088. }
  2089. Block* b = gogo->finish_block(location);
  2090. gogo->add_block(b, location);
  2091. gogo->lower_block(function, b);
  2092. // We already ran the determine_types pass, so we need to run it
  2093. // just for the call statement now. The other types are known.
  2094. call_statement->determine_types();
  2095. gogo->flatten_block(function, b);
  2096. if (may_call_recover || recover_arg != NULL)
  2097. {
  2098. // Dig up the call expression, which may have been changed
  2099. // during lowering.
  2100. go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
  2101. Expression_statement* es =
  2102. static_cast<Expression_statement*>(call_statement);
  2103. Call_expression* ce = es->expr()->call_expression();
  2104. if (ce == NULL)
  2105. go_assert(saw_errors());
  2106. else
  2107. {
  2108. if (may_call_recover)
  2109. ce->set_is_deferred();
  2110. if (recover_arg != NULL)
  2111. ce->set_recover_arg(recover_arg);
  2112. }
  2113. }
  2114. // That is all the thunk has to do.
  2115. gogo->finish_function(location);
  2116. }
  2117. // Get the function and argument expressions.
  2118. bool
  2119. Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
  2120. {
  2121. if (this->call_->is_error_expression())
  2122. return false;
  2123. Call_expression* ce = this->call_->call_expression();
  2124. Expression* fn = ce->fn();
  2125. Func_expression* fe = fn->func_expression();
  2126. go_assert(fe != NULL);
  2127. *pfn = Expression::make_func_code_reference(fe->named_object(),
  2128. fe->location());
  2129. const Expression_list* args = ce->args();
  2130. if (args == NULL || args->empty())
  2131. *parg = Expression::make_nil(this->location());
  2132. else
  2133. {
  2134. go_assert(args->size() == 1);
  2135. *parg = args->front();
  2136. }
  2137. return true;
  2138. }
  2139. // Class Go_statement.
  2140. Bstatement*
  2141. Go_statement::do_get_backend(Translate_context* context)
  2142. {
  2143. Expression* fn;
  2144. Expression* arg;
  2145. if (!this->get_fn_and_arg(&fn, &arg))
  2146. return context->backend()->error_statement();
  2147. Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
  2148. fn, arg);
  2149. Bexpression* bcall = call->get_backend(context);
  2150. return context->backend()->expression_statement(bcall);
  2151. }
  2152. // Dump the AST representation for go statement.
  2153. void
  2154. Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2155. {
  2156. ast_dump_context->print_indent();
  2157. ast_dump_context->ostream() << "go ";
  2158. ast_dump_context->dump_expression(this->call());
  2159. ast_dump_context->ostream() << std::endl;
  2160. }
  2161. // Make a go statement.
  2162. Statement*
  2163. Statement::make_go_statement(Call_expression* call, Location location)
  2164. {
  2165. return new Go_statement(call, location);
  2166. }
  2167. // Class Defer_statement.
  2168. Bstatement*
  2169. Defer_statement::do_get_backend(Translate_context* context)
  2170. {
  2171. Expression* fn;
  2172. Expression* arg;
  2173. if (!this->get_fn_and_arg(&fn, &arg))
  2174. return context->backend()->error_statement();
  2175. Location loc = this->location();
  2176. Expression* ds = context->function()->func_value()->defer_stack(loc);
  2177. Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
  2178. ds, fn, arg);
  2179. Bexpression* bcall = call->get_backend(context);
  2180. return context->backend()->expression_statement(bcall);
  2181. }
  2182. // Dump the AST representation for defer statement.
  2183. void
  2184. Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2185. {
  2186. ast_dump_context->print_indent();
  2187. ast_dump_context->ostream() << "defer ";
  2188. ast_dump_context->dump_expression(this->call());
  2189. ast_dump_context->ostream() << std::endl;
  2190. }
  2191. // Make a defer statement.
  2192. Statement*
  2193. Statement::make_defer_statement(Call_expression* call,
  2194. Location location)
  2195. {
  2196. return new Defer_statement(call, location);
  2197. }
  2198. // Class Return_statement.
  2199. // Traverse assignments. We treat each return value as a top level
  2200. // RHS in an expression.
  2201. bool
  2202. Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
  2203. {
  2204. Expression_list* vals = this->vals_;
  2205. if (vals != NULL)
  2206. {
  2207. for (Expression_list::iterator p = vals->begin();
  2208. p != vals->end();
  2209. ++p)
  2210. tassign->value(&*p, true, true);
  2211. }
  2212. return true;
  2213. }
  2214. // Lower a return statement. If we are returning a function call
  2215. // which returns multiple values which match the current function,
  2216. // split up the call's results. If the return statement lists
  2217. // explicit values, implement this statement by assigning the values
  2218. // to the result variables and change this statement to a naked
  2219. // return. This lets panic/recover work correctly.
  2220. Statement*
  2221. Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
  2222. Statement_inserter*)
  2223. {
  2224. if (this->is_lowered_)
  2225. return this;
  2226. Expression_list* vals = this->vals_;
  2227. this->vals_ = NULL;
  2228. this->is_lowered_ = true;
  2229. Location loc = this->location();
  2230. size_t vals_count = vals == NULL ? 0 : vals->size();
  2231. Function::Results* results = function->func_value()->result_variables();
  2232. size_t results_count = results == NULL ? 0 : results->size();
  2233. if (vals_count == 0)
  2234. {
  2235. if (results_count > 0 && !function->func_value()->results_are_named())
  2236. {
  2237. this->report_error(_("not enough arguments to return"));
  2238. return this;
  2239. }
  2240. return this;
  2241. }
  2242. if (results_count == 0)
  2243. {
  2244. this->report_error(_("return with value in function "
  2245. "with no return type"));
  2246. return this;
  2247. }
  2248. // If the current function has multiple return values, and we are
  2249. // returning a single call expression, split up the call expression.
  2250. if (results_count > 1
  2251. && vals->size() == 1
  2252. && vals->front()->call_expression() != NULL)
  2253. {
  2254. Call_expression* call = vals->front()->call_expression();
  2255. call->set_expected_result_count(results_count);
  2256. delete vals;
  2257. vals = new Expression_list;
  2258. for (size_t i = 0; i < results_count; ++i)
  2259. vals->push_back(Expression::make_call_result(call, i));
  2260. vals_count = results_count;
  2261. }
  2262. if (vals_count < results_count)
  2263. {
  2264. this->report_error(_("not enough arguments to return"));
  2265. return this;
  2266. }
  2267. if (vals_count > results_count)
  2268. {
  2269. this->report_error(_("too many values in return statement"));
  2270. return this;
  2271. }
  2272. Block* b = new Block(enclosing, loc);
  2273. Expression_list* lhs = new Expression_list();
  2274. Expression_list* rhs = new Expression_list();
  2275. Expression_list::const_iterator pe = vals->begin();
  2276. int i = 1;
  2277. for (Function::Results::const_iterator pr = results->begin();
  2278. pr != results->end();
  2279. ++pr, ++pe, ++i)
  2280. {
  2281. Named_object* rv = *pr;
  2282. Expression* e = *pe;
  2283. // Check types now so that we give a good error message. The
  2284. // result type is known. We determine the expression type
  2285. // early.
  2286. Type *rvtype = rv->result_var_value()->type();
  2287. Type_context type_context(rvtype, false);
  2288. e->determine_type(&type_context);
  2289. std::string reason;
  2290. if (Type::are_assignable(rvtype, e->type(), &reason))
  2291. {
  2292. Expression* ve = Expression::make_var_reference(rv, e->location());
  2293. lhs->push_back(ve);
  2294. rhs->push_back(e);
  2295. }
  2296. else
  2297. {
  2298. if (reason.empty())
  2299. error_at(e->location(), "incompatible type for return value %d", i);
  2300. else
  2301. error_at(e->location(),
  2302. "incompatible type for return value %d (%s)",
  2303. i, reason.c_str());
  2304. }
  2305. }
  2306. go_assert(lhs->size() == rhs->size());
  2307. if (lhs->empty())
  2308. ;
  2309. else if (lhs->size() == 1)
  2310. {
  2311. b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
  2312. loc));
  2313. delete lhs;
  2314. delete rhs;
  2315. }
  2316. else
  2317. b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
  2318. b->add_statement(this);
  2319. delete vals;
  2320. return Statement::make_block_statement(b, loc);
  2321. }
  2322. // Convert a return statement to the backend representation.
  2323. Bstatement*
  2324. Return_statement::do_get_backend(Translate_context* context)
  2325. {
  2326. Location loc = this->location();
  2327. Function* function = context->function()->func_value();
  2328. Function::Results* results = function->result_variables();
  2329. std::vector<Bexpression*> retvals;
  2330. if (results != NULL && !results->empty())
  2331. {
  2332. retvals.reserve(results->size());
  2333. for (Function::Results::const_iterator p = results->begin();
  2334. p != results->end();
  2335. p++)
  2336. {
  2337. Expression* vr = Expression::make_var_reference(*p, loc);
  2338. retvals.push_back(vr->get_backend(context));
  2339. }
  2340. }
  2341. return context->backend()->return_statement(function->get_decl(),
  2342. retvals, loc);
  2343. }
  2344. // Dump the AST representation for a return statement.
  2345. void
  2346. Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2347. {
  2348. ast_dump_context->print_indent();
  2349. ast_dump_context->ostream() << "return " ;
  2350. ast_dump_context->dump_expression_list(this->vals_);
  2351. ast_dump_context->ostream() << std::endl;
  2352. }
  2353. // Make a return statement.
  2354. Return_statement*
  2355. Statement::make_return_statement(Expression_list* vals,
  2356. Location location)
  2357. {
  2358. return new Return_statement(vals, location);
  2359. }
  2360. // Make a statement that returns the result of a call expression.
  2361. Statement*
  2362. Statement::make_return_from_call(Call_expression* call, Location location)
  2363. {
  2364. size_t rc = call->result_count();
  2365. if (rc == 0)
  2366. return Statement::make_statement(call, true);
  2367. else
  2368. {
  2369. Expression_list* vals = new Expression_list();
  2370. if (rc == 1)
  2371. vals->push_back(call);
  2372. else
  2373. {
  2374. for (size_t i = 0; i < rc; ++i)
  2375. vals->push_back(Expression::make_call_result(call, i));
  2376. }
  2377. return Statement::make_return_statement(vals, location);
  2378. }
  2379. }
  2380. // A break or continue statement.
  2381. class Bc_statement : public Statement
  2382. {
  2383. public:
  2384. Bc_statement(bool is_break, Unnamed_label* label, Location location)
  2385. : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
  2386. label_(label), is_break_(is_break)
  2387. { }
  2388. bool
  2389. is_break() const
  2390. { return this->is_break_; }
  2391. protected:
  2392. int
  2393. do_traverse(Traverse*)
  2394. { return TRAVERSE_CONTINUE; }
  2395. bool
  2396. do_may_fall_through() const
  2397. { return false; }
  2398. Bstatement*
  2399. do_get_backend(Translate_context* context)
  2400. { return this->label_->get_goto(context, this->location()); }
  2401. void
  2402. do_dump_statement(Ast_dump_context*) const;
  2403. private:
  2404. // The label that this branches to.
  2405. Unnamed_label* label_;
  2406. // True if this is "break", false if it is "continue".
  2407. bool is_break_;
  2408. };
  2409. // Dump the AST representation for a break/continue statement
  2410. void
  2411. Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2412. {
  2413. ast_dump_context->print_indent();
  2414. ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
  2415. if (this->label_ != NULL)
  2416. {
  2417. ast_dump_context->ostream() << " ";
  2418. ast_dump_context->dump_label_name(this->label_);
  2419. }
  2420. ast_dump_context->ostream() << std::endl;
  2421. }
  2422. // Make a break statement.
  2423. Statement*
  2424. Statement::make_break_statement(Unnamed_label* label, Location location)
  2425. {
  2426. return new Bc_statement(true, label, location);
  2427. }
  2428. // Make a continue statement.
  2429. Statement*
  2430. Statement::make_continue_statement(Unnamed_label* label,
  2431. Location location)
  2432. {
  2433. return new Bc_statement(false, label, location);
  2434. }
  2435. // A goto statement.
  2436. class Goto_statement : public Statement
  2437. {
  2438. public:
  2439. Goto_statement(Label* label, Location location)
  2440. : Statement(STATEMENT_GOTO, location),
  2441. label_(label)
  2442. { }
  2443. protected:
  2444. int
  2445. do_traverse(Traverse*)
  2446. { return TRAVERSE_CONTINUE; }
  2447. void
  2448. do_check_types(Gogo*);
  2449. bool
  2450. do_may_fall_through() const
  2451. { return false; }
  2452. Bstatement*
  2453. do_get_backend(Translate_context*);
  2454. void
  2455. do_dump_statement(Ast_dump_context*) const;
  2456. private:
  2457. Label* label_;
  2458. };
  2459. // Check types for a label. There aren't any types per se, but we use
  2460. // this to give an error if the label was never defined.
  2461. void
  2462. Goto_statement::do_check_types(Gogo*)
  2463. {
  2464. if (!this->label_->is_defined())
  2465. {
  2466. error_at(this->location(), "reference to undefined label %qs",
  2467. Gogo::message_name(this->label_->name()).c_str());
  2468. this->set_is_error();
  2469. }
  2470. }
  2471. // Convert the goto statement to the backend representation.
  2472. Bstatement*
  2473. Goto_statement::do_get_backend(Translate_context* context)
  2474. {
  2475. Blabel* blabel = this->label_->get_backend_label(context);
  2476. return context->backend()->goto_statement(blabel, this->location());
  2477. }
  2478. // Dump the AST representation for a goto statement.
  2479. void
  2480. Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2481. {
  2482. ast_dump_context->print_indent();
  2483. ast_dump_context->ostream() << "goto " << this->label_->name() << std::endl;
  2484. }
  2485. // Make a goto statement.
  2486. Statement*
  2487. Statement::make_goto_statement(Label* label, Location location)
  2488. {
  2489. return new Goto_statement(label, location);
  2490. }
  2491. // A goto statement to an unnamed label.
  2492. class Goto_unnamed_statement : public Statement
  2493. {
  2494. public:
  2495. Goto_unnamed_statement(Unnamed_label* label, Location location)
  2496. : Statement(STATEMENT_GOTO_UNNAMED, location),
  2497. label_(label)
  2498. { }
  2499. protected:
  2500. int
  2501. do_traverse(Traverse*)
  2502. { return TRAVERSE_CONTINUE; }
  2503. bool
  2504. do_may_fall_through() const
  2505. { return false; }
  2506. Bstatement*
  2507. do_get_backend(Translate_context* context)
  2508. { return this->label_->get_goto(context, this->location()); }
  2509. void
  2510. do_dump_statement(Ast_dump_context*) const;
  2511. private:
  2512. Unnamed_label* label_;
  2513. };
  2514. // Dump the AST representation for an unnamed goto statement
  2515. void
  2516. Goto_unnamed_statement::do_dump_statement(
  2517. Ast_dump_context* ast_dump_context) const
  2518. {
  2519. ast_dump_context->print_indent();
  2520. ast_dump_context->ostream() << "goto ";
  2521. ast_dump_context->dump_label_name(this->label_);
  2522. ast_dump_context->ostream() << std::endl;
  2523. }
  2524. // Make a goto statement to an unnamed label.
  2525. Statement*
  2526. Statement::make_goto_unnamed_statement(Unnamed_label* label,
  2527. Location location)
  2528. {
  2529. return new Goto_unnamed_statement(label, location);
  2530. }
  2531. // Class Label_statement.
  2532. // Traversal.
  2533. int
  2534. Label_statement::do_traverse(Traverse*)
  2535. {
  2536. return TRAVERSE_CONTINUE;
  2537. }
  2538. // Return the backend representation of the statement defining this
  2539. // label.
  2540. Bstatement*
  2541. Label_statement::do_get_backend(Translate_context* context)
  2542. {
  2543. Blabel* blabel = this->label_->get_backend_label(context);
  2544. return context->backend()->label_definition_statement(blabel);
  2545. }
  2546. // Dump the AST for a label definition statement.
  2547. void
  2548. Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2549. {
  2550. ast_dump_context->print_indent();
  2551. ast_dump_context->ostream() << this->label_->name() << ":" << std::endl;
  2552. }
  2553. // Make a label statement.
  2554. Statement*
  2555. Statement::make_label_statement(Label* label, Location location)
  2556. {
  2557. return new Label_statement(label, location);
  2558. }
  2559. // An unnamed label statement.
  2560. class Unnamed_label_statement : public Statement
  2561. {
  2562. public:
  2563. Unnamed_label_statement(Unnamed_label* label)
  2564. : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
  2565. label_(label)
  2566. { }
  2567. protected:
  2568. int
  2569. do_traverse(Traverse*)
  2570. { return TRAVERSE_CONTINUE; }
  2571. Bstatement*
  2572. do_get_backend(Translate_context* context)
  2573. { return this->label_->get_definition(context); }
  2574. void
  2575. do_dump_statement(Ast_dump_context*) const;
  2576. private:
  2577. // The label.
  2578. Unnamed_label* label_;
  2579. };
  2580. // Dump the AST representation for an unnamed label definition statement.
  2581. void
  2582. Unnamed_label_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
  2583. const
  2584. {
  2585. ast_dump_context->print_indent();
  2586. ast_dump_context->dump_label_name(this->label_);
  2587. ast_dump_context->ostream() << ":" << std::endl;
  2588. }
  2589. // Make an unnamed label statement.
  2590. Statement*
  2591. Statement::make_unnamed_label_statement(Unnamed_label* label)
  2592. {
  2593. return new Unnamed_label_statement(label);
  2594. }
  2595. // An if statement.
  2596. class If_statement : public Statement
  2597. {
  2598. public:
  2599. If_statement(Expression* cond, Block* then_block, Block* else_block,
  2600. Location location)
  2601. : Statement(STATEMENT_IF, location),
  2602. cond_(cond), then_block_(then_block), else_block_(else_block)
  2603. { }
  2604. protected:
  2605. int
  2606. do_traverse(Traverse*);
  2607. void
  2608. do_determine_types();
  2609. void
  2610. do_check_types(Gogo*);
  2611. bool
  2612. do_may_fall_through() const;
  2613. Bstatement*
  2614. do_get_backend(Translate_context*);
  2615. void
  2616. do_dump_statement(Ast_dump_context*) const;
  2617. private:
  2618. Expression* cond_;
  2619. Block* then_block_;
  2620. Block* else_block_;
  2621. };
  2622. // Traversal.
  2623. int
  2624. If_statement::do_traverse(Traverse* traverse)
  2625. {
  2626. if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
  2627. || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
  2628. return TRAVERSE_EXIT;
  2629. if (this->else_block_ != NULL)
  2630. {
  2631. if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
  2632. return TRAVERSE_EXIT;
  2633. }
  2634. return TRAVERSE_CONTINUE;
  2635. }
  2636. void
  2637. If_statement::do_determine_types()
  2638. {
  2639. Type_context context(Type::lookup_bool_type(), false);
  2640. this->cond_->determine_type(&context);
  2641. this->then_block_->determine_types();
  2642. if (this->else_block_ != NULL)
  2643. this->else_block_->determine_types();
  2644. }
  2645. // Check types.
  2646. void
  2647. If_statement::do_check_types(Gogo*)
  2648. {
  2649. Type* type = this->cond_->type();
  2650. if (type->is_error())
  2651. this->set_is_error();
  2652. else if (!type->is_boolean_type())
  2653. this->report_error(_("expected boolean expression"));
  2654. }
  2655. // Whether the overall statement may fall through.
  2656. bool
  2657. If_statement::do_may_fall_through() const
  2658. {
  2659. return (this->else_block_ == NULL
  2660. || this->then_block_->may_fall_through()
  2661. || this->else_block_->may_fall_through());
  2662. }
  2663. // Get the backend representation.
  2664. Bstatement*
  2665. If_statement::do_get_backend(Translate_context* context)
  2666. {
  2667. go_assert(this->cond_->type()->is_boolean_type()
  2668. || this->cond_->type()->is_error());
  2669. Bexpression* cond = this->cond_->get_backend(context);
  2670. Bblock* then_block = this->then_block_->get_backend(context);
  2671. Bblock* else_block = (this->else_block_ == NULL
  2672. ? NULL
  2673. : this->else_block_->get_backend(context));
  2674. return context->backend()->if_statement(cond, then_block, else_block,
  2675. this->location());
  2676. }
  2677. // Dump the AST representation for an if statement
  2678. void
  2679. If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  2680. {
  2681. ast_dump_context->print_indent();
  2682. ast_dump_context->ostream() << "if ";
  2683. ast_dump_context->dump_expression(this->cond_);
  2684. ast_dump_context->ostream() << std::endl;
  2685. if (ast_dump_context->dump_subblocks())
  2686. {
  2687. ast_dump_context->dump_block(this->then_block_);
  2688. if (this->else_block_ != NULL)
  2689. {
  2690. ast_dump_context->print_indent();
  2691. ast_dump_context->ostream() << "else" << std::endl;
  2692. ast_dump_context->dump_block(this->else_block_);
  2693. }
  2694. }
  2695. }
  2696. // Make an if statement.
  2697. Statement*
  2698. Statement::make_if_statement(Expression* cond, Block* then_block,
  2699. Block* else_block, Location location)
  2700. {
  2701. return new If_statement(cond, then_block, else_block, location);
  2702. }
  2703. // Class Case_clauses::Hash_integer_value.
  2704. class Case_clauses::Hash_integer_value
  2705. {
  2706. public:
  2707. size_t
  2708. operator()(Expression*) const;
  2709. };
  2710. size_t
  2711. Case_clauses::Hash_integer_value::operator()(Expression* pe) const
  2712. {
  2713. Numeric_constant nc;
  2714. mpz_t ival;
  2715. if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival))
  2716. go_unreachable();
  2717. size_t ret = mpz_get_ui(ival);
  2718. mpz_clear(ival);
  2719. return ret;
  2720. }
  2721. // Class Case_clauses::Eq_integer_value.
  2722. class Case_clauses::Eq_integer_value
  2723. {
  2724. public:
  2725. bool
  2726. operator()(Expression*, Expression*) const;
  2727. };
  2728. bool
  2729. Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
  2730. {
  2731. Numeric_constant anc;
  2732. mpz_t aval;
  2733. Numeric_constant bnc;
  2734. mpz_t bval;
  2735. if (!a->numeric_constant_value(&anc)
  2736. || !anc.to_int(&aval)
  2737. || !b->numeric_constant_value(&bnc)
  2738. || !bnc.to_int(&bval))
  2739. go_unreachable();
  2740. bool ret = mpz_cmp(aval, bval) == 0;
  2741. mpz_clear(aval);
  2742. mpz_clear(bval);
  2743. return ret;
  2744. }
  2745. // Class Case_clauses::Case_clause.
  2746. // Traversal.
  2747. int
  2748. Case_clauses::Case_clause::traverse(Traverse* traverse)
  2749. {
  2750. if (this->cases_ != NULL
  2751. && (traverse->traverse_mask()
  2752. & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
  2753. {
  2754. if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
  2755. return TRAVERSE_EXIT;
  2756. }
  2757. if (this->statements_ != NULL)
  2758. {
  2759. if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
  2760. return TRAVERSE_EXIT;
  2761. }
  2762. return TRAVERSE_CONTINUE;
  2763. }
  2764. // Check whether all the case expressions are integer constants.
  2765. bool
  2766. Case_clauses::Case_clause::is_constant() const
  2767. {
  2768. if (this->cases_ != NULL)
  2769. {
  2770. for (Expression_list::const_iterator p = this->cases_->begin();
  2771. p != this->cases_->end();
  2772. ++p)
  2773. if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
  2774. return false;
  2775. }
  2776. return true;
  2777. }
  2778. // Lower a case clause for a nonconstant switch. VAL_TEMP is the
  2779. // value we are switching on; it may be NULL. If START_LABEL is not
  2780. // NULL, it goes at the start of the statements, after the condition
  2781. // test. We branch to FINISH_LABEL at the end of the statements.
  2782. void
  2783. Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
  2784. Unnamed_label* start_label,
  2785. Unnamed_label* finish_label) const
  2786. {
  2787. Location loc = this->location_;
  2788. Unnamed_label* next_case_label;
  2789. if (this->cases_ == NULL || this->cases_->empty())
  2790. {
  2791. go_assert(this->is_default_);
  2792. next_case_label = NULL;
  2793. }
  2794. else
  2795. {
  2796. Expression* cond = NULL;
  2797. for (Expression_list::const_iterator p = this->cases_->begin();
  2798. p != this->cases_->end();
  2799. ++p)
  2800. {
  2801. Expression* ref = Expression::make_temporary_reference(val_temp,
  2802. loc);
  2803. Expression* this_cond = Expression::make_binary(OPERATOR_EQEQ, ref,
  2804. *p, loc);
  2805. if (cond == NULL)
  2806. cond = this_cond;
  2807. else
  2808. cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
  2809. }
  2810. Block* then_block = new Block(b, loc);
  2811. next_case_label = new Unnamed_label(Linemap::unknown_location());
  2812. Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
  2813. loc);
  2814. then_block->add_statement(s);
  2815. // if !COND { goto NEXT_CASE_LABEL }
  2816. cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
  2817. s = Statement::make_if_statement(cond, then_block, NULL, loc);
  2818. b->add_statement(s);
  2819. }
  2820. if (start_label != NULL)
  2821. b->add_statement(Statement::make_unnamed_label_statement(start_label));
  2822. if (this->statements_ != NULL)
  2823. b->add_statement(Statement::make_block_statement(this->statements_, loc));
  2824. Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
  2825. b->add_statement(s);
  2826. if (next_case_label != NULL)
  2827. b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
  2828. }
  2829. // Determine types.
  2830. void
  2831. Case_clauses::Case_clause::determine_types(Type* type)
  2832. {
  2833. if (this->cases_ != NULL)
  2834. {
  2835. Type_context case_context(type, false);
  2836. for (Expression_list::iterator p = this->cases_->begin();
  2837. p != this->cases_->end();
  2838. ++p)
  2839. (*p)->determine_type(&case_context);
  2840. }
  2841. if (this->statements_ != NULL)
  2842. this->statements_->determine_types();
  2843. }
  2844. // Check types. Returns false if there was an error.
  2845. bool
  2846. Case_clauses::Case_clause::check_types(Type* type)
  2847. {
  2848. if (this->cases_ != NULL)
  2849. {
  2850. for (Expression_list::iterator p = this->cases_->begin();
  2851. p != this->cases_->end();
  2852. ++p)
  2853. {
  2854. if (!Type::are_assignable(type, (*p)->type(), NULL)
  2855. && !Type::are_assignable((*p)->type(), type, NULL))
  2856. {
  2857. error_at((*p)->location(),
  2858. "type mismatch between switch value and case clause");
  2859. return false;
  2860. }
  2861. }
  2862. }
  2863. return true;
  2864. }
  2865. // Return true if this clause may fall through to the following
  2866. // statements. Note that this is not the same as whether the case
  2867. // uses the "fallthrough" keyword.
  2868. bool
  2869. Case_clauses::Case_clause::may_fall_through() const
  2870. {
  2871. if (this->statements_ == NULL)
  2872. return true;
  2873. return this->statements_->may_fall_through();
  2874. }
  2875. // Convert the case values and statements to the backend
  2876. // representation. BREAK_LABEL is the label which break statements
  2877. // should branch to. CASE_CONSTANTS is used to detect duplicate
  2878. // constants. *CASES should be passed as an empty vector; the values
  2879. // for this case will be added to it. If this is the default case,
  2880. // *CASES will remain empty. This returns the statement to execute if
  2881. // one of these cases is selected.
  2882. Bstatement*
  2883. Case_clauses::Case_clause::get_backend(Translate_context* context,
  2884. Unnamed_label* break_label,
  2885. Case_constants* case_constants,
  2886. std::vector<Bexpression*>* cases) const
  2887. {
  2888. if (this->cases_ != NULL)
  2889. {
  2890. go_assert(!this->is_default_);
  2891. for (Expression_list::const_iterator p = this->cases_->begin();
  2892. p != this->cases_->end();
  2893. ++p)
  2894. {
  2895. Expression* e = *p;
  2896. if (e->classification() != Expression::EXPRESSION_INTEGER)
  2897. {
  2898. Numeric_constant nc;
  2899. mpz_t ival;
  2900. if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival))
  2901. {
  2902. // Something went wrong. This can happen with a
  2903. // negative constant and an unsigned switch value.
  2904. go_assert(saw_errors());
  2905. continue;
  2906. }
  2907. go_assert(nc.type() != NULL);
  2908. e = Expression::make_integer_z(&ival, nc.type(), e->location());
  2909. mpz_clear(ival);
  2910. }
  2911. std::pair<Case_constants::iterator, bool> ins =
  2912. case_constants->insert(e);
  2913. if (!ins.second)
  2914. {
  2915. // Value was already present.
  2916. error_at(this->location_, "duplicate case in switch");
  2917. e = Expression::make_error(this->location_);
  2918. }
  2919. cases->push_back(e->get_backend(context));
  2920. }
  2921. }
  2922. Bstatement* statements;
  2923. if (this->statements_ == NULL)
  2924. statements = NULL;
  2925. else
  2926. {
  2927. Bblock* bblock = this->statements_->get_backend(context);
  2928. statements = context->backend()->block_statement(bblock);
  2929. }
  2930. Bstatement* break_stat;
  2931. if (this->is_fallthrough_)
  2932. break_stat = NULL;
  2933. else
  2934. break_stat = break_label->get_goto(context, this->location_);
  2935. if (statements == NULL)
  2936. return break_stat;
  2937. else if (break_stat == NULL)
  2938. return statements;
  2939. else
  2940. return context->backend()->compound_statement(statements, break_stat);
  2941. }
  2942. // Dump the AST representation for a case clause
  2943. void
  2944. Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
  2945. const
  2946. {
  2947. ast_dump_context->print_indent();
  2948. if (this->is_default_)
  2949. {
  2950. ast_dump_context->ostream() << "default:";
  2951. }
  2952. else
  2953. {
  2954. ast_dump_context->ostream() << "case ";
  2955. ast_dump_context->dump_expression_list(this->cases_);
  2956. ast_dump_context->ostream() << ":" ;
  2957. }
  2958. ast_dump_context->dump_block(this->statements_);
  2959. if (this->is_fallthrough_)
  2960. {
  2961. ast_dump_context->print_indent();
  2962. ast_dump_context->ostream() << " (fallthrough)" << std::endl;
  2963. }
  2964. }
  2965. // Class Case_clauses.
  2966. // Traversal.
  2967. int
  2968. Case_clauses::traverse(Traverse* traverse)
  2969. {
  2970. for (Clauses::iterator p = this->clauses_.begin();
  2971. p != this->clauses_.end();
  2972. ++p)
  2973. {
  2974. if (p->traverse(traverse) == TRAVERSE_EXIT)
  2975. return TRAVERSE_EXIT;
  2976. }
  2977. return TRAVERSE_CONTINUE;
  2978. }
  2979. // Check whether all the case expressions are constant.
  2980. bool
  2981. Case_clauses::is_constant() const
  2982. {
  2983. for (Clauses::const_iterator p = this->clauses_.begin();
  2984. p != this->clauses_.end();
  2985. ++p)
  2986. if (!p->is_constant())
  2987. return false;
  2988. return true;
  2989. }
  2990. // Lower case clauses for a nonconstant switch.
  2991. void
  2992. Case_clauses::lower(Block* b, Temporary_statement* val_temp,
  2993. Unnamed_label* break_label) const
  2994. {
  2995. // The default case.
  2996. const Case_clause* default_case = NULL;
  2997. // The label for the fallthrough of the previous case.
  2998. Unnamed_label* last_fallthrough_label = NULL;
  2999. // The label for the start of the default case. This is used if the
  3000. // case before the default case falls through.
  3001. Unnamed_label* default_start_label = NULL;
  3002. // The label for the end of the default case. This normally winds
  3003. // up as BREAK_LABEL, but it will be different if the default case
  3004. // falls through.
  3005. Unnamed_label* default_finish_label = NULL;
  3006. for (Clauses::const_iterator p = this->clauses_.begin();
  3007. p != this->clauses_.end();
  3008. ++p)
  3009. {
  3010. // The label to use for the start of the statements for this
  3011. // case. This is NULL unless the previous case falls through.
  3012. Unnamed_label* start_label = last_fallthrough_label;
  3013. // The label to jump to after the end of the statements for this
  3014. // case.
  3015. Unnamed_label* finish_label = break_label;
  3016. last_fallthrough_label = NULL;
  3017. if (p->is_fallthrough() && p + 1 != this->clauses_.end())
  3018. {
  3019. finish_label = new Unnamed_label(p->location());
  3020. last_fallthrough_label = finish_label;
  3021. }
  3022. if (!p->is_default())
  3023. p->lower(b, val_temp, start_label, finish_label);
  3024. else
  3025. {
  3026. // We have to move the default case to the end, so that we
  3027. // only use it if all the other tests fail.
  3028. default_case = &*p;
  3029. default_start_label = start_label;
  3030. default_finish_label = finish_label;
  3031. }
  3032. }
  3033. if (default_case != NULL)
  3034. default_case->lower(b, val_temp, default_start_label,
  3035. default_finish_label);
  3036. }
  3037. // Determine types.
  3038. void
  3039. Case_clauses::determine_types(Type* type)
  3040. {
  3041. for (Clauses::iterator p = this->clauses_.begin();
  3042. p != this->clauses_.end();
  3043. ++p)
  3044. p->determine_types(type);
  3045. }
  3046. // Check types. Returns false if there was an error.
  3047. bool
  3048. Case_clauses::check_types(Type* type)
  3049. {
  3050. bool ret = true;
  3051. for (Clauses::iterator p = this->clauses_.begin();
  3052. p != this->clauses_.end();
  3053. ++p)
  3054. {
  3055. if (!p->check_types(type))
  3056. ret = false;
  3057. }
  3058. return ret;
  3059. }
  3060. // Return true if these clauses may fall through to the statements
  3061. // following the switch statement.
  3062. bool
  3063. Case_clauses::may_fall_through() const
  3064. {
  3065. bool found_default = false;
  3066. for (Clauses::const_iterator p = this->clauses_.begin();
  3067. p != this->clauses_.end();
  3068. ++p)
  3069. {
  3070. if (p->may_fall_through() && !p->is_fallthrough())
  3071. return true;
  3072. if (p->is_default())
  3073. found_default = true;
  3074. }
  3075. return !found_default;
  3076. }
  3077. // Convert the cases to the backend representation. This sets
  3078. // *ALL_CASES and *ALL_STATEMENTS.
  3079. void
  3080. Case_clauses::get_backend(Translate_context* context,
  3081. Unnamed_label* break_label,
  3082. std::vector<std::vector<Bexpression*> >* all_cases,
  3083. std::vector<Bstatement*>* all_statements) const
  3084. {
  3085. Case_constants case_constants;
  3086. size_t c = this->clauses_.size();
  3087. all_cases->resize(c);
  3088. all_statements->resize(c);
  3089. size_t i = 0;
  3090. for (Clauses::const_iterator p = this->clauses_.begin();
  3091. p != this->clauses_.end();
  3092. ++p, ++i)
  3093. {
  3094. std::vector<Bexpression*> cases;
  3095. Bstatement* stat = p->get_backend(context, break_label, &case_constants,
  3096. &cases);
  3097. (*all_cases)[i].swap(cases);
  3098. (*all_statements)[i] = stat;
  3099. }
  3100. }
  3101. // Dump the AST representation for case clauses (from a switch statement)
  3102. void
  3103. Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
  3104. {
  3105. for (Clauses::const_iterator p = this->clauses_.begin();
  3106. p != this->clauses_.end();
  3107. ++p)
  3108. p->dump_clause(ast_dump_context);
  3109. }
  3110. // A constant switch statement. A Switch_statement is lowered to this
  3111. // when all the cases are constants.
  3112. class Constant_switch_statement : public Statement
  3113. {
  3114. public:
  3115. Constant_switch_statement(Expression* val, Case_clauses* clauses,
  3116. Unnamed_label* break_label,
  3117. Location location)
  3118. : Statement(STATEMENT_CONSTANT_SWITCH, location),
  3119. val_(val), clauses_(clauses), break_label_(break_label)
  3120. { }
  3121. protected:
  3122. int
  3123. do_traverse(Traverse*);
  3124. void
  3125. do_determine_types();
  3126. void
  3127. do_check_types(Gogo*);
  3128. Bstatement*
  3129. do_get_backend(Translate_context*);
  3130. void
  3131. do_dump_statement(Ast_dump_context*) const;
  3132. private:
  3133. // The value to switch on.
  3134. Expression* val_;
  3135. // The case clauses.
  3136. Case_clauses* clauses_;
  3137. // The break label, if needed.
  3138. Unnamed_label* break_label_;
  3139. };
  3140. // Traversal.
  3141. int
  3142. Constant_switch_statement::do_traverse(Traverse* traverse)
  3143. {
  3144. if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
  3145. return TRAVERSE_EXIT;
  3146. return this->clauses_->traverse(traverse);
  3147. }
  3148. // Determine types.
  3149. void
  3150. Constant_switch_statement::do_determine_types()
  3151. {
  3152. this->val_->determine_type_no_context();
  3153. this->clauses_->determine_types(this->val_->type());
  3154. }
  3155. // Check types.
  3156. void
  3157. Constant_switch_statement::do_check_types(Gogo*)
  3158. {
  3159. if (!this->clauses_->check_types(this->val_->type()))
  3160. this->set_is_error();
  3161. }
  3162. // Convert to GENERIC.
  3163. Bstatement*
  3164. Constant_switch_statement::do_get_backend(Translate_context* context)
  3165. {
  3166. Bexpression* switch_val_expr = this->val_->get_backend(context);
  3167. Unnamed_label* break_label = this->break_label_;
  3168. if (break_label == NULL)
  3169. break_label = new Unnamed_label(this->location());
  3170. std::vector<std::vector<Bexpression*> > all_cases;
  3171. std::vector<Bstatement*> all_statements;
  3172. this->clauses_->get_backend(context, break_label, &all_cases,
  3173. &all_statements);
  3174. Bfunction* bfunction = context->function()->func_value()->get_decl();
  3175. Bstatement* switch_statement;
  3176. switch_statement = context->backend()->switch_statement(bfunction,
  3177. switch_val_expr,
  3178. all_cases,
  3179. all_statements,
  3180. this->location());
  3181. Bstatement* ldef = break_label->get_definition(context);
  3182. return context->backend()->compound_statement(switch_statement, ldef);
  3183. }
  3184. // Dump the AST representation for a constant switch statement.
  3185. void
  3186. Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
  3187. const
  3188. {
  3189. ast_dump_context->print_indent();
  3190. ast_dump_context->ostream() << "switch ";
  3191. ast_dump_context->dump_expression(this->val_);
  3192. if (ast_dump_context->dump_subblocks())
  3193. {
  3194. ast_dump_context->ostream() << " {" << std::endl;
  3195. this->clauses_->dump_clauses(ast_dump_context);
  3196. ast_dump_context->ostream() << "}";
  3197. }
  3198. ast_dump_context->ostream() << std::endl;
  3199. }
  3200. // Class Switch_statement.
  3201. // Traversal.
  3202. int
  3203. Switch_statement::do_traverse(Traverse* traverse)
  3204. {
  3205. if (this->val_ != NULL)
  3206. {
  3207. if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
  3208. return TRAVERSE_EXIT;
  3209. }
  3210. return this->clauses_->traverse(traverse);
  3211. }
  3212. // Lower a Switch_statement to a Constant_switch_statement or a series
  3213. // of if statements.
  3214. Statement*
  3215. Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
  3216. Statement_inserter*)
  3217. {
  3218. Location loc = this->location();
  3219. if (this->val_ != NULL
  3220. && (this->val_->is_error_expression()
  3221. || this->val_->type()->is_error()))
  3222. return Statement::make_error_statement(loc);
  3223. if (this->val_ != NULL
  3224. && this->val_->type()->integer_type() != NULL
  3225. && !this->clauses_->empty()
  3226. && this->clauses_->is_constant())
  3227. return new Constant_switch_statement(this->val_, this->clauses_,
  3228. this->break_label_, loc);
  3229. if (this->val_ != NULL
  3230. && !this->val_->type()->is_comparable()
  3231. && !Type::are_compatible_for_comparison(true, this->val_->type(),
  3232. Type::make_nil_type(), NULL))
  3233. {
  3234. error_at(this->val_->location(),
  3235. "cannot switch on value whose type that may not be compared");
  3236. return Statement::make_error_statement(loc);
  3237. }
  3238. Block* b = new Block(enclosing, loc);
  3239. if (this->clauses_->empty())
  3240. {
  3241. Expression* val = this->val_;
  3242. if (val == NULL)
  3243. val = Expression::make_boolean(true, loc);
  3244. return Statement::make_statement(val, true);
  3245. }
  3246. // var val_temp VAL_TYPE = VAL
  3247. Expression* val = this->val_;
  3248. if (val == NULL)
  3249. val = Expression::make_boolean(true, loc);
  3250. Type* type = val->type();
  3251. if (type->is_abstract())
  3252. type = type->make_non_abstract_type();
  3253. Temporary_statement* val_temp = Statement::make_temporary(type, val, loc);
  3254. b->add_statement(val_temp);
  3255. this->clauses_->lower(b, val_temp, this->break_label());
  3256. Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
  3257. b->add_statement(s);
  3258. return Statement::make_block_statement(b, loc);
  3259. }
  3260. // Return the break label for this switch statement, creating it if
  3261. // necessary.
  3262. Unnamed_label*
  3263. Switch_statement::break_label()
  3264. {
  3265. if (this->break_label_ == NULL)
  3266. this->break_label_ = new Unnamed_label(this->location());
  3267. return this->break_label_;
  3268. }
  3269. // Dump the AST representation for a switch statement.
  3270. void
  3271. Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  3272. {
  3273. ast_dump_context->print_indent();
  3274. ast_dump_context->ostream() << "switch ";
  3275. if (this->val_ != NULL)
  3276. {
  3277. ast_dump_context->dump_expression(this->val_);
  3278. }
  3279. if (ast_dump_context->dump_subblocks())
  3280. {
  3281. ast_dump_context->ostream() << " {" << std::endl;
  3282. this->clauses_->dump_clauses(ast_dump_context);
  3283. ast_dump_context->print_indent();
  3284. ast_dump_context->ostream() << "}";
  3285. }
  3286. ast_dump_context->ostream() << std::endl;
  3287. }
  3288. // Return whether this switch may fall through.
  3289. bool
  3290. Switch_statement::do_may_fall_through() const
  3291. {
  3292. if (this->clauses_ == NULL)
  3293. return true;
  3294. // If we have a break label, then some case needed it. That implies
  3295. // that the switch statement as a whole can fall through.
  3296. if (this->break_label_ != NULL)
  3297. return true;
  3298. return this->clauses_->may_fall_through();
  3299. }
  3300. // Make a switch statement.
  3301. Switch_statement*
  3302. Statement::make_switch_statement(Expression* val, Location location)
  3303. {
  3304. return new Switch_statement(val, location);
  3305. }
  3306. // Class Type_case_clauses::Type_case_clause.
  3307. // Traversal.
  3308. int
  3309. Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
  3310. {
  3311. if (!this->is_default_
  3312. && ((traverse->traverse_mask()
  3313. & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
  3314. && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
  3315. return TRAVERSE_EXIT;
  3316. if (this->statements_ != NULL)
  3317. return this->statements_->traverse(traverse);
  3318. return TRAVERSE_CONTINUE;
  3319. }
  3320. // Lower one clause in a type switch. Add statements to the block B.
  3321. // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
  3322. // BREAK_LABEL is the label at the end of the type switch.
  3323. // *STMTS_LABEL, if not NULL, is a label to put at the start of the
  3324. // statements.
  3325. void
  3326. Type_case_clauses::Type_case_clause::lower(Type* switch_val_type,
  3327. Block* b,
  3328. Temporary_statement* descriptor_temp,
  3329. Unnamed_label* break_label,
  3330. Unnamed_label** stmts_label) const
  3331. {
  3332. Location loc = this->location_;
  3333. Unnamed_label* next_case_label = NULL;
  3334. if (!this->is_default_)
  3335. {
  3336. Type* type = this->type_;
  3337. std::string reason;
  3338. if (switch_val_type->interface_type() != NULL
  3339. && !type->is_nil_constant_as_type()
  3340. && type->interface_type() == NULL
  3341. && !switch_val_type->interface_type()->implements_interface(type,
  3342. &reason))
  3343. {
  3344. if (reason.empty())
  3345. error_at(this->location_, "impossible type switch case");
  3346. else
  3347. error_at(this->location_, "impossible type switch case (%s)",
  3348. reason.c_str());
  3349. }
  3350. Expression* ref = Expression::make_temporary_reference(descriptor_temp,
  3351. loc);
  3352. Expression* cond;
  3353. // The language permits case nil, which is of course a constant
  3354. // rather than a type. It will appear here as an invalid
  3355. // forwarding type.
  3356. if (type->is_nil_constant_as_type())
  3357. cond = Expression::make_binary(OPERATOR_EQEQ, ref,
  3358. Expression::make_nil(loc),
  3359. loc);
  3360. else
  3361. cond = Runtime::make_call((type->interface_type() == NULL
  3362. ? Runtime::IFACETYPEEQ
  3363. : Runtime::IFACEI2TP),
  3364. loc, 2,
  3365. Expression::make_type_descriptor(type, loc),
  3366. ref);
  3367. Unnamed_label* dest;
  3368. if (!this->is_fallthrough_)
  3369. {
  3370. // if !COND { goto NEXT_CASE_LABEL }
  3371. next_case_label = new Unnamed_label(Linemap::unknown_location());
  3372. dest = next_case_label;
  3373. cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
  3374. }
  3375. else
  3376. {
  3377. // if COND { goto STMTS_LABEL }
  3378. go_assert(stmts_label != NULL);
  3379. if (*stmts_label == NULL)
  3380. *stmts_label = new Unnamed_label(Linemap::unknown_location());
  3381. dest = *stmts_label;
  3382. }
  3383. Block* then_block = new Block(b, loc);
  3384. Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
  3385. then_block->add_statement(s);
  3386. s = Statement::make_if_statement(cond, then_block, NULL, loc);
  3387. b->add_statement(s);
  3388. }
  3389. if (this->statements_ != NULL
  3390. || (!this->is_fallthrough_
  3391. && stmts_label != NULL
  3392. && *stmts_label != NULL))
  3393. {
  3394. go_assert(!this->is_fallthrough_);
  3395. if (stmts_label != NULL && *stmts_label != NULL)
  3396. {
  3397. go_assert(!this->is_default_);
  3398. if (this->statements_ != NULL)
  3399. (*stmts_label)->set_location(this->statements_->start_location());
  3400. Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
  3401. b->add_statement(s);
  3402. *stmts_label = NULL;
  3403. }
  3404. if (this->statements_ != NULL)
  3405. b->add_statement(Statement::make_block_statement(this->statements_,
  3406. loc));
  3407. }
  3408. if (this->is_fallthrough_)
  3409. go_assert(next_case_label == NULL);
  3410. else
  3411. {
  3412. Location gloc = (this->statements_ == NULL
  3413. ? loc
  3414. : this->statements_->end_location());
  3415. b->add_statement(Statement::make_goto_unnamed_statement(break_label,
  3416. gloc));
  3417. if (next_case_label != NULL)
  3418. {
  3419. Statement* s =
  3420. Statement::make_unnamed_label_statement(next_case_label);
  3421. b->add_statement(s);
  3422. }
  3423. }
  3424. }
  3425. // Return true if this type clause may fall through to the statements
  3426. // following the switch.
  3427. bool
  3428. Type_case_clauses::Type_case_clause::may_fall_through() const
  3429. {
  3430. if (this->is_fallthrough_)
  3431. {
  3432. // This case means that we automatically fall through to the
  3433. // next case (it's used for T1 in case T1, T2:). It does not
  3434. // mean that we fall through to the end of the type switch as a
  3435. // whole. There is sure to be a next case and that next case
  3436. // will determine whether we fall through to the statements
  3437. // after the type switch.
  3438. return false;
  3439. }
  3440. if (this->statements_ == NULL)
  3441. return true;
  3442. return this->statements_->may_fall_through();
  3443. }
  3444. // Dump the AST representation for a type case clause
  3445. void
  3446. Type_case_clauses::Type_case_clause::dump_clause(
  3447. Ast_dump_context* ast_dump_context) const
  3448. {
  3449. ast_dump_context->print_indent();
  3450. if (this->is_default_)
  3451. {
  3452. ast_dump_context->ostream() << "default:";
  3453. }
  3454. else
  3455. {
  3456. ast_dump_context->ostream() << "case ";
  3457. ast_dump_context->dump_type(this->type_);
  3458. ast_dump_context->ostream() << ":" ;
  3459. }
  3460. ast_dump_context->dump_block(this->statements_);
  3461. if (this->is_fallthrough_)
  3462. {
  3463. ast_dump_context->print_indent();
  3464. ast_dump_context->ostream() << " (fallthrough)" << std::endl;
  3465. }
  3466. }
  3467. // Class Type_case_clauses.
  3468. // Traversal.
  3469. int
  3470. Type_case_clauses::traverse(Traverse* traverse)
  3471. {
  3472. for (Type_clauses::iterator p = this->clauses_.begin();
  3473. p != this->clauses_.end();
  3474. ++p)
  3475. {
  3476. if (p->traverse(traverse) == TRAVERSE_EXIT)
  3477. return TRAVERSE_EXIT;
  3478. }
  3479. return TRAVERSE_CONTINUE;
  3480. }
  3481. // Check for duplicate types.
  3482. void
  3483. Type_case_clauses::check_duplicates() const
  3484. {
  3485. typedef Unordered_set_hash(const Type*, Type_hash_identical,
  3486. Type_identical) Types_seen;
  3487. Types_seen types_seen;
  3488. for (Type_clauses::const_iterator p = this->clauses_.begin();
  3489. p != this->clauses_.end();
  3490. ++p)
  3491. {
  3492. Type* t = p->type();
  3493. if (t == NULL)
  3494. continue;
  3495. if (t->is_nil_constant_as_type())
  3496. t = Type::make_nil_type();
  3497. std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
  3498. if (!ins.second)
  3499. error_at(p->location(), "duplicate type in switch");
  3500. }
  3501. }
  3502. // Lower the clauses in a type switch. Add statements to the block B.
  3503. // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
  3504. // BREAK_LABEL is the label at the end of the type switch.
  3505. void
  3506. Type_case_clauses::lower(Type* switch_val_type, Block* b,
  3507. Temporary_statement* descriptor_temp,
  3508. Unnamed_label* break_label) const
  3509. {
  3510. const Type_case_clause* default_case = NULL;
  3511. Unnamed_label* stmts_label = NULL;
  3512. for (Type_clauses::const_iterator p = this->clauses_.begin();
  3513. p != this->clauses_.end();
  3514. ++p)
  3515. {
  3516. if (!p->is_default())
  3517. p->lower(switch_val_type, b, descriptor_temp, break_label,
  3518. &stmts_label);
  3519. else
  3520. {
  3521. // We are generating a series of tests, which means that we
  3522. // need to move the default case to the end.
  3523. default_case = &*p;
  3524. }
  3525. }
  3526. go_assert(stmts_label == NULL);
  3527. if (default_case != NULL)
  3528. default_case->lower(switch_val_type, b, descriptor_temp, break_label,
  3529. NULL);
  3530. }
  3531. // Return true if these clauses may fall through to the statements
  3532. // following the switch statement.
  3533. bool
  3534. Type_case_clauses::may_fall_through() const
  3535. {
  3536. bool found_default = false;
  3537. for (Type_clauses::const_iterator p = this->clauses_.begin();
  3538. p != this->clauses_.end();
  3539. ++p)
  3540. {
  3541. if (p->may_fall_through())
  3542. return true;
  3543. if (p->is_default())
  3544. found_default = true;
  3545. }
  3546. return !found_default;
  3547. }
  3548. // Dump the AST representation for case clauses (from a switch statement)
  3549. void
  3550. Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
  3551. {
  3552. for (Type_clauses::const_iterator p = this->clauses_.begin();
  3553. p != this->clauses_.end();
  3554. ++p)
  3555. p->dump_clause(ast_dump_context);
  3556. }
  3557. // Class Type_switch_statement.
  3558. // Traversal.
  3559. int
  3560. Type_switch_statement::do_traverse(Traverse* traverse)
  3561. {
  3562. if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
  3563. return TRAVERSE_EXIT;
  3564. if (this->clauses_ != NULL)
  3565. return this->clauses_->traverse(traverse);
  3566. return TRAVERSE_CONTINUE;
  3567. }
  3568. // Lower a type switch statement to a series of if statements. The gc
  3569. // compiler is able to generate a table in some cases. However, that
  3570. // does not work for us because we may have type descriptors in
  3571. // different shared libraries, so we can't compare them with simple
  3572. // equality testing.
  3573. Statement*
  3574. Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
  3575. Statement_inserter*)
  3576. {
  3577. const Location loc = this->location();
  3578. if (this->clauses_ != NULL)
  3579. this->clauses_->check_duplicates();
  3580. Block* b = new Block(enclosing, loc);
  3581. Type* val_type = this->expr_->type();
  3582. if (val_type->interface_type() == NULL)
  3583. {
  3584. if (!val_type->is_error())
  3585. this->report_error(_("cannot type switch on non-interface value"));
  3586. return Statement::make_error_statement(loc);
  3587. }
  3588. // var descriptor_temp DESCRIPTOR_TYPE
  3589. Type* descriptor_type = Type::make_type_descriptor_ptr_type();
  3590. Temporary_statement* descriptor_temp =
  3591. Statement::make_temporary(descriptor_type, NULL, loc);
  3592. b->add_statement(descriptor_temp);
  3593. // descriptor_temp = ifacetype(val_temp) FIXME: This should be
  3594. // inlined.
  3595. bool is_empty = val_type->interface_type()->is_empty();
  3596. Expression* call = Runtime::make_call((is_empty
  3597. ? Runtime::EFACETYPE
  3598. : Runtime::IFACETYPE),
  3599. loc, 1, this->expr_);
  3600. Temporary_reference_expression* lhs =
  3601. Expression::make_temporary_reference(descriptor_temp, loc);
  3602. lhs->set_is_lvalue();
  3603. Statement* s = Statement::make_assignment(lhs, call, loc);
  3604. b->add_statement(s);
  3605. if (this->clauses_ != NULL)
  3606. this->clauses_->lower(val_type, b, descriptor_temp, this->break_label());
  3607. s = Statement::make_unnamed_label_statement(this->break_label_);
  3608. b->add_statement(s);
  3609. return Statement::make_block_statement(b, loc);
  3610. }
  3611. // Return whether this switch may fall through.
  3612. bool
  3613. Type_switch_statement::do_may_fall_through() const
  3614. {
  3615. if (this->clauses_ == NULL)
  3616. return true;
  3617. // If we have a break label, then some case needed it. That implies
  3618. // that the switch statement as a whole can fall through.
  3619. if (this->break_label_ != NULL)
  3620. return true;
  3621. return this->clauses_->may_fall_through();
  3622. }
  3623. // Return the break label for this type switch statement, creating it
  3624. // if necessary.
  3625. Unnamed_label*
  3626. Type_switch_statement::break_label()
  3627. {
  3628. if (this->break_label_ == NULL)
  3629. this->break_label_ = new Unnamed_label(this->location());
  3630. return this->break_label_;
  3631. }
  3632. // Dump the AST representation for a type switch statement
  3633. void
  3634. Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
  3635. const
  3636. {
  3637. ast_dump_context->print_indent();
  3638. ast_dump_context->ostream() << "switch ";
  3639. if (!this->name_.empty())
  3640. ast_dump_context->ostream() << this->name_ << " = ";
  3641. ast_dump_context->dump_expression(this->expr_);
  3642. ast_dump_context->ostream() << " .(type)";
  3643. if (ast_dump_context->dump_subblocks())
  3644. {
  3645. ast_dump_context->ostream() << " {" << std::endl;
  3646. this->clauses_->dump_clauses(ast_dump_context);
  3647. ast_dump_context->ostream() << "}";
  3648. }
  3649. ast_dump_context->ostream() << std::endl;
  3650. }
  3651. // Make a type switch statement.
  3652. Type_switch_statement*
  3653. Statement::make_type_switch_statement(const std::string& name, Expression* expr,
  3654. Location location)
  3655. {
  3656. return new Type_switch_statement(name, expr, location);
  3657. }
  3658. // Class Send_statement.
  3659. // Traversal.
  3660. int
  3661. Send_statement::do_traverse(Traverse* traverse)
  3662. {
  3663. if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
  3664. return TRAVERSE_EXIT;
  3665. return this->traverse_expression(traverse, &this->val_);
  3666. }
  3667. // Determine types.
  3668. void
  3669. Send_statement::do_determine_types()
  3670. {
  3671. this->channel_->determine_type_no_context();
  3672. Type* type = this->channel_->type();
  3673. Type_context context;
  3674. if (type->channel_type() != NULL)
  3675. context.type = type->channel_type()->element_type();
  3676. this->val_->determine_type(&context);
  3677. }
  3678. // Check types.
  3679. void
  3680. Send_statement::do_check_types(Gogo*)
  3681. {
  3682. Type* type = this->channel_->type();
  3683. if (type->is_error())
  3684. {
  3685. this->set_is_error();
  3686. return;
  3687. }
  3688. Channel_type* channel_type = type->channel_type();
  3689. if (channel_type == NULL)
  3690. {
  3691. error_at(this->location(), "left operand of %<<-%> must be channel");
  3692. this->set_is_error();
  3693. return;
  3694. }
  3695. Type* element_type = channel_type->element_type();
  3696. if (!Type::are_assignable(element_type, this->val_->type(), NULL))
  3697. {
  3698. this->report_error(_("incompatible types in send"));
  3699. return;
  3700. }
  3701. if (!channel_type->may_send())
  3702. {
  3703. this->report_error(_("invalid send on receive-only channel"));
  3704. return;
  3705. }
  3706. }
  3707. // Flatten a send statement. We may need a temporary for interface
  3708. // conversion.
  3709. Statement*
  3710. Send_statement::do_flatten(Gogo*, Named_object*, Block*,
  3711. Statement_inserter* inserter)
  3712. {
  3713. Type* element_type = this->channel_->type()->channel_type()->element_type();
  3714. if (!Type::are_identical(element_type, this->val_->type(), false, NULL)
  3715. && this->val_->type()->interface_type() != NULL
  3716. && !this->val_->is_variable())
  3717. {
  3718. Temporary_statement* temp =
  3719. Statement::make_temporary(NULL, this->val_, this->location());
  3720. inserter->insert(temp);
  3721. this->val_ = Expression::make_temporary_reference(temp,
  3722. this->location());
  3723. }
  3724. return this;
  3725. }
  3726. // Convert a send statement to the backend representation.
  3727. Bstatement*
  3728. Send_statement::do_get_backend(Translate_context* context)
  3729. {
  3730. Location loc = this->location();
  3731. Channel_type* channel_type = this->channel_->type()->channel_type();
  3732. Type* element_type = channel_type->element_type();
  3733. Expression* val = Expression::convert_for_assignment(context->gogo(),
  3734. element_type,
  3735. this->val_, loc);
  3736. bool is_small;
  3737. bool can_take_address;
  3738. switch (element_type->base()->classification())
  3739. {
  3740. case Type::TYPE_BOOLEAN:
  3741. case Type::TYPE_INTEGER:
  3742. case Type::TYPE_FUNCTION:
  3743. case Type::TYPE_POINTER:
  3744. case Type::TYPE_MAP:
  3745. case Type::TYPE_CHANNEL:
  3746. is_small = true;
  3747. can_take_address = false;
  3748. break;
  3749. case Type::TYPE_FLOAT:
  3750. case Type::TYPE_COMPLEX:
  3751. case Type::TYPE_STRING:
  3752. case Type::TYPE_INTERFACE:
  3753. is_small = false;
  3754. can_take_address = false;
  3755. break;
  3756. case Type::TYPE_STRUCT:
  3757. is_small = false;
  3758. can_take_address = true;
  3759. break;
  3760. case Type::TYPE_ARRAY:
  3761. is_small = false;
  3762. can_take_address = !element_type->is_slice_type();
  3763. break;
  3764. default:
  3765. case Type::TYPE_ERROR:
  3766. case Type::TYPE_VOID:
  3767. case Type::TYPE_SINK:
  3768. case Type::TYPE_NIL:
  3769. case Type::TYPE_NAMED:
  3770. case Type::TYPE_FORWARD:
  3771. go_assert(saw_errors());
  3772. return context->backend()->error_statement();
  3773. }
  3774. // Only try to take the address of a variable. We have already
  3775. // moved variables to the heap, so this should not cause that to
  3776. // happen unnecessarily.
  3777. if (can_take_address
  3778. && val->var_expression() == NULL
  3779. && val->temporary_reference_expression() == NULL)
  3780. can_take_address = false;
  3781. Expression* td = Expression::make_type_descriptor(this->channel_->type(),
  3782. loc);
  3783. Runtime::Function code;
  3784. Bstatement* btemp = NULL;
  3785. if (is_small)
  3786. {
  3787. // Type is small enough to handle as uint64.
  3788. code = Runtime::SEND_SMALL;
  3789. val = Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
  3790. val, loc);
  3791. }
  3792. else if (can_take_address)
  3793. {
  3794. // Must pass address of value. The function doesn't change the
  3795. // value, so just take its address directly.
  3796. code = Runtime::SEND_BIG;
  3797. val = Expression::make_unary(OPERATOR_AND, val, loc);
  3798. }
  3799. else
  3800. {
  3801. // Must pass address of value, but the value is small enough
  3802. // that it might be in registers. Copy value into temporary
  3803. // variable to take address.
  3804. code = Runtime::SEND_BIG;
  3805. Temporary_statement* temp = Statement::make_temporary(element_type,
  3806. val, loc);
  3807. Expression* ref = Expression::make_temporary_reference(temp, loc);
  3808. val = Expression::make_unary(OPERATOR_AND, ref, loc);
  3809. btemp = temp->get_backend(context);
  3810. }
  3811. Expression* call = Runtime::make_call(code, loc, 3, td, this->channel_, val);
  3812. context->gogo()->lower_expression(context->function(), NULL, &call);
  3813. Bexpression* bcall = call->get_backend(context);
  3814. Bstatement* s = context->backend()->expression_statement(bcall);
  3815. if (btemp == NULL)
  3816. return s;
  3817. else
  3818. return context->backend()->compound_statement(btemp, s);
  3819. }
  3820. // Dump the AST representation for a send statement
  3821. void
  3822. Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  3823. {
  3824. ast_dump_context->print_indent();
  3825. ast_dump_context->dump_expression(this->channel_);
  3826. ast_dump_context->ostream() << " <- ";
  3827. ast_dump_context->dump_expression(this->val_);
  3828. ast_dump_context->ostream() << std::endl;
  3829. }
  3830. // Make a send statement.
  3831. Send_statement*
  3832. Statement::make_send_statement(Expression* channel, Expression* val,
  3833. Location location)
  3834. {
  3835. return new Send_statement(channel, val, location);
  3836. }
  3837. // Class Select_clauses::Select_clause.
  3838. // Traversal.
  3839. int
  3840. Select_clauses::Select_clause::traverse(Traverse* traverse)
  3841. {
  3842. if (!this->is_lowered_
  3843. && (traverse->traverse_mask()
  3844. & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
  3845. {
  3846. if (this->channel_ != NULL)
  3847. {
  3848. if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
  3849. return TRAVERSE_EXIT;
  3850. }
  3851. if (this->val_ != NULL)
  3852. {
  3853. if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
  3854. return TRAVERSE_EXIT;
  3855. }
  3856. if (this->closed_ != NULL)
  3857. {
  3858. if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
  3859. return TRAVERSE_EXIT;
  3860. }
  3861. }
  3862. if (this->statements_ != NULL)
  3863. {
  3864. if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
  3865. return TRAVERSE_EXIT;
  3866. }
  3867. return TRAVERSE_CONTINUE;
  3868. }
  3869. // Lowering. We call a function to register this clause, and arrange
  3870. // to set any variables in any receive clause.
  3871. void
  3872. Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
  3873. Block* b, Temporary_statement* sel)
  3874. {
  3875. Location loc = this->location_;
  3876. Expression* selref = Expression::make_temporary_reference(sel, loc);
  3877. Expression* index_expr = Expression::make_integer_ul(this->index_, NULL,
  3878. loc);
  3879. if (this->is_default_)
  3880. {
  3881. go_assert(this->channel_ == NULL && this->val_ == NULL);
  3882. this->lower_default(b, selref, index_expr);
  3883. this->is_lowered_ = true;
  3884. return;
  3885. }
  3886. // Evaluate the channel before the select statement.
  3887. Temporary_statement* channel_temp = Statement::make_temporary(NULL,
  3888. this->channel_,
  3889. loc);
  3890. b->add_statement(channel_temp);
  3891. Expression* chanref = Expression::make_temporary_reference(channel_temp,
  3892. loc);
  3893. if (this->is_send_)
  3894. this->lower_send(b, selref, chanref, index_expr);
  3895. else
  3896. this->lower_recv(gogo, function, b, selref, chanref, index_expr);
  3897. // Now all references should be handled through the statements, not
  3898. // through here.
  3899. this->is_lowered_ = true;
  3900. this->val_ = NULL;
  3901. this->var_ = NULL;
  3902. }
  3903. // Lower a default clause in a select statement.
  3904. void
  3905. Select_clauses::Select_clause::lower_default(Block* b, Expression* selref,
  3906. Expression* index_expr)
  3907. {
  3908. Location loc = this->location_;
  3909. Expression* call = Runtime::make_call(Runtime::SELECTDEFAULT, loc, 2, selref,
  3910. index_expr);
  3911. b->add_statement(Statement::make_statement(call, true));
  3912. }
  3913. // Lower a send clause in a select statement.
  3914. void
  3915. Select_clauses::Select_clause::lower_send(Block* b, Expression* selref,
  3916. Expression* chanref,
  3917. Expression* index_expr)
  3918. {
  3919. Location loc = this->location_;
  3920. Channel_type* ct = this->channel_->type()->channel_type();
  3921. if (ct == NULL)
  3922. return;
  3923. Type* valtype = ct->element_type();
  3924. // Note that copying the value to a temporary here means that we
  3925. // evaluate the send values in the required order.
  3926. Temporary_statement* val = Statement::make_temporary(valtype, this->val_,
  3927. loc);
  3928. b->add_statement(val);
  3929. Expression* valref = Expression::make_temporary_reference(val, loc);
  3930. Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
  3931. Expression* call = Runtime::make_call(Runtime::SELECTSEND, loc, 4, selref,
  3932. chanref, valaddr, index_expr);
  3933. b->add_statement(Statement::make_statement(call, true));
  3934. }
  3935. // Lower a receive clause in a select statement.
  3936. void
  3937. Select_clauses::Select_clause::lower_recv(Gogo* gogo, Named_object* function,
  3938. Block* b, Expression* selref,
  3939. Expression* chanref,
  3940. Expression* index_expr)
  3941. {
  3942. Location loc = this->location_;
  3943. Channel_type* ct = this->channel_->type()->channel_type();
  3944. if (ct == NULL)
  3945. return;
  3946. Type* valtype = ct->element_type();
  3947. Temporary_statement* val = Statement::make_temporary(valtype, NULL, loc);
  3948. b->add_statement(val);
  3949. Expression* valref = Expression::make_temporary_reference(val, loc);
  3950. Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
  3951. Temporary_statement* closed_temp = NULL;
  3952. Expression* call;
  3953. if (this->closed_ == NULL && this->closedvar_ == NULL)
  3954. call = Runtime::make_call(Runtime::SELECTRECV, loc, 4, selref, chanref,
  3955. valaddr, index_expr);
  3956. else
  3957. {
  3958. closed_temp = Statement::make_temporary(Type::lookup_bool_type(), NULL,
  3959. loc);
  3960. b->add_statement(closed_temp);
  3961. Expression* cref = Expression::make_temporary_reference(closed_temp,
  3962. loc);
  3963. Expression* caddr = Expression::make_unary(OPERATOR_AND, cref, loc);
  3964. call = Runtime::make_call(Runtime::SELECTRECV2, loc, 5, selref, chanref,
  3965. valaddr, caddr, index_expr);
  3966. }
  3967. b->add_statement(Statement::make_statement(call, true));
  3968. // If the block of statements is executed, arrange for the received
  3969. // value to move from VAL to the place where the statements expect
  3970. // it.
  3971. Block* init = NULL;
  3972. if (this->var_ != NULL)
  3973. {
  3974. go_assert(this->val_ == NULL);
  3975. valref = Expression::make_temporary_reference(val, loc);
  3976. this->var_->var_value()->set_init(valref);
  3977. this->var_->var_value()->clear_type_from_chan_element();
  3978. }
  3979. else if (this->val_ != NULL && !this->val_->is_sink_expression())
  3980. {
  3981. init = new Block(b, loc);
  3982. valref = Expression::make_temporary_reference(val, loc);
  3983. init->add_statement(Statement::make_assignment(this->val_, valref, loc));
  3984. }
  3985. if (this->closedvar_ != NULL)
  3986. {
  3987. go_assert(this->closed_ == NULL);
  3988. Expression* cref = Expression::make_temporary_reference(closed_temp,
  3989. loc);
  3990. this->closedvar_->var_value()->set_init(cref);
  3991. }
  3992. else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
  3993. {
  3994. if (init == NULL)
  3995. init = new Block(b, loc);
  3996. Expression* cref = Expression::make_temporary_reference(closed_temp,
  3997. loc);
  3998. init->add_statement(Statement::make_assignment(this->closed_, cref,
  3999. loc));
  4000. }
  4001. if (init != NULL)
  4002. {
  4003. gogo->lower_block(function, init);
  4004. if (this->statements_ != NULL)
  4005. init->add_statement(Statement::make_block_statement(this->statements_,
  4006. loc));
  4007. this->statements_ = init;
  4008. }
  4009. }
  4010. // Determine types.
  4011. void
  4012. Select_clauses::Select_clause::determine_types()
  4013. {
  4014. go_assert(this->is_lowered_);
  4015. if (this->statements_ != NULL)
  4016. this->statements_->determine_types();
  4017. }
  4018. // Check types.
  4019. void
  4020. Select_clauses::Select_clause::check_types()
  4021. {
  4022. if (this->is_default_)
  4023. return;
  4024. Channel_type* ct = this->channel_->type()->channel_type();
  4025. if (ct == NULL)
  4026. {
  4027. error_at(this->channel_->location(), "expected channel");
  4028. return;
  4029. }
  4030. if (this->is_send_ && !ct->may_send())
  4031. error_at(this->location(), "invalid send on receive-only channel");
  4032. else if (!this->is_send_ && !ct->may_receive())
  4033. error_at(this->location(), "invalid receive on send-only channel");
  4034. }
  4035. // Whether this clause may fall through to the statement which follows
  4036. // the overall select statement.
  4037. bool
  4038. Select_clauses::Select_clause::may_fall_through() const
  4039. {
  4040. if (this->statements_ == NULL)
  4041. return true;
  4042. return this->statements_->may_fall_through();
  4043. }
  4044. // Return the backend representation for the statements to execute.
  4045. Bstatement*
  4046. Select_clauses::Select_clause::get_statements_backend(
  4047. Translate_context* context)
  4048. {
  4049. if (this->statements_ == NULL)
  4050. return NULL;
  4051. Bblock* bblock = this->statements_->get_backend(context);
  4052. return context->backend()->block_statement(bblock);
  4053. }
  4054. // Dump the AST representation for a select case clause
  4055. void
  4056. Select_clauses::Select_clause::dump_clause(
  4057. Ast_dump_context* ast_dump_context) const
  4058. {
  4059. ast_dump_context->print_indent();
  4060. if (this->is_default_)
  4061. {
  4062. ast_dump_context->ostream() << "default:";
  4063. }
  4064. else
  4065. {
  4066. ast_dump_context->ostream() << "case " ;
  4067. if (this->is_send_)
  4068. {
  4069. ast_dump_context->dump_expression(this->channel_);
  4070. ast_dump_context->ostream() << " <- " ;
  4071. if (this->val_ != NULL)
  4072. ast_dump_context->dump_expression(this->val_);
  4073. }
  4074. else
  4075. {
  4076. if (this->val_ != NULL)
  4077. ast_dump_context->dump_expression(this->val_);
  4078. if (this->closed_ != NULL)
  4079. {
  4080. // FIXME: can val_ == NULL and closed_ ! = NULL?
  4081. ast_dump_context->ostream() << " , " ;
  4082. ast_dump_context->dump_expression(this->closed_);
  4083. }
  4084. if (this->closedvar_ != NULL || this->var_ != NULL)
  4085. ast_dump_context->ostream() << " := " ;
  4086. ast_dump_context->ostream() << " <- " ;
  4087. ast_dump_context->dump_expression(this->channel_);
  4088. }
  4089. ast_dump_context->ostream() << ":" ;
  4090. }
  4091. ast_dump_context->dump_block(this->statements_);
  4092. }
  4093. // Class Select_clauses.
  4094. // Traversal.
  4095. int
  4096. Select_clauses::traverse(Traverse* traverse)
  4097. {
  4098. for (Clauses::iterator p = this->clauses_.begin();
  4099. p != this->clauses_.end();
  4100. ++p)
  4101. {
  4102. if (p->traverse(traverse) == TRAVERSE_EXIT)
  4103. return TRAVERSE_EXIT;
  4104. }
  4105. return TRAVERSE_CONTINUE;
  4106. }
  4107. // Lowering. Here we pull out the channel and the send values, to
  4108. // enforce the order of evaluation. We also add explicit send and
  4109. // receive statements to the clauses.
  4110. void
  4111. Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b,
  4112. Temporary_statement* sel)
  4113. {
  4114. for (Clauses::iterator p = this->clauses_.begin();
  4115. p != this->clauses_.end();
  4116. ++p)
  4117. p->lower(gogo, function, b, sel);
  4118. }
  4119. // Determine types.
  4120. void
  4121. Select_clauses::determine_types()
  4122. {
  4123. for (Clauses::iterator p = this->clauses_.begin();
  4124. p != this->clauses_.end();
  4125. ++p)
  4126. p->determine_types();
  4127. }
  4128. // Check types.
  4129. void
  4130. Select_clauses::check_types()
  4131. {
  4132. for (Clauses::iterator p = this->clauses_.begin();
  4133. p != this->clauses_.end();
  4134. ++p)
  4135. p->check_types();
  4136. }
  4137. // Return whether these select clauses fall through to the statement
  4138. // following the overall select statement.
  4139. bool
  4140. Select_clauses::may_fall_through() const
  4141. {
  4142. for (Clauses::const_iterator p = this->clauses_.begin();
  4143. p != this->clauses_.end();
  4144. ++p)
  4145. if (p->may_fall_through())
  4146. return true;
  4147. return false;
  4148. }
  4149. // Convert to the backend representation. We have already accumulated
  4150. // all the select information. Now we call selectgo, which will
  4151. // return the index of the clause to execute.
  4152. Bstatement*
  4153. Select_clauses::get_backend(Translate_context* context,
  4154. Temporary_statement* sel,
  4155. Unnamed_label *break_label,
  4156. Location location)
  4157. {
  4158. size_t count = this->clauses_.size();
  4159. std::vector<std::vector<Bexpression*> > cases(count);
  4160. std::vector<Bstatement*> clauses(count);
  4161. Type* int32_type = Type::lookup_integer_type("int32");
  4162. int i = 0;
  4163. for (Clauses::iterator p = this->clauses_.begin();
  4164. p != this->clauses_.end();
  4165. ++p, ++i)
  4166. {
  4167. int index = p->index();
  4168. Expression* index_expr = Expression::make_integer_ul(index, int32_type,
  4169. location);
  4170. cases[i].push_back(index_expr->get_backend(context));
  4171. Bstatement* s = p->get_statements_backend(context);
  4172. Location gloc = (p->statements() == NULL
  4173. ? p->location()
  4174. : p->statements()->end_location());
  4175. Bstatement* g = break_label->get_goto(context, gloc);
  4176. if (s == NULL)
  4177. clauses[i] = g;
  4178. else
  4179. clauses[i] = context->backend()->compound_statement(s, g);
  4180. }
  4181. Expression* selref = Expression::make_temporary_reference(sel, location);
  4182. Expression* call = Runtime::make_call(Runtime::SELECTGO, location, 1,
  4183. selref);
  4184. context->gogo()->lower_expression(context->function(), NULL, &call);
  4185. Bexpression* bcall = call->get_backend(context);
  4186. if (count == 0)
  4187. return context->backend()->expression_statement(bcall);
  4188. std::vector<Bstatement*> statements;
  4189. statements.reserve(2);
  4190. Bfunction* bfunction = context->function()->func_value()->get_decl();
  4191. Bstatement* switch_stmt = context->backend()->switch_statement(bfunction,
  4192. bcall,
  4193. cases,
  4194. clauses,
  4195. location);
  4196. statements.push_back(switch_stmt);
  4197. Bstatement* ldef = break_label->get_definition(context);
  4198. statements.push_back(ldef);
  4199. return context->backend()->statement_list(statements);
  4200. }
  4201. // Dump the AST representation for select clauses.
  4202. void
  4203. Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
  4204. {
  4205. for (Clauses::const_iterator p = this->clauses_.begin();
  4206. p != this->clauses_.end();
  4207. ++p)
  4208. p->dump_clause(ast_dump_context);
  4209. }
  4210. // Class Select_statement.
  4211. // Return the break label for this switch statement, creating it if
  4212. // necessary.
  4213. Unnamed_label*
  4214. Select_statement::break_label()
  4215. {
  4216. if (this->break_label_ == NULL)
  4217. this->break_label_ = new Unnamed_label(this->location());
  4218. return this->break_label_;
  4219. }
  4220. // Lower a select statement. This will still return a select
  4221. // statement, but it will be modified to implement the order of
  4222. // evaluation rules, and to include the send and receive statements as
  4223. // explicit statements in the clauses.
  4224. Statement*
  4225. Select_statement::do_lower(Gogo* gogo, Named_object* function,
  4226. Block* enclosing, Statement_inserter*)
  4227. {
  4228. if (this->is_lowered_)
  4229. return this;
  4230. Location loc = this->location();
  4231. Block* b = new Block(enclosing, loc);
  4232. go_assert(this->sel_ == NULL);
  4233. Expression* size_expr = Expression::make_integer_ul(this->clauses_->size(),
  4234. NULL, loc);
  4235. Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 1, size_expr);
  4236. this->sel_ = Statement::make_temporary(NULL, call, loc);
  4237. b->add_statement(this->sel_);
  4238. this->clauses_->lower(gogo, function, b, this->sel_);
  4239. this->is_lowered_ = true;
  4240. b->add_statement(this);
  4241. return Statement::make_block_statement(b, loc);
  4242. }
  4243. // Whether the select statement itself may fall through to the following
  4244. // statement.
  4245. bool
  4246. Select_statement::do_may_fall_through() const
  4247. {
  4248. // A select statement is terminating if no break statement
  4249. // refers to it and all of its clauses are terminating.
  4250. if (this->break_label_ != NULL)
  4251. return true;
  4252. return this->clauses_->may_fall_through();
  4253. }
  4254. // Return the backend representation for a select statement.
  4255. Bstatement*
  4256. Select_statement::do_get_backend(Translate_context* context)
  4257. {
  4258. return this->clauses_->get_backend(context, this->sel_, this->break_label(),
  4259. this->location());
  4260. }
  4261. // Dump the AST representation for a select statement.
  4262. void
  4263. Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  4264. {
  4265. ast_dump_context->print_indent();
  4266. ast_dump_context->ostream() << "select";
  4267. if (ast_dump_context->dump_subblocks())
  4268. {
  4269. ast_dump_context->ostream() << " {" << std::endl;
  4270. this->clauses_->dump_clauses(ast_dump_context);
  4271. ast_dump_context->ostream() << "}";
  4272. }
  4273. ast_dump_context->ostream() << std::endl;
  4274. }
  4275. // Make a select statement.
  4276. Select_statement*
  4277. Statement::make_select_statement(Location location)
  4278. {
  4279. return new Select_statement(location);
  4280. }
  4281. // Class For_statement.
  4282. // Traversal.
  4283. int
  4284. For_statement::do_traverse(Traverse* traverse)
  4285. {
  4286. if (this->init_ != NULL)
  4287. {
  4288. if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
  4289. return TRAVERSE_EXIT;
  4290. }
  4291. if (this->cond_ != NULL)
  4292. {
  4293. if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
  4294. return TRAVERSE_EXIT;
  4295. }
  4296. if (this->post_ != NULL)
  4297. {
  4298. if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
  4299. return TRAVERSE_EXIT;
  4300. }
  4301. return this->statements_->traverse(traverse);
  4302. }
  4303. // Lower a For_statement into if statements and gotos. Getting rid of
  4304. // complex statements make it easier to handle garbage collection.
  4305. Statement*
  4306. For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
  4307. Statement_inserter*)
  4308. {
  4309. Statement* s;
  4310. Location loc = this->location();
  4311. Block* b = new Block(enclosing, this->location());
  4312. if (this->init_ != NULL)
  4313. {
  4314. s = Statement::make_block_statement(this->init_,
  4315. this->init_->start_location());
  4316. b->add_statement(s);
  4317. }
  4318. Unnamed_label* entry = NULL;
  4319. if (this->cond_ != NULL)
  4320. {
  4321. entry = new Unnamed_label(this->location());
  4322. b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
  4323. }
  4324. Unnamed_label* top = new Unnamed_label(this->location());
  4325. b->add_statement(Statement::make_unnamed_label_statement(top));
  4326. s = Statement::make_block_statement(this->statements_,
  4327. this->statements_->start_location());
  4328. b->add_statement(s);
  4329. Location end_loc = this->statements_->end_location();
  4330. Unnamed_label* cont = this->continue_label_;
  4331. if (cont != NULL)
  4332. b->add_statement(Statement::make_unnamed_label_statement(cont));
  4333. if (this->post_ != NULL)
  4334. {
  4335. s = Statement::make_block_statement(this->post_,
  4336. this->post_->start_location());
  4337. b->add_statement(s);
  4338. end_loc = this->post_->end_location();
  4339. }
  4340. if (this->cond_ == NULL)
  4341. b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
  4342. else
  4343. {
  4344. b->add_statement(Statement::make_unnamed_label_statement(entry));
  4345. Location cond_loc = this->cond_->location();
  4346. Block* then_block = new Block(b, cond_loc);
  4347. s = Statement::make_goto_unnamed_statement(top, cond_loc);
  4348. then_block->add_statement(s);
  4349. s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
  4350. b->add_statement(s);
  4351. }
  4352. Unnamed_label* brk = this->break_label_;
  4353. if (brk != NULL)
  4354. b->add_statement(Statement::make_unnamed_label_statement(brk));
  4355. b->set_end_location(end_loc);
  4356. return Statement::make_block_statement(b, loc);
  4357. }
  4358. // Return the break label, creating it if necessary.
  4359. Unnamed_label*
  4360. For_statement::break_label()
  4361. {
  4362. if (this->break_label_ == NULL)
  4363. this->break_label_ = new Unnamed_label(this->location());
  4364. return this->break_label_;
  4365. }
  4366. // Return the continue LABEL_EXPR.
  4367. Unnamed_label*
  4368. For_statement::continue_label()
  4369. {
  4370. if (this->continue_label_ == NULL)
  4371. this->continue_label_ = new Unnamed_label(this->location());
  4372. return this->continue_label_;
  4373. }
  4374. // Set the break and continue labels a for statement. This is used
  4375. // when lowering a for range statement.
  4376. void
  4377. For_statement::set_break_continue_labels(Unnamed_label* break_label,
  4378. Unnamed_label* continue_label)
  4379. {
  4380. go_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
  4381. this->break_label_ = break_label;
  4382. this->continue_label_ = continue_label;
  4383. }
  4384. // Whether the overall statement may fall through.
  4385. bool
  4386. For_statement::do_may_fall_through() const
  4387. {
  4388. // A for loop is terminating if it has no condition and
  4389. // no break statement.
  4390. if(this->cond_ != NULL)
  4391. return true;
  4392. if(this->break_label_ != NULL)
  4393. return true;
  4394. return false;
  4395. }
  4396. // Dump the AST representation for a for statement.
  4397. void
  4398. For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  4399. {
  4400. if (this->init_ != NULL && ast_dump_context->dump_subblocks())
  4401. {
  4402. ast_dump_context->print_indent();
  4403. ast_dump_context->indent();
  4404. ast_dump_context->ostream() << "// INIT " << std::endl;
  4405. ast_dump_context->dump_block(this->init_);
  4406. ast_dump_context->unindent();
  4407. }
  4408. ast_dump_context->print_indent();
  4409. ast_dump_context->ostream() << "for ";
  4410. if (this->cond_ != NULL)
  4411. ast_dump_context->dump_expression(this->cond_);
  4412. if (ast_dump_context->dump_subblocks())
  4413. {
  4414. ast_dump_context->ostream() << " {" << std::endl;
  4415. ast_dump_context->dump_block(this->statements_);
  4416. if (this->init_ != NULL)
  4417. {
  4418. ast_dump_context->print_indent();
  4419. ast_dump_context->ostream() << "// POST " << std::endl;
  4420. ast_dump_context->dump_block(this->post_);
  4421. }
  4422. ast_dump_context->unindent();
  4423. ast_dump_context->print_indent();
  4424. ast_dump_context->ostream() << "}";
  4425. }
  4426. ast_dump_context->ostream() << std::endl;
  4427. }
  4428. // Make a for statement.
  4429. For_statement*
  4430. Statement::make_for_statement(Block* init, Expression* cond, Block* post,
  4431. Location location)
  4432. {
  4433. return new For_statement(init, cond, post, location);
  4434. }
  4435. // Class For_range_statement.
  4436. // Traversal.
  4437. int
  4438. For_range_statement::do_traverse(Traverse* traverse)
  4439. {
  4440. if (this->index_var_ != NULL)
  4441. {
  4442. if (this->traverse_expression(traverse, &this->index_var_)
  4443. == TRAVERSE_EXIT)
  4444. return TRAVERSE_EXIT;
  4445. }
  4446. if (this->value_var_ != NULL)
  4447. {
  4448. if (this->traverse_expression(traverse, &this->value_var_)
  4449. == TRAVERSE_EXIT)
  4450. return TRAVERSE_EXIT;
  4451. }
  4452. if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
  4453. return TRAVERSE_EXIT;
  4454. return this->statements_->traverse(traverse);
  4455. }
  4456. // Lower a for range statement. For simplicity we lower this into a
  4457. // for statement, which will then be lowered in turn to goto
  4458. // statements.
  4459. Statement*
  4460. For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
  4461. Statement_inserter*)
  4462. {
  4463. Type* range_type = this->range_->type();
  4464. if (range_type->points_to() != NULL
  4465. && range_type->points_to()->array_type() != NULL
  4466. && !range_type->points_to()->is_slice_type())
  4467. range_type = range_type->points_to();
  4468. Type* index_type;
  4469. Type* value_type = NULL;
  4470. if (range_type->array_type() != NULL)
  4471. {
  4472. index_type = Type::lookup_integer_type("int");
  4473. value_type = range_type->array_type()->element_type();
  4474. }
  4475. else if (range_type->is_string_type())
  4476. {
  4477. index_type = Type::lookup_integer_type("int");
  4478. value_type = Type::lookup_integer_type("int32");
  4479. }
  4480. else if (range_type->map_type() != NULL)
  4481. {
  4482. index_type = range_type->map_type()->key_type();
  4483. value_type = range_type->map_type()->val_type();
  4484. }
  4485. else if (range_type->channel_type() != NULL)
  4486. {
  4487. index_type = range_type->channel_type()->element_type();
  4488. if (this->value_var_ != NULL)
  4489. {
  4490. if (!this->value_var_->type()->is_error())
  4491. this->report_error(_("too many variables for range clause "
  4492. "with channel"));
  4493. return Statement::make_error_statement(this->location());
  4494. }
  4495. }
  4496. else
  4497. {
  4498. this->report_error(_("range clause must have "
  4499. "array, slice, string, map, or channel type"));
  4500. return Statement::make_error_statement(this->location());
  4501. }
  4502. Location loc = this->location();
  4503. Block* temp_block = new Block(enclosing, loc);
  4504. Named_object* range_object = NULL;
  4505. Temporary_statement* range_temp = NULL;
  4506. Var_expression* ve = this->range_->var_expression();
  4507. if (ve != NULL)
  4508. range_object = ve->named_object();
  4509. else
  4510. {
  4511. range_temp = Statement::make_temporary(NULL, this->range_, loc);
  4512. temp_block->add_statement(range_temp);
  4513. this->range_ = NULL;
  4514. }
  4515. Temporary_statement* index_temp = Statement::make_temporary(index_type,
  4516. NULL, loc);
  4517. temp_block->add_statement(index_temp);
  4518. Temporary_statement* value_temp = NULL;
  4519. if (this->value_var_ != NULL)
  4520. {
  4521. value_temp = Statement::make_temporary(value_type, NULL, loc);
  4522. temp_block->add_statement(value_temp);
  4523. }
  4524. Block* body = new Block(temp_block, loc);
  4525. Block* init;
  4526. Expression* cond;
  4527. Block* iter_init;
  4528. Block* post;
  4529. // Arrange to do a loop appropriate for the type. We will produce
  4530. // for INIT ; COND ; POST {
  4531. // ITER_INIT
  4532. // INDEX = INDEX_TEMP
  4533. // VALUE = VALUE_TEMP // If there is a value
  4534. // original statements
  4535. // }
  4536. if (range_type->is_slice_type())
  4537. this->lower_range_slice(gogo, temp_block, body, range_object, range_temp,
  4538. index_temp, value_temp, &init, &cond, &iter_init,
  4539. &post);
  4540. else if (range_type->array_type() != NULL)
  4541. this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
  4542. index_temp, value_temp, &init, &cond, &iter_init,
  4543. &post);
  4544. else if (range_type->is_string_type())
  4545. this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
  4546. index_temp, value_temp, &init, &cond, &iter_init,
  4547. &post);
  4548. else if (range_type->map_type() != NULL)
  4549. this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
  4550. index_temp, value_temp, &init, &cond, &iter_init,
  4551. &post);
  4552. else if (range_type->channel_type() != NULL)
  4553. this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
  4554. index_temp, value_temp, &init, &cond, &iter_init,
  4555. &post);
  4556. else
  4557. go_unreachable();
  4558. if (iter_init != NULL)
  4559. body->add_statement(Statement::make_block_statement(iter_init, loc));
  4560. if (this->index_var_ != NULL)
  4561. {
  4562. Statement* assign;
  4563. Expression* index_ref =
  4564. Expression::make_temporary_reference(index_temp, loc);
  4565. if (this->value_var_ == NULL)
  4566. assign = Statement::make_assignment(this->index_var_, index_ref, loc);
  4567. else
  4568. {
  4569. Expression_list* lhs = new Expression_list();
  4570. lhs->push_back(this->index_var_);
  4571. lhs->push_back(this->value_var_);
  4572. Expression_list* rhs = new Expression_list();
  4573. rhs->push_back(index_ref);
  4574. rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
  4575. assign = Statement::make_tuple_assignment(lhs, rhs, loc);
  4576. }
  4577. body->add_statement(assign);
  4578. }
  4579. body->add_statement(Statement::make_block_statement(this->statements_, loc));
  4580. body->set_end_location(this->statements_->end_location());
  4581. For_statement* loop = Statement::make_for_statement(init, cond, post,
  4582. this->location());
  4583. loop->add_statements(body);
  4584. loop->set_break_continue_labels(this->break_label_, this->continue_label_);
  4585. temp_block->add_statement(loop);
  4586. return Statement::make_block_statement(temp_block, loc);
  4587. }
  4588. // Return a reference to the range, which may be in RANGE_OBJECT or in
  4589. // RANGE_TEMP.
  4590. Expression*
  4591. For_range_statement::make_range_ref(Named_object* range_object,
  4592. Temporary_statement* range_temp,
  4593. Location loc)
  4594. {
  4595. if (range_object != NULL)
  4596. return Expression::make_var_reference(range_object, loc);
  4597. else
  4598. return Expression::make_temporary_reference(range_temp, loc);
  4599. }
  4600. // Return a call to the predeclared function FUNCNAME passing a
  4601. // reference to the temporary variable ARG.
  4602. Expression*
  4603. For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
  4604. Expression* arg,
  4605. Location loc)
  4606. {
  4607. Named_object* no = gogo->lookup_global(funcname);
  4608. go_assert(no != NULL && no->is_function_declaration());
  4609. Expression* func = Expression::make_func_reference(no, NULL, loc);
  4610. Expression_list* params = new Expression_list();
  4611. params->push_back(arg);
  4612. return Expression::make_call(func, params, false, loc);
  4613. }
  4614. // Lower a for range over an array.
  4615. void
  4616. For_range_statement::lower_range_array(Gogo* gogo,
  4617. Block* enclosing,
  4618. Block* body_block,
  4619. Named_object* range_object,
  4620. Temporary_statement* range_temp,
  4621. Temporary_statement* index_temp,
  4622. Temporary_statement* value_temp,
  4623. Block** pinit,
  4624. Expression** pcond,
  4625. Block** piter_init,
  4626. Block** ppost)
  4627. {
  4628. Location loc = this->location();
  4629. // The loop we generate:
  4630. // len_temp := len(range)
  4631. // range_temp := range
  4632. // for index_temp = 0; index_temp < len_temp; index_temp++ {
  4633. // value_temp = range_temp[index_temp]
  4634. // index = index_temp
  4635. // value = value_temp
  4636. // original body
  4637. // }
  4638. // Set *PINIT to
  4639. // var len_temp int
  4640. // len_temp = len(range)
  4641. // index_temp = 0
  4642. Block* init = new Block(enclosing, loc);
  4643. Expression* ref = this->make_range_ref(range_object, range_temp, loc);
  4644. range_temp = Statement::make_temporary(NULL, ref, loc);
  4645. Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
  4646. Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
  4647. len_call, loc);
  4648. init->add_statement(range_temp);
  4649. init->add_statement(len_temp);
  4650. Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
  4651. Temporary_reference_expression* tref =
  4652. Expression::make_temporary_reference(index_temp, loc);
  4653. tref->set_is_lvalue();
  4654. Statement* s = Statement::make_assignment(tref, zexpr, loc);
  4655. init->add_statement(s);
  4656. *pinit = init;
  4657. // Set *PCOND to
  4658. // index_temp < len_temp
  4659. ref = Expression::make_temporary_reference(index_temp, loc);
  4660. Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
  4661. Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
  4662. *pcond = lt;
  4663. // Set *PITER_INIT to
  4664. // value_temp = range[index_temp]
  4665. Block* iter_init = NULL;
  4666. if (value_temp != NULL)
  4667. {
  4668. iter_init = new Block(body_block, loc);
  4669. ref = Expression::make_temporary_reference(range_temp, loc);
  4670. Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
  4671. Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
  4672. tref = Expression::make_temporary_reference(value_temp, loc);
  4673. tref->set_is_lvalue();
  4674. s = Statement::make_assignment(tref, index, loc);
  4675. iter_init->add_statement(s);
  4676. }
  4677. *piter_init = iter_init;
  4678. // Set *PPOST to
  4679. // index_temp++
  4680. Block* post = new Block(enclosing, loc);
  4681. tref = Expression::make_temporary_reference(index_temp, loc);
  4682. tref->set_is_lvalue();
  4683. s = Statement::make_inc_statement(tref);
  4684. post->add_statement(s);
  4685. *ppost = post;
  4686. }
  4687. // Lower a for range over a slice.
  4688. void
  4689. For_range_statement::lower_range_slice(Gogo* gogo,
  4690. Block* enclosing,
  4691. Block* body_block,
  4692. Named_object* range_object,
  4693. Temporary_statement* range_temp,
  4694. Temporary_statement* index_temp,
  4695. Temporary_statement* value_temp,
  4696. Block** pinit,
  4697. Expression** pcond,
  4698. Block** piter_init,
  4699. Block** ppost)
  4700. {
  4701. Location loc = this->location();
  4702. // The loop we generate:
  4703. // for_temp := range
  4704. // len_temp := len(for_temp)
  4705. // for index_temp = 0; index_temp < len_temp; index_temp++ {
  4706. // value_temp = for_temp[index_temp]
  4707. // index = index_temp
  4708. // value = value_temp
  4709. // original body
  4710. // }
  4711. //
  4712. // Using for_temp means that we don't need to check bounds when
  4713. // fetching range_temp[index_temp].
  4714. // Set *PINIT to
  4715. // range_temp := range
  4716. // var len_temp int
  4717. // len_temp = len(range_temp)
  4718. // index_temp = 0
  4719. Block* init = new Block(enclosing, loc);
  4720. Expression* ref = this->make_range_ref(range_object, range_temp, loc);
  4721. Temporary_statement* for_temp = Statement::make_temporary(NULL, ref, loc);
  4722. init->add_statement(for_temp);
  4723. ref = Expression::make_temporary_reference(for_temp, loc);
  4724. Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
  4725. Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
  4726. len_call, loc);
  4727. init->add_statement(len_temp);
  4728. Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
  4729. Temporary_reference_expression* tref =
  4730. Expression::make_temporary_reference(index_temp, loc);
  4731. tref->set_is_lvalue();
  4732. Statement* s = Statement::make_assignment(tref, zexpr, loc);
  4733. init->add_statement(s);
  4734. *pinit = init;
  4735. // Set *PCOND to
  4736. // index_temp < len_temp
  4737. ref = Expression::make_temporary_reference(index_temp, loc);
  4738. Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
  4739. Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
  4740. *pcond = lt;
  4741. // Set *PITER_INIT to
  4742. // value_temp = range[index_temp]
  4743. Block* iter_init = NULL;
  4744. if (value_temp != NULL)
  4745. {
  4746. iter_init = new Block(body_block, loc);
  4747. ref = Expression::make_temporary_reference(for_temp, loc);
  4748. Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
  4749. Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
  4750. tref = Expression::make_temporary_reference(value_temp, loc);
  4751. tref->set_is_lvalue();
  4752. s = Statement::make_assignment(tref, index, loc);
  4753. iter_init->add_statement(s);
  4754. }
  4755. *piter_init = iter_init;
  4756. // Set *PPOST to
  4757. // index_temp++
  4758. Block* post = new Block(enclosing, loc);
  4759. tref = Expression::make_temporary_reference(index_temp, loc);
  4760. tref->set_is_lvalue();
  4761. s = Statement::make_inc_statement(tref);
  4762. post->add_statement(s);
  4763. *ppost = post;
  4764. }
  4765. // Lower a for range over a string.
  4766. void
  4767. For_range_statement::lower_range_string(Gogo*,
  4768. Block* enclosing,
  4769. Block* body_block,
  4770. Named_object* range_object,
  4771. Temporary_statement* range_temp,
  4772. Temporary_statement* index_temp,
  4773. Temporary_statement* value_temp,
  4774. Block** pinit,
  4775. Expression** pcond,
  4776. Block** piter_init,
  4777. Block** ppost)
  4778. {
  4779. Location loc = this->location();
  4780. // The loop we generate:
  4781. // var next_index_temp int
  4782. // for index_temp = 0; ; index_temp = next_index_temp {
  4783. // next_index_temp, value_temp = stringiter2(range, index_temp)
  4784. // if next_index_temp == 0 {
  4785. // break
  4786. // }
  4787. // index = index_temp
  4788. // value = value_temp
  4789. // original body
  4790. // }
  4791. // Set *PINIT to
  4792. // var next_index_temp int
  4793. // index_temp = 0
  4794. Block* init = new Block(enclosing, loc);
  4795. Temporary_statement* next_index_temp =
  4796. Statement::make_temporary(index_temp->type(), NULL, loc);
  4797. init->add_statement(next_index_temp);
  4798. Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
  4799. Temporary_reference_expression* ref =
  4800. Expression::make_temporary_reference(index_temp, loc);
  4801. ref->set_is_lvalue();
  4802. Statement* s = Statement::make_assignment(ref, zexpr, loc);
  4803. init->add_statement(s);
  4804. *pinit = init;
  4805. // The loop has no condition.
  4806. *pcond = NULL;
  4807. // Set *PITER_INIT to
  4808. // next_index_temp = runtime.stringiter(range, index_temp)
  4809. // or
  4810. // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
  4811. // followed by
  4812. // if next_index_temp == 0 {
  4813. // break
  4814. // }
  4815. Block* iter_init = new Block(body_block, loc);
  4816. Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
  4817. Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
  4818. Call_expression* call = Runtime::make_call((value_temp == NULL
  4819. ? Runtime::STRINGITER
  4820. : Runtime::STRINGITER2),
  4821. loc, 2, p1, p2);
  4822. if (value_temp == NULL)
  4823. {
  4824. ref = Expression::make_temporary_reference(next_index_temp, loc);
  4825. ref->set_is_lvalue();
  4826. s = Statement::make_assignment(ref, call, loc);
  4827. }
  4828. else
  4829. {
  4830. Expression_list* lhs = new Expression_list();
  4831. ref = Expression::make_temporary_reference(next_index_temp, loc);
  4832. ref->set_is_lvalue();
  4833. lhs->push_back(ref);
  4834. ref = Expression::make_temporary_reference(value_temp, loc);
  4835. ref->set_is_lvalue();
  4836. lhs->push_back(ref);
  4837. Expression_list* rhs = new Expression_list();
  4838. rhs->push_back(Expression::make_call_result(call, 0));
  4839. rhs->push_back(Expression::make_call_result(call, 1));
  4840. s = Statement::make_tuple_assignment(lhs, rhs, loc);
  4841. }
  4842. iter_init->add_statement(s);
  4843. ref = Expression::make_temporary_reference(next_index_temp, loc);
  4844. zexpr = Expression::make_integer_ul(0, NULL, loc);
  4845. Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
  4846. Block* then_block = new Block(iter_init, loc);
  4847. s = Statement::make_break_statement(this->break_label(), loc);
  4848. then_block->add_statement(s);
  4849. s = Statement::make_if_statement(equals, then_block, NULL, loc);
  4850. iter_init->add_statement(s);
  4851. *piter_init = iter_init;
  4852. // Set *PPOST to
  4853. // index_temp = next_index_temp
  4854. Block* post = new Block(enclosing, loc);
  4855. Temporary_reference_expression* lhs =
  4856. Expression::make_temporary_reference(index_temp, loc);
  4857. lhs->set_is_lvalue();
  4858. Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
  4859. s = Statement::make_assignment(lhs, rhs, loc);
  4860. post->add_statement(s);
  4861. *ppost = post;
  4862. }
  4863. // Lower a for range over a map.
  4864. void
  4865. For_range_statement::lower_range_map(Gogo*,
  4866. Block* enclosing,
  4867. Block* body_block,
  4868. Named_object* range_object,
  4869. Temporary_statement* range_temp,
  4870. Temporary_statement* index_temp,
  4871. Temporary_statement* value_temp,
  4872. Block** pinit,
  4873. Expression** pcond,
  4874. Block** piter_init,
  4875. Block** ppost)
  4876. {
  4877. Location loc = this->location();
  4878. // The runtime uses a struct to handle ranges over a map. The
  4879. // struct is four pointers long. The first pointer is NULL when we
  4880. // have completed the iteration.
  4881. // The loop we generate:
  4882. // var hiter map_iteration_struct
  4883. // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
  4884. // mapiter2(hiter, &index_temp, &value_temp)
  4885. // index = index_temp
  4886. // value = value_temp
  4887. // original body
  4888. // }
  4889. // Set *PINIT to
  4890. // var hiter map_iteration_struct
  4891. // runtime.mapiterinit(range, &hiter)
  4892. Block* init = new Block(enclosing, loc);
  4893. Type* map_iteration_type = Runtime::map_iteration_type();
  4894. Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
  4895. NULL, loc);
  4896. init->add_statement(hiter);
  4897. Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
  4898. Expression* ref = Expression::make_temporary_reference(hiter, loc);
  4899. Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
  4900. Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
  4901. init->add_statement(Statement::make_statement(call, true));
  4902. *pinit = init;
  4903. // Set *PCOND to
  4904. // hiter[0] != nil
  4905. ref = Expression::make_temporary_reference(hiter, loc);
  4906. Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
  4907. Expression* index = Expression::make_index(ref, zexpr, NULL, NULL, loc);
  4908. Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
  4909. Expression::make_nil(loc),
  4910. loc);
  4911. *pcond = ne;
  4912. // Set *PITER_INIT to
  4913. // mapiter1(hiter, &index_temp)
  4914. // or
  4915. // mapiter2(hiter, &index_temp, &value_temp)
  4916. Block* iter_init = new Block(body_block, loc);
  4917. ref = Expression::make_temporary_reference(hiter, loc);
  4918. p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
  4919. ref = Expression::make_temporary_reference(index_temp, loc);
  4920. p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
  4921. if (value_temp == NULL)
  4922. call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
  4923. else
  4924. {
  4925. ref = Expression::make_temporary_reference(value_temp, loc);
  4926. Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
  4927. call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
  4928. }
  4929. iter_init->add_statement(Statement::make_statement(call, true));
  4930. *piter_init = iter_init;
  4931. // Set *PPOST to
  4932. // mapiternext(&hiter)
  4933. Block* post = new Block(enclosing, loc);
  4934. ref = Expression::make_temporary_reference(hiter, loc);
  4935. p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
  4936. call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
  4937. post->add_statement(Statement::make_statement(call, true));
  4938. *ppost = post;
  4939. }
  4940. // Lower a for range over a channel.
  4941. void
  4942. For_range_statement::lower_range_channel(Gogo*,
  4943. Block*,
  4944. Block* body_block,
  4945. Named_object* range_object,
  4946. Temporary_statement* range_temp,
  4947. Temporary_statement* index_temp,
  4948. Temporary_statement* value_temp,
  4949. Block** pinit,
  4950. Expression** pcond,
  4951. Block** piter_init,
  4952. Block** ppost)
  4953. {
  4954. go_assert(value_temp == NULL);
  4955. Location loc = this->location();
  4956. // The loop we generate:
  4957. // for {
  4958. // index_temp, ok_temp = <-range
  4959. // if !ok_temp {
  4960. // break
  4961. // }
  4962. // index = index_temp
  4963. // original body
  4964. // }
  4965. // We have no initialization code, no condition, and no post code.
  4966. *pinit = NULL;
  4967. *pcond = NULL;
  4968. *ppost = NULL;
  4969. // Set *PITER_INIT to
  4970. // index_temp, ok_temp = <-range
  4971. // if !ok_temp {
  4972. // break
  4973. // }
  4974. Block* iter_init = new Block(body_block, loc);
  4975. Temporary_statement* ok_temp =
  4976. Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
  4977. iter_init->add_statement(ok_temp);
  4978. Expression* cref = this->make_range_ref(range_object, range_temp, loc);
  4979. Temporary_reference_expression* iref =
  4980. Expression::make_temporary_reference(index_temp, loc);
  4981. iref->set_is_lvalue();
  4982. Temporary_reference_expression* oref =
  4983. Expression::make_temporary_reference(ok_temp, loc);
  4984. oref->set_is_lvalue();
  4985. Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
  4986. loc);
  4987. iter_init->add_statement(s);
  4988. Block* then_block = new Block(iter_init, loc);
  4989. s = Statement::make_break_statement(this->break_label(), loc);
  4990. then_block->add_statement(s);
  4991. oref = Expression::make_temporary_reference(ok_temp, loc);
  4992. Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
  4993. s = Statement::make_if_statement(cond, then_block, NULL, loc);
  4994. iter_init->add_statement(s);
  4995. *piter_init = iter_init;
  4996. }
  4997. // Return the break LABEL_EXPR.
  4998. Unnamed_label*
  4999. For_range_statement::break_label()
  5000. {
  5001. if (this->break_label_ == NULL)
  5002. this->break_label_ = new Unnamed_label(this->location());
  5003. return this->break_label_;
  5004. }
  5005. // Return the continue LABEL_EXPR.
  5006. Unnamed_label*
  5007. For_range_statement::continue_label()
  5008. {
  5009. if (this->continue_label_ == NULL)
  5010. this->continue_label_ = new Unnamed_label(this->location());
  5011. return this->continue_label_;
  5012. }
  5013. // Dump the AST representation for a for range statement.
  5014. void
  5015. For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
  5016. {
  5017. ast_dump_context->print_indent();
  5018. ast_dump_context->ostream() << "for ";
  5019. ast_dump_context->dump_expression(this->index_var_);
  5020. if (this->value_var_ != NULL)
  5021. {
  5022. ast_dump_context->ostream() << ", ";
  5023. ast_dump_context->dump_expression(this->value_var_);
  5024. }
  5025. ast_dump_context->ostream() << " = range ";
  5026. ast_dump_context->dump_expression(this->range_);
  5027. if (ast_dump_context->dump_subblocks())
  5028. {
  5029. ast_dump_context->ostream() << " {" << std::endl;
  5030. ast_dump_context->indent();
  5031. ast_dump_context->dump_block(this->statements_);
  5032. ast_dump_context->unindent();
  5033. ast_dump_context->print_indent();
  5034. ast_dump_context->ostream() << "}";
  5035. }
  5036. ast_dump_context->ostream() << std::endl;
  5037. }
  5038. // Make a for statement with a range clause.
  5039. For_range_statement*
  5040. Statement::make_for_range_statement(Expression* index_var,
  5041. Expression* value_var,
  5042. Expression* range,
  5043. Location location)
  5044. {
  5045. return new For_range_statement(index_var, value_var, range, location);
  5046. }