symdef.pas 214 KB

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