symdef.pas 215 KB

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