symdef.pas 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:asizeint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function OwnerHierarchyName: string; override;
  69. function in_currentunit: boolean;
  70. { regvars }
  71. function is_intregable : boolean;
  72. function is_fpuregable : boolean;
  73. { generics }
  74. procedure initgeneric;
  75. private
  76. savesize : asizeuint;
  77. end;
  78. tfiletyp = (ft_text,ft_typed,ft_untyped);
  79. tfiledef = class(tstoreddef)
  80. filetyp : tfiletyp;
  81. typedfiledef : tdef;
  82. typedfiledefderef : tderef;
  83. constructor createtext;
  84. constructor createuntyped;
  85. constructor createtyped(def : tdef);
  86. constructor ppuload(ppufile:tcompilerppufile);
  87. function getcopy : tstoreddef;override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderef;override;
  90. procedure deref;override;
  91. function GetTypeName:string;override;
  92. function getmangledparaname:TSymStr;override;
  93. procedure setsize;
  94. end;
  95. tvariantdef = class(tstoreddef)
  96. varianttype : tvarianttype;
  97. constructor create(v : tvarianttype);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. function getcopy : tstoreddef;override;
  100. function GetTypeName:string;override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. function getvardef:longint;override;
  103. procedure setsize;
  104. function is_publishable : boolean;override;
  105. function needs_inittable : boolean;override;
  106. end;
  107. tformaldef = class(tstoreddef)
  108. typed:boolean;
  109. constructor create(Atyped:boolean);
  110. constructor ppuload(ppufile:tcompilerppufile);
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. function GetTypeName:string;override;
  113. end;
  114. tforwarddef = class(tstoreddef)
  115. tosymname : pshortstring;
  116. forwardpos : tfileposinfo;
  117. constructor create(const s:string;const pos:tfileposinfo);
  118. destructor destroy;override;
  119. function getcopy:tstoreddef;override;
  120. function GetTypeName:string;override;
  121. end;
  122. tundefineddef = class(tstoreddef)
  123. constructor create;
  124. constructor ppuload(ppufile:tcompilerppufile);
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. function GetTypeName:string;override;
  127. end;
  128. terrordef = class(tstoreddef)
  129. constructor create;
  130. procedure ppuwrite(ppufile:tcompilerppufile);override;
  131. function GetTypeName:string;override;
  132. function getmangledparaname : TSymStr;override;
  133. end;
  134. tabstractpointerdef = class(tstoreddef)
  135. pointeddef : tdef;
  136. pointeddefderef : tderef;
  137. constructor create(dt:tdeftyp;def:tdef);
  138. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. procedure buildderef;override;
  141. procedure deref;override;
  142. end;
  143. tpointerdef = class(tabstractpointerdef)
  144. is_far : boolean;
  145. has_pointer_math : boolean;
  146. constructor create(def:tdef);
  147. constructor createfar(def:tdef);
  148. function getcopy:tstoreddef;override;
  149. constructor ppuload(ppufile:tcompilerppufile);
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function GetTypeName:string;override;
  152. end;
  153. tprocdef = class;
  154. { tabstractrecorddef }
  155. tabstractrecorddef= class(tstoreddef)
  156. objname,
  157. objrealname : PShortString;
  158. { for C++ classes: name of the library this class is imported from }
  159. { for Java classes/records: package name }
  160. import_lib : PShortString;
  161. symtable : TSymtable;
  162. cloneddef : tabstractrecorddef;
  163. cloneddefderef : tderef;
  164. objectoptions : tobjectoptions;
  165. { for targets that initialise typed constants via explicit assignments
  166. instead of by generating an initialised data sectino }
  167. tcinitcode : tnode;
  168. constructor create(const n:string; dt:tdeftyp);
  169. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  170. procedure ppuwrite(ppufile:tcompilerppufile);override;
  171. destructor destroy; override;
  172. procedure check_forwards; virtual;
  173. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  174. function GetSymtable(t:tGetSymtable):TSymtable;override;
  175. function is_packed:boolean;
  176. function RttiName: string;
  177. { enumerator support }
  178. function search_enumerator_get: tprocdef; virtual;
  179. function search_enumerator_move: tprocdef; virtual;
  180. function search_enumerator_current: tsym; virtual;
  181. { JVM }
  182. function jvm_full_typename(with_package_name: boolean): string;
  183. end;
  184. trecorddef = class(tabstractrecorddef)
  185. public
  186. isunion : boolean;
  187. constructor create(const n:string; p:TSymtable);
  188. constructor ppuload(ppufile:tcompilerppufile);
  189. destructor destroy;override;
  190. function getcopy : tstoreddef;override;
  191. procedure ppuwrite(ppufile:tcompilerppufile);override;
  192. procedure buildderef;override;
  193. procedure deref;override;
  194. function size:asizeint;override;
  195. function alignment : shortint;override;
  196. function padalignment: shortint;
  197. function GetTypeName:string;override;
  198. { debug }
  199. function needs_inittable : boolean;override;
  200. { jvm }
  201. function is_related(d : tdef) : boolean;override;
  202. end;
  203. tobjectdef = class;
  204. { TImplementedInterface }
  205. TImplementedInterface = class
  206. IntfDef : tobjectdef;
  207. IntfDefDeref : tderef;
  208. IType : tinterfaceentrytype;
  209. IOffset : longint;
  210. VtblImplIntf : TImplementedInterface;
  211. NameMappings : TFPHashList;
  212. ProcDefs : TFPObjectList;
  213. ImplementsGetter : tsym;
  214. constructor create(aintf: tobjectdef);
  215. constructor create_deref(d:tderef);
  216. destructor destroy; override;
  217. function getcopy:TImplementedInterface;
  218. procedure buildderef;
  219. procedure deref;
  220. procedure AddMapping(const origname, newname: string);
  221. function GetMapping(const origname: string):string;
  222. procedure AddImplProc(pd:tprocdef);
  223. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  224. end;
  225. { tvmtentry }
  226. tvmtentry = record
  227. procdef : tprocdef;
  228. procdefderef : tderef;
  229. visibility : tvisibility;
  230. end;
  231. pvmtentry = ^tvmtentry;
  232. { tobjectdef }
  233. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  234. pmvcallstaticinfo = ^tmvcallstaticinfo;
  235. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  236. tobjectdef = class(tabstractrecorddef)
  237. private
  238. fcurrent_dispid: longint;
  239. public
  240. dwarf_struct_lab : tasmsymbol;
  241. childof : tobjectdef;
  242. childofderef : tderef;
  243. { for Object Pascal helpers }
  244. extendeddef : tabstractrecorddef;
  245. extendeddefderef: tderef;
  246. { for Objective-C: protocols and classes can have the same name there }
  247. objextname : pshortstring;
  248. { to be able to have a variable vmt position }
  249. { and no vmt field for objects without virtuals }
  250. vmtentries : TFPList;
  251. vmcallstaticinfo : pmvcallstaticinfo;
  252. vmt_offset : longint;
  253. iidguid : pguid;
  254. iidstr : pshortstring;
  255. { store implemented interfaces defs and name mappings }
  256. ImplementedInterfaces : TFPObjectList;
  257. { number of abstract methods (used by JVM target to determine whether
  258. or not the class should be marked as abstract: must be done if 1 or
  259. more abstract methods) }
  260. abstractcnt : longint;
  261. writing_class_record_dbginfo,
  262. { a class of this type has been created in this module }
  263. created_in_current_module,
  264. { a loadvmtnode for this class has been created in this
  265. module, so if a classrefdef variable of this or a parent
  266. class is used somewhere to instantiate a class, then this
  267. class may be instantiated
  268. }
  269. maybe_created_in_current_module,
  270. { a "class of" this particular class has been created in
  271. this module
  272. }
  273. classref_created_in_current_module : boolean;
  274. objecttype : tobjecttyp;
  275. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  276. constructor ppuload(ppufile:tcompilerppufile);
  277. destructor destroy;override;
  278. function getcopy : tstoreddef;override;
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;
  280. function GetTypeName:string;override;
  281. procedure buildderef;override;
  282. procedure deref;override;
  283. procedure buildderefimpl;override;
  284. procedure derefimpl;override;
  285. procedure resetvmtentries;
  286. procedure copyvmtentries(objdef:tobjectdef);
  287. function getparentdef:tdef;override;
  288. function size : asizeint;override;
  289. function alignment:shortint;override;
  290. function vmtmethodoffset(index:longint):longint;
  291. function members_need_inittable : boolean;
  292. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  293. { this should be called when this class implements an interface }
  294. procedure prepareguid;
  295. function is_publishable : boolean;override;
  296. function is_related(d : tdef) : boolean;override;
  297. function needs_inittable : boolean;override;
  298. function rtti_mangledname(rt:trttitype):string;override;
  299. function vmt_mangledname : TSymStr;
  300. procedure check_forwards; override;
  301. procedure insertvmt;
  302. procedure set_parent(c : tobjectdef);
  303. function find_destructor: tprocdef;
  304. function implements_any_interfaces: boolean;
  305. { dispinterface support }
  306. function get_next_dispid: longint;
  307. { enumerator support }
  308. function search_enumerator_get: tprocdef; override;
  309. function search_enumerator_move: tprocdef; override;
  310. function search_enumerator_current: tsym; override;
  311. { WPO }
  312. procedure register_created_object_type;override;
  313. procedure register_maybe_created_object_type;
  314. procedure register_created_classref_type;
  315. procedure register_vmt_call(index:longint);
  316. { ObjC }
  317. procedure finish_objc_data;
  318. function check_objc_types: boolean;
  319. { C++ }
  320. procedure finish_cpp_data;
  321. end;
  322. tclassrefdef = class(tabstractpointerdef)
  323. constructor create(def:tdef);
  324. constructor ppuload(ppufile:tcompilerppufile);
  325. procedure ppuwrite(ppufile:tcompilerppufile);override;
  326. function getcopy:tstoreddef;override;
  327. function GetTypeName:string;override;
  328. function is_publishable : boolean;override;
  329. function rtti_mangledname(rt:trttitype):string;override;
  330. procedure register_created_object_type;override;
  331. end;
  332. tarraydef = class(tstoreddef)
  333. lowrange,
  334. highrange : asizeint;
  335. rangedef : tdef;
  336. rangedefderef : tderef;
  337. arrayoptions : tarraydefoptions;
  338. symtable : TSymtable;
  339. protected
  340. _elementdef : tdef;
  341. _elementdefderef : tderef;
  342. procedure setelementdef(def:tdef);
  343. public
  344. function elesize : asizeint;
  345. function elepackedbitsize : asizeint;
  346. function elecount : asizeuint;
  347. constructor create_from_pointer(def:tdef);
  348. constructor create(l,h:asizeint;def:tdef);
  349. constructor ppuload(ppufile:tcompilerppufile);
  350. destructor destroy; override;
  351. function getcopy : tstoreddef;override;
  352. procedure ppuwrite(ppufile:tcompilerppufile);override;
  353. function GetTypeName:string;override;
  354. function getmangledparaname : TSymStr;override;
  355. procedure buildderef;override;
  356. procedure deref;override;
  357. function size : asizeint;override;
  358. function alignment : shortint;override;
  359. { returns the label of the range check string }
  360. function needs_inittable : boolean;override;
  361. property elementdef : tdef read _elementdef write setelementdef;
  362. function is_publishable : boolean;override;
  363. end;
  364. torddef = class(tstoreddef)
  365. low,high : TConstExprInt;
  366. ordtype : tordtype;
  367. constructor create(t : tordtype;v,b : TConstExprInt);
  368. constructor ppuload(ppufile:tcompilerppufile);
  369. function getcopy : tstoreddef;override;
  370. procedure ppuwrite(ppufile:tcompilerppufile);override;
  371. function is_publishable : boolean;override;
  372. function GetTypeName:string;override;
  373. function alignment:shortint;override;
  374. procedure setsize;
  375. function packedbitsize: asizeint; override;
  376. function getvardef : longint;override;
  377. end;
  378. tfloatdef = class(tstoreddef)
  379. floattype : tfloattype;
  380. constructor create(t : tfloattype);
  381. constructor ppuload(ppufile:tcompilerppufile);
  382. function getcopy : tstoreddef;override;
  383. procedure ppuwrite(ppufile:tcompilerppufile);override;
  384. function GetTypeName:string;override;
  385. function is_publishable : boolean;override;
  386. function alignment:shortint;override;
  387. procedure setsize;
  388. function getvardef:longint;override;
  389. end;
  390. { tabstractprocdef }
  391. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  392. pno_ownername, pno_noclassmarker, pno_noleadingdollar);
  393. tprocnameoptions = set of tprocnameoption;
  394. tproccopytyp = (pc_normal,
  395. { always creates a top-level function, removes all
  396. special parameters (self, vmt, parentfp, ...) }
  397. pc_bareproc
  398. );
  399. tabstractprocdef = class(tstoreddef)
  400. { saves a definition to the return type }
  401. returndef : tdef;
  402. returndefderef : tderef;
  403. parast : TSymtable;
  404. paras : tparalist;
  405. proctypeoption : tproctypeoption;
  406. proccalloption : tproccalloption;
  407. procoptions : tprocoptions;
  408. callerargareasize,
  409. calleeargareasize: pint;
  410. {$ifdef m68k}
  411. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  412. {$endif}
  413. funcretloc : array[tcallercallee] of TCGPara;
  414. has_paraloc_info : tcallercallee; { paraloc info is available }
  415. { number of user visible parameters }
  416. maxparacount,
  417. minparacount : byte;
  418. constructor create(dt:tdeftyp;level:byte);
  419. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  420. destructor destroy;override;
  421. procedure ppuwrite(ppufile:tcompilerppufile);override;
  422. procedure buildderef;override;
  423. procedure deref;override;
  424. procedure calcparas;
  425. function typename_paras(pno: tprocnameoptions): ansistring;
  426. function is_methodpointer:boolean;virtual;
  427. function is_addressonly:boolean;virtual;
  428. function no_self_node:boolean;
  429. { get either a copy as a procdef or procvardef }
  430. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; virtual;
  431. procedure check_mark_as_nested;
  432. procedure init_paraloc_info(side: tcallercallee);
  433. function stack_tainting_parameter(side: tcallercallee): boolean;
  434. private
  435. procedure count_para(p:TObject;arg:pointer);
  436. procedure insert_para(p:TObject;arg:pointer);
  437. end;
  438. tprocvardef = class(tabstractprocdef)
  439. {$ifdef jvm}
  440. { class representing this procvar on the Java side }
  441. classdef : tobjectdef;
  442. classdefderef : tderef;
  443. {$endif}
  444. constructor create(level:byte);
  445. constructor ppuload(ppufile:tcompilerppufile);
  446. function getcopy : tstoreddef;override;
  447. procedure ppuwrite(ppufile:tcompilerppufile);override;
  448. {$ifdef jvm}
  449. procedure buildderef;override;
  450. procedure deref;override;
  451. {$endif}
  452. function GetSymtable(t:tGetSymtable):TSymtable;override;
  453. function size : asizeint;override;
  454. function GetTypeName:string;override;
  455. function is_publishable : boolean;override;
  456. function is_methodpointer:boolean;override;
  457. function is_addressonly:boolean;override;
  458. function getmangledparaname:TSymStr;override;
  459. end;
  460. tmessageinf = record
  461. case integer of
  462. 0 : (str : pshortstring);
  463. 1 : (i : longint);
  464. end;
  465. tinlininginfo = record
  466. { node tree }
  467. code : tnode;
  468. flags : tprocinfoflags;
  469. end;
  470. pinlininginfo = ^tinlininginfo;
  471. { kinds of synthetic procdefs that can be generated }
  472. tsynthetickind = (
  473. tsk_none,
  474. tsk_anon_inherited, // anonymous inherited call
  475. tsk_jvm_clone, // Java-style clone method
  476. tsk_record_deepcopy, // deepcopy for records field by field
  477. tsk_record_initialize, // initialize for records field by field (explicit rather than via rtti)
  478. tsk_empty, // an empty routine
  479. tsk_tcinit, // initialisation of typed constants
  480. tsk_callthrough, // call through to another routine with the same parameters/return type (its def is stored in the skpara field)
  481. tsk_jvm_enum_values, // Java "values" class method of JLEnum descendants
  482. tsk_jvm_enum_valueof, // Java "valueOf" class method of JLEnum descendants
  483. tsk_jvm_enum_classconstr, // Java class constructor for JLEnum descendants
  484. tsk_jvm_enum_jumps_constr, // Java constructor for JLEnum descendants for enums with jumps
  485. tsk_jvm_enum_fpcordinal, // Java FPCOrdinal function that returns the enum's ordinal value from an FPC POV
  486. tsk_jvm_enum_fpcvalueof, // Java FPCValueOf function that returns the enum instance corresponding to an ordinal from an FPC POV
  487. tsk_jvm_enum_long2set, // Java fpcLongToEnumSet function that returns an enumset corresponding to a bit pattern in a jlong
  488. tsk_jvm_enum_bitset2set, // Java fpcBitSetToEnumSet function that returns an enumset corresponding to a BitSet
  489. tsk_jvm_enum_set2Set, // Java fpcEnumSetToEnumSet function that returns an enumset corresponding to another enumset (different enum kind)
  490. tsk_jvm_procvar_invoke, // Java invoke method that calls a wrapped procvar
  491. tsk_jvm_virtual_clmethod // Java wrapper for virtual class method
  492. );
  493. {$ifdef oldregvars}
  494. { register variables }
  495. pregvarinfo = ^tregvarinfo;
  496. tregvarinfo = record
  497. regvars : array[1..maxvarregs] of tsym;
  498. regvars_para : array[1..maxvarregs] of boolean;
  499. regvars_refs : array[1..maxvarregs] of longint;
  500. fpuregvars : array[1..maxfpuvarregs] of tsym;
  501. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  502. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  503. end;
  504. {$endif oldregvars}
  505. { tprocdef }
  506. tprocdef = class(tabstractprocdef)
  507. private
  508. {$ifdef symansistr}
  509. _mangledname : ansistring;
  510. {$else symansistr}
  511. _mangledname : pshortstring;
  512. {$endif}
  513. public
  514. messageinf : tmessageinf;
  515. dispid : longint;
  516. {$ifndef EXTDEBUG}
  517. { where is this function defined and what were the symbol
  518. flags, needed here because there
  519. is only one symbol for all overloaded functions
  520. EXTDEBUG has fileinfo in tdef (PFV) }
  521. fileinfo : tfileposinfo;
  522. {$endif}
  523. symoptions : tsymoptions;
  524. deprecatedmsg : pshortstring;
  525. { symbol owning this definition }
  526. procsym : tsym;
  527. procsymderef : tderef;
  528. { alias names }
  529. aliasnames : TCmdStrList;
  530. { symtables }
  531. localst : TSymtable;
  532. funcretsym : tsym;
  533. funcretsymderef : tderef;
  534. struct : tabstractrecorddef;
  535. structderef : tderef;
  536. {$if defined(powerpc) or defined(m68k)}
  537. { library symbol for AmigaOS/MorphOS }
  538. libsym : tsym;
  539. libsymderef : tderef;
  540. {$endif powerpc or m68k}
  541. { name of the result variable to insert in the localsymtable }
  542. resultname : pshortstring;
  543. { import info }
  544. import_dll,
  545. import_name : pshortstring;
  546. { info for inlining the subroutine, if this pointer is nil,
  547. the procedure can't be inlined }
  548. inlininginfo : pinlininginfo;
  549. {$ifdef jvm}
  550. { generated assembler code; used by JVM backend so it can afterwards
  551. easily write out all methods grouped per class }
  552. exprasmlist : TAsmList;
  553. {$endif jvm}
  554. { temporary reference to structure containing copies of all local
  555. variables and parameters accessed by nested routines; reference to
  556. this structure is passed as "parent frame pointer" on targets that
  557. lack this concept (at least JVM and LLVM); no need to save to/
  558. restore from ppu, since nested routines are always in the same
  559. unit (no need to save to ppu) }
  560. parentfpstruct: tsym;
  561. { pointer to parentfpstruct's type (not yet valid during parsing, so
  562. cannot be used for $parentfp parameter) (no need to save to ppu) }
  563. parentfpstructptrtype: tdef;
  564. { code to copy the parameters accessed from nested routines into the
  565. parentfpstruct (no need to save to ppu) }
  566. parentfpinitblock: tnode;
  567. {$ifdef oldregvars}
  568. regvarinfo: pregvarinfo;
  569. {$endif oldregvars}
  570. { interrupt vector }
  571. interruptvector : longint;
  572. { First/last assembler symbol/instruction in aasmoutput list.
  573. Note: initialised after compiling the code for the procdef, but
  574. not saved to/restored from ppu. Used when inserting debug info }
  575. procstarttai,
  576. procendtai : tai;
  577. import_nr : word;
  578. extnumber : word;
  579. {$ifdef i386}
  580. fpu_used : byte;
  581. {$endif i386}
  582. visibility : tvisibility;
  583. { set to a value different from tsk_none in case this procdef is for
  584. a routine that has to be internally generated by the compiler }
  585. synthetickind : tsynthetickind;
  586. { optional parameter for the synthetic routine generation logic }
  587. skpara: pointer;
  588. { true, if the procedure is only declared
  589. (forward procedure) }
  590. forwarddef,
  591. { true if the procedure is declared in the interface }
  592. interfacedef : boolean;
  593. { true if the procedure has a forward declaration }
  594. hasforward : boolean;
  595. constructor create(level:byte);
  596. constructor ppuload(ppufile:tcompilerppufile);
  597. destructor destroy;override;
  598. procedure ppuwrite(ppufile:tcompilerppufile);override;
  599. procedure buildderef;override;
  600. procedure buildderefimpl;override;
  601. procedure deref;override;
  602. procedure derefimpl;override;
  603. function GetSymtable(t:tGetSymtable):TSymtable;override;
  604. { warnings:
  605. * the symtablestack top has to be the symtable to which the copy
  606. should be added
  607. * getcopy does not create a finished/ready-to-use procdef; it
  608. needs to be finalised afterwards by calling
  609. symcreat.finish_copied_procdef() afterwards
  610. }
  611. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; override;
  612. function getcopy: tstoreddef; override;
  613. function GetTypeName : string;override;
  614. function mangledname : TSymStr;
  615. procedure setmangledname(const s : TSymStr);
  616. function fullprocname(showhidden:boolean):string;
  617. function customprocname(pno: tprocnameoptions):ansistring;
  618. function defaultmangledname: TSymStr;
  619. function cplusplusmangledname : TSymStr;
  620. function objcmangledname : TSymStr;
  621. {$ifdef jvm}
  622. function jvmmangledbasename(signature: boolean): TSymStr;
  623. {$endif}
  624. function is_methodpointer:boolean;override;
  625. function is_addressonly:boolean;override;
  626. procedure make_external;
  627. end;
  628. { single linked list of overloaded procs }
  629. pprocdeflist = ^tprocdeflist;
  630. tprocdeflist = record
  631. def : tprocdef;
  632. defderef : tderef;
  633. next : pprocdeflist;
  634. end;
  635. tstringdef = class(tstoreddef)
  636. stringtype : tstringtype;
  637. len : asizeint;
  638. constructor createshort(l : byte);
  639. constructor loadshort(ppufile:tcompilerppufile);
  640. constructor createlong(l : asizeint);
  641. constructor loadlong(ppufile:tcompilerppufile);
  642. constructor createansi;
  643. constructor loadansi(ppufile:tcompilerppufile);
  644. constructor createwide;
  645. constructor loadwide(ppufile:tcompilerppufile);
  646. constructor createunicode;
  647. constructor loadunicode(ppufile:tcompilerppufile);
  648. function getcopy : tstoreddef;override;
  649. function stringtypname:string;
  650. procedure ppuwrite(ppufile:tcompilerppufile);override;
  651. function GetTypeName:string;override;
  652. function getmangledparaname:TSymStr;override;
  653. function is_publishable : boolean;override;
  654. function alignment : shortint;override;
  655. function needs_inittable : boolean;override;
  656. function getvardef:longint;override;
  657. function is_related(d : tdef) : boolean;override;
  658. end;
  659. { tenumdef }
  660. tenumdef = class(tstoreddef)
  661. minval,
  662. maxval : asizeint;
  663. basedef : tenumdef;
  664. basedefderef : tderef;
  665. symtable : TSymtable;
  666. {$ifdef jvm}
  667. { class representing this enum on the Java side }
  668. classdef : tobjectdef;
  669. classdefderef : tderef;
  670. {$endif}
  671. has_jumps : boolean;
  672. constructor create;
  673. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  674. constructor ppuload(ppufile:tcompilerppufile);
  675. destructor destroy;override;
  676. function getcopy : tstoreddef;override;
  677. procedure ppuwrite(ppufile:tcompilerppufile);override;
  678. procedure buildderef;override;
  679. procedure deref;override;
  680. function GetTypeName:string;override;
  681. function is_publishable : boolean;override;
  682. procedure calcsavesize;
  683. function packedbitsize: asizeint; override;
  684. procedure setmax(_max:asizeint);
  685. procedure setmin(_min:asizeint);
  686. function min:asizeint;
  687. function max:asizeint;
  688. function getfirstsym:tsym;
  689. { returns basedef if assigned, otherwise self }
  690. function getbasedef: tenumdef;
  691. end;
  692. tsetdef = class(tstoreddef)
  693. elementdef : tdef;
  694. elementdefderef : tderef;
  695. setbase,
  696. setmax : aword;
  697. constructor create(def:tdef;low, high : asizeint);
  698. constructor ppuload(ppufile:tcompilerppufile);
  699. function getcopy : tstoreddef;override;
  700. procedure ppuwrite(ppufile:tcompilerppufile);override;
  701. procedure buildderef;override;
  702. procedure deref;override;
  703. function GetTypeName:string;override;
  704. function is_publishable : boolean;override;
  705. end;
  706. tdefawaresymtablestack = class(TSymtablestack)
  707. private
  708. procedure addhelpers(st: TSymtable);
  709. procedure removehelpers(st: TSymtable);
  710. public
  711. procedure push(st: TSymtable); override;
  712. procedure pop(st: TSymtable); override;
  713. end;
  714. var
  715. current_structdef: tabstractrecorddef; { used for private functions check !! }
  716. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  717. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  718. { default types }
  719. generrordef, { error in definition }
  720. voidpointertype, { pointer for Void-pointeddef }
  721. charpointertype, { pointer for Char-pointeddef }
  722. widecharpointertype, { pointer for WideChar-pointeddef }
  723. voidfarpointertype,
  724. cundefinedtype,
  725. cformaltype, { unique formal definition }
  726. ctypedformaltype, { unique typed formal definition }
  727. voidtype, { Void (procedure) }
  728. cchartype, { Char }
  729. cwidechartype, { WideChar }
  730. pasbool8type, { boolean type }
  731. pasbool16type,
  732. pasbool32type,
  733. pasbool64type,
  734. bool8type,
  735. bool16type,
  736. bool32type,
  737. bool64type, { implement me }
  738. u8inttype, { 8-Bit unsigned integer }
  739. s8inttype, { 8-Bit signed integer }
  740. u16inttype, { 16-Bit unsigned integer }
  741. s16inttype, { 16-Bit signed integer }
  742. u32inttype, { 32-Bit unsigned integer }
  743. s32inttype, { 32-Bit signed integer }
  744. u64inttype, { 64-bit unsigned integer }
  745. s64inttype, { 64-bit signed integer }
  746. s32floattype, { 32 bit floating point number }
  747. s64floattype, { 64 bit floating point number }
  748. s80floattype, { 80 bit floating point number }
  749. sc80floattype, { 80 bit floating point number but stored like in C }
  750. s64currencytype, { pointer to a currency type }
  751. cshortstringtype, { pointer to type of short string const }
  752. clongstringtype, { pointer to type of long string const }
  753. cansistringtype, { pointer to type of ansi string const }
  754. cwidestringtype, { pointer to type of wide string const }
  755. cunicodestringtype,
  756. openshortstringtype, { pointer to type of an open shortstring,
  757. needed for readln() }
  758. openchararraytype, { pointer to type of an open array of char,
  759. needed for readln() }
  760. cfiletype, { get the same definition for all file }
  761. { used for stabs }
  762. methodpointertype, { typecasting of methodpointers to extract self }
  763. hresultdef,
  764. { we use only one variant def for every variant class }
  765. cvarianttype,
  766. colevarianttype,
  767. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  768. sinttype,
  769. uinttype,
  770. { unsigned and signed ord type with the same size as a pointer }
  771. ptruinttype,
  772. ptrsinttype,
  773. { several types to simulate more or less C++ objects for GDB }
  774. vmttype,
  775. vmtarraytype,
  776. pvmttype : tdef; { type of classrefs, used for stabs }
  777. { pointer to the anchestor of all classes }
  778. class_tobject : tobjectdef;
  779. { pointer to the ancestor of all COM interfaces }
  780. interface_iunknown : tobjectdef;
  781. { pointer to the ancestor of all dispinterfaces }
  782. interface_idispatch : tobjectdef;
  783. { pointer to the TGUID type
  784. of all interfaces }
  785. rec_tguid : trecorddef;
  786. { pointer to jump buffer }
  787. rec_jmp_buf : trecorddef;
  788. { Objective-C base types }
  789. objc_metaclasstype,
  790. objc_superclasstype,
  791. objc_idtype,
  792. objc_seltype : tpointerdef;
  793. objc_objecttype : trecorddef;
  794. { base type of @protocol(protocolname) Objective-C statements }
  795. objc_protocoltype : tobjectdef;
  796. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  797. objc_fastenumeration : tobjectdef;
  798. objc_fastenumerationstate : trecorddef;
  799. { Java base types }
  800. { java.lang.Object }
  801. java_jlobject : tobjectdef;
  802. { java.lang.Throwable }
  803. java_jlthrowable : tobjectdef;
  804. { FPC base type for records }
  805. java_fpcbaserecordtype : tobjectdef;
  806. { java.lang.String }
  807. java_jlstring : tobjectdef;
  808. { java.lang.Enum }
  809. java_jlenum : tobjectdef;
  810. { java.util.EnumSet }
  811. java_juenumset : tobjectdef;
  812. { java.util.BitSet }
  813. java_jubitset : tobjectdef;
  814. { FPC java implementation of ansistrings }
  815. java_ansistring : tobjectdef;
  816. { FPC java implementation of shortstrings }
  817. java_shortstring : tobjectdef;
  818. { FPC java procvar base class }
  819. java_procvarbase : tobjectdef;
  820. const
  821. {$ifdef i386}
  822. pbestrealtype : ^tdef = @s80floattype;
  823. {$endif}
  824. {$ifdef x86_64}
  825. pbestrealtype : ^tdef = @s80floattype;
  826. {$endif}
  827. {$ifdef m68k}
  828. pbestrealtype : ^tdef = @s64floattype;
  829. {$endif}
  830. {$ifdef alpha}
  831. pbestrealtype : ^tdef = @s64floattype;
  832. {$endif}
  833. {$ifdef powerpc}
  834. pbestrealtype : ^tdef = @s64floattype;
  835. {$endif}
  836. {$ifdef POWERPC64}
  837. pbestrealtype : ^tdef = @s64floattype;
  838. {$endif}
  839. {$ifdef ia64}
  840. pbestrealtype : ^tdef = @s64floattype;
  841. {$endif}
  842. {$ifdef SPARC}
  843. pbestrealtype : ^tdef = @s64floattype;
  844. {$endif SPARC}
  845. {$ifdef vis}
  846. pbestrealtype : ^tdef = @s64floattype;
  847. {$endif vis}
  848. {$ifdef ARM}
  849. pbestrealtype : ^tdef = @s64floattype;
  850. {$endif ARM}
  851. {$ifdef MIPS}
  852. pbestrealtype : ^tdef = @s64floattype;
  853. {$endif MIPS}
  854. {$ifdef AVR}
  855. pbestrealtype : ^tdef = @s64floattype;
  856. {$endif AVR}
  857. {$ifdef JVM}
  858. pbestrealtype : ^tdef = @s64floattype;
  859. {$endif JVM}
  860. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  861. function make_dllmangledname(const dllname,importname:TSymStr;
  862. import_nr : word; pco : tproccalloption):TSymStr;
  863. { should be in the types unit, but the types unit uses the node stuff :( }
  864. function is_interfacecom(def: tdef): boolean;
  865. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  866. function is_any_interface_kind(def: tdef): boolean;
  867. function is_interfacecorba(def: tdef): boolean;
  868. function is_interface(def: tdef): boolean;
  869. function is_dispinterface(def: tdef): boolean;
  870. function is_object(def: tdef): boolean;
  871. function is_class(def: tdef): boolean;
  872. function is_cppclass(def: tdef): boolean;
  873. function is_objectpascal_helper(def: tdef): boolean;
  874. function is_objcclass(def: tdef): boolean;
  875. function is_objcclassref(def: tdef): boolean;
  876. function is_objcprotocol(def: tdef): boolean;
  877. function is_objccategory(def: tdef): boolean;
  878. function is_objc_class_or_protocol(def: tdef): boolean;
  879. function is_objc_protocol_or_category(def: tdef): boolean;
  880. function is_classhelper(def: tdef): boolean;
  881. function is_class_or_interface(def: tdef): boolean;
  882. function is_class_or_interface_or_objc(def: tdef): boolean;
  883. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  884. function is_class_or_interface_or_object(def: tdef): boolean;
  885. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  886. function is_implicit_pointer_object_type(def: tdef): boolean;
  887. function is_class_or_object(def: tdef): boolean;
  888. function is_record(def: tdef): boolean;
  889. function is_javaclass(def: tdef): boolean;
  890. function is_javaclassref(def: tdef): boolean;
  891. function is_javainterface(def: tdef): boolean;
  892. function is_java_class_or_interface(def: tdef): boolean;
  893. procedure loadobjctypes;
  894. procedure maybeloadcocoatypes;
  895. function use_vectorfpu(def : tdef) : boolean;
  896. { returns a pointerdef for def, reusing an existing one in case it exists
  897. in the current module }
  898. function getpointerdef(def: tdef): tpointerdef;
  899. implementation
  900. uses
  901. SysUtils,
  902. cutils,
  903. { global }
  904. verbose,
  905. { target }
  906. systems,aasmcpu,paramgr,
  907. { symtable }
  908. symsym,symtable,symutil,defutil,objcdef,
  909. {$ifdef jvm}
  910. jvmdef,
  911. {$endif}
  912. { module }
  913. fmodule,
  914. { other }
  915. gendef,
  916. fpccrc
  917. ;
  918. {****************************************************************************
  919. Helpers
  920. ****************************************************************************}
  921. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  922. var
  923. s,hs,
  924. prefix : TSymStr;
  925. oldlen,
  926. newlen,
  927. i : longint;
  928. crc : dword;
  929. hp : tparavarsym;
  930. begin
  931. prefix:='';
  932. if not assigned(st) then
  933. internalerror(200204212);
  934. { sub procedures }
  935. while (st.symtabletype=localsymtable) do
  936. begin
  937. if st.defowner.typ<>procdef then
  938. internalerror(200204173);
  939. { Add the full mangledname of procedure to prevent
  940. conflicts with 2 overloads having both a nested procedure
  941. with the same name, see tb0314 (PFV) }
  942. s:=tprocdef(st.defowner).procsym.name;
  943. oldlen:=length(s);
  944. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  945. begin
  946. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  947. if not(vo_is_hidden_para in hp.varoptions) then
  948. s:=s+'$'+hp.vardef.mangledparaname;
  949. end;
  950. if not is_void(tprocdef(st.defowner).returndef) then
  951. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  952. newlen:=length(s);
  953. { Replace with CRC if the parameter line is very long }
  954. if (newlen-oldlen>12) and
  955. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  956. begin
  957. crc:=0;
  958. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  959. begin
  960. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  961. if not(vo_is_hidden_para in hp.varoptions) then
  962. begin
  963. hs:=hp.vardef.mangledparaname;
  964. crc:=UpdateCrc32(crc,hs[1],length(hs));
  965. end;
  966. end;
  967. hs:=hp.vardef.mangledparaname;
  968. crc:=UpdateCrc32(crc,hs[1],length(hs));
  969. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  970. end;
  971. if prefix<>'' then
  972. prefix:=s+'_'+prefix
  973. else
  974. prefix:=s;
  975. if length(prefix)>100 then
  976. begin
  977. crc:=0;
  978. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  979. prefix:='$CRC'+hexstr(crc,8);
  980. end;
  981. st:=st.defowner.owner;
  982. end;
  983. { object/classes symtable, nested type definitions in classes require the while loop }
  984. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  985. begin
  986. if not (st.defowner.typ in [objectdef,recorddef]) then
  987. internalerror(200204174);
  988. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  989. st:=st.defowner.owner;
  990. end;
  991. { symtable must now be static or global }
  992. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  993. internalerror(200204175);
  994. result:='';
  995. if typeprefix<>'' then
  996. result:=result+typeprefix+'_';
  997. { Add P$ for program, which can have the same name as
  998. a unit }
  999. if (TSymtable(main_module.localsymtable)=st) and
  1000. (not main_module.is_unit) then
  1001. result:=result+'P$'+st.name^
  1002. else
  1003. result:=result+st.name^;
  1004. if prefix<>'' then
  1005. result:=result+'_'+prefix;
  1006. if suffix<>'' then
  1007. result:=result+'_'+suffix;
  1008. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1009. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1010. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1011. { those in the debug map, leading to troubles with dsymutil). So always }
  1012. { add an underscore on darwin. }
  1013. if (target_info.system in systems_darwin) then
  1014. result := '_' + result;
  1015. end;
  1016. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1017. var
  1018. crc : cardinal;
  1019. i : longint;
  1020. use_crc : boolean;
  1021. dllprefix : TSymStr;
  1022. begin
  1023. if (target_info.system in (systems_all_windows + systems_nativent +
  1024. [system_i386_emx, system_i386_os2]))
  1025. and (dllname <> '') then
  1026. begin
  1027. dllprefix:=lower(ExtractFileName(dllname));
  1028. { Remove .dll suffix if present }
  1029. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1030. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1031. use_crc:=false;
  1032. for i:=1 to length(dllprefix) do
  1033. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1034. begin
  1035. use_crc:=true;
  1036. break;
  1037. end;
  1038. if use_crc then
  1039. begin
  1040. crc:=0;
  1041. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1042. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1043. end
  1044. else
  1045. dllprefix:='_$dll$'+dllprefix+'$';
  1046. if importname<>'' then
  1047. result:=dllprefix+importname
  1048. else
  1049. result:=dllprefix+'_index_'+tostr(import_nr);
  1050. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1051. { This allows to import VC++ mangled names from DLLs. }
  1052. { Do not perform replacement, if external symbol is not imported from DLL. }
  1053. if (dllname<>'') then
  1054. begin
  1055. Replace(result,'?','__q$$');
  1056. {$ifdef arm}
  1057. { @ symbol is not allowed in ARM assembler only }
  1058. Replace(result,'@','__a$$');
  1059. {$endif arm}
  1060. end;
  1061. end
  1062. else
  1063. begin
  1064. if importname<>'' then
  1065. begin
  1066. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1067. result:=importname
  1068. else
  1069. result:=target_info.Cprefix+importname;
  1070. end
  1071. else
  1072. result:='_index_'+tostr(import_nr);
  1073. end;
  1074. end;
  1075. {****************************************************************************
  1076. TDEFAWARESYMTABLESTACK
  1077. (symtablestack descendant that does some special actions on
  1078. the pushed/popped symtables)
  1079. ****************************************************************************}
  1080. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  1081. var
  1082. i: integer;
  1083. s: string;
  1084. list: TFPObjectList;
  1085. def: tdef;
  1086. begin
  1087. { search the symtable from first to last; the helper to use will be the
  1088. last one in the list }
  1089. for i:=0 to st.symlist.count-1 do
  1090. begin
  1091. if not (st.symlist[i] is ttypesym) then
  1092. continue;
  1093. def:=ttypesym(st.SymList[i]).typedef;
  1094. if is_objectpascal_helper(def) then
  1095. begin
  1096. s:=make_mangledname('',tobjectdef(def).extendeddef.symtable,'');
  1097. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1098. if not assigned(list) then
  1099. begin
  1100. list:=TFPObjectList.Create(false);
  1101. current_module.extendeddefs.Add(s,list);
  1102. end;
  1103. list.Add(def);
  1104. end
  1105. else
  1106. { add nested helpers as well }
  1107. if def.typ in [recorddef,objectdef] then
  1108. addhelpers(tabstractrecorddef(def).symtable);
  1109. end;
  1110. end;
  1111. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  1112. var
  1113. i, j: integer;
  1114. tmpst: TSymtable;
  1115. list: TFPObjectList;
  1116. begin
  1117. for i:=current_module.extendeddefs.count-1 downto 0 do
  1118. begin
  1119. list:=TFPObjectList(current_module.extendeddefs[i]);
  1120. for j:=list.count-1 downto 0 do
  1121. begin
  1122. if not (list[j] is tobjectdef) then
  1123. Internalerror(2011031501);
  1124. tmpst:=tobjectdef(list[j]).owner;
  1125. repeat
  1126. if tmpst=st then
  1127. begin
  1128. list.delete(j);
  1129. break;
  1130. end
  1131. else
  1132. begin
  1133. if assigned(tmpst.defowner) then
  1134. tmpst:=tmpst.defowner.owner
  1135. else
  1136. tmpst:=nil;
  1137. end;
  1138. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1139. end;
  1140. if list.count=0 then
  1141. current_module.extendeddefs.delete(i);
  1142. end;
  1143. end;
  1144. procedure tdefawaresymtablestack.push(st: TSymtable);
  1145. begin
  1146. { nested helpers will be added as well }
  1147. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1148. (sto_has_helper in st.tableoptions) then
  1149. addhelpers(st);
  1150. inherited push(st);
  1151. end;
  1152. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1153. begin
  1154. inherited pop(st);
  1155. { nested helpers will be removed as well }
  1156. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1157. (sto_has_helper in st.tableoptions) then
  1158. removehelpers(st);
  1159. end;
  1160. {****************************************************************************
  1161. TDEF (base class for definitions)
  1162. ****************************************************************************}
  1163. constructor tstoreddef.create(dt:tdeftyp);
  1164. var
  1165. insertstack : psymtablestackitem;
  1166. begin
  1167. inherited create(dt);
  1168. savesize := 0;
  1169. {$ifdef EXTDEBUG}
  1170. fileinfo := current_filepos;
  1171. {$endif}
  1172. generictokenbuf:=nil;
  1173. genericdef:=nil;
  1174. { Don't register forwarddefs, they are disposed at the
  1175. end of an type block }
  1176. if (dt=forwarddef) then
  1177. exit;
  1178. { Register in current_module }
  1179. if assigned(current_module) then
  1180. begin
  1181. current_module.deflist.Add(self);
  1182. DefId:=current_module.deflist.Count-1;
  1183. end;
  1184. { Register in symtable stack }
  1185. if assigned(symtablestack) then
  1186. begin
  1187. insertstack:=symtablestack.stack;
  1188. while assigned(insertstack) and
  1189. (insertstack^.symtable.symtabletype=withsymtable) do
  1190. insertstack:=insertstack^.next;
  1191. if not assigned(insertstack) then
  1192. internalerror(200602044);
  1193. insertstack^.symtable.insertdef(self);
  1194. end;
  1195. end;
  1196. destructor tstoreddef.destroy;
  1197. begin
  1198. { Direct calls are not allowed, use symtable.deletedef() }
  1199. if assigned(owner) then
  1200. internalerror(200612311);
  1201. if assigned(generictokenbuf) then
  1202. begin
  1203. generictokenbuf.free;
  1204. generictokenbuf:=nil;
  1205. end;
  1206. inherited destroy;
  1207. end;
  1208. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1209. var
  1210. sizeleft,i : longint;
  1211. buf : array[0..255] of byte;
  1212. begin
  1213. inherited create(dt);
  1214. DefId:=ppufile.getlongint;
  1215. current_module.deflist[DefId]:=self;
  1216. {$ifdef EXTDEBUG}
  1217. fillchar(fileinfo,sizeof(fileinfo),0);
  1218. {$endif}
  1219. { load }
  1220. ppufile.getderef(typesymderef);
  1221. ppufile.getsmallset(defoptions);
  1222. ppufile.getsmallset(defstates);
  1223. if df_generic in defoptions then
  1224. begin
  1225. sizeleft:=ppufile.getlongint;
  1226. initgeneric;
  1227. while sizeleft>0 do
  1228. begin
  1229. if sizeleft>sizeof(buf) then
  1230. i:=sizeof(buf)
  1231. else
  1232. i:=sizeleft;
  1233. ppufile.getdata(buf,i);
  1234. generictokenbuf.write(buf,i);
  1235. dec(sizeleft,i);
  1236. end;
  1237. end;
  1238. if df_specialization in defoptions then
  1239. ppufile.getderef(genericdefderef);
  1240. end;
  1241. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1242. var
  1243. prefix : string[4];
  1244. begin
  1245. if rt=fullrtti then
  1246. begin
  1247. prefix:='RTTI';
  1248. include(defstates,ds_rtti_table_used);
  1249. end
  1250. else
  1251. begin
  1252. prefix:='INIT';
  1253. include(defstates,ds_init_table_used);
  1254. end;
  1255. if assigned(typesym) and
  1256. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1257. result:=make_mangledname(prefix,owner,typesym.name)
  1258. else
  1259. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1260. end;
  1261. function tstoreddef.OwnerHierarchyName: string;
  1262. var
  1263. tmp: tdef;
  1264. begin
  1265. tmp:=self;
  1266. result:='';
  1267. repeat
  1268. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  1269. tmp:=tdef(tmp.owner.defowner)
  1270. else
  1271. break;
  1272. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1273. until tmp=nil;
  1274. end;
  1275. function tstoreddef.in_currentunit: boolean;
  1276. var
  1277. st: tsymtable;
  1278. begin
  1279. st:=owner;
  1280. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1281. st:=st.defowner.owner;
  1282. result:=st.iscurrentunit;
  1283. end;
  1284. function tstoreddef.getcopy : tstoreddef;
  1285. begin
  1286. Message(sym_e_cant_create_unique_type);
  1287. getcopy:=terrordef.create;
  1288. end;
  1289. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1290. var
  1291. sizeleft,i : longint;
  1292. buf : array[0..255] of byte;
  1293. oldintfcrc : boolean;
  1294. begin
  1295. ppufile.putlongint(DefId);
  1296. ppufile.putderef(typesymderef);
  1297. ppufile.putsmallset(defoptions);
  1298. oldintfcrc:=ppufile.do_crc;
  1299. ppufile.do_crc:=false;
  1300. ppufile.putsmallset(defstates);
  1301. if df_generic in defoptions then
  1302. begin
  1303. if assigned(generictokenbuf) then
  1304. begin
  1305. sizeleft:=generictokenbuf.size;
  1306. generictokenbuf.seek(0);
  1307. end
  1308. else
  1309. sizeleft:=0;
  1310. ppufile.putlongint(sizeleft);
  1311. while sizeleft>0 do
  1312. begin
  1313. if sizeleft>sizeof(buf) then
  1314. i:=sizeof(buf)
  1315. else
  1316. i:=sizeleft;
  1317. generictokenbuf.read(buf,i);
  1318. ppufile.putdata(buf,i);
  1319. dec(sizeleft,i);
  1320. end;
  1321. end;
  1322. ppufile.do_crc:=oldintfcrc;
  1323. if df_specialization in defoptions then
  1324. ppufile.putderef(genericdefderef);
  1325. end;
  1326. procedure tstoreddef.buildderef;
  1327. begin
  1328. typesymderef.build(typesym);
  1329. genericdefderef.build(genericdef);
  1330. end;
  1331. procedure tstoreddef.buildderefimpl;
  1332. begin
  1333. end;
  1334. procedure tstoreddef.deref;
  1335. begin
  1336. typesym:=ttypesym(typesymderef.resolve);
  1337. if df_specialization in defoptions then
  1338. genericdef:=tstoreddef(genericdefderef.resolve);
  1339. end;
  1340. procedure tstoreddef.derefimpl;
  1341. begin
  1342. end;
  1343. function tstoreddef.size : asizeint;
  1344. begin
  1345. size:=savesize;
  1346. end;
  1347. function tstoreddef.getvardef:longint;
  1348. begin
  1349. result:=varUndefined;
  1350. end;
  1351. function tstoreddef.alignment : shortint;
  1352. begin
  1353. { natural alignment by default }
  1354. alignment:=size_2_align(savesize);
  1355. { can happen if savesize = 0, e.g. for voiddef or
  1356. an empty record
  1357. }
  1358. if (alignment=0) then
  1359. alignment:=1;
  1360. end;
  1361. { returns true, if the definition can be published }
  1362. function tstoreddef.is_publishable : boolean;
  1363. begin
  1364. is_publishable:=false;
  1365. end;
  1366. { needs an init table }
  1367. function tstoreddef.needs_inittable : boolean;
  1368. begin
  1369. needs_inittable:=false;
  1370. end;
  1371. function tstoreddef.is_intregable : boolean;
  1372. var
  1373. recsize,temp: longint;
  1374. begin
  1375. is_intregable:=false;
  1376. case typ of
  1377. orddef,
  1378. pointerdef,
  1379. enumdef,
  1380. classrefdef:
  1381. is_intregable:=true;
  1382. procvardef :
  1383. is_intregable:=tprocvardef(self).is_addressonly;
  1384. objectdef:
  1385. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1386. setdef:
  1387. is_intregable:=is_smallset(self);
  1388. recorddef:
  1389. begin
  1390. recsize:=size;
  1391. is_intregable:=
  1392. ispowerof2(recsize,temp) and
  1393. (recsize <= sizeof(asizeint));
  1394. end;
  1395. end;
  1396. end;
  1397. function tstoreddef.is_fpuregable : boolean;
  1398. begin
  1399. {$ifdef x86}
  1400. result:=use_vectorfpu(self);
  1401. {$else x86}
  1402. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1403. {$endif x86}
  1404. end;
  1405. procedure tstoreddef.initgeneric;
  1406. begin
  1407. if assigned(generictokenbuf) then
  1408. internalerror(200512131);
  1409. generictokenbuf:=tdynamicarray.create(256);
  1410. end;
  1411. {****************************************************************************
  1412. Tstringdef
  1413. ****************************************************************************}
  1414. constructor tstringdef.createshort(l : byte);
  1415. begin
  1416. inherited create(stringdef);
  1417. stringtype:=st_shortstring;
  1418. len:=l;
  1419. savesize:=len+1;
  1420. end;
  1421. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1422. begin
  1423. inherited ppuload(stringdef,ppufile);
  1424. stringtype:=st_shortstring;
  1425. len:=ppufile.getbyte;
  1426. savesize:=len+1;
  1427. end;
  1428. constructor tstringdef.createlong(l : asizeint);
  1429. begin
  1430. inherited create(stringdef);
  1431. stringtype:=st_longstring;
  1432. len:=l;
  1433. savesize:=sizeof(pint);
  1434. end;
  1435. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1436. begin
  1437. inherited ppuload(stringdef,ppufile);
  1438. stringtype:=st_longstring;
  1439. len:=ppufile.getasizeint;
  1440. savesize:=sizeof(pint);
  1441. end;
  1442. constructor tstringdef.createansi;
  1443. begin
  1444. inherited create(stringdef);
  1445. stringtype:=st_ansistring;
  1446. len:=-1;
  1447. savesize:=sizeof(pint);
  1448. end;
  1449. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1450. begin
  1451. inherited ppuload(stringdef,ppufile);
  1452. stringtype:=st_ansistring;
  1453. len:=ppufile.getaint;
  1454. savesize:=sizeof(pint);
  1455. end;
  1456. constructor tstringdef.createwide;
  1457. begin
  1458. inherited create(stringdef);
  1459. stringtype:=st_widestring;
  1460. len:=-1;
  1461. savesize:=sizeof(pint);
  1462. end;
  1463. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1464. begin
  1465. inherited ppuload(stringdef,ppufile);
  1466. stringtype:=st_widestring;
  1467. len:=ppufile.getaint;
  1468. savesize:=sizeof(pint);
  1469. end;
  1470. constructor tstringdef.createunicode;
  1471. begin
  1472. inherited create(stringdef);
  1473. stringtype:=st_unicodestring;
  1474. len:=-1;
  1475. savesize:=sizeof(pint);
  1476. end;
  1477. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1478. begin
  1479. inherited ppuload(stringdef,ppufile);
  1480. stringtype:=st_unicodestring;
  1481. len:=ppufile.getaint;
  1482. savesize:=sizeof(pint);
  1483. end;
  1484. function tstringdef.getcopy : tstoreddef;
  1485. begin
  1486. result:=tstringdef.create(typ);
  1487. result.typ:=stringdef;
  1488. tstringdef(result).stringtype:=stringtype;
  1489. tstringdef(result).len:=len;
  1490. tstringdef(result).savesize:=savesize;
  1491. end;
  1492. function tstringdef.stringtypname:string;
  1493. const
  1494. typname:array[tstringtype] of string[10]=(
  1495. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1496. );
  1497. begin
  1498. stringtypname:=typname[stringtype];
  1499. end;
  1500. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1501. begin
  1502. inherited ppuwrite(ppufile);
  1503. if stringtype=st_shortstring then
  1504. begin
  1505. {$ifdef extdebug}
  1506. if len > 255 then internalerror(12122002);
  1507. {$endif}
  1508. ppufile.putbyte(byte(len))
  1509. end
  1510. else
  1511. ppufile.putaint(len);
  1512. case stringtype of
  1513. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1514. st_longstring : ppufile.writeentry(iblongstringdef);
  1515. st_ansistring : ppufile.writeentry(ibansistringdef);
  1516. st_widestring : ppufile.writeentry(ibwidestringdef);
  1517. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1518. end;
  1519. end;
  1520. function tstringdef.needs_inittable : boolean;
  1521. begin
  1522. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1523. end;
  1524. function tstringdef.GetTypeName : string;
  1525. const
  1526. names : array[tstringtype] of string[15] = (
  1527. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1528. begin
  1529. GetTypeName:=names[stringtype];
  1530. end;
  1531. function tstringdef.getvardef : longint;
  1532. const
  1533. vardef : array[tstringtype] of longint = (
  1534. varUndefined,varUndefined,varString,varOleStr,varUString);
  1535. begin
  1536. result:=vardef[stringtype];
  1537. end;
  1538. function tstringdef.is_related(d: tdef): boolean;
  1539. begin
  1540. result:=
  1541. (target_info.system=system_jvm_java32) and
  1542. (((stringtype in [st_unicodestring,st_widestring]) and
  1543. ((d=java_jlobject) or
  1544. (d=java_jlstring))) or
  1545. ((stringtype=st_ansistring) and
  1546. ((d=java_jlobject) or
  1547. (d=java_ansistring))));
  1548. end;
  1549. function tstringdef.alignment : shortint;
  1550. begin
  1551. case stringtype of
  1552. st_unicodestring,
  1553. st_widestring,
  1554. st_ansistring:
  1555. alignment:=size_2_align(savesize);
  1556. st_longstring,
  1557. st_shortstring:
  1558. { char to string accesses byte 0 and 1 with one word access }
  1559. if (tf_requires_proper_alignment in target_info.flags) or
  1560. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1561. (m_mac in current_settings.modeswitches) then
  1562. alignment:=size_2_align(2)
  1563. else
  1564. alignment:=size_2_align(1);
  1565. else
  1566. internalerror(200412301);
  1567. end;
  1568. end;
  1569. function tstringdef.getmangledparaname : TSymStr;
  1570. begin
  1571. getmangledparaname:='STRING';
  1572. end;
  1573. function tstringdef.is_publishable : boolean;
  1574. begin
  1575. is_publishable:=true;
  1576. end;
  1577. {****************************************************************************
  1578. TENUMDEF
  1579. ****************************************************************************}
  1580. constructor tenumdef.create;
  1581. begin
  1582. inherited create(enumdef);
  1583. minval:=0;
  1584. maxval:=0;
  1585. calcsavesize;
  1586. has_jumps:=false;
  1587. basedef:=nil;
  1588. symtable:=tenumsymtable.create(self);
  1589. end;
  1590. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1591. begin
  1592. inherited create(enumdef);
  1593. minval:=_min;
  1594. maxval:=_max;
  1595. basedef:=_basedef;
  1596. calcsavesize;
  1597. has_jumps:=false;
  1598. symtable:=basedef.symtable.getcopy;
  1599. include(defoptions, df_copied_def);
  1600. end;
  1601. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1602. begin
  1603. inherited ppuload(enumdef,ppufile);
  1604. minval:=ppufile.getaint;
  1605. maxval:=ppufile.getaint;
  1606. savesize:=ppufile.getaint;
  1607. has_jumps:=false;
  1608. {$ifdef jvm}
  1609. ppufile.getderef(classdefderef);
  1610. {$endif}
  1611. if df_copied_def in defoptions then
  1612. begin
  1613. symtable:=nil;
  1614. ppufile.getderef(basedefderef);
  1615. end
  1616. else
  1617. begin
  1618. // create with nil defowner first to prevent values changes on insert
  1619. symtable:=tenumsymtable.create(nil);
  1620. tenumsymtable(symtable).ppuload(ppufile);
  1621. symtable.defowner:=self;
  1622. end;
  1623. end;
  1624. destructor tenumdef.destroy;
  1625. begin
  1626. symtable.free;
  1627. symtable:=nil;
  1628. inherited destroy;
  1629. end;
  1630. function tenumdef.getcopy : tstoreddef;
  1631. begin
  1632. if assigned(basedef) then
  1633. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1634. else
  1635. begin
  1636. result:=tenumdef.create;
  1637. tenumdef(result).minval:=minval;
  1638. tenumdef(result).maxval:=maxval;
  1639. tenumdef(result).symtable.free;
  1640. tenumdef(result).symtable:=symtable.getcopy;
  1641. tenumdef(result).basedef:=self;
  1642. end;
  1643. {$ifdef jvm}
  1644. tenumdef(result).classdef:=classdef;
  1645. {$endif}
  1646. tenumdef(result).has_jumps:=has_jumps;
  1647. tenumdef(result).basedefderef:=basedefderef;
  1648. include(tenumdef(result).defoptions,df_copied_def);
  1649. end;
  1650. procedure tenumdef.calcsavesize;
  1651. begin
  1652. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1653. savesize:=8
  1654. else
  1655. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1656. savesize:=4
  1657. else
  1658. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1659. savesize:=2
  1660. else
  1661. savesize:=1;
  1662. end;
  1663. function tenumdef.packedbitsize: asizeint;
  1664. var
  1665. sizeval: tconstexprint;
  1666. power: longint;
  1667. begin
  1668. result := 0;
  1669. if (minval >= 0) and
  1670. (maxval <= 1) then
  1671. result := 1
  1672. else
  1673. begin
  1674. if (minval>=0) then
  1675. sizeval:=maxval
  1676. else
  1677. { don't count 0 twice }
  1678. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1679. { 256 must become 512 etc. }
  1680. nextpowerof2(sizeval+1,power);
  1681. result := power;
  1682. end;
  1683. end;
  1684. procedure tenumdef.setmax(_max:asizeint);
  1685. begin
  1686. maxval:=_max;
  1687. calcsavesize;
  1688. end;
  1689. procedure tenumdef.setmin(_min:asizeint);
  1690. begin
  1691. minval:=_min;
  1692. calcsavesize;
  1693. end;
  1694. function tenumdef.min:asizeint;
  1695. begin
  1696. min:=minval;
  1697. end;
  1698. function tenumdef.max:asizeint;
  1699. begin
  1700. max:=maxval;
  1701. end;
  1702. function tenumdef.getfirstsym: tsym;
  1703. var
  1704. i:integer;
  1705. begin
  1706. for i := 0 to symtable.SymList.Count - 1 do
  1707. begin
  1708. result:=tsym(symtable.SymList[i]);
  1709. if tenumsym(result).value=minval then
  1710. exit;
  1711. end;
  1712. result:=nil;
  1713. end;
  1714. function tenumdef.getbasedef: tenumdef;
  1715. begin
  1716. if not assigned(basedef) then
  1717. result:=self
  1718. else
  1719. result:=basedef;
  1720. end;
  1721. procedure tenumdef.buildderef;
  1722. begin
  1723. inherited buildderef;
  1724. if df_copied_def in defoptions then
  1725. basedefderef.build(basedef)
  1726. else
  1727. tenumsymtable(symtable).buildderef;
  1728. {$ifdef jvm}
  1729. classdefderef.build(classdef);
  1730. {$endif}
  1731. end;
  1732. procedure tenumdef.deref;
  1733. begin
  1734. inherited deref;
  1735. if df_copied_def in defoptions then
  1736. begin
  1737. basedef:=tenumdef(basedefderef.resolve);
  1738. symtable:=basedef.symtable.getcopy;
  1739. end
  1740. else
  1741. tenumsymtable(symtable).deref;
  1742. {$ifdef jvm}
  1743. classdef:=tobjectdef(classdefderef.resolve);
  1744. {$endif}
  1745. end;
  1746. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1747. begin
  1748. inherited ppuwrite(ppufile);
  1749. ppufile.putaint(min);
  1750. ppufile.putaint(max);
  1751. ppufile.putaint(savesize);
  1752. {$ifdef jvm}
  1753. ppufile.putderef(classdefderef);
  1754. {$endif}
  1755. if df_copied_def in defoptions then
  1756. ppufile.putderef(basedefderef);
  1757. ppufile.writeentry(ibenumdef);
  1758. if not (df_copied_def in defoptions) then
  1759. tenumsymtable(symtable).ppuwrite(ppufile);
  1760. end;
  1761. function tenumdef.is_publishable : boolean;
  1762. begin
  1763. is_publishable:=true;
  1764. end;
  1765. function tenumdef.GetTypeName : string;
  1766. begin
  1767. GetTypeName:='<enumeration type>';
  1768. end;
  1769. {****************************************************************************
  1770. TORDDEF
  1771. ****************************************************************************}
  1772. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1773. begin
  1774. inherited create(orddef);
  1775. low:=v;
  1776. high:=b;
  1777. ordtype:=t;
  1778. setsize;
  1779. end;
  1780. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1781. begin
  1782. inherited ppuload(orddef,ppufile);
  1783. ordtype:=tordtype(ppufile.getbyte);
  1784. low:=ppufile.getexprint;
  1785. high:=ppufile.getexprint;
  1786. setsize;
  1787. end;
  1788. function torddef.getcopy : tstoreddef;
  1789. begin
  1790. result:=torddef.create(ordtype,low,high);
  1791. result.typ:=orddef;
  1792. torddef(result).low:=low;
  1793. torddef(result).high:=high;
  1794. torddef(result).ordtype:=ordtype;
  1795. torddef(result).savesize:=savesize;
  1796. end;
  1797. function torddef.alignment:shortint;
  1798. begin
  1799. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1800. (ordtype in [s64bit,u64bit]) then
  1801. result := 4
  1802. else
  1803. result := inherited alignment;
  1804. end;
  1805. procedure torddef.setsize;
  1806. const
  1807. sizetbl : array[tordtype] of longint = (
  1808. 0,
  1809. 1,2,4,8,
  1810. 1,2,4,8,
  1811. 1,2,4,8,
  1812. 1,2,4,8,
  1813. 1,2,8
  1814. );
  1815. begin
  1816. savesize:=sizetbl[ordtype];
  1817. end;
  1818. function torddef.packedbitsize: asizeint;
  1819. var
  1820. sizeval: tconstexprint;
  1821. power: longint;
  1822. begin
  1823. result := 0;
  1824. if ordtype = uvoid then
  1825. exit;
  1826. {$ifndef cpu64bitalu}
  1827. if (ordtype in [s64bit,u64bit]) then
  1828. {$else not cpu64bitalu}
  1829. if (ordtype = u64bit) or
  1830. ((ordtype = s64bit) and
  1831. ((low <= (system.low(int64) div 2)) or
  1832. (high > (system.high(int64) div 2)))) then
  1833. {$endif cpu64bitalu}
  1834. result := 64
  1835. else if (low >= 0) and
  1836. (high <= 1) then
  1837. result := 1
  1838. else
  1839. begin
  1840. if (low>=0) then
  1841. sizeval:=high
  1842. else
  1843. { don't count 0 twice }
  1844. sizeval:=(cutils.max(-low,high)*2)-1;
  1845. { 256 must become 512 etc. }
  1846. nextpowerof2(sizeval+1,power);
  1847. result := power;
  1848. end;
  1849. end;
  1850. function torddef.getvardef : longint;
  1851. const
  1852. basetype2vardef : array[tordtype] of longint = (
  1853. varUndefined,
  1854. varbyte,varword,varlongword,varqword,
  1855. varshortint,varsmallint,varinteger,varint64,
  1856. varboolean,varboolean,varboolean,varboolean,
  1857. varboolean,varboolean,varUndefined,varUndefined,
  1858. varUndefined,varUndefined,varCurrency);
  1859. begin
  1860. result:=basetype2vardef[ordtype];
  1861. end;
  1862. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1863. begin
  1864. inherited ppuwrite(ppufile);
  1865. ppufile.putbyte(byte(ordtype));
  1866. ppufile.putexprint(low);
  1867. ppufile.putexprint(high);
  1868. ppufile.writeentry(iborddef);
  1869. end;
  1870. function torddef.is_publishable : boolean;
  1871. begin
  1872. is_publishable:=(ordtype<>uvoid);
  1873. end;
  1874. function torddef.GetTypeName : string;
  1875. const
  1876. names : array[tordtype] of string[20] = (
  1877. 'untyped',
  1878. 'Byte','Word','DWord','QWord',
  1879. 'ShortInt','SmallInt','LongInt','Int64',
  1880. 'Boolean','Boolean16','Boolean32','Boolean64',
  1881. 'ByteBool','WordBool','LongBool','QWordBool',
  1882. 'Char','WideChar','Currency');
  1883. begin
  1884. GetTypeName:=names[ordtype];
  1885. end;
  1886. {****************************************************************************
  1887. TFLOATDEF
  1888. ****************************************************************************}
  1889. constructor tfloatdef.create(t : tfloattype);
  1890. begin
  1891. inherited create(floatdef);
  1892. floattype:=t;
  1893. setsize;
  1894. end;
  1895. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1896. begin
  1897. inherited ppuload(floatdef,ppufile);
  1898. floattype:=tfloattype(ppufile.getbyte);
  1899. setsize;
  1900. end;
  1901. function tfloatdef.getcopy : tstoreddef;
  1902. begin
  1903. result:=tfloatdef.create(floattype);
  1904. result.typ:=floatdef;
  1905. tfloatdef(result).savesize:=savesize;
  1906. end;
  1907. function tfloatdef.alignment:shortint;
  1908. begin
  1909. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1910. case floattype of
  1911. s80real: result:=16;
  1912. s64real,
  1913. s64currency,
  1914. s64comp : result:=4;
  1915. else
  1916. result := inherited alignment;
  1917. end
  1918. else
  1919. result := inherited alignment;
  1920. end;
  1921. procedure tfloatdef.setsize;
  1922. begin
  1923. case floattype of
  1924. s32real : savesize:=4;
  1925. s80real : savesize:=10;
  1926. sc80real:
  1927. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1928. system_x86_64_linux,system_x86_64_freebsd,
  1929. system_x86_64_solaris,system_x86_64_embedded] then
  1930. savesize:=16
  1931. else
  1932. savesize:=12;
  1933. s64real,
  1934. s64currency,
  1935. s64comp : savesize:=8;
  1936. else
  1937. savesize:=0;
  1938. end;
  1939. end;
  1940. function tfloatdef.getvardef : longint;
  1941. const
  1942. floattype2vardef : array[tfloattype] of longint = (
  1943. varSingle,varDouble,varUndefined,varUndefined,
  1944. varUndefined,varCurrency,varUndefined);
  1945. begin
  1946. if (upper(typename)='TDATETIME') and
  1947. assigned(owner) and
  1948. assigned(owner.name) and
  1949. (owner.name^='SYSTEM') then
  1950. result:=varDate
  1951. else
  1952. result:=floattype2vardef[floattype];
  1953. end;
  1954. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1955. begin
  1956. inherited ppuwrite(ppufile);
  1957. ppufile.putbyte(byte(floattype));
  1958. ppufile.writeentry(ibfloatdef);
  1959. end;
  1960. function tfloatdef.is_publishable : boolean;
  1961. begin
  1962. is_publishable:=true;
  1963. end;
  1964. function tfloatdef.GetTypeName : string;
  1965. const
  1966. names : array[tfloattype] of string[20] = (
  1967. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1968. begin
  1969. GetTypeName:=names[floattype];
  1970. end;
  1971. {****************************************************************************
  1972. TFILEDEF
  1973. ****************************************************************************}
  1974. constructor tfiledef.createtext;
  1975. begin
  1976. inherited create(filedef);
  1977. filetyp:=ft_text;
  1978. typedfiledef:=nil;
  1979. setsize;
  1980. end;
  1981. constructor tfiledef.createuntyped;
  1982. begin
  1983. inherited create(filedef);
  1984. filetyp:=ft_untyped;
  1985. typedfiledef:=nil;
  1986. setsize;
  1987. end;
  1988. constructor tfiledef.createtyped(def:tdef);
  1989. begin
  1990. inherited create(filedef);
  1991. filetyp:=ft_typed;
  1992. typedfiledef:=def;
  1993. setsize;
  1994. end;
  1995. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1996. begin
  1997. inherited ppuload(filedef,ppufile);
  1998. filetyp:=tfiletyp(ppufile.getbyte);
  1999. if filetyp=ft_typed then
  2000. ppufile.getderef(typedfiledefderef)
  2001. else
  2002. typedfiledef:=nil;
  2003. setsize;
  2004. end;
  2005. function tfiledef.getcopy : tstoreddef;
  2006. begin
  2007. case filetyp of
  2008. ft_typed:
  2009. result:=tfiledef.createtyped(typedfiledef);
  2010. ft_untyped:
  2011. result:=tfiledef.createuntyped;
  2012. ft_text:
  2013. result:=tfiledef.createtext;
  2014. else
  2015. internalerror(2004121201);
  2016. end;
  2017. end;
  2018. procedure tfiledef.buildderef;
  2019. begin
  2020. inherited buildderef;
  2021. if filetyp=ft_typed then
  2022. typedfiledefderef.build(typedfiledef);
  2023. end;
  2024. procedure tfiledef.deref;
  2025. begin
  2026. inherited deref;
  2027. if filetyp=ft_typed then
  2028. typedfiledef:=tdef(typedfiledefderef.resolve);
  2029. end;
  2030. procedure tfiledef.setsize;
  2031. begin
  2032. {$ifdef cpu64bitaddr}
  2033. case filetyp of
  2034. ft_text :
  2035. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  2036. savesize:=632{+8}
  2037. else
  2038. savesize:=628{+8};
  2039. ft_typed,
  2040. ft_untyped :
  2041. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  2042. savesize:=372
  2043. else
  2044. savesize:=368;
  2045. end;
  2046. {$endif cpu64bitaddr}
  2047. {$ifdef cpu32bitaddr}
  2048. case filetyp of
  2049. ft_text :
  2050. savesize:=592{+4};
  2051. ft_typed,
  2052. ft_untyped :
  2053. savesize:=332;
  2054. end;
  2055. {$endif cpu32bitaddr}
  2056. {$ifdef cpu8bitaddr}
  2057. case filetyp of
  2058. ft_text :
  2059. savesize:=127;
  2060. ft_typed,
  2061. ft_untyped :
  2062. savesize:=127;
  2063. end;
  2064. {$endif cpu8bitaddr}
  2065. end;
  2066. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2067. begin
  2068. inherited ppuwrite(ppufile);
  2069. ppufile.putbyte(byte(filetyp));
  2070. if filetyp=ft_typed then
  2071. ppufile.putderef(typedfiledefderef);
  2072. ppufile.writeentry(ibfiledef);
  2073. end;
  2074. function tfiledef.GetTypeName : string;
  2075. begin
  2076. case filetyp of
  2077. ft_untyped:
  2078. GetTypeName:='File';
  2079. ft_typed:
  2080. GetTypeName:='File Of '+typedfiledef.typename;
  2081. ft_text:
  2082. GetTypeName:='Text'
  2083. end;
  2084. end;
  2085. function tfiledef.getmangledparaname : TSymStr;
  2086. begin
  2087. case filetyp of
  2088. ft_untyped:
  2089. getmangledparaname:='FILE';
  2090. ft_typed:
  2091. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2092. ft_text:
  2093. getmangledparaname:='TEXT'
  2094. end;
  2095. end;
  2096. {****************************************************************************
  2097. TVARIANTDEF
  2098. ****************************************************************************}
  2099. constructor tvariantdef.create(v : tvarianttype);
  2100. begin
  2101. inherited create(variantdef);
  2102. varianttype:=v;
  2103. setsize;
  2104. end;
  2105. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2106. begin
  2107. inherited ppuload(variantdef,ppufile);
  2108. varianttype:=tvarianttype(ppufile.getbyte);
  2109. setsize;
  2110. end;
  2111. function tvariantdef.getcopy : tstoreddef;
  2112. begin
  2113. result:=tvariantdef.create(varianttype);
  2114. end;
  2115. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2116. begin
  2117. inherited ppuwrite(ppufile);
  2118. ppufile.putbyte(byte(varianttype));
  2119. ppufile.writeentry(ibvariantdef);
  2120. end;
  2121. function tvariantdef.getvardef : longint;
  2122. begin
  2123. Result:=varVariant;
  2124. end;
  2125. procedure tvariantdef.setsize;
  2126. begin
  2127. {$ifdef cpu64bitaddr}
  2128. savesize:=24;
  2129. {$else cpu64bitaddr}
  2130. savesize:=16;
  2131. {$endif cpu64bitaddr}
  2132. end;
  2133. function tvariantdef.GetTypeName : string;
  2134. begin
  2135. case varianttype of
  2136. vt_normalvariant:
  2137. GetTypeName:='Variant';
  2138. vt_olevariant:
  2139. GetTypeName:='OleVariant';
  2140. end;
  2141. end;
  2142. function tvariantdef.needs_inittable : boolean;
  2143. begin
  2144. needs_inittable:=true;
  2145. end;
  2146. function tvariantdef.is_publishable : boolean;
  2147. begin
  2148. is_publishable:=true;
  2149. end;
  2150. {****************************************************************************
  2151. TABSTRACtpointerdef
  2152. ****************************************************************************}
  2153. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2154. begin
  2155. inherited create(dt);
  2156. pointeddef:=def;
  2157. savesize:=sizeof(pint);
  2158. end;
  2159. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2160. begin
  2161. inherited ppuload(dt,ppufile);
  2162. ppufile.getderef(pointeddefderef);
  2163. savesize:=sizeof(pint);
  2164. end;
  2165. procedure tabstractpointerdef.buildderef;
  2166. begin
  2167. inherited buildderef;
  2168. pointeddefderef.build(pointeddef);
  2169. end;
  2170. procedure tabstractpointerdef.deref;
  2171. begin
  2172. inherited deref;
  2173. pointeddef:=tdef(pointeddefderef.resolve);
  2174. end;
  2175. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2176. begin
  2177. inherited ppuwrite(ppufile);
  2178. ppufile.putderef(pointeddefderef);
  2179. end;
  2180. {****************************************************************************
  2181. tpointerdef
  2182. ****************************************************************************}
  2183. constructor tpointerdef.create(def:tdef);
  2184. begin
  2185. inherited create(pointerdef,def);
  2186. is_far:=false;
  2187. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2188. end;
  2189. constructor tpointerdef.createfar(def:tdef);
  2190. begin
  2191. inherited create(pointerdef,def);
  2192. is_far:=true;
  2193. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2194. end;
  2195. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2196. begin
  2197. inherited ppuload(pointerdef,ppufile);
  2198. is_far:=(ppufile.getbyte<>0);
  2199. has_pointer_math:=(ppufile.getbyte<>0);
  2200. end;
  2201. function tpointerdef.getcopy : tstoreddef;
  2202. begin
  2203. { don't use direct pointeddef if it is a forwarddef because in other case
  2204. one of them will be destroyed on forward type resolve and the second will
  2205. point to garbage }
  2206. if pointeddef.typ=forwarddef then
  2207. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  2208. else
  2209. result:=tpointerdef.create(pointeddef);
  2210. tpointerdef(result).is_far:=is_far;
  2211. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2212. tpointerdef(result).savesize:=savesize;
  2213. end;
  2214. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2215. begin
  2216. inherited ppuwrite(ppufile);
  2217. ppufile.putbyte(byte(is_far));
  2218. ppufile.putbyte(byte(has_pointer_math));
  2219. ppufile.writeentry(ibpointerdef);
  2220. end;
  2221. function tpointerdef.GetTypeName : string;
  2222. begin
  2223. if is_far then
  2224. GetTypeName:='^'+pointeddef.typename+';far'
  2225. else
  2226. GetTypeName:='^'+pointeddef.typename;
  2227. end;
  2228. {****************************************************************************
  2229. TCLASSREFDEF
  2230. ****************************************************************************}
  2231. constructor tclassrefdef.create(def:tdef);
  2232. begin
  2233. inherited create(classrefdef,def);
  2234. end;
  2235. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2236. begin
  2237. inherited ppuload(classrefdef,ppufile);
  2238. end;
  2239. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2240. begin
  2241. inherited ppuwrite(ppufile);
  2242. ppufile.writeentry(ibclassrefdef);
  2243. end;
  2244. function tclassrefdef.getcopy:tstoreddef;
  2245. begin
  2246. if pointeddef.typ=forwarddef then
  2247. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2248. else
  2249. result:=tclassrefdef.create(pointeddef);
  2250. tclassrefdef(result).savesize:=savesize;
  2251. end;
  2252. function tclassrefdef.GetTypeName : string;
  2253. begin
  2254. GetTypeName:='Class Of '+pointeddef.typename;
  2255. end;
  2256. function tclassrefdef.is_publishable : boolean;
  2257. begin
  2258. result:=true;
  2259. end;
  2260. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2261. begin
  2262. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2263. result:=inherited rtti_mangledname(rt)
  2264. else
  2265. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2266. end;
  2267. procedure tclassrefdef.register_created_object_type;
  2268. begin
  2269. tobjectdef(pointeddef).register_created_classref_type;
  2270. end;
  2271. {***************************************************************************
  2272. TSETDEF
  2273. ***************************************************************************}
  2274. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2275. var
  2276. setallocbits: aint;
  2277. packedsavesize: aint;
  2278. begin
  2279. inherited create(setdef);
  2280. elementdef:=def;
  2281. setmax:=high;
  2282. if (current_settings.setalloc=0) then
  2283. begin
  2284. setbase:=0;
  2285. if (high<32) then
  2286. savesize:=Sizeof(longint)
  2287. else if (high<256) then
  2288. savesize:=32
  2289. else
  2290. savesize:=(high+7) div 8
  2291. end
  2292. else
  2293. begin
  2294. setallocbits:=current_settings.setalloc*8;
  2295. setbase:=low and not(setallocbits-1);
  2296. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2297. savesize:=packedsavesize;
  2298. if savesize=3 then
  2299. savesize:=4;
  2300. end;
  2301. end;
  2302. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2303. begin
  2304. inherited ppuload(setdef,ppufile);
  2305. ppufile.getderef(elementdefderef);
  2306. savesize:=ppufile.getaint;
  2307. setbase:=ppufile.getaint;
  2308. setmax:=ppufile.getaint;
  2309. end;
  2310. function tsetdef.getcopy : tstoreddef;
  2311. begin
  2312. result:=tsetdef.create(elementdef,setbase,setmax);
  2313. { the copy might have been created with a different setalloc setting }
  2314. tsetdef(result).savesize:=savesize;
  2315. end;
  2316. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2317. begin
  2318. inherited ppuwrite(ppufile);
  2319. ppufile.putderef(elementdefderef);
  2320. ppufile.putaint(savesize);
  2321. ppufile.putaint(setbase);
  2322. ppufile.putaint(setmax);
  2323. ppufile.writeentry(ibsetdef);
  2324. end;
  2325. procedure tsetdef.buildderef;
  2326. begin
  2327. inherited buildderef;
  2328. elementdefderef.build(elementdef);
  2329. end;
  2330. procedure tsetdef.deref;
  2331. begin
  2332. inherited deref;
  2333. elementdef:=tdef(elementdefderef.resolve);
  2334. end;
  2335. function tsetdef.is_publishable : boolean;
  2336. begin
  2337. is_publishable:=savesize in [1,2,4];
  2338. end;
  2339. function tsetdef.GetTypeName : string;
  2340. begin
  2341. if assigned(elementdef) then
  2342. GetTypeName:='Set Of '+elementdef.typename
  2343. else
  2344. GetTypeName:='Empty Set';
  2345. end;
  2346. {***************************************************************************
  2347. TFORMALDEF
  2348. ***************************************************************************}
  2349. constructor tformaldef.create(Atyped:boolean);
  2350. begin
  2351. inherited create(formaldef);
  2352. typed:=Atyped;
  2353. savesize:=0;
  2354. end;
  2355. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2356. begin
  2357. inherited ppuload(formaldef,ppufile);
  2358. typed:=boolean(ppufile.getbyte);
  2359. savesize:=0;
  2360. end;
  2361. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2362. begin
  2363. inherited ppuwrite(ppufile);
  2364. ppufile.putbyte(byte(typed));
  2365. ppufile.writeentry(ibformaldef);
  2366. end;
  2367. function tformaldef.GetTypeName : string;
  2368. begin
  2369. if typed then
  2370. GetTypeName:='<Typed formal type>'
  2371. else
  2372. GetTypeName:='<Formal type>';
  2373. end;
  2374. {***************************************************************************
  2375. TARRAYDEF
  2376. ***************************************************************************}
  2377. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2378. begin
  2379. inherited create(arraydef);
  2380. lowrange:=l;
  2381. highrange:=h;
  2382. rangedef:=def;
  2383. _elementdef:=nil;
  2384. arrayoptions:=[];
  2385. symtable:=tarraysymtable.create(self);
  2386. end;
  2387. destructor tarraydef.destroy;
  2388. begin
  2389. symtable.free;
  2390. symtable:=nil;
  2391. inherited;
  2392. end;
  2393. constructor tarraydef.create_from_pointer(def:tdef);
  2394. begin
  2395. { use -1 so that the elecount will not overflow }
  2396. self.create(0,high(asizeint)-1,s32inttype);
  2397. arrayoptions:=[ado_IsConvertedPointer];
  2398. setelementdef(def);
  2399. end;
  2400. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2401. begin
  2402. inherited ppuload(arraydef,ppufile);
  2403. { the addresses are calculated later }
  2404. ppufile.getderef(_elementdefderef);
  2405. ppufile.getderef(rangedefderef);
  2406. lowrange:=ppufile.getaint;
  2407. highrange:=ppufile.getaint;
  2408. ppufile.getsmallset(arrayoptions);
  2409. symtable:=tarraysymtable.create(self);
  2410. tarraysymtable(symtable).ppuload(ppufile)
  2411. end;
  2412. function tarraydef.getcopy : tstoreddef;
  2413. begin
  2414. result:=tarraydef.create(lowrange,highrange,rangedef);
  2415. tarraydef(result).arrayoptions:=arrayoptions;
  2416. tarraydef(result)._elementdef:=_elementdef;
  2417. end;
  2418. procedure tarraydef.buildderef;
  2419. begin
  2420. inherited buildderef;
  2421. tarraysymtable(symtable).buildderef;
  2422. _elementdefderef.build(_elementdef);
  2423. rangedefderef.build(rangedef);
  2424. end;
  2425. procedure tarraydef.deref;
  2426. begin
  2427. inherited deref;
  2428. tarraysymtable(symtable).deref;
  2429. _elementdef:=tdef(_elementdefderef.resolve);
  2430. rangedef:=tdef(rangedefderef.resolve);
  2431. end;
  2432. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2433. begin
  2434. inherited ppuwrite(ppufile);
  2435. ppufile.putderef(_elementdefderef);
  2436. ppufile.putderef(rangedefderef);
  2437. ppufile.putaint(lowrange);
  2438. ppufile.putaint(highrange);
  2439. ppufile.putsmallset(arrayoptions);
  2440. ppufile.writeentry(ibarraydef);
  2441. tarraysymtable(symtable).ppuwrite(ppufile);
  2442. end;
  2443. function tarraydef.elesize : asizeint;
  2444. begin
  2445. if (ado_IsBitPacked in arrayoptions) then
  2446. internalerror(2006080101);
  2447. if assigned(_elementdef) then
  2448. result:=_elementdef.size
  2449. else
  2450. result:=0;
  2451. end;
  2452. function tarraydef.elepackedbitsize : asizeint;
  2453. begin
  2454. if not(ado_IsBitPacked in arrayoptions) then
  2455. internalerror(2006080102);
  2456. if assigned(_elementdef) then
  2457. result:=_elementdef.packedbitsize
  2458. else
  2459. result:=0;
  2460. end;
  2461. function tarraydef.elecount : asizeuint;
  2462. var
  2463. qhigh,qlow : qword;
  2464. begin
  2465. if ado_IsDynamicArray in arrayoptions then
  2466. begin
  2467. result:=0;
  2468. exit;
  2469. end;
  2470. if (highrange>0) and (lowrange<0) then
  2471. begin
  2472. qhigh:=highrange;
  2473. qlow:=qword(-lowrange);
  2474. { prevent overflow, return 0 to indicate overflow }
  2475. if qhigh+qlow>qword(high(asizeint)-1) then
  2476. result:=0
  2477. else
  2478. result:=qhigh+qlow+1;
  2479. end
  2480. else
  2481. result:=int64(highrange)-lowrange+1;
  2482. end;
  2483. function tarraydef.size : asizeint;
  2484. var
  2485. cachedelecount : asizeuint;
  2486. cachedelesize : asizeint;
  2487. begin
  2488. if ado_IsDynamicArray in arrayoptions then
  2489. begin
  2490. size:=sizeof(pint);
  2491. exit;
  2492. end;
  2493. { Tarraydef.size may never be called for an open array! }
  2494. if highrange<lowrange then
  2495. internalerror(99080501);
  2496. if not (ado_IsBitPacked in arrayoptions) then
  2497. cachedelesize:=elesize
  2498. else
  2499. cachedelesize := elepackedbitsize;
  2500. cachedelecount:=elecount;
  2501. if (cachedelesize = 0) then
  2502. begin
  2503. size := 0;
  2504. exit;
  2505. end;
  2506. if (cachedelecount = 0) then
  2507. begin
  2508. size := -1;
  2509. exit;
  2510. end;
  2511. { prevent overflow, return -1 to indicate overflow }
  2512. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2513. if (cachedelecount > asizeuint(high(asizeint))) or
  2514. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2515. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2516. accessing the array, see ncgmem (PFV) }
  2517. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2518. begin
  2519. result:=-1;
  2520. exit;
  2521. end;
  2522. result:=cachedelesize*asizeint(cachedelecount);
  2523. if (ado_IsBitPacked in arrayoptions) then
  2524. { can't just add 7 and divide by 8, because that may overflow }
  2525. result:=result div 8 + ord((result mod 8)<>0);
  2526. end;
  2527. procedure tarraydef.setelementdef(def:tdef);
  2528. begin
  2529. _elementdef:=def;
  2530. if not(
  2531. (ado_IsDynamicArray in arrayoptions) or
  2532. (ado_IsConvertedPointer in arrayoptions) or
  2533. (highrange<lowrange)
  2534. ) and
  2535. (size=-1) then
  2536. Message(sym_e_segment_too_large);
  2537. end;
  2538. function tarraydef.alignment : shortint;
  2539. begin
  2540. { alignment of dyn. arrays doesn't depend on the element size }
  2541. if (ado_IsDynamicArray in arrayoptions) then
  2542. alignment:=size_2_align(sizeof(pint))
  2543. { alignment is the alignment of the elements }
  2544. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2545. ((elementdef.typ=objectdef) and
  2546. is_object(elementdef)) then
  2547. alignment:=elementdef.alignment
  2548. { alignment is the size of the elements }
  2549. else if not (ado_IsBitPacked in arrayoptions) then
  2550. alignment:=size_2_align(elesize)
  2551. else
  2552. alignment:=packedbitsloadsize(elepackedbitsize);
  2553. end;
  2554. function tarraydef.needs_inittable : boolean;
  2555. begin
  2556. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2557. end;
  2558. function tarraydef.GetTypeName : string;
  2559. begin
  2560. if (ado_IsConstString in arrayoptions) then
  2561. result:='Constant String'
  2562. else if (ado_isarrayofconst in arrayoptions) or
  2563. (ado_isConstructor in arrayoptions) then
  2564. begin
  2565. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2566. GetTypeName:='Array Of Const'
  2567. else
  2568. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  2569. end
  2570. else if (ado_IsDynamicArray in arrayoptions) then
  2571. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  2572. else if ((highrange=-1) and (lowrange=0)) then
  2573. GetTypeName:='{Open} Array Of '+elementdef.typename
  2574. else
  2575. begin
  2576. result := '';
  2577. if (ado_IsBitPacked in arrayoptions) then
  2578. result:='BitPacked ';
  2579. if rangedef.typ=enumdef then
  2580. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2581. else
  2582. result:=result+'Array['+tostr(lowrange)+'..'+
  2583. tostr(highrange)+'] Of '+elementdef.typename
  2584. end;
  2585. end;
  2586. function tarraydef.getmangledparaname : TSymStr;
  2587. begin
  2588. if ado_isarrayofconst in arrayoptions then
  2589. getmangledparaname:='array_of_const'
  2590. else
  2591. if ((highrange=-1) and (lowrange=0)) then
  2592. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2593. else
  2594. internalerror(200204176);
  2595. end;
  2596. function tarraydef.is_publishable : boolean;
  2597. begin
  2598. Result:=ado_IsDynamicArray in arrayoptions;
  2599. end;
  2600. {***************************************************************************
  2601. tabstractrecorddef
  2602. ***************************************************************************}
  2603. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2604. begin
  2605. inherited create(dt);
  2606. objname:=stringdup(upper(n));
  2607. objrealname:=stringdup(n);
  2608. objectoptions:=[];
  2609. if assigned(current_module.namespace) then
  2610. begin
  2611. import_lib:=stringdup(current_module.namespace^);
  2612. replace(import_lib^,'.','/');
  2613. end;
  2614. end;
  2615. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2616. begin
  2617. inherited ppuload(dt,ppufile);
  2618. objrealname:=stringdup(ppufile.getstring);
  2619. objname:=stringdup(upper(objrealname^));
  2620. import_lib:=stringdup(ppufile.getstring);
  2621. { only used for external C++ classes and Java classes/records }
  2622. if (import_lib^='') then
  2623. stringdispose(import_lib);
  2624. ppufile.getsmallset(objectoptions);
  2625. end;
  2626. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2627. begin
  2628. inherited ppuwrite(ppufile);
  2629. ppufile.putstring(objrealname^);
  2630. if assigned(import_lib) then
  2631. ppufile.putstring(import_lib^)
  2632. else
  2633. ppufile.putstring('');
  2634. ppufile.putsmallset(objectoptions);
  2635. end;
  2636. destructor tabstractrecorddef.destroy;
  2637. begin
  2638. stringdispose(objname);
  2639. stringdispose(objrealname);
  2640. stringdispose(import_lib);
  2641. tcinitcode.free;
  2642. inherited destroy;
  2643. end;
  2644. procedure tabstractrecorddef.check_forwards;
  2645. begin
  2646. tstoredsymtable(symtable).check_forwards;
  2647. end;
  2648. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2649. var
  2650. i: longint;
  2651. sym: tsym;
  2652. begin
  2653. for i:=0 to symtable.SymList.Count-1 do
  2654. begin
  2655. sym:=tsym(symtable.SymList[i]);
  2656. if sym.typ=procsym then
  2657. begin
  2658. result:=tprocsym(sym).find_procdef_bytype(pt);
  2659. if assigned(result) then
  2660. exit;
  2661. end;
  2662. end;
  2663. result:=nil;
  2664. end;
  2665. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2666. begin
  2667. if t=gs_record then
  2668. GetSymtable:=symtable
  2669. else
  2670. GetSymtable:=nil;
  2671. end;
  2672. function tabstractrecorddef.is_packed:boolean;
  2673. begin
  2674. result:=tabstractrecordsymtable(symtable).is_packed;
  2675. end;
  2676. function tabstractrecorddef.RttiName: string;
  2677. begin
  2678. Result:=OwnerHierarchyName+objrealname^;
  2679. end;
  2680. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2681. var
  2682. sym : tsym;
  2683. i : integer;
  2684. pd : tprocdef;
  2685. hashedid : THashedIDString;
  2686. begin
  2687. result:=nil;
  2688. hashedid.id:='GETENUMERATOR';
  2689. sym:=tsym(symtable.FindWithHash(hashedid));
  2690. if assigned(sym) and (sym.typ=procsym) then
  2691. begin
  2692. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2693. begin
  2694. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2695. if (pd.proctypeoption = potype_function) and
  2696. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2697. (pd.visibility >= vis_public) then
  2698. begin
  2699. result:=pd;
  2700. exit;
  2701. end;
  2702. end;
  2703. end;
  2704. end;
  2705. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2706. var
  2707. sym : tsym;
  2708. i : integer;
  2709. pd : tprocdef;
  2710. hashedid : THashedIDString;
  2711. begin
  2712. result:=nil;
  2713. // first search for po_enumerator_movenext method modifier
  2714. // then search for public function MoveNext: Boolean
  2715. for i:=0 to symtable.SymList.Count-1 do
  2716. begin
  2717. sym:=TSym(symtable.SymList[i]);
  2718. if (sym.typ=procsym) then
  2719. begin
  2720. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2721. if assigned(pd) then
  2722. begin
  2723. result:=pd;
  2724. exit;
  2725. end;
  2726. end;
  2727. end;
  2728. hashedid.id:='MOVENEXT';
  2729. sym:=tsym(symtable.FindWithHash(hashedid));
  2730. if assigned(sym) and (sym.typ=procsym) then
  2731. begin
  2732. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2733. begin
  2734. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2735. if (pd.proctypeoption = potype_function) and
  2736. is_boolean(pd.returndef) and
  2737. (pd.minparacount = 0) and
  2738. (pd.visibility >= vis_public) then
  2739. begin
  2740. result:=pd;
  2741. exit;
  2742. end;
  2743. end;
  2744. end;
  2745. end;
  2746. function tabstractrecorddef.search_enumerator_current: tsym;
  2747. var
  2748. sym: tsym;
  2749. i: integer;
  2750. hashedid : THashedIDString;
  2751. begin
  2752. result:=nil;
  2753. // first search for ppo_enumerator_current property modifier
  2754. // then search for public property Current
  2755. for i:=0 to symtable.SymList.Count-1 do
  2756. begin
  2757. sym:=TSym(symtable.SymList[i]);
  2758. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2759. begin
  2760. result:=sym;
  2761. exit;
  2762. end;
  2763. end;
  2764. hashedid.id:='CURRENT';
  2765. sym:=tsym(symtable.FindWithHash(hashedid));
  2766. if assigned(sym) and (sym.typ=propertysym) and
  2767. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2768. begin
  2769. result:=sym;
  2770. exit;
  2771. end;
  2772. end;
  2773. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  2774. var
  2775. st: tsymtable;
  2776. enclosingdef: tdef;
  2777. begin
  2778. if typ=objectdef then
  2779. result:=tobjectdef(self).objextname^
  2780. else if assigned(typesym) then
  2781. result:=typesym.realname
  2782. { have to generate anonymous nested type in current unit/class/record }
  2783. else
  2784. internalerror(2011032601);
  2785. st:=owner;
  2786. while assigned(st) and
  2787. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  2788. begin
  2789. { nested classes are named as "OuterClass$InnerClass" }
  2790. enclosingdef:=tdef(st.defowner);
  2791. if enclosingdef.typ=procdef then
  2792. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  2793. else if enclosingdef.typ=objectdef then
  2794. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  2795. else if assigned(enclosingdef.typesym) then
  2796. result:=enclosingdef.typesym.realname+'$'+result
  2797. else
  2798. internalerror(2011060305);
  2799. st:=enclosingdef.owner;
  2800. end;
  2801. if with_package_name and
  2802. assigned(import_lib) then
  2803. result:=import_lib^+'/'+result;
  2804. end;
  2805. {***************************************************************************
  2806. trecorddef
  2807. ***************************************************************************}
  2808. constructor trecorddef.create(const n:string; p:TSymtable);
  2809. begin
  2810. inherited create(n,recorddef);
  2811. symtable:=p;
  2812. { we can own the symtable only if nobody else owns a copy so far }
  2813. if symtable.refcount=1 then
  2814. symtable.defowner:=self;
  2815. isunion:=false;
  2816. end;
  2817. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2818. begin
  2819. inherited ppuload(recorddef,ppufile);
  2820. if df_copied_def in defoptions then
  2821. ppufile.getderef(cloneddefderef)
  2822. else
  2823. begin
  2824. symtable:=trecordsymtable.create(objrealname^,0);
  2825. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2826. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2827. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2828. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2829. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  2830. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  2831. trecordsymtable(symtable).ppuload(ppufile);
  2832. { requires usefieldalignment to be set }
  2833. symtable.defowner:=self;
  2834. end;
  2835. isunion:=false;
  2836. end;
  2837. destructor trecorddef.destroy;
  2838. begin
  2839. if assigned(symtable) then
  2840. begin
  2841. symtable.free;
  2842. symtable:=nil;
  2843. end;
  2844. inherited destroy;
  2845. end;
  2846. function trecorddef.getcopy : tstoreddef;
  2847. begin
  2848. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2849. trecorddef(result).isunion:=isunion;
  2850. include(trecorddef(result).defoptions,df_copied_def);
  2851. if assigned(tcinitcode) then
  2852. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  2853. if assigned(import_lib) then
  2854. trecorddef(result).import_lib:=stringdup(import_lib^);
  2855. end;
  2856. function trecorddef.needs_inittable : boolean;
  2857. begin
  2858. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2859. end;
  2860. function trecorddef.is_related(d: tdef): boolean;
  2861. begin
  2862. { records are implemented via classes in the JVM target, and are
  2863. all descendents of the java_fpcbaserecordtype class }
  2864. is_related:=false;
  2865. if (target_info.system=system_jvm_java32) then
  2866. begin
  2867. if d.typ=objectdef then
  2868. begin
  2869. d:=find_real_class_definition(tobjectdef(d),false);
  2870. if (d=java_jlobject) or
  2871. (d=java_fpcbaserecordtype) then
  2872. is_related:=true
  2873. end;
  2874. end;
  2875. end;
  2876. procedure trecorddef.buildderef;
  2877. begin
  2878. inherited buildderef;
  2879. if df_copied_def in defoptions then
  2880. cloneddefderef.build(symtable.defowner)
  2881. else
  2882. tstoredsymtable(symtable).buildderef;
  2883. end;
  2884. procedure trecorddef.deref;
  2885. begin
  2886. inherited deref;
  2887. { now dereference the definitions }
  2888. if df_copied_def in defoptions then
  2889. begin
  2890. cloneddef:=trecorddef(cloneddefderef.resolve);
  2891. symtable:=cloneddef.symtable.getcopy;
  2892. end
  2893. else
  2894. tstoredsymtable(symtable).deref;
  2895. { assign TGUID? load only from system unit }
  2896. if not(assigned(rec_tguid)) and
  2897. (upper(typename)='TGUID') and
  2898. assigned(owner) and
  2899. assigned(owner.name) and
  2900. (owner.name^='SYSTEM') then
  2901. rec_tguid:=self;
  2902. { assign JMP_BUF? load only from system unit }
  2903. if not(assigned(rec_jmp_buf)) and
  2904. (upper(typename)='JMP_BUF') and
  2905. assigned(owner) and
  2906. assigned(owner.name) and
  2907. (owner.name^='SYSTEM') then
  2908. rec_jmp_buf:=self;
  2909. end;
  2910. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2911. begin
  2912. inherited ppuwrite(ppufile);
  2913. if df_copied_def in defoptions then
  2914. ppufile.putderef(cloneddefderef)
  2915. else
  2916. begin
  2917. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2918. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2919. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2920. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2921. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  2922. ppufile.putword(trecordsymtable(symtable).paddingsize);
  2923. end;
  2924. ppufile.writeentry(ibrecorddef);
  2925. if not(df_copied_def in defoptions) then
  2926. trecordsymtable(symtable).ppuwrite(ppufile);
  2927. end;
  2928. function trecorddef.size:asizeint;
  2929. begin
  2930. result:=trecordsymtable(symtable).datasize;
  2931. end;
  2932. function trecorddef.alignment:shortint;
  2933. begin
  2934. alignment:=trecordsymtable(symtable).recordalignment;
  2935. end;
  2936. function trecorddef.padalignment:shortint;
  2937. begin
  2938. padalignment := trecordsymtable(symtable).padalignment;
  2939. end;
  2940. function trecorddef.GetTypeName : string;
  2941. begin
  2942. GetTypeName:='<record type>'
  2943. end;
  2944. {***************************************************************************
  2945. TABSTRACTPROCDEF
  2946. ***************************************************************************}
  2947. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2948. begin
  2949. inherited create(dt);
  2950. parast:=tparasymtable.create(self,level);
  2951. paras:=nil;
  2952. minparacount:=0;
  2953. maxparacount:=0;
  2954. proctypeoption:=potype_none;
  2955. proccalloption:=pocall_none;
  2956. procoptions:=[];
  2957. returndef:=voidtype;
  2958. savesize:=sizeof(pint);
  2959. callerargareasize:=0;
  2960. calleeargareasize:=0;
  2961. has_paraloc_info:=callnoside;
  2962. funcretloc[callerside].init;
  2963. funcretloc[calleeside].init;
  2964. check_mark_as_nested;
  2965. end;
  2966. destructor tabstractprocdef.destroy;
  2967. begin
  2968. if assigned(paras) then
  2969. begin
  2970. {$ifdef MEMDEBUG}
  2971. memprocpara.start;
  2972. {$endif MEMDEBUG}
  2973. paras.free;
  2974. paras:=nil;
  2975. {$ifdef MEMDEBUG}
  2976. memprocpara.stop;
  2977. {$endif MEMDEBUG}
  2978. end;
  2979. if assigned(parast) then
  2980. begin
  2981. {$ifdef MEMDEBUG}
  2982. memprocparast.start;
  2983. {$endif MEMDEBUG}
  2984. parast.free;
  2985. parast:=nil;
  2986. {$ifdef MEMDEBUG}
  2987. memprocparast.stop;
  2988. {$endif MEMDEBUG}
  2989. end;
  2990. funcretloc[callerside].done;
  2991. funcretloc[calleeside].done;
  2992. inherited destroy;
  2993. end;
  2994. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2995. begin
  2996. if (tsym(p).typ<>paravarsym) then
  2997. exit;
  2998. inc(plongint(arg)^);
  2999. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3000. begin
  3001. if not assigned(tparavarsym(p).defaultconstsym) then
  3002. inc(minparacount);
  3003. inc(maxparacount);
  3004. end;
  3005. end;
  3006. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3007. begin
  3008. if (tsym(p).typ<>paravarsym) then
  3009. exit;
  3010. paras.add(p);
  3011. end;
  3012. procedure tabstractprocdef.calcparas;
  3013. var
  3014. paracount : longint;
  3015. begin
  3016. { This can already be assigned when
  3017. we need to reresolve this unit (PFV) }
  3018. if assigned(paras) then
  3019. paras.free;
  3020. paras:=tparalist.create(false);
  3021. paracount:=0;
  3022. minparacount:=0;
  3023. maxparacount:=0;
  3024. parast.SymList.ForEachCall(@count_para,@paracount);
  3025. paras.capacity:=paracount;
  3026. { Insert parameters in table }
  3027. parast.SymList.ForEachCall(@insert_para,nil);
  3028. { Order parameters }
  3029. paras.sortparas;
  3030. end;
  3031. procedure tabstractprocdef.buildderef;
  3032. begin
  3033. { released procdef? }
  3034. if not assigned(parast) then
  3035. exit;
  3036. inherited buildderef;
  3037. returndefderef.build(returndef);
  3038. if po_explicitparaloc in procoptions then
  3039. funcretloc[callerside].buildderef;
  3040. { parast }
  3041. tparasymtable(parast).buildderef;
  3042. end;
  3043. procedure tabstractprocdef.deref;
  3044. begin
  3045. inherited deref;
  3046. returndef:=tdef(returndefderef.resolve);
  3047. if po_explicitparaloc in procoptions then
  3048. begin
  3049. funcretloc[callerside].deref;
  3050. has_paraloc_info:=callerside;
  3051. end
  3052. else
  3053. begin
  3054. { deref is called after loading from a ppu, but also after another
  3055. unit has been reloaded/recompiled and all references must be
  3056. re-resolved. Since the funcretloc contains a reference to a tdef,
  3057. reset it so that we won't try to access the stale def }
  3058. funcretloc[callerside].init;
  3059. has_paraloc_info:=callnoside;
  3060. end;
  3061. { parast }
  3062. tparasymtable(parast).deref;
  3063. { recalculated parameters }
  3064. calcparas;
  3065. end;
  3066. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3067. begin
  3068. inherited ppuload(dt,ppufile);
  3069. parast:=nil;
  3070. Paras:=nil;
  3071. minparacount:=0;
  3072. maxparacount:=0;
  3073. ppufile.getderef(returndefderef);
  3074. { TODO: remove fpu_used loading}
  3075. ppufile.getbyte;
  3076. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3077. proccalloption:=tproccalloption(ppufile.getbyte);
  3078. ppufile.getnormalset(procoptions);
  3079. funcretloc[callerside].init;
  3080. if po_explicitparaloc in procoptions then
  3081. funcretloc[callerside].ppuload(ppufile);
  3082. savesize:=sizeof(pint);
  3083. if (po_explicitparaloc in procoptions) then
  3084. has_paraloc_info:=callerside;
  3085. end;
  3086. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3087. var
  3088. oldintfcrc : boolean;
  3089. begin
  3090. { released procdef? }
  3091. if not assigned(parast) then
  3092. exit;
  3093. inherited ppuwrite(ppufile);
  3094. ppufile.putderef(returndefderef);
  3095. oldintfcrc:=ppufile.do_interface_crc;
  3096. ppufile.do_interface_crc:=false;
  3097. ppufile.putbyte(0);
  3098. ppufile.putbyte(ord(proctypeoption));
  3099. ppufile.putbyte(ord(proccalloption));
  3100. ppufile.putnormalset(procoptions);
  3101. ppufile.do_interface_crc:=oldintfcrc;
  3102. if (po_explicitparaloc in procoptions) then
  3103. funcretloc[callerside].ppuwrite(ppufile);
  3104. end;
  3105. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3106. var
  3107. hs,s : ansistring;
  3108. hp : TParavarsym;
  3109. hpc : tconstsym;
  3110. first : boolean;
  3111. i : integer;
  3112. begin
  3113. s:='';
  3114. first:=true;
  3115. for i:=0 to paras.count-1 do
  3116. begin
  3117. hp:=tparavarsym(paras[i]);
  3118. if not(vo_is_hidden_para in hp.varoptions) or
  3119. (pno_showhidden in pno) then
  3120. begin
  3121. if first then
  3122. begin
  3123. s:=s+'(';
  3124. first:=false;
  3125. end
  3126. else
  3127. s:=s+';';
  3128. if vo_is_hidden_para in hp.varoptions then
  3129. s:=s+'<';
  3130. case hp.varspez of
  3131. vs_var :
  3132. s:=s+'var ';
  3133. vs_const :
  3134. s:=s+'const ';
  3135. vs_out :
  3136. s:=s+'out ';
  3137. vs_constref :
  3138. s:=s+'constref ';
  3139. end;
  3140. if (pno_paranames in pno) then
  3141. s:=s+hp.realname+':';
  3142. if hp.univpara then
  3143. s:=s+'univ ';
  3144. if assigned(hp.vardef.typesym) then
  3145. begin
  3146. hs:=hp.vardef.typesym.realname;
  3147. if hs[1]<>'$' then
  3148. s:=s+hp.vardef.OwnerHierarchyName+hs
  3149. else
  3150. s:=s+hp.vardef.GetTypeName;
  3151. end
  3152. else
  3153. s:=s+hp.vardef.GetTypeName;
  3154. { default value }
  3155. if assigned(hp.defaultconstsym) then
  3156. begin
  3157. hpc:=tconstsym(hp.defaultconstsym);
  3158. hs:='';
  3159. case hpc.consttyp of
  3160. conststring,
  3161. constresourcestring :
  3162. begin
  3163. If hpc.value.len>0 then
  3164. begin
  3165. setLength(hs,hpc.value.len);
  3166. { don't write past the end of hs if the constant
  3167. is > 255 chars }
  3168. move(hpc.value.valueptr^,hs[1],length(hs));
  3169. { make sure that constant strings with newline chars
  3170. don't create a linebreak in the assembler code,
  3171. since comments are line-based. Also remove nulls
  3172. because the comments are written as a pchar. }
  3173. ReplaceCase(hs,#0,'.');
  3174. ReplaceCase(hs,#10,'.');
  3175. ReplaceCase(hs,#13,'.');
  3176. end;
  3177. end;
  3178. constreal :
  3179. str(pbestreal(hpc.value.valueptr)^,hs);
  3180. constpointer :
  3181. hs:=tostr(hpc.value.valueordptr);
  3182. constord :
  3183. begin
  3184. if is_boolean(hpc.constdef) then
  3185. begin
  3186. if hpc.value.valueord<>0 then
  3187. hs:='TRUE'
  3188. else
  3189. hs:='FALSE';
  3190. end
  3191. else
  3192. hs:=tostr(hpc.value.valueord);
  3193. end;
  3194. constnil :
  3195. hs:='nil';
  3196. constset :
  3197. hs:='<set>';
  3198. end;
  3199. if hs<>'' then
  3200. s:=s+'="'+hs+'"';
  3201. end;
  3202. if vo_is_hidden_para in hp.varoptions then
  3203. s:=s+'>';
  3204. end;
  3205. end;
  3206. if not first then
  3207. s:=s+')';
  3208. if (po_varargs in procoptions) then
  3209. s:=s+';VarArgs';
  3210. typename_paras:=s;
  3211. end;
  3212. function tabstractprocdef.is_methodpointer:boolean;
  3213. begin
  3214. result:=false;
  3215. end;
  3216. function tabstractprocdef.is_addressonly:boolean;
  3217. begin
  3218. result:=true;
  3219. end;
  3220. function tabstractprocdef.no_self_node: boolean;
  3221. begin
  3222. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  3223. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  3224. end;
  3225. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  3226. var
  3227. j, nestinglevel: longint;
  3228. pvs, npvs: tparavarsym;
  3229. csym, ncsym: tconstsym;
  3230. begin
  3231. nestinglevel:=parast.symtablelevel;
  3232. if newtyp=procdef then
  3233. begin
  3234. if (copytyp<>pc_bareproc) then
  3235. result:=tprocdef.create(nestinglevel)
  3236. else
  3237. result:=tprocdef.create(normal_function_level);
  3238. tprocdef(result).visibility:=vis_public;
  3239. end
  3240. else
  3241. begin
  3242. result:=tprocvardef.create(nestinglevel);
  3243. end;
  3244. tabstractprocdef(result).returndef:=returndef;
  3245. tabstractprocdef(result).returndefderef:=returndefderef;
  3246. tabstractprocdef(result).parast:=tparasymtable.create(tabstractprocdef(result),parast.symtablelevel);
  3247. pvs:=nil;
  3248. npvs:=nil;
  3249. for j:=0 to parast.symlist.count-1 do
  3250. begin
  3251. case tsym(parast.symlist[j]).typ of
  3252. paravarsym:
  3253. begin
  3254. pvs:=tparavarsym(parast.symlist[j]);
  3255. { in case of bare proc, don't copy self, vmt or framepointer
  3256. parameters }
  3257. if (copytyp=pc_bareproc) and
  3258. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result]*pvs.varoptions)<>[]) then
  3259. continue;
  3260. npvs:=tparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  3261. pvs.vardef,pvs.varoptions);
  3262. npvs.defaultconstsym:=pvs.defaultconstsym;
  3263. tabstractprocdef(result).parast.insert(npvs);
  3264. end;
  3265. constsym:
  3266. begin
  3267. // ignore, reuse original constym. Should also be duplicated
  3268. // be safe though
  3269. end
  3270. else
  3271. internalerror(201160604);
  3272. end;
  3273. end;
  3274. tabstractprocdef(result).savesize:=savesize;
  3275. tabstractprocdef(result).proctypeoption:=proctypeoption;
  3276. tabstractprocdef(result).proccalloption:=proccalloption;
  3277. tabstractprocdef(result).procoptions:=procoptions;
  3278. if (copytyp=pc_bareproc) then
  3279. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  3280. if newtyp=procvardef then
  3281. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  3282. tabstractprocdef(result).callerargareasize:=callerargareasize;
  3283. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  3284. tabstractprocdef(result).maxparacount:=maxparacount;
  3285. tabstractprocdef(result).minparacount:=minparacount;
  3286. if po_explicitparaloc in procoptions then
  3287. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  3288. { recalculate parameter info }
  3289. tabstractprocdef(result).has_paraloc_info:=callnoside;
  3290. {$ifdef m68k}
  3291. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  3292. {$endif}
  3293. if (typ=procdef) and
  3294. (newtyp=procvardef) and
  3295. (owner.symtabletype=ObjectSymtable) then
  3296. include(tprocvardef(result).procoptions,po_methodpointer);
  3297. end;
  3298. procedure tabstractprocdef.check_mark_as_nested;
  3299. begin
  3300. { nested procvars require that nested functions use the Delphi-style
  3301. nested procedure calling convention }
  3302. if (parast.symtablelevel>normal_function_level) and
  3303. (m_nested_procvars in current_settings.modeswitches) then
  3304. include(procoptions,po_delphi_nested_cc);
  3305. end;
  3306. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  3307. begin
  3308. if (side in [callerside,callbothsides]) and
  3309. not(has_paraloc_info in [callerside,callbothsides]) then
  3310. begin
  3311. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  3312. if has_paraloc_info in [calleeside,callbothsides] then
  3313. has_paraloc_info:=callbothsides
  3314. else
  3315. has_paraloc_info:=callerside;
  3316. end;
  3317. if (side in [calleeside,callbothsides]) and
  3318. not(has_paraloc_info in [calleeside,callbothsides]) then
  3319. begin
  3320. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  3321. if has_paraloc_info in [callerside,callbothsides] then
  3322. has_paraloc_info:=callbothsides
  3323. else
  3324. has_paraloc_info:=calleeside;
  3325. end;
  3326. end;
  3327. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  3328. var
  3329. p: tparavarsym;
  3330. ploc: PCGParalocation;
  3331. i: longint;
  3332. begin
  3333. result:=false;
  3334. init_paraloc_info(side);
  3335. for i:=0 to parast.SymList.Count-1 do
  3336. if tsym(parast.SymList[i]).typ=paravarsym then
  3337. begin
  3338. p:=tparavarsym(parast.SymList[i]);
  3339. { check if no parameter is located on the stack }
  3340. if is_open_array(p.vardef) or
  3341. is_array_of_const(p.vardef) then
  3342. begin
  3343. result:=true;
  3344. exit;
  3345. end;
  3346. ploc:=p.paraloc[side].location;
  3347. while assigned(ploc) do
  3348. begin
  3349. if (ploc^.loc=LOC_REFERENCE) then
  3350. begin
  3351. result:=true;
  3352. exit
  3353. end;
  3354. ploc:=ploc^.next;
  3355. end;
  3356. end;
  3357. end;
  3358. {***************************************************************************
  3359. TPROCDEF
  3360. ***************************************************************************}
  3361. constructor tprocdef.create(level:byte);
  3362. begin
  3363. inherited create(procdef,level);
  3364. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  3365. {$ifdef symansistr}
  3366. _mangledname:='';
  3367. {$else symansistr}
  3368. _mangledname:=nil;
  3369. {$endif symansistr}
  3370. fileinfo:=current_filepos;
  3371. extnumber:=$ffff;
  3372. aliasnames:=TCmdStrList.create;
  3373. funcretsym:=nil;
  3374. forwarddef:=true;
  3375. interfacedef:=false;
  3376. hasforward:=false;
  3377. struct := nil;
  3378. import_dll:=nil;
  3379. import_name:=nil;
  3380. import_nr:=0;
  3381. inlininginfo:=nil;
  3382. deprecatedmsg:=nil;
  3383. {$ifdef i386}
  3384. fpu_used:=maxfpuregs;
  3385. {$endif i386}
  3386. interruptvector:=-1;
  3387. end;
  3388. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  3389. var
  3390. i,aliasnamescount : longint;
  3391. level : byte;
  3392. begin
  3393. inherited ppuload(procdef,ppufile);
  3394. {$ifdef symansistr}
  3395. if po_has_mangledname in procoptions then
  3396. _mangledname:=ppufile.getansistring
  3397. else
  3398. _mangledname:='';
  3399. {$else symansistr}
  3400. if po_has_mangledname in procoptions then
  3401. _mangledname:=stringdup(ppufile.getstring)
  3402. else
  3403. _mangledname:=nil;
  3404. {$endif symansistr}
  3405. extnumber:=ppufile.getword;
  3406. level:=ppufile.getbyte;
  3407. ppufile.getderef(structderef);
  3408. ppufile.getderef(procsymderef);
  3409. ppufile.getposinfo(fileinfo);
  3410. visibility:=tvisibility(ppufile.getbyte);
  3411. ppufile.getsmallset(symoptions);
  3412. if sp_has_deprecated_msg in symoptions then
  3413. deprecatedmsg:=stringdup(ppufile.getstring)
  3414. else
  3415. deprecatedmsg:=nil;
  3416. {$ifdef powerpc}
  3417. { library symbol for AmigaOS/MorphOS }
  3418. ppufile.getderef(libsymderef);
  3419. {$endif powerpc}
  3420. { import stuff }
  3421. if po_has_importdll in procoptions then
  3422. import_dll:=stringdup(ppufile.getstring)
  3423. else
  3424. import_dll:=nil;
  3425. if po_has_importname in procoptions then
  3426. import_name:=stringdup(ppufile.getstring)
  3427. else
  3428. import_name:=nil;
  3429. import_nr:=ppufile.getword;
  3430. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3431. if target_info.system in systems_interrupt_table then
  3432. interruptvector:=ppufile.getlongint;
  3433. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3434. if (po_msgint in procoptions) then
  3435. messageinf.i:=ppufile.getlongint;
  3436. if (po_msgstr in procoptions) then
  3437. messageinf.str:=stringdup(ppufile.getstring);
  3438. if (po_dispid in procoptions) then
  3439. dispid:=ppufile.getlongint;
  3440. { inline stuff }
  3441. if (po_has_inlininginfo in procoptions) then
  3442. begin
  3443. ppufile.getderef(funcretsymderef);
  3444. new(inlininginfo);
  3445. ppufile.getsmallset(inlininginfo^.flags);
  3446. end
  3447. else
  3448. begin
  3449. inlininginfo:=nil;
  3450. funcretsym:=nil;
  3451. end;
  3452. aliasnames:=TCmdStrList.create;
  3453. { count alias names }
  3454. aliasnamescount:=ppufile.getbyte;
  3455. for i:=1 to aliasnamescount do
  3456. aliasnames.insert(ppufile.getstring);
  3457. { load para symtable }
  3458. parast:=tparasymtable.create(self,level);
  3459. tparasymtable(parast).ppuload(ppufile);
  3460. { load local symtable }
  3461. if (po_has_inlininginfo in procoptions) then
  3462. begin
  3463. localst:=tlocalsymtable.create(self,level);
  3464. tlocalsymtable(localst).ppuload(ppufile);
  3465. end
  3466. else
  3467. localst:=nil;
  3468. { inline stuff }
  3469. if (po_has_inlininginfo in procoptions) then
  3470. inlininginfo^.code:=ppuloadnodetree(ppufile);
  3471. { default values for no persistent data }
  3472. if (cs_link_deffile in current_settings.globalswitches) and
  3473. (tf_need_export in target_info.flags) and
  3474. (po_exports in procoptions) then
  3475. deffile.AddExport(mangledname);
  3476. forwarddef:=false;
  3477. interfacedef:=false;
  3478. hasforward:=false;
  3479. { Disable po_has_inlining until the derefimpl is done }
  3480. exclude(procoptions,po_has_inlininginfo);
  3481. {$ifdef i386}
  3482. fpu_used:=maxfpuregs;
  3483. {$endif i386}
  3484. end;
  3485. destructor tprocdef.destroy;
  3486. begin
  3487. aliasnames.free;
  3488. aliasnames:=nil;
  3489. if assigned(localst) and
  3490. (localst.symtabletype<>staticsymtable) then
  3491. begin
  3492. {$ifdef MEMDEBUG}
  3493. memproclocalst.start;
  3494. {$endif MEMDEBUG}
  3495. localst.free;
  3496. localst:=nil;
  3497. {$ifdef MEMDEBUG}
  3498. memproclocalst.start;
  3499. {$endif MEMDEBUG}
  3500. end;
  3501. if assigned(inlininginfo) then
  3502. begin
  3503. {$ifdef MEMDEBUG}
  3504. memprocnodetree.start;
  3505. {$endif MEMDEBUG}
  3506. tnode(inlininginfo^.code).free;
  3507. {$ifdef MEMDEBUG}
  3508. memprocnodetree.start;
  3509. {$endif MEMDEBUG}
  3510. dispose(inlininginfo);
  3511. inlininginfo:=nil;
  3512. end;
  3513. {$ifdef jvm}
  3514. exprasmlist.free;
  3515. {$endif}
  3516. stringdispose(resultname);
  3517. stringdispose(import_dll);
  3518. stringdispose(import_name);
  3519. stringdispose(deprecatedmsg);
  3520. if (po_msgstr in procoptions) then
  3521. stringdispose(messageinf.str);
  3522. {$ifndef symansistr}
  3523. if assigned(_mangledname) then
  3524. begin
  3525. {$ifdef MEMDEBUG}
  3526. memmanglednames.start;
  3527. {$endif MEMDEBUG}
  3528. stringdispose(_mangledname);
  3529. {$ifdef MEMDEBUG}
  3530. memmanglednames.stop;
  3531. {$endif MEMDEBUG}
  3532. end;
  3533. {$endif symansistr}
  3534. inherited destroy;
  3535. end;
  3536. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  3537. var
  3538. oldintfcrc : boolean;
  3539. aliasnamescount : longint;
  3540. item : TCmdStrListItem;
  3541. begin
  3542. { released procdef? }
  3543. if not assigned(parast) then
  3544. exit;
  3545. inherited ppuwrite(ppufile);
  3546. {$ifdef symansistr}
  3547. if po_has_mangledname in procoptions then
  3548. ppufile.putansistring(_mangledname);
  3549. {$else symansistr}
  3550. if po_has_mangledname in procoptions then
  3551. ppufile.putstring(_mangledname^);
  3552. {$endif symansistr}
  3553. ppufile.putword(extnumber);
  3554. ppufile.putbyte(parast.symtablelevel);
  3555. ppufile.putderef(structderef);
  3556. ppufile.putderef(procsymderef);
  3557. ppufile.putposinfo(fileinfo);
  3558. ppufile.putbyte(byte(visibility));
  3559. ppufile.putsmallset(symoptions);
  3560. if sp_has_deprecated_msg in symoptions then
  3561. ppufile.putstring(deprecatedmsg^);
  3562. {$ifdef powerpc}
  3563. { library symbol for AmigaOS/MorphOS }
  3564. ppufile.putderef(libsymderef);
  3565. {$endif powerpc}
  3566. { import }
  3567. if po_has_importdll in procoptions then
  3568. ppufile.putstring(import_dll^);
  3569. if po_has_importname in procoptions then
  3570. ppufile.putstring(import_name^);
  3571. ppufile.putword(import_nr);
  3572. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3573. if target_info.system in systems_interrupt_table then
  3574. ppufile.putlongint(interruptvector);
  3575. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3576. if (po_msgint in procoptions) then
  3577. ppufile.putlongint(messageinf.i);
  3578. if (po_msgstr in procoptions) then
  3579. ppufile.putstring(messageinf.str^);
  3580. if (po_dispid in procoptions) then
  3581. ppufile.putlongint(dispid);
  3582. { inline stuff }
  3583. oldintfcrc:=ppufile.do_crc;
  3584. ppufile.do_crc:=false;
  3585. if (po_has_inlininginfo in procoptions) then
  3586. begin
  3587. ppufile.putderef(funcretsymderef);
  3588. ppufile.putsmallset(inlininginfo^.flags);
  3589. end;
  3590. { count alias names }
  3591. aliasnamescount:=0;
  3592. item:=TCmdStrListItem(aliasnames.first);
  3593. while assigned(item) do
  3594. begin
  3595. inc(aliasnamescount);
  3596. item:=TCmdStrListItem(item.next);
  3597. end;
  3598. if aliasnamescount>255 then
  3599. internalerror(200711021);
  3600. ppufile.putbyte(aliasnamescount);
  3601. item:=TCmdStrListItem(aliasnames.first);
  3602. while assigned(item) do
  3603. begin
  3604. ppufile.putstring(item.str);
  3605. item:=TCmdStrListItem(item.next);
  3606. end;
  3607. ppufile.do_crc:=oldintfcrc;
  3608. { write this entry }
  3609. ppufile.writeentry(ibprocdef);
  3610. { Save the para symtable, this is taken from the interface }
  3611. tparasymtable(parast).ppuwrite(ppufile);
  3612. { save localsymtable for inline procedures or when local
  3613. browser info is requested, this has no influence on the crc }
  3614. if (po_has_inlininginfo in procoptions) then
  3615. begin
  3616. oldintfcrc:=ppufile.do_crc;
  3617. ppufile.do_crc:=false;
  3618. tlocalsymtable(localst).ppuwrite(ppufile);
  3619. ppufile.do_crc:=oldintfcrc;
  3620. end;
  3621. { node tree for inlining }
  3622. oldintfcrc:=ppufile.do_crc;
  3623. ppufile.do_crc:=false;
  3624. if (po_has_inlininginfo in procoptions) then
  3625. ppuwritenodetree(ppufile,inlininginfo^.code);
  3626. ppufile.do_crc:=oldintfcrc;
  3627. end;
  3628. function tprocdef.fullprocname(showhidden:boolean):string;
  3629. var
  3630. pno: tprocnameoptions;
  3631. begin
  3632. pno:=[];
  3633. if showhidden then
  3634. include(pno,pno_showhidden);
  3635. result:=customprocname(pno);
  3636. end;
  3637. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  3638. var
  3639. s, rn : ansistring;
  3640. t : ttoken;
  3641. begin
  3642. {$ifdef EXTDEBUG}
  3643. include(pno,pno_showhidden);
  3644. {$endif EXTDEBUG}
  3645. s:='';
  3646. if proctypeoption=potype_operator then
  3647. begin
  3648. for t:=NOTOKEN to last_overloaded do
  3649. if procsym.realname='$'+overloaded_names[t] then
  3650. begin
  3651. s:='operator ';
  3652. if (pno_ownername in pno) and
  3653. assigned(struct) then
  3654. s:=s+struct.RttiName+'.';
  3655. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  3656. break;
  3657. end;
  3658. end
  3659. else
  3660. begin
  3661. if (po_classmethod in procoptions) and
  3662. not(pno_noclassmarker in pno) then
  3663. s:='class ';
  3664. case proctypeoption of
  3665. potype_constructor,
  3666. potype_class_constructor:
  3667. s:=s+'constructor ';
  3668. potype_class_destructor,
  3669. potype_destructor:
  3670. s:=s+'destructor ';
  3671. else
  3672. if (pno_proctypeoption in pno) and
  3673. assigned(returndef) and
  3674. not(is_void(returndef)) then
  3675. s:=s+'function '
  3676. else
  3677. s:=s+'procedure ';
  3678. end;
  3679. if (pno_ownername in pno) and
  3680. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  3681. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  3682. rn:=procsym.realname;
  3683. if (pno_noleadingdollar in pno) and
  3684. (rn[1]='$') then
  3685. delete(rn,1,1);
  3686. s:=s+rn+typename_paras(pno);
  3687. end;
  3688. if not(proctypeoption in [potype_constructor,potype_destructor,
  3689. potype_class_constructor,potype_class_destructor]) and
  3690. assigned(returndef) and
  3691. not(is_void(returndef)) then
  3692. s:=s+':'+returndef.GetTypeName;
  3693. if owner.symtabletype=localsymtable then
  3694. s:=s+' is nested';
  3695. s:=s+';';
  3696. { forced calling convention? }
  3697. if (po_hascallingconvention in procoptions) then
  3698. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3699. if (po_staticmethod in procoptions) and
  3700. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3701. s:=s+' Static;';
  3702. customprocname:=s;
  3703. end;
  3704. function tprocdef.is_methodpointer:boolean;
  3705. begin
  3706. { don't check assigned(_class), that's also the case for nested
  3707. procedures inside methods }
  3708. result:=owner.symtabletype=ObjectSymtable;
  3709. end;
  3710. function tprocdef.is_addressonly:boolean;
  3711. begin
  3712. result:=assigned(owner) and
  3713. (owner.symtabletype<>ObjectSymtable) and
  3714. (not(m_nested_procvars in current_settings.modeswitches) or
  3715. not is_nested_pd(self));
  3716. end;
  3717. procedure tprocdef.make_external;
  3718. begin
  3719. include(procoptions,po_external);
  3720. forwarddef:=false;
  3721. end;
  3722. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3723. begin
  3724. case t of
  3725. gs_local :
  3726. GetSymtable:=localst;
  3727. gs_para :
  3728. GetSymtable:=parast;
  3729. else
  3730. GetSymtable:=nil;
  3731. end;
  3732. end;
  3733. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  3734. var
  3735. i : tcallercallee;
  3736. j : longint;
  3737. pvs : tparavarsym;
  3738. begin
  3739. result:=inherited getcopyas(newtyp,copytyp);
  3740. if newtyp=procvardef then
  3741. exit;
  3742. { don't copy mangled name, can be different }
  3743. tprocdef(result).messageinf:=messageinf;
  3744. tprocdef(result).dispid:=dispid;
  3745. if po_msgstr in procoptions then
  3746. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  3747. tprocdef(result).symoptions:=symoptions;
  3748. if assigned(deprecatedmsg) then
  3749. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  3750. { will have to be associated with appropriate procsym }
  3751. tprocdef(result).procsym:=nil;
  3752. if copytyp<>pc_bareproc then
  3753. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  3754. if assigned(funcretsym) then
  3755. begin
  3756. if funcretsym.owner=parast then
  3757. begin
  3758. j:=parast.symlist.indexof(funcretsym);
  3759. if j<0 then
  3760. internalerror(2011040606);
  3761. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  3762. end
  3763. else if funcretsym.owner=localst then
  3764. begin
  3765. { nothing to do, will be inserted for the new procdef while
  3766. parsing its body (by pdecsub.insert_funcret_local) }
  3767. end
  3768. else
  3769. internalerror(2011040605);
  3770. end;
  3771. { will have to be associated with a new struct }
  3772. tprocdef(result).struct:=nil;
  3773. {$if defined(powerpc) or defined(m68k)}
  3774. tprocdef(result).libsym:=libsym;
  3775. {$endif powerpc or m68k}
  3776. if assigned(resultname) then
  3777. tprocdef(result).resultname:=stringdup(resultname^);
  3778. if assigned(import_dll) then
  3779. tprocdef(result).import_dll:=stringdup(import_dll^);
  3780. if assigned(import_name) then
  3781. tprocdef(result).import_name:=stringdup(import_name^);
  3782. tprocdef(result).import_nr:=import_nr;
  3783. tprocdef(result).extnumber:=$ffff;
  3784. {$ifdef i386}
  3785. tprocdef(result).fpu_used:=fpu_used;
  3786. {$endif i386}
  3787. tprocdef(result).visibility:=visibility;
  3788. tprocdef(result).synthetickind:=synthetickind;
  3789. { we need a separate implementation for the copied def }
  3790. tprocdef(result).forwarddef:=true;
  3791. tprocdef(result).interfacedef:=true;
  3792. { create new paralist }
  3793. tprocdef(result).calcparas;
  3794. end;
  3795. function tprocdef.getcopy: tstoreddef;
  3796. begin
  3797. result:=getcopyas(procdef,pc_normal);
  3798. end;
  3799. procedure tprocdef.buildderef;
  3800. begin
  3801. inherited buildderef;
  3802. structderef.build(struct);
  3803. { procsym that originaly defined this definition, should be in the
  3804. same symtable }
  3805. procsymderef.build(procsym);
  3806. {$ifdef powerpc}
  3807. { library symbol for AmigaOS/MorphOS }
  3808. libsymderef.build(libsym);
  3809. {$endif powerpc}
  3810. end;
  3811. procedure tprocdef.buildderefimpl;
  3812. begin
  3813. inherited buildderefimpl;
  3814. { Localst is not available for main/unit init }
  3815. if assigned(localst) then
  3816. begin
  3817. tlocalsymtable(localst).buildderef;
  3818. tlocalsymtable(localst).buildderefimpl;
  3819. end;
  3820. { inline tree }
  3821. if (po_has_inlininginfo in procoptions) then
  3822. begin
  3823. funcretsymderef.build(funcretsym);
  3824. inlininginfo^.code.buildderefimpl;
  3825. end;
  3826. end;
  3827. procedure tprocdef.deref;
  3828. begin
  3829. inherited deref;
  3830. struct:=tabstractrecorddef(structderef.resolve);
  3831. { procsym that originaly defined this definition, should be in the
  3832. same symtable }
  3833. procsym:=tprocsym(procsymderef.resolve);
  3834. {$ifdef powerpc}
  3835. { library symbol for AmigaOS/MorphOS }
  3836. libsym:=tsym(libsymderef.resolve);
  3837. {$endif powerpc}
  3838. end;
  3839. procedure tprocdef.derefimpl;
  3840. begin
  3841. { Enable has_inlininginfo when the inlininginfo
  3842. structure is available. The has_inlininginfo was disabled
  3843. after the load, since the data was invalid }
  3844. if assigned(inlininginfo) then
  3845. include(procoptions,po_has_inlininginfo);
  3846. { Locals }
  3847. if assigned(localst) then
  3848. begin
  3849. tlocalsymtable(localst).deref;
  3850. tlocalsymtable(localst).derefimpl;
  3851. end;
  3852. { Inline }
  3853. if (po_has_inlininginfo in procoptions) then
  3854. begin
  3855. inlininginfo^.code.derefimpl;
  3856. { funcretsym, this is always located in the localst }
  3857. funcretsym:=tsym(funcretsymderef.resolve);
  3858. end
  3859. else
  3860. begin
  3861. { safety }
  3862. { Not safe! A unit may be reresolved after its interface has been
  3863. parsed but before its implementation has been parsed, and in that
  3864. case the funcretsym is still required!
  3865. funcretsym:=nil; }
  3866. end;
  3867. end;
  3868. function tprocdef.GetTypeName : string;
  3869. begin
  3870. GetTypeName := FullProcName(false);
  3871. end;
  3872. function tprocdef.mangledname : TSymStr;
  3873. begin
  3874. {$ifdef symansistr}
  3875. if _mangledname<>'' then
  3876. {$else symansistr}
  3877. if assigned(_mangledname) then
  3878. {$endif symansistr}
  3879. begin
  3880. {$ifdef compress}
  3881. {$error add support for ansistrings in case of symansistr}
  3882. mangledname:=minilzw_decode(_mangledname^);
  3883. {$else}
  3884. {$ifdef symansistr}
  3885. mangledname:=_mangledname;
  3886. {$else symansistr}
  3887. mangledname:=_mangledname^;
  3888. {$endif symansistr}
  3889. {$endif}
  3890. exit;
  3891. end;
  3892. {$ifndef jvm}
  3893. mangledname:=defaultmangledname;
  3894. {$else not jvm}
  3895. mangledname:=jvmmangledbasename(false);
  3896. if (po_has_importdll in procoptions) then
  3897. begin
  3898. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  3899. if assigned(import_dll) then
  3900. mangledname:=import_dll^+'/'+mangledname
  3901. else
  3902. internalerror(2010122607);
  3903. end
  3904. else
  3905. jvmaddtypeownerprefix(owner,mangledname);
  3906. {$endif not jvm}
  3907. {$ifdef compress}
  3908. {$error add support for ansistrings in case of symansistr}
  3909. _mangledname:=stringdup(minilzw_encode(mangledname));
  3910. {$else}
  3911. {$ifdef symansistr}
  3912. _mangledname:=mangledname;
  3913. {$else symansistr}
  3914. _mangledname:=stringdup(mangledname);
  3915. {$endif symansistr}
  3916. {$endif}
  3917. end;
  3918. function tprocdef.defaultmangledname: TSymStr;
  3919. var
  3920. hp : TParavarsym;
  3921. hs : TSymStr;
  3922. crc : dword;
  3923. newlen,
  3924. oldlen,
  3925. i : integer;
  3926. begin
  3927. { we need to use the symtable where the procsym is inserted,
  3928. because that is visible to the world }
  3929. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  3930. oldlen:=length(defaultmangledname);
  3931. { add parameter types }
  3932. for i:=0 to paras.count-1 do
  3933. begin
  3934. hp:=tparavarsym(paras[i]);
  3935. if not(vo_is_hidden_para in hp.varoptions) then
  3936. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  3937. end;
  3938. { add resultdef, add $$ as separator to make it unique from a
  3939. parameter separator }
  3940. if not is_void(returndef) then
  3941. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  3942. newlen:=length(defaultmangledname);
  3943. { Replace with CRC if the parameter line is very long }
  3944. if (newlen-oldlen>12) and
  3945. ((newlen>100) or (newlen-oldlen>64)) then
  3946. begin
  3947. crc:=0;
  3948. for i:=0 to paras.count-1 do
  3949. begin
  3950. hp:=tparavarsym(paras[i]);
  3951. if not(vo_is_hidden_para in hp.varoptions) then
  3952. begin
  3953. hs:=hp.vardef.mangledparaname;
  3954. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3955. end;
  3956. end;
  3957. hs:=hp.vardef.mangledparaname;
  3958. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3959. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3960. end;
  3961. end;
  3962. function tprocdef.cplusplusmangledname : TSymStr;
  3963. function getcppparaname(p : tdef) : TSymStr;
  3964. const
  3965. {$ifdef NAMEMANGLING_GCC2}
  3966. ordtype2str : array[tordtype] of string[2] = (
  3967. '',
  3968. 'Uc','Us','Ui','Us',
  3969. 'Sc','s','i','x',
  3970. 'b','b','b','b','b',
  3971. 'c','w','x');
  3972. {$else NAMEMANGLING_GCC2}
  3973. ordtype2str : array[tordtype] of string[1] = (
  3974. 'v',
  3975. 'h','t','j','y',
  3976. 'a','s','i','x',
  3977. 'b','b','b','b',
  3978. 'b','b','b','b',
  3979. 'c','w','x');
  3980. floattype2str : array[tfloattype] of string[1] = (
  3981. 'f','d','e','e',
  3982. 'd','d','g');
  3983. {$endif NAMEMANGLING_GCC2}
  3984. var
  3985. s : TSymStr;
  3986. begin
  3987. case p.typ of
  3988. orddef:
  3989. s:=ordtype2str[torddef(p).ordtype];
  3990. pointerdef:
  3991. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3992. {$ifndef NAMEMANGLING_GCC2}
  3993. floatdef:
  3994. s:=floattype2str[tfloatdef(p).floattype];
  3995. {$endif NAMEMANGLING_GCC2}
  3996. else
  3997. internalerror(2103001);
  3998. end;
  3999. getcppparaname:=s;
  4000. end;
  4001. var
  4002. s,s2 : TSymStr;
  4003. hp : TParavarsym;
  4004. i : integer;
  4005. begin
  4006. {$ifdef NAMEMANGLING_GCC2}
  4007. { outdated gcc 2.x name mangling scheme }
  4008. s := procsym.realname;
  4009. if procsym.owner.symtabletype=ObjectSymtable then
  4010. begin
  4011. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4012. case proctypeoption of
  4013. potype_destructor:
  4014. s:='_$_'+tostr(length(s2))+s2;
  4015. potype_constructor:
  4016. s:='___'+tostr(length(s2))+s2;
  4017. else
  4018. s:='_'+s+'__'+tostr(length(s2))+s2;
  4019. end;
  4020. end
  4021. else s:=s+'__';
  4022. s:=s+'F';
  4023. { concat modifiers }
  4024. { !!!!! }
  4025. { now we handle the parameters }
  4026. if maxparacount>0 then
  4027. begin
  4028. for i:=0 to paras.count-1 do
  4029. begin
  4030. hp:=tparavarsym(paras[i]);
  4031. { no hidden parameters form part of a C++ mangled name:
  4032. a) self is not included
  4033. b) there are no "high" or other hidden parameters
  4034. }
  4035. if vo_is_hidden_para in hp.varoptions then
  4036. continue;
  4037. s2:=getcppparaname(hp.vardef);
  4038. if hp.varspez in [vs_var,vs_out] then
  4039. s2:='R'+s2;
  4040. s:=s+s2;
  4041. end;
  4042. end
  4043. else
  4044. s:=s+'v';
  4045. cplusplusmangledname:=s;
  4046. {$else NAMEMANGLING_GCC2}
  4047. { gcc 3.x and 4.x name mangling scheme }
  4048. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4049. if procsym.owner.symtabletype=ObjectSymtable then
  4050. begin
  4051. s:='_ZN';
  4052. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4053. s:=s+tostr(length(s2))+s2;
  4054. case proctypeoption of
  4055. potype_constructor:
  4056. s:=s+'C1';
  4057. potype_destructor:
  4058. s:=s+'D1';
  4059. else
  4060. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4061. end;
  4062. s:=s+'E';
  4063. end
  4064. else
  4065. s:=procsym.realname;
  4066. { now we handle the parameters }
  4067. if maxparacount>0 then
  4068. begin
  4069. for i:=0 to paras.count-1 do
  4070. begin
  4071. hp:=tparavarsym(paras[i]);
  4072. { no hidden parameters form part of a C++ mangled name:
  4073. a) self is not included
  4074. b) there are no "high" or other hidden parameters
  4075. }
  4076. if vo_is_hidden_para in hp.varoptions then
  4077. continue;
  4078. s2:=getcppparaname(hp.vardef);
  4079. if hp.varspez in [vs_var,vs_out] then
  4080. s2:='R'+s2;
  4081. s:=s+s2;
  4082. end;
  4083. end
  4084. else
  4085. s:=s+'v';
  4086. cplusplusmangledname:=s;
  4087. {$endif NAMEMANGLING_GCC2}
  4088. end;
  4089. function tprocdef.objcmangledname : TSymStr;
  4090. var
  4091. manglednamelen: longint;
  4092. iscatmethod : boolean;
  4093. begin
  4094. if not (po_msgstr in procoptions) then
  4095. internalerror(2009030901);
  4096. { we may very well need longer strings to handle these... }
  4097. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  4098. length('+"[ ]"')+length(messageinf.str^);
  4099. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  4100. if (iscatmethod) then
  4101. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  4102. if manglednamelen>255 then
  4103. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  4104. if not(po_classmethod in procoptions) then
  4105. result:='"-['
  4106. else
  4107. result:='"+[';
  4108. { quotes are necessary because the +/- otherwise confuse the assembler
  4109. into expecting a number
  4110. }
  4111. if iscatmethod then
  4112. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  4113. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  4114. if iscatmethod then
  4115. result:=result+')';
  4116. result:=result+' '+messageinf.str^+']"';
  4117. end;
  4118. {$ifdef jvm}
  4119. function tprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  4120. var
  4121. vs: tparavarsym;
  4122. i: longint;
  4123. founderror: tdef;
  4124. tmpresult: TSymStr;
  4125. container: tsymtable;
  4126. begin
  4127. { format:
  4128. * method definition (in Jasmin):
  4129. (private|protected|public) [static] method(parametertypes)returntype
  4130. * method invocation
  4131. package/class/method(parametertypes)returntype
  4132. -> store common part: method(parametertypes)returntype and
  4133. adorn as required when using it.
  4134. }
  4135. if not signature then
  4136. begin
  4137. { method name }
  4138. { special names for constructors and class constructors }
  4139. if proctypeoption=potype_constructor then
  4140. tmpresult:='<init>'
  4141. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  4142. tmpresult:='<clinit>'
  4143. else if po_has_importname in procoptions then
  4144. begin
  4145. if assigned(import_name) then
  4146. tmpresult:=import_name^
  4147. else
  4148. internalerror(2010122608);
  4149. end
  4150. else
  4151. begin
  4152. tmpresult:=procsym.realname;
  4153. if tmpresult[1]='$' then
  4154. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  4155. { nested functions }
  4156. container:=owner;
  4157. while container.symtabletype=localsymtable do
  4158. begin
  4159. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  4160. container:=container.defowner.owner;
  4161. end;
  4162. end;
  4163. end
  4164. else
  4165. tmpresult:='';
  4166. { parameter types }
  4167. tmpresult:=tmpresult+'(';
  4168. { not the case for the main program (not required for defaultmangledname
  4169. because setmangledname() is called for the main program; in case of
  4170. the JVM, this only sets the importname, however) }
  4171. if assigned(paras) then
  4172. begin
  4173. init_paraloc_info(callerside);
  4174. for i:=0 to paras.count-1 do
  4175. begin
  4176. vs:=tparavarsym(paras[i]);
  4177. { function result is not part of the mangled name }
  4178. if vo_is_funcret in vs.varoptions then
  4179. exit;
  4180. { self pointer neither, except for class methods (the JVM only
  4181. supports static class methods natively, so the self pointer
  4182. here is a regular parameter as far as the JVM is concerned }
  4183. if not(po_classmethod in procoptions) and
  4184. (vo_is_self in vs.varoptions) then
  4185. continue;
  4186. { passing by reference is emulated by passing an array of one
  4187. element containing the value; for types that aren't pointers
  4188. in regular Pascal, simply passing the underlying pointer type
  4189. does achieve regular call-by-reference semantics though;
  4190. formaldefs always have to be passed like that because their
  4191. contents can be replaced }
  4192. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  4193. tmpresult:=tmpresult+'[';
  4194. { Add the parameter type. }
  4195. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  4196. { an internalerror here is also triggered in case of errors in the source code }
  4197. tmpresult:='<error>';
  4198. end;
  4199. end;
  4200. tmpresult:=tmpresult+')';
  4201. { And the type of the function result (void in case of a procedure and
  4202. constructor). }
  4203. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  4204. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  4205. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  4206. { an internalerror here is also triggered in case of errors in the source code }
  4207. tmpresult:='<error>';
  4208. result:=tmpresult;
  4209. end;
  4210. {$endif jvm}
  4211. procedure tprocdef.setmangledname(const s : TSymStr);
  4212. begin
  4213. { This is not allowed anymore, the forward declaration
  4214. already needs to create the correct mangledname, no changes
  4215. afterwards are allowed (PFV) }
  4216. { Exception: interface definitions in mode macpas, since in that }
  4217. { case no reference to the old name can exist yet (JM) }
  4218. {$ifdef symansistr}
  4219. if _mangledname<>'' then
  4220. if ((m_mac in current_settings.modeswitches) and
  4221. (interfacedef)) then
  4222. _mangledname:=''
  4223. else
  4224. internalerror(200411171);
  4225. {$else symansistr}
  4226. if assigned(_mangledname) then
  4227. if ((m_mac in current_settings.modeswitches) and
  4228. (interfacedef)) then
  4229. stringdispose(_mangledname)
  4230. else
  4231. internalerror(200411171);
  4232. {$endif symansistr}
  4233. {$ifdef jvm}
  4234. { this routine can be called for compilerproces. can't set mangled
  4235. name since it must be calculated, but it uses import_name when set
  4236. -> set that one }
  4237. import_name:=stringdup(s);
  4238. include(procoptions,po_has_importname);
  4239. include(procoptions,po_has_mangledname);
  4240. {$else}
  4241. {$ifdef compress}
  4242. {$error add support for symansistr}
  4243. _mangledname:=stringdup(minilzw_encode(s));
  4244. {$else}
  4245. {$ifdef symansistr}
  4246. _mangledname:=s;
  4247. {$else symansistr}
  4248. _mangledname:=stringdup(s);
  4249. {$endif symansistr}
  4250. {$endif}
  4251. {$endif jvm}
  4252. include(procoptions,po_has_mangledname);
  4253. end;
  4254. {***************************************************************************
  4255. TPROCVARDEF
  4256. ***************************************************************************}
  4257. constructor tprocvardef.create(level:byte);
  4258. begin
  4259. inherited create(procvardef,level);
  4260. end;
  4261. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  4262. begin
  4263. inherited ppuload(procvardef,ppufile);
  4264. { load para symtable }
  4265. parast:=tparasymtable.create(self,ppufile.getbyte);
  4266. {$ifdef jvm}
  4267. ppufile.getderef(classdefderef);
  4268. {$endif}
  4269. tparasymtable(parast).ppuload(ppufile);
  4270. end;
  4271. function tprocvardef.getcopy : tstoreddef;
  4272. var
  4273. i : tcallercallee;
  4274. j : longint;
  4275. begin
  4276. result:=tprocvardef.create(parast.symtablelevel);
  4277. tprocvardef(result).returndef:=returndef;
  4278. tprocvardef(result).returndefderef:=returndefderef;
  4279. tprocvardef(result).parast:=parast.getcopy;
  4280. tprocvardef(result).savesize:=savesize;
  4281. { create paralist copy }
  4282. tprocvardef(result).paras:=tparalist.create(false);
  4283. tprocvardef(result).paras.count:=paras.count;
  4284. for j:=0 to paras.count-1 do
  4285. tprocvardef(result).paras[j]:=paras[j];
  4286. tprocvardef(result).proctypeoption:=proctypeoption;
  4287. tprocvardef(result).proccalloption:=proccalloption;
  4288. tprocvardef(result).procoptions:=procoptions;
  4289. tprocvardef(result).callerargareasize:=callerargareasize;
  4290. tprocvardef(result).calleeargareasize:=calleeargareasize;
  4291. tprocvardef(result).maxparacount:=maxparacount;
  4292. tprocvardef(result).minparacount:=minparacount;
  4293. for i:=low(tcallercallee) to high(tcallercallee) do
  4294. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  4295. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  4296. {$ifdef m68k}
  4297. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  4298. {$endif}
  4299. {$ifdef jvm}
  4300. tprocvardef(result).classdef:=classdef;
  4301. {$endif}
  4302. end;
  4303. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  4304. begin
  4305. inherited ppuwrite(ppufile);
  4306. { Save the para symtable level (necessary to distinguish nested
  4307. procvars) }
  4308. ppufile.putbyte(parast.symtablelevel);
  4309. {$ifdef jvm}
  4310. ppufile.putderef(classdefderef);
  4311. {$endif}
  4312. { Write this entry }
  4313. ppufile.writeentry(ibprocvardef);
  4314. { Save the para symtable, this is taken from the interface }
  4315. tparasymtable(parast).ppuwrite(ppufile);
  4316. end;
  4317. {$ifdef jvm}
  4318. procedure tprocvardef.buildderef;
  4319. begin
  4320. inherited buildderef;
  4321. classdefderef.build(classdef);
  4322. end;
  4323. procedure tprocvardef.deref;
  4324. begin
  4325. inherited deref;
  4326. classdef:=tobjectdef(classdefderef.resolve);
  4327. end;
  4328. {$endif}
  4329. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  4330. begin
  4331. case t of
  4332. gs_para :
  4333. GetSymtable:=parast;
  4334. else
  4335. GetSymtable:=nil;
  4336. end;
  4337. end;
  4338. function tprocvardef.size : asizeint;
  4339. begin
  4340. if ((po_methodpointer in procoptions) or
  4341. is_nested_pd(self)) and
  4342. not(po_addressonly in procoptions) then
  4343. size:=2*sizeof(pint)
  4344. else
  4345. size:=sizeof(pint);
  4346. end;
  4347. function tprocvardef.is_methodpointer:boolean;
  4348. begin
  4349. result:=(po_methodpointer in procoptions);
  4350. end;
  4351. function tprocvardef.is_addressonly:boolean;
  4352. begin
  4353. result:=(not(po_methodpointer in procoptions) and
  4354. not is_nested_pd(self)) or
  4355. (po_addressonly in procoptions);
  4356. end;
  4357. function tprocvardef.getmangledparaname:TSymStr;
  4358. begin
  4359. if not(po_methodpointer in procoptions) then
  4360. if not is_nested_pd(self) then
  4361. result:='procvar'
  4362. else
  4363. result:='nestedprovar'
  4364. else
  4365. result:='procvarofobj'
  4366. end;
  4367. function tprocvardef.is_publishable : boolean;
  4368. begin
  4369. is_publishable:=(po_methodpointer in procoptions);
  4370. end;
  4371. function tprocvardef.GetTypeName : string;
  4372. var
  4373. s: string;
  4374. pno : tprocnameoptions;
  4375. begin
  4376. {$ifdef EXTDEBUG}
  4377. pno:=[pno_showhidden];
  4378. {$else EXTDEBUG}
  4379. pno:=[];
  4380. {$endif EXTDEBUG}
  4381. s:='<';
  4382. if po_classmethod in procoptions then
  4383. s := s+'class method type of'
  4384. else
  4385. if po_addressonly in procoptions then
  4386. s := s+'address of'
  4387. else
  4388. s := s+'procedure variable type of';
  4389. if assigned(returndef) and
  4390. (returndef<>voidtype) then
  4391. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  4392. else
  4393. s:=s+' procedure'+typename_paras(pno);
  4394. if po_methodpointer in procoptions then
  4395. s := s+' of object';
  4396. if is_nested_pd(self) then
  4397. s := s+' is nested';
  4398. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  4399. end;
  4400. {***************************************************************************
  4401. TOBJECTDEF
  4402. ***************************************************************************}
  4403. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  4404. begin
  4405. inherited create(n,objectdef);
  4406. fcurrent_dispid:=0;
  4407. objecttype:=ot;
  4408. childof:=nil;
  4409. if objecttype=odt_helper then
  4410. owner.includeoption(sto_has_helper);
  4411. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  4412. { create space for vmt !! }
  4413. vmtentries:=TFPList.Create;
  4414. vmt_offset:=0;
  4415. set_parent(c);
  4416. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  4417. prepareguid;
  4418. { setup implemented interfaces }
  4419. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  4420. ImplementedInterfaces:=TFPObjectList.Create(true)
  4421. else
  4422. ImplementedInterfaces:=nil;
  4423. writing_class_record_dbginfo:=false;
  4424. end;
  4425. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  4426. var
  4427. i,
  4428. implintfcount : longint;
  4429. d : tderef;
  4430. ImplIntf : TImplementedInterface;
  4431. vmtentry : pvmtentry;
  4432. begin
  4433. inherited ppuload(objectdef,ppufile);
  4434. objecttype:=tobjecttyp(ppufile.getbyte);
  4435. objextname:=stringdup(ppufile.getstring);
  4436. { only used for external Objective-C classes/protocols }
  4437. if (objextname^='') then
  4438. stringdispose(objextname);
  4439. symtable:=tObjectSymtable.create(self,objrealname^,0);
  4440. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  4441. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  4442. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4443. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4444. vmt_offset:=ppufile.getlongint;
  4445. ppufile.getderef(childofderef);
  4446. { load guid }
  4447. iidstr:=nil;
  4448. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4449. begin
  4450. new(iidguid);
  4451. ppufile.getguid(iidguid^);
  4452. iidstr:=stringdup(ppufile.getstring);
  4453. end;
  4454. abstractcnt:=ppufile.getlongint;
  4455. if objecttype=odt_helper then
  4456. ppufile.getderef(extendeddefderef);
  4457. vmtentries:=TFPList.Create;
  4458. vmtentries.count:=ppufile.getlongint;
  4459. for i:=0 to vmtentries.count-1 do
  4460. begin
  4461. ppufile.getderef(d);
  4462. new(vmtentry);
  4463. vmtentry^.procdef:=nil;
  4464. vmtentry^.procdefderef:=d;
  4465. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  4466. vmtentries[i]:=vmtentry;
  4467. end;
  4468. { load implemented interfaces }
  4469. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  4470. begin
  4471. ImplementedInterfaces:=TFPObjectList.Create(true);
  4472. implintfcount:=ppufile.getlongint;
  4473. for i:=0 to implintfcount-1 do
  4474. begin
  4475. ppufile.getderef(d);
  4476. ImplIntf:=TImplementedInterface.Create_deref(d);
  4477. ImplIntf.IOffset:=ppufile.getlongint;
  4478. ImplementedInterfaces.Add(ImplIntf);
  4479. end;
  4480. end
  4481. else
  4482. ImplementedInterfaces:=nil;
  4483. if df_copied_def in defoptions then
  4484. ppufile.getderef(cloneddefderef)
  4485. else
  4486. tObjectSymtable(symtable).ppuload(ppufile);
  4487. { handles the predefined class tobject }
  4488. { the last TOBJECT which is loaded gets }
  4489. { it ! }
  4490. if (childof=nil) and
  4491. (objecttype in [odt_class,odt_javaclass]) and
  4492. (objname^='TOBJECT') then
  4493. class_tobject:=self;
  4494. if (childof=nil) and
  4495. (objecttype=odt_interfacecom) then
  4496. if (objname^='IUNKNOWN') then
  4497. interface_iunknown:=self
  4498. else
  4499. if (objname^='IDISPATCH') then
  4500. interface_idispatch:=self;
  4501. if (childof=nil) and
  4502. (objecttype=odt_objcclass) and
  4503. (objname^='PROTOCOL') then
  4504. objc_protocoltype:=self;
  4505. if (objecttype=odt_javaclass) and
  4506. not(oo_is_formal in objectoptions) then
  4507. begin
  4508. if (objname^='JLOBJECT') then
  4509. java_jlobject:=self
  4510. else if (objname^='JLTHROWABLE') then
  4511. java_jlthrowable:=self
  4512. else if (objname^='FPCBASERECORDTYPE') then
  4513. java_fpcbaserecordtype:=self
  4514. else if (objname^='JLSTRING') then
  4515. java_jlstring:=self
  4516. else if (objname^='ANSISTRINGCLASS') then
  4517. java_ansistring:=self
  4518. else if (objname^='SHORTSTRINGCLASS') then
  4519. java_shortstring:=self
  4520. else if (objname^='JLENUM') then
  4521. java_jlenum:=self
  4522. else if (objname^='JUENUMSET') then
  4523. java_juenumset:=self
  4524. else if (objname^='FPCBITSET') then
  4525. java_jubitset:=self
  4526. else if (objname^='FPCBASEPROCVARTYPE') then
  4527. java_procvarbase:=self;
  4528. end;
  4529. writing_class_record_dbginfo:=false;
  4530. end;
  4531. destructor tobjectdef.destroy;
  4532. begin
  4533. if assigned(symtable) then
  4534. begin
  4535. symtable.free;
  4536. symtable:=nil;
  4537. end;
  4538. stringdispose(objextname);
  4539. stringdispose(iidstr);
  4540. if assigned(ImplementedInterfaces) then
  4541. begin
  4542. ImplementedInterfaces.free;
  4543. ImplementedInterfaces:=nil;
  4544. end;
  4545. if assigned(iidguid) then
  4546. begin
  4547. dispose(iidguid);
  4548. iidguid:=nil;
  4549. end;
  4550. if assigned(vmtentries) then
  4551. begin
  4552. resetvmtentries;
  4553. vmtentries.free;
  4554. vmtentries:=nil;
  4555. end;
  4556. if assigned(vmcallstaticinfo) then
  4557. begin
  4558. freemem(vmcallstaticinfo);
  4559. vmcallstaticinfo:=nil;
  4560. end;
  4561. inherited destroy;
  4562. end;
  4563. function tobjectdef.getcopy : tstoreddef;
  4564. var
  4565. i : longint;
  4566. begin
  4567. result:=tobjectdef.create(objecttype,objrealname^,childof);
  4568. { the constructor allocates a symtable which we release to avoid memory leaks }
  4569. tobjectdef(result).symtable.free;
  4570. tobjectdef(result).symtable:=symtable.getcopy;
  4571. if assigned(objextname) then
  4572. tobjectdef(result).objextname:=stringdup(objextname^);
  4573. if assigned(import_lib) then
  4574. tobjectdef(result).import_lib:=stringdup(import_lib^);
  4575. tobjectdef(result).objectoptions:=objectoptions;
  4576. include(tobjectdef(result).defoptions,df_copied_def);
  4577. tobjectdef(result).extendeddef:=extendeddef;
  4578. if assigned(tcinitcode) then
  4579. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  4580. tobjectdef(result).vmt_offset:=vmt_offset;
  4581. if assigned(iidguid) then
  4582. begin
  4583. new(tobjectdef(result).iidguid);
  4584. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  4585. end;
  4586. if assigned(iidstr) then
  4587. tobjectdef(result).iidstr:=stringdup(iidstr^);
  4588. tobjectdef(result).abstractcnt:=abstractcnt;
  4589. if assigned(ImplementedInterfaces) then
  4590. begin
  4591. for i:=0 to ImplementedInterfaces.count-1 do
  4592. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  4593. end;
  4594. if assigned(vmtentries) then
  4595. begin
  4596. tobjectdef(result).vmtentries:=TFPList.Create;
  4597. tobjectdef(result).copyvmtentries(self);
  4598. end;
  4599. end;
  4600. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  4601. var
  4602. i : longint;
  4603. vmtentry : pvmtentry;
  4604. ImplIntf : TImplementedInterface;
  4605. old_do_indirect_crc: boolean;
  4606. begin
  4607. { if class1 in unit A changes, and class2 in unit B inherits from it
  4608. (so unit B uses unit A), then unit B with class2 will be recompiled.
  4609. However, if there is also a class3 in unit C that only depends on
  4610. unit B, then unit C will not be recompiled because nothing changed
  4611. to the interface of unit B. Nevertheless, unit C can indirectly
  4612. depend on unit A via derefs, and these must be updated -> the
  4613. indirect crc keeps track of such changes. }
  4614. old_do_indirect_crc:=ppufile.do_indirect_crc;
  4615. ppufile.do_indirect_crc:=true;
  4616. inherited ppuwrite(ppufile);
  4617. ppufile.putbyte(byte(objecttype));
  4618. if assigned(objextname) then
  4619. ppufile.putstring(objextname^)
  4620. else
  4621. ppufile.putstring('');
  4622. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  4623. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  4624. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  4625. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  4626. ppufile.putlongint(vmt_offset);
  4627. ppufile.putderef(childofderef);
  4628. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4629. begin
  4630. ppufile.putguid(iidguid^);
  4631. ppufile.putstring(iidstr^);
  4632. end;
  4633. ppufile.putlongint(abstractcnt);
  4634. if objecttype=odt_helper then
  4635. ppufile.putderef(extendeddefderef);
  4636. ppufile.putlongint(vmtentries.count);
  4637. for i:=0 to vmtentries.count-1 do
  4638. begin
  4639. vmtentry:=pvmtentry(vmtentries[i]);
  4640. ppufile.putderef(vmtentry^.procdefderef);
  4641. ppufile.putbyte(byte(vmtentry^.visibility));
  4642. end;
  4643. if assigned(ImplementedInterfaces) then
  4644. begin
  4645. ppufile.putlongint(ImplementedInterfaces.Count);
  4646. for i:=0 to ImplementedInterfaces.Count-1 do
  4647. begin
  4648. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4649. ppufile.putderef(ImplIntf.intfdefderef);
  4650. ppufile.putlongint(ImplIntf.Ioffset);
  4651. end;
  4652. end;
  4653. if df_copied_def in defoptions then
  4654. ppufile.putderef(cloneddefderef);
  4655. ppufile.writeentry(ibobjectdef);
  4656. if not(df_copied_def in defoptions) then
  4657. tObjectSymtable(symtable).ppuwrite(ppufile);
  4658. ppufile.do_indirect_crc:=old_do_indirect_crc;
  4659. end;
  4660. function tobjectdef.GetTypeName:string;
  4661. begin
  4662. { in this case we will go in endless recursion, because then }
  4663. { there is no tsym associated yet with the def. It can occur }
  4664. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  4665. { instead of the actual type name }
  4666. if not assigned(typesym) then
  4667. result:='<Currently Parsed Class>'
  4668. else
  4669. result:=typesymbolprettyname;
  4670. end;
  4671. procedure tobjectdef.buildderef;
  4672. var
  4673. i : longint;
  4674. vmtentry : pvmtentry;
  4675. begin
  4676. inherited buildderef;
  4677. childofderef.build(childof);
  4678. if df_copied_def in defoptions then
  4679. cloneddefderef.build(symtable.defowner)
  4680. else
  4681. tstoredsymtable(symtable).buildderef;
  4682. if objecttype=odt_helper then
  4683. extendeddefderef.build(extendeddef);
  4684. for i:=0 to vmtentries.count-1 do
  4685. begin
  4686. vmtentry:=pvmtentry(vmtentries[i]);
  4687. vmtentry^.procdefderef.build(vmtentry^.procdef);
  4688. end;
  4689. if assigned(ImplementedInterfaces) then
  4690. begin
  4691. for i:=0 to ImplementedInterfaces.count-1 do
  4692. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  4693. end;
  4694. end;
  4695. procedure tobjectdef.deref;
  4696. var
  4697. i : longint;
  4698. vmtentry : pvmtentry;
  4699. begin
  4700. inherited deref;
  4701. childof:=tobjectdef(childofderef.resolve);
  4702. if df_copied_def in defoptions then
  4703. begin
  4704. cloneddef:=tobjectdef(cloneddefderef.resolve);
  4705. symtable:=cloneddef.symtable.getcopy;
  4706. end
  4707. else
  4708. tstoredsymtable(symtable).deref;
  4709. if objecttype=odt_helper then
  4710. extendeddef:=tobjectdef(extendeddefderef.resolve);
  4711. for i:=0 to vmtentries.count-1 do
  4712. begin
  4713. vmtentry:=pvmtentry(vmtentries[i]);
  4714. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  4715. end;
  4716. if assigned(ImplementedInterfaces) then
  4717. begin
  4718. for i:=0 to ImplementedInterfaces.count-1 do
  4719. TImplementedInterface(ImplementedInterfaces[i]).deref;
  4720. end;
  4721. end;
  4722. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  4723. var
  4724. pd: tprocdef absolute def;
  4725. st: tsymtable;
  4726. psym: tsym;
  4727. nname: TIDString;
  4728. begin
  4729. if (tdef(def).typ<>procdef) then
  4730. exit;
  4731. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  4732. owner = symtable in which objcclassdef is defined
  4733. }
  4734. st:=pd.owner.defowner.owner;
  4735. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  4736. { check for an existing procsym with our special name }
  4737. psym:=tsym(st.find(nname));
  4738. if not assigned(psym) then
  4739. begin
  4740. psym:=tprocsym.create(nname);
  4741. { avoid warning about this symbol being unused }
  4742. psym.IncRefCount;
  4743. { don't check for duplicates:
  4744. a) we checked above
  4745. b) in case we are in the implementation section of a unit, this
  4746. will also check for this symbol in the interface section
  4747. (since you normally cannot have symbols with the same name
  4748. both interface and implementation), and it's possible to
  4749. have class helpers for the same class in the interface and
  4750. in the implementation, and they cannot be merged since only
  4751. the once in the interface must be saved to the ppu/visible
  4752. from other units }
  4753. st.insert(psym,false);
  4754. end
  4755. else if (psym.typ<>procsym) then
  4756. internalerror(2009111501);
  4757. { add ourselves to this special procsym }
  4758. tprocsym(psym).procdeflist.add(def);
  4759. end;
  4760. procedure tobjectdef.buildderefimpl;
  4761. begin
  4762. inherited buildderefimpl;
  4763. if not (df_copied_def in defoptions) then
  4764. tstoredsymtable(symtable).buildderefimpl;
  4765. end;
  4766. procedure tobjectdef.derefimpl;
  4767. begin
  4768. inherited derefimpl;
  4769. if not (df_copied_def in defoptions) then
  4770. tstoredsymtable(symtable).derefimpl;
  4771. { the procdefs are not owned by the class helper procsyms, so they
  4772. are not stored/restored either -> re-add them here }
  4773. if (objecttype=odt_objcclass) or
  4774. (oo_is_classhelper in objectoptions) then
  4775. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  4776. end;
  4777. procedure tobjectdef.resetvmtentries;
  4778. var
  4779. i : longint;
  4780. begin
  4781. for i:=0 to vmtentries.Count-1 do
  4782. Dispose(pvmtentry(vmtentries[i]));
  4783. vmtentries.clear;
  4784. end;
  4785. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  4786. var
  4787. i : longint;
  4788. vmtentry : pvmtentry;
  4789. begin
  4790. resetvmtentries;
  4791. vmtentries.count:=objdef.vmtentries.count;
  4792. for i:=0 to objdef.vmtentries.count-1 do
  4793. begin
  4794. new(vmtentry);
  4795. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  4796. vmtentries[i]:=vmtentry;
  4797. end;
  4798. end;
  4799. function tobjectdef.getparentdef:tdef;
  4800. begin
  4801. { TODO: Remove getparentdef hack}
  4802. { With 2 forward declared classes with the child class before the
  4803. parent class the child class is written earlier to the ppu. Leaving it
  4804. possible to have a reference to the parent class for property overriding,
  4805. but the parent class still has the childof not resolved yet (PFV) }
  4806. if childof=nil then
  4807. childof:=tobjectdef(childofderef.resolve);
  4808. result:=childof;
  4809. end;
  4810. procedure tobjectdef.prepareguid;
  4811. begin
  4812. { set up guid }
  4813. if not assigned(iidguid) then
  4814. begin
  4815. new(iidguid);
  4816. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  4817. end;
  4818. { setup iidstring }
  4819. if not assigned(iidstr) then
  4820. iidstr:=stringdup(''); { default is empty string }
  4821. end;
  4822. procedure tobjectdef.set_parent( c : tobjectdef);
  4823. begin
  4824. if assigned(childof) then
  4825. exit;
  4826. childof:=c;
  4827. if not assigned(c) then
  4828. exit;
  4829. { inherit options and status }
  4830. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  4831. { initially has the same number of abstract methods as the parent }
  4832. abstractcnt:=c.abstractcnt;
  4833. { add the data of the anchestor class/object }
  4834. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  4835. begin
  4836. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  4837. { inherit recordalignment }
  4838. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  4839. { if both the parent and this record use C-alignment, also inherit
  4840. the current field alignment }
  4841. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  4842. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  4843. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  4844. { the padding is not inherited for Objective-C classes (maybe not
  4845. for cppclass either?) }
  4846. if objecttype=odt_objcclass then
  4847. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  4848. if (oo_has_vmt in objectoptions) and
  4849. (oo_has_vmt in c.objectoptions) then
  4850. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  4851. { if parent has a vmt field then the offset is the same for the child PM }
  4852. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  4853. begin
  4854. vmt_offset:=c.vmt_offset;
  4855. include(objectoptions,oo_has_vmt);
  4856. end;
  4857. end;
  4858. end;
  4859. procedure tobjectdef.insertvmt;
  4860. var
  4861. vs: tfieldvarsym;
  4862. begin
  4863. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  4864. exit;
  4865. if (oo_has_vmt in objectoptions) then
  4866. internalerror(12345)
  4867. else
  4868. begin
  4869. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  4870. tObjectSymtable(symtable).fieldalignment);
  4871. if (tf_requires_proper_alignment in target_info.flags) then
  4872. begin
  4873. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  4874. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  4875. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  4876. end;
  4877. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  4878. hidesym(vs);
  4879. tObjectSymtable(symtable).insert(vs);
  4880. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  4881. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  4882. vmt_offset:=vs.fieldoffset
  4883. else
  4884. vmt_offset:=vs.fieldoffset div 8;
  4885. include(objectoptions,oo_has_vmt);
  4886. end;
  4887. end;
  4888. procedure tobjectdef.check_forwards;
  4889. begin
  4890. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  4891. inherited;
  4892. if (oo_is_forward in objectoptions) then
  4893. begin
  4894. { ok, in future, the forward can be resolved }
  4895. Message1(sym_e_class_forward_not_resolved,objrealname^);
  4896. exclude(objectoptions,oo_is_forward);
  4897. end;
  4898. end;
  4899. { true if prot implements d (or if they are equal) }
  4900. function is_related_interface_multiple(prot: tobjectdef; d : tdef) : boolean;
  4901. var
  4902. i : longint;
  4903. begin
  4904. { objcprotocols have multiple inheritance, all protocols from which
  4905. the current protocol inherits are stored in implementedinterfaces }
  4906. result:=prot=d;
  4907. if result then
  4908. exit;
  4909. for i:=0 to prot.ImplementedInterfaces.count-1 do
  4910. begin
  4911. result:=is_related_interface_multiple(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  4912. if result then
  4913. exit;
  4914. end;
  4915. end;
  4916. { true, if self inherits from d (or if they are equal) }
  4917. function tobjectdef.is_related(d : tdef) : boolean;
  4918. var
  4919. hp : tobjectdef;
  4920. realself: tobjectdef;
  4921. begin
  4922. if (d.typ=objectdef) then
  4923. d:=find_real_class_definition(tobjectdef(d),false);
  4924. realself:=find_real_class_definition(self,false);
  4925. if realself=d then
  4926. begin
  4927. is_related:=true;
  4928. exit;
  4929. end;
  4930. if (d.typ<>objectdef) then
  4931. begin
  4932. is_related:=false;
  4933. exit;
  4934. end;
  4935. { Objective-C protocols and Java interfaces can use multiple
  4936. inheritance }
  4937. if (objecttype in [odt_objcprotocol,odt_interfacejava]) then
  4938. begin
  4939. is_related:=is_related_interface_multiple(realself,d);
  4940. exit
  4941. end;
  4942. { formally declared Objective-C and Java classes match Objective-C/Java
  4943. classes with the same name. In case of Java, the package must also
  4944. match}
  4945. if (objecttype in [odt_objcclass,odt_javaclass]) and
  4946. (tobjectdef(d).objecttype=objecttype) and
  4947. ((oo_is_formal in objectoptions) or
  4948. (oo_is_formal in tobjectdef(d).objectoptions)) and
  4949. (objrealname^=tobjectdef(d).objrealname^) then
  4950. begin
  4951. { check package name for Java }
  4952. if objecttype=odt_objcclass then
  4953. is_related:=true
  4954. else
  4955. begin
  4956. is_related:=
  4957. assigned(import_lib)=assigned(tobjectdef(d).import_lib);
  4958. if is_related and
  4959. assigned(import_lib) then
  4960. is_related:=import_lib^=tobjectdef(d).import_lib^;
  4961. end;
  4962. exit;
  4963. end;
  4964. hp:=realself.childof;
  4965. while assigned(hp) do
  4966. begin
  4967. if hp=d then
  4968. begin
  4969. is_related:=true;
  4970. exit;
  4971. end;
  4972. hp:=hp.childof;
  4973. end;
  4974. is_related:=false;
  4975. end;
  4976. function tobjectdef.find_destructor: tprocdef;
  4977. var
  4978. objdef: tobjectdef;
  4979. begin
  4980. objdef:=self;
  4981. while assigned(objdef) do
  4982. begin
  4983. result:=objdef.find_procdef_bytype(potype_destructor);
  4984. if assigned(result) then
  4985. exit;
  4986. objdef:=objdef.childof;
  4987. end;
  4988. result:=nil;
  4989. end;
  4990. function tobjectdef.implements_any_interfaces: boolean;
  4991. begin
  4992. result := (ImplementedInterfaces.Count > 0) or
  4993. (assigned(childof) and childof.implements_any_interfaces);
  4994. end;
  4995. function tobjectdef.size : asizeint;
  4996. begin
  4997. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  4998. result:=sizeof(pint)
  4999. else
  5000. result:=tObjectSymtable(symtable).datasize;
  5001. end;
  5002. function tobjectdef.alignment:shortint;
  5003. begin
  5004. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5005. alignment:=sizeof(pint)
  5006. else
  5007. alignment:=tObjectSymtable(symtable).recordalignment;
  5008. end;
  5009. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5010. begin
  5011. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5012. case objecttype of
  5013. odt_class:
  5014. { the +2*sizeof(pint) is size and -size }
  5015. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  5016. odt_helper,
  5017. odt_objcclass,
  5018. odt_objcprotocol:
  5019. vmtmethodoffset:=0;
  5020. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5021. vmtmethodoffset:=index*sizeof(pint);
  5022. odt_javaclass,
  5023. odt_interfacejava:
  5024. { invalid }
  5025. vmtmethodoffset:=-1;
  5026. else
  5027. {$ifdef WITHDMT}
  5028. vmtmethodoffset:=(index+4)*sizeof(pint);
  5029. {$else WITHDMT}
  5030. vmtmethodoffset:=(index+3)*sizeof(pint);
  5031. {$endif WITHDMT}
  5032. end;
  5033. end;
  5034. function tobjectdef.vmt_mangledname : TSymStr;
  5035. begin
  5036. if not(oo_has_vmt in objectoptions) then
  5037. Message1(parser_n_object_has_no_vmt,objrealname^);
  5038. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5039. end;
  5040. function tobjectdef.needs_inittable : boolean;
  5041. begin
  5042. case objecttype of
  5043. odt_helper,
  5044. odt_class :
  5045. needs_inittable:=false;
  5046. odt_dispinterface,
  5047. odt_interfacecom:
  5048. needs_inittable:=true;
  5049. odt_interfacecorba:
  5050. needs_inittable:=is_related(interface_iunknown);
  5051. odt_object:
  5052. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  5053. odt_cppclass,
  5054. odt_objcclass,
  5055. odt_objcprotocol,
  5056. odt_javaclass,
  5057. odt_interfacejava:
  5058. needs_inittable:=false;
  5059. else
  5060. internalerror(200108267);
  5061. end;
  5062. end;
  5063. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5064. begin
  5065. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5066. result:=inherited rtti_mangledname(rt)
  5067. else
  5068. begin
  5069. { necessary in case of a dynamic array of nsobject, or
  5070. if an nsobject field appears in a record that needs
  5071. init/finalisation }
  5072. if rt=initrtti then
  5073. begin
  5074. result:=voidpointertype.rtti_mangledname(rt);
  5075. exit;
  5076. end;
  5077. if not(target_info.system in systems_objc_nfabi) then
  5078. begin
  5079. result:=target_asm.labelprefix;
  5080. case objecttype of
  5081. odt_objcclass:
  5082. begin
  5083. case rt of
  5084. objcclassrtti:
  5085. if not(oo_is_classhelper in objectoptions) then
  5086. result:=result+'_OBJC_CLASS_'
  5087. else
  5088. result:=result+'_OBJC_CATEGORY_';
  5089. objcmetartti:
  5090. if not(oo_is_classhelper in objectoptions) then
  5091. result:=result+'_OBJC_METACLASS_'
  5092. else
  5093. internalerror(2009111511);
  5094. else
  5095. internalerror(2009092302);
  5096. end;
  5097. end;
  5098. odt_objcprotocol:
  5099. result:=result+'_OBJC_PROTOCOL_';
  5100. end;
  5101. end
  5102. else
  5103. begin
  5104. case objecttype of
  5105. odt_objcclass:
  5106. begin
  5107. if (oo_is_classhelper in objectoptions) and
  5108. (rt<>objcclassrtti) then
  5109. internalerror(2009111512);
  5110. case rt of
  5111. objcclassrtti:
  5112. if not(oo_is_classhelper in objectoptions) then
  5113. result:='_OBJC_CLASS_$_'
  5114. else
  5115. result:='_OBJC_$_CATEGORY_';
  5116. objcmetartti:
  5117. result:='_OBJC_METACLASS_$_';
  5118. objcclassrortti:
  5119. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5120. objcmetarortti:
  5121. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5122. else
  5123. internalerror(2009092303);
  5124. end;
  5125. end;
  5126. odt_objcprotocol:
  5127. begin
  5128. result:=lower(target_asm.labelprefix);
  5129. case rt of
  5130. objcclassrtti:
  5131. result:=result+'_OBJC_PROTOCOL_$_';
  5132. objcmetartti:
  5133. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5134. else
  5135. internalerror(2009092501);
  5136. end;
  5137. end;
  5138. end;
  5139. end;
  5140. result:=result+objextname^;
  5141. end;
  5142. end;
  5143. function tobjectdef.members_need_inittable : boolean;
  5144. begin
  5145. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5146. end;
  5147. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  5148. var
  5149. ImplIntf : TImplementedInterface;
  5150. i : longint;
  5151. begin
  5152. result:=nil;
  5153. if not assigned(ImplementedInterfaces) then
  5154. exit;
  5155. for i:=0 to ImplementedInterfaces.Count-1 do
  5156. begin
  5157. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5158. if ImplIntf.intfdef=aintfdef then
  5159. begin
  5160. result:=ImplIntf;
  5161. exit;
  5162. end;
  5163. end;
  5164. end;
  5165. function tobjectdef.is_publishable : boolean;
  5166. begin
  5167. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5168. end;
  5169. function tobjectdef.get_next_dispid: longint;
  5170. begin
  5171. inc(fcurrent_dispid);
  5172. result:=fcurrent_dispid;
  5173. end;
  5174. function tobjectdef.search_enumerator_get: tprocdef;
  5175. begin
  5176. result:=inherited;
  5177. if not assigned(result) and assigned(childof) then
  5178. result:=childof.search_enumerator_get;
  5179. end;
  5180. function tobjectdef.search_enumerator_move: tprocdef;
  5181. begin
  5182. result:=inherited;
  5183. if not assigned(result) and assigned(childof) then
  5184. result:=childof.search_enumerator_move;
  5185. end;
  5186. function tobjectdef.search_enumerator_current: tsym;
  5187. begin
  5188. result:=inherited;
  5189. if not assigned(result) and assigned(childof) then
  5190. result:=childof.search_enumerator_current;
  5191. end;
  5192. procedure tobjectdef.register_created_classref_type;
  5193. begin
  5194. if not classref_created_in_current_module then
  5195. begin
  5196. classref_created_in_current_module:=true;
  5197. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5198. end;
  5199. end;
  5200. procedure tobjectdef.register_created_object_type;
  5201. begin
  5202. if not created_in_current_module then
  5203. begin
  5204. created_in_current_module:=true;
  5205. current_module.wpoinfo.addcreatedobjtype(self);
  5206. end;
  5207. end;
  5208. procedure tobjectdef.register_maybe_created_object_type;
  5209. begin
  5210. { if we know it has been created for sure, no need
  5211. to also record that it maybe can be created in
  5212. this module
  5213. }
  5214. if not (created_in_current_module) and
  5215. not (maybe_created_in_current_module) then
  5216. begin
  5217. maybe_created_in_current_module:=true;
  5218. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5219. end;
  5220. end;
  5221. procedure tobjectdef.register_vmt_call(index: longint);
  5222. begin
  5223. if (is_object(self) or is_class(self)) then
  5224. current_module.wpoinfo.addcalledvmtentry(self,index);
  5225. end;
  5226. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5227. var
  5228. def: tdef absolute data;
  5229. pd: tprocdef absolute data;
  5230. i,
  5231. paracount: longint;
  5232. begin
  5233. if (def.typ=procdef) then
  5234. begin
  5235. { add all messages also under a dummy name to the symtable in
  5236. which the objcclass/protocol/category is declared, so they can
  5237. be called via id.<name>
  5238. }
  5239. create_class_helper_for_procdef(pd,nil);
  5240. { we have to wait until now to set the mangled name because it
  5241. depends on the (possibly external) class name, which is defined
  5242. at the very end. }
  5243. if not(po_msgstr in pd.procoptions) then
  5244. begin
  5245. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5246. { recover to avoid internalerror later on }
  5247. include(pd.procoptions,po_msgstr);
  5248. pd.messageinf.str:=stringdup('MissingDeclaration');
  5249. end;
  5250. { Mangled name is already set in case this is a copy of
  5251. another type. }
  5252. if not(po_has_mangledname in pd.procoptions) then
  5253. begin
  5254. { check whether the number of formal parameters is correct,
  5255. and whether they have valid Objective-C types }
  5256. paracount:=0;
  5257. for i:=1 to length(pd.messageinf.str^) do
  5258. if pd.messageinf.str^[i]=':' then
  5259. inc(paracount);
  5260. for i:=0 to pd.paras.count-1 do
  5261. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5262. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5263. dec(paracount);
  5264. if (paracount<>0) then
  5265. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5266. pd.setmangledname(pd.objcmangledname);
  5267. end
  5268. else
  5269. { all checks already done }
  5270. exit;
  5271. if not(oo_is_external in pd.struct.objectoptions) then
  5272. begin
  5273. if (po_varargs in pd.procoptions) then
  5274. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  5275. else
  5276. begin
  5277. { check for "array of const" parameters }
  5278. for i:=0 to pd.parast.symlist.count-1 do
  5279. begin
  5280. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  5281. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  5282. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  5283. end;
  5284. end;
  5285. end;
  5286. end;
  5287. end;
  5288. procedure mark_private_fields_used(data: tobject; arg: pointer);
  5289. var
  5290. sym: tsym absolute data;
  5291. begin
  5292. if (sym.typ=fieldvarsym) and
  5293. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  5294. sym.IncRefCount;
  5295. end;
  5296. procedure tobjectdef.finish_objc_data;
  5297. begin
  5298. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  5299. if (oo_is_external in objectoptions) then
  5300. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  5301. end;
  5302. procedure verify_objc_vardef(data: tobject; arg: pointer);
  5303. var
  5304. sym: tabstractvarsym absolute data;
  5305. res: pboolean absolute arg;
  5306. founderrordef: tdef;
  5307. begin
  5308. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  5309. exit;
  5310. if (sym.typ=paravarsym) and
  5311. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  5312. is_array_of_const(tparavarsym(sym).vardef)) then
  5313. exit;
  5314. if not objcchecktype(sym.vardef,founderrordef) then
  5315. begin
  5316. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5317. res^:=false;
  5318. end;
  5319. end;
  5320. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  5321. var
  5322. def: tdef absolute data;
  5323. res: pboolean absolute arg;
  5324. founderrordef: tdef;
  5325. begin
  5326. if (def.typ<>procdef) then
  5327. exit;
  5328. { check parameter types for validity }
  5329. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  5330. { check the result type for validity }
  5331. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  5332. begin
  5333. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5334. res^:=false;
  5335. end;
  5336. end;
  5337. function tobjectdef.check_objc_types: boolean;
  5338. begin
  5339. { done in separate step from finish_objc_data, because when
  5340. finish_objc_data is called, not all forwarddefs have been resolved
  5341. yet and we need to know all types here }
  5342. result:=true;
  5343. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  5344. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  5345. end;
  5346. procedure do_cpp_import_info(data: tobject; arg: pointer);
  5347. var
  5348. def: tdef absolute data;
  5349. pd: tprocdef absolute data;
  5350. begin
  5351. if (def.typ=procdef) then
  5352. begin
  5353. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  5354. if (oo_is_external in pd.struct.objectoptions) then
  5355. begin
  5356. { copied from psub.read_proc }
  5357. if assigned(tobjectdef(pd.struct).import_lib) then
  5358. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  5359. else
  5360. begin
  5361. { add import name to external list for DLL scanning }
  5362. if tf_has_dllscanner in target_info.flags then
  5363. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  5364. end;
  5365. end;
  5366. end;
  5367. end;
  5368. procedure tobjectdef.finish_cpp_data;
  5369. begin
  5370. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  5371. end;
  5372. {****************************************************************************
  5373. TImplementedInterface
  5374. ****************************************************************************}
  5375. constructor TImplementedInterface.create(aintf: tobjectdef);
  5376. begin
  5377. inherited create;
  5378. intfdef:=aintf;
  5379. IOffset:=-1;
  5380. IType:=etStandard;
  5381. NameMappings:=nil;
  5382. procdefs:=nil;
  5383. end;
  5384. constructor TImplementedInterface.create_deref(d:tderef);
  5385. begin
  5386. inherited create;
  5387. intfdef:=nil;
  5388. intfdefderef:=d;
  5389. IOffset:=-1;
  5390. IType:=etStandard;
  5391. NameMappings:=nil;
  5392. procdefs:=nil;
  5393. end;
  5394. destructor TImplementedInterface.destroy;
  5395. var
  5396. i : longint;
  5397. mappedname : pshortstring;
  5398. begin
  5399. if assigned(NameMappings) then
  5400. begin
  5401. for i:=0 to NameMappings.Count-1 do
  5402. begin
  5403. mappedname:=pshortstring(NameMappings[i]);
  5404. stringdispose(mappedname);
  5405. end;
  5406. NameMappings.free;
  5407. NameMappings:=nil;
  5408. end;
  5409. if assigned(procdefs) then
  5410. begin
  5411. procdefs.free;
  5412. procdefs:=nil;
  5413. end;
  5414. inherited destroy;
  5415. end;
  5416. procedure TImplementedInterface.buildderef;
  5417. begin
  5418. intfdefderef.build(intfdef);
  5419. end;
  5420. procedure TImplementedInterface.deref;
  5421. begin
  5422. intfdef:=tobjectdef(intfdefderef.resolve);
  5423. end;
  5424. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  5425. begin
  5426. if not assigned(NameMappings) then
  5427. NameMappings:=TFPHashList.Create;
  5428. NameMappings.Add(origname,stringdup(newname));
  5429. end;
  5430. function TImplementedInterface.GetMapping(const origname: string):string;
  5431. var
  5432. mappedname : pshortstring;
  5433. begin
  5434. result:='';
  5435. if not assigned(NameMappings) then
  5436. exit;
  5437. mappedname:=PShortstring(NameMappings.Find(origname));
  5438. if assigned(mappedname) then
  5439. result:=mappedname^;
  5440. end;
  5441. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  5442. begin
  5443. if not assigned(procdefs) then
  5444. procdefs:=TFPObjectList.Create(false);
  5445. { duplicate entries must be stored, because multiple }
  5446. { interfaces can declare methods with the same name }
  5447. { and all of these get their own VMT entry }
  5448. procdefs.Add(pd);
  5449. end;
  5450. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  5451. var
  5452. i : longint;
  5453. begin
  5454. result:=false;
  5455. { interfaces being implemented through delegation are not mergable (FK) }
  5456. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  5457. exit;
  5458. weight:=0;
  5459. { empty interface is mergeable }
  5460. if ProcDefs.Count=0 then
  5461. begin
  5462. result:=true;
  5463. exit;
  5464. end;
  5465. { The interface to merge must at least the number of
  5466. procedures of this interface }
  5467. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  5468. exit;
  5469. for i:=0 to ProcDefs.Count-1 do
  5470. begin
  5471. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  5472. exit;
  5473. end;
  5474. weight:=ProcDefs.Count;
  5475. result:=true;
  5476. end;
  5477. function TImplementedInterface.getcopy:TImplementedInterface;
  5478. begin
  5479. Result:=TImplementedInterface.Create(nil);
  5480. { 1) the procdefs list will be freed once for each copy
  5481. 2) since the procdefs list owns its elements, those will also be freed for each copy
  5482. 3) idem for the name mappings
  5483. }
  5484. { warning: this is completely wrong on so many levels...
  5485. Move(pointer(self)^,pointer(result)^,InstanceSize);
  5486. We need to make clean copies of the different fields
  5487. this is not implemented yet, and thus we generate an internal
  5488. error instead PM 2011-06-14 }
  5489. internalerror(2011061401);
  5490. end;
  5491. {****************************************************************************
  5492. TFORWARDDEF
  5493. ****************************************************************************}
  5494. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  5495. begin
  5496. inherited create(forwarddef);
  5497. tosymname:=stringdup(s);
  5498. forwardpos:=pos;
  5499. end;
  5500. function tforwarddef.GetTypeName:string;
  5501. begin
  5502. GetTypeName:='unresolved forward to '+tosymname^;
  5503. end;
  5504. destructor tforwarddef.destroy;
  5505. begin
  5506. stringdispose(tosymname);
  5507. inherited destroy;
  5508. end;
  5509. function tforwarddef.getcopy:tstoreddef;
  5510. begin
  5511. result:=tforwarddef.create(tosymname^, forwardpos);
  5512. end;
  5513. {****************************************************************************
  5514. TUNDEFINEDDEF
  5515. ****************************************************************************}
  5516. constructor tundefineddef.create;
  5517. begin
  5518. inherited create(undefineddef);
  5519. end;
  5520. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  5521. begin
  5522. inherited ppuload(undefineddef,ppufile);
  5523. end;
  5524. function tundefineddef.GetTypeName:string;
  5525. begin
  5526. GetTypeName:='<undefined type>';
  5527. end;
  5528. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  5529. begin
  5530. inherited ppuwrite(ppufile);
  5531. ppufile.writeentry(ibundefineddef);
  5532. end;
  5533. {****************************************************************************
  5534. TERRORDEF
  5535. ****************************************************************************}
  5536. constructor terrordef.create;
  5537. begin
  5538. inherited create(errordef);
  5539. { prevent consecutive faults }
  5540. savesize:=1;
  5541. end;
  5542. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  5543. begin
  5544. { Can't write errordefs to ppu }
  5545. internalerror(200411063);
  5546. end;
  5547. function terrordef.GetTypeName:string;
  5548. begin
  5549. GetTypeName:='<erroneous type>';
  5550. end;
  5551. function terrordef.getmangledparaname:TSymStr;
  5552. begin
  5553. getmangledparaname:='error';
  5554. end;
  5555. {****************************************************************************
  5556. Definition Helpers
  5557. ****************************************************************************}
  5558. function is_interfacecom(def: tdef): boolean;
  5559. begin
  5560. is_interfacecom:=
  5561. assigned(def) and
  5562. (def.typ=objectdef) and
  5563. (tobjectdef(def).objecttype=odt_interfacecom);
  5564. end;
  5565. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  5566. begin
  5567. is_interfacecom_or_dispinterface:=
  5568. assigned(def) and
  5569. (def.typ=objectdef) and
  5570. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  5571. end;
  5572. function is_any_interface_kind(def: tdef): boolean;
  5573. begin
  5574. result:=
  5575. assigned(def) and
  5576. (def.typ=objectdef) and
  5577. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  5578. is_objccategory(def));
  5579. end;
  5580. function is_interfacecorba(def: tdef): boolean;
  5581. begin
  5582. is_interfacecorba:=
  5583. assigned(def) and
  5584. (def.typ=objectdef) and
  5585. (tobjectdef(def).objecttype=odt_interfacecorba);
  5586. end;
  5587. function is_interface(def: tdef): boolean;
  5588. begin
  5589. is_interface:=
  5590. assigned(def) and
  5591. (def.typ=objectdef) and
  5592. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  5593. end;
  5594. function is_dispinterface(def: tdef): boolean;
  5595. begin
  5596. result:=
  5597. assigned(def) and
  5598. (def.typ=objectdef) and
  5599. (tobjectdef(def).objecttype=odt_dispinterface);
  5600. end;
  5601. function is_class(def: tdef): boolean;
  5602. begin
  5603. is_class:=
  5604. assigned(def) and
  5605. (def.typ=objectdef) and
  5606. (tobjectdef(def).objecttype=odt_class);
  5607. end;
  5608. function is_object(def: tdef): boolean;
  5609. begin
  5610. is_object:=
  5611. assigned(def) and
  5612. (def.typ=objectdef) and
  5613. (tobjectdef(def).objecttype=odt_object);
  5614. end;
  5615. function is_cppclass(def: tdef): boolean;
  5616. begin
  5617. is_cppclass:=
  5618. assigned(def) and
  5619. (def.typ=objectdef) and
  5620. (tobjectdef(def).objecttype=odt_cppclass);
  5621. end;
  5622. function is_objcclass(def: tdef): boolean;
  5623. begin
  5624. is_objcclass:=
  5625. assigned(def) and
  5626. (def.typ=objectdef) and
  5627. (tobjectdef(def).objecttype=odt_objcclass);
  5628. end;
  5629. function is_objectpascal_helper(def: tdef): boolean;
  5630. begin
  5631. result:=
  5632. assigned(def) and
  5633. (def.typ=objectdef) and
  5634. (tobjectdef(def).objecttype=odt_helper);
  5635. end;
  5636. function is_objcclassref(def: tdef): boolean;
  5637. begin
  5638. is_objcclassref:=
  5639. assigned(def) and
  5640. (def.typ=classrefdef) and
  5641. is_objcclass(tclassrefdef(def).pointeddef);
  5642. end;
  5643. function is_objcprotocol(def: tdef): boolean;
  5644. begin
  5645. result:=
  5646. assigned(def) and
  5647. (def.typ=objectdef) and
  5648. (tobjectdef(def).objecttype=odt_objcprotocol);
  5649. end;
  5650. function is_objccategory(def: tdef): boolean;
  5651. begin
  5652. result:=
  5653. assigned(def) and
  5654. (def.typ=objectdef) and
  5655. { if used as a forward type }
  5656. ((tobjectdef(def).objecttype=odt_objccategory) or
  5657. { if used as after it has been resolved }
  5658. ((tobjectdef(def).objecttype=odt_objcclass) and
  5659. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  5660. end;
  5661. function is_objc_class_or_protocol(def: tdef): boolean;
  5662. begin
  5663. result:=
  5664. assigned(def) and
  5665. (def.typ=objectdef) and
  5666. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  5667. end;
  5668. function is_objc_protocol_or_category(def: tdef): boolean;
  5669. begin
  5670. result:=
  5671. assigned(def) and
  5672. (def.typ=objectdef) and
  5673. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  5674. ((tobjectdef(def).objecttype = odt_objcclass) and
  5675. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  5676. end;
  5677. function is_classhelper(def: tdef): boolean;
  5678. begin
  5679. result:=
  5680. is_objectpascal_helper(def) or
  5681. is_objccategory(def);
  5682. end;
  5683. function is_class_or_interface(def: tdef): boolean;
  5684. begin
  5685. result:=
  5686. assigned(def) and
  5687. (def.typ=objectdef) and
  5688. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  5689. end;
  5690. function is_class_or_interface_or_objc(def: tdef): boolean;
  5691. begin
  5692. result:=
  5693. assigned(def) and
  5694. (def.typ=objectdef) and
  5695. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  5696. end;
  5697. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  5698. begin
  5699. result:=
  5700. assigned(def) and
  5701. (def.typ=objectdef) and
  5702. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  5703. end;
  5704. function is_class_or_interface_or_object(def: tdef): boolean;
  5705. begin
  5706. result:=
  5707. assigned(def) and
  5708. (def.typ=objectdef) and
  5709. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  5710. end;
  5711. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  5712. begin
  5713. result:=
  5714. assigned(def) and
  5715. (def.typ=objectdef) and
  5716. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5717. end;
  5718. function is_implicit_pointer_object_type(def: tdef): boolean;
  5719. begin
  5720. result:=
  5721. assigned(def) and
  5722. (((def.typ=objectdef) and
  5723. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  5724. ((target_info.system=system_jvm_java32) and
  5725. (def.typ=recorddef)));
  5726. end;
  5727. function is_class_or_object(def: tdef): boolean;
  5728. begin
  5729. result:=
  5730. assigned(def) and
  5731. (def.typ=objectdef) and
  5732. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  5733. end;
  5734. function is_record(def: tdef): boolean;
  5735. begin
  5736. result:=
  5737. assigned(def) and
  5738. (def.typ=recorddef);
  5739. end;
  5740. function is_javaclass(def: tdef): boolean;
  5741. begin
  5742. result:=
  5743. assigned(def) and
  5744. (def.typ=objectdef) and
  5745. (tobjectdef(def).objecttype=odt_javaclass);
  5746. end;
  5747. function is_javaclassref(def: tdef): boolean;
  5748. begin
  5749. is_javaclassref:=
  5750. assigned(def) and
  5751. (def.typ=classrefdef) and
  5752. is_javaclass(tclassrefdef(def).pointeddef);
  5753. end;
  5754. function is_javainterface(def: tdef): boolean;
  5755. begin
  5756. result:=
  5757. assigned(def) and
  5758. (def.typ=objectdef) and
  5759. (tobjectdef(def).objecttype=odt_interfacejava);
  5760. end;
  5761. function is_java_class_or_interface(def: tdef): boolean;
  5762. begin
  5763. result:=
  5764. assigned(def) and
  5765. (def.typ=objectdef) and
  5766. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  5767. end;
  5768. procedure loadobjctypes;
  5769. begin
  5770. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  5771. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  5772. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  5773. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  5774. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  5775. end;
  5776. procedure maybeloadcocoatypes;
  5777. var
  5778. tsym: ttypesym;
  5779. begin
  5780. if assigned(objc_fastenumeration) then
  5781. exit;
  5782. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  5783. if assigned(tsym) then
  5784. objc_fastenumeration:=tobjectdef(tsym.typedef)
  5785. else
  5786. objc_fastenumeration:=nil;
  5787. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  5788. if assigned(tsym) then
  5789. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  5790. else
  5791. objc_fastenumerationstate:=nil;
  5792. end;
  5793. function use_vectorfpu(def : tdef) : boolean;
  5794. begin
  5795. {$ifdef x86}
  5796. {$define use_vectorfpuimplemented}
  5797. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  5798. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  5799. {$endif x86}
  5800. {$ifdef arm}
  5801. {$define use_vectorfpuimplemented}
  5802. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  5803. {$endif arm}
  5804. {$ifndef use_vectorfpuimplemented}
  5805. use_vectorfpu:=false;
  5806. {$endif}
  5807. end;
  5808. function getpointerdef(def: tdef): tpointerdef;
  5809. var
  5810. res: PHashSetItem;
  5811. begin
  5812. if not assigned(current_module) then
  5813. internalerror(2011071101);
  5814. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  5815. if not assigned(res^.Data) then
  5816. begin
  5817. { since these pointerdefs can be reused anywhere in the current
  5818. unit, add them to the global/staticsymtable }
  5819. symtablestack.push(current_module.localsymtable);
  5820. res^.Data:=tpointerdef.create(def);
  5821. symtablestack.pop(current_module.localsymtable);
  5822. end;
  5823. result:=tpointerdef(res^.Data);
  5824. end;
  5825. end.