symdef.pas 276 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268
  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. { node }
  28. node,
  29. { aasm }
  30. aasmtai,
  31. cpuinfo,
  32. cgbase,
  33. parabase
  34. ;
  35. type
  36. {************************************************
  37. TDef
  38. ************************************************}
  39. tgenericconstraintdata=class
  40. interfaces : tfpobjectlist;
  41. interfacesderef : tfplist;
  42. flags : tgenericconstraintflags;
  43. constructor create;
  44. destructor destroy;override;
  45. procedure ppuload(ppufile:tcompilerppufile);
  46. procedure ppuwrite(ppufile:tcompilerppufile);
  47. procedure buildderef;
  48. procedure deref;
  49. end;
  50. { tstoreddef }
  51. tstoreddef = class(tdef)
  52. private
  53. {$ifdef symansistr}
  54. _fullownerhierarchyname : ansistring;
  55. {$else symansistr}
  56. _fullownerhierarchyname : pshortstring;
  57. {$endif symansistr}
  58. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  59. protected
  60. typesymderef : tderef;
  61. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  62. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  63. { a (possibly) reusable def is always created on the basis of another
  64. def, and contains a reference to this other def. If this other
  65. def is in a non-persistent symboltable, the new def cannot actually
  66. be safely reused everywhere in the current module. This routine
  67. abtracts that checking, and also restores the symtable stack
  68. (which had to be reset before creating the new def, so that the new
  69. def did not automatically get added to its top) }
  70. class procedure setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  71. public
  72. {$ifdef EXTDEBUG}
  73. fileinfo : tfileposinfo;
  74. {$endif}
  75. { generic support }
  76. genericdef : tstoreddef;
  77. genericdefderef : tderef;
  78. generictokenbuf : tdynamicarray;
  79. { this list contains references to the symbols that make up the
  80. generic parameters; the symbols are not owned by this list
  81. Note: this list is allocated on demand! }
  82. genericparas : tfphashobjectlist;
  83. genericparaderefs : tfplist;
  84. { contains additional data if this def is a generic constraint
  85. Note: this class is allocated on demand! }
  86. genconstraintdata : tgenericconstraintdata;
  87. constructor create(dt:tdeftyp;doregister:boolean);
  88. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  89. destructor destroy;override;
  90. function getcopy : tstoreddef;virtual;
  91. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  92. procedure buildderef;override;
  93. procedure buildderefimpl;override;
  94. procedure deref;override;
  95. procedure derefimpl;override;
  96. function size:asizeint;override;
  97. function getvardef:longint;override;
  98. function alignment:shortint;override;
  99. function is_publishable : boolean;override;
  100. function needs_inittable : boolean;override;
  101. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  102. function rtti_mangledname(rt:trttitype):TSymStr;override;
  103. function OwnerHierarchyName: string; override;
  104. function fullownerhierarchyname(skipprocparams:boolean):TSymStr;override;
  105. function needs_separate_initrtti:boolean;override;
  106. function in_currentunit: boolean;
  107. { regvars }
  108. function is_intregable : boolean;
  109. function is_fpuregable : boolean;
  110. { def can be put into a register if it is const/immutable }
  111. function is_const_intregable : boolean;
  112. { generics }
  113. procedure initgeneric;
  114. { this function can be used to determine whether a def is really a
  115. generic declaration or just a normal type declared inside another
  116. generic }
  117. function is_generic:boolean;inline;
  118. { same as above for specializations }
  119. function is_specialization:boolean;inline;
  120. { registers this def in the unit's deflist; no-op if already registered }
  121. procedure register_def; override;
  122. { add the def to the top of the symtable stack if it's not yet owned
  123. by another symtable }
  124. procedure maybe_put_in_symtable_stack;
  125. private
  126. savesize : asizeuint;
  127. end;
  128. tfiletyp = (ft_text,ft_typed,ft_untyped);
  129. tfiledef = class(tstoreddef)
  130. filetyp : tfiletyp;
  131. typedfiledef : tdef;
  132. typedfiledefderef : tderef;
  133. constructor createtext;virtual;
  134. constructor createuntyped;virtual;
  135. constructor createtyped(def : tdef);virtual;
  136. constructor ppuload(ppufile:tcompilerppufile);
  137. function getcopy : tstoreddef;override;
  138. { do not override this routine in platform-specific subclasses,
  139. override ppuwrite_platform instead }
  140. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  141. procedure buildderef;override;
  142. procedure deref;override;
  143. function GetTypeName:string;override;
  144. function getmangledparaname:TSymStr;override;
  145. function size:asizeint;override;
  146. procedure setsize;
  147. end;
  148. tfiledefclass = class of tfiledef;
  149. tvariantdef = class(tstoreddef)
  150. varianttype : tvarianttype;
  151. constructor create(v : tvarianttype);virtual;
  152. constructor ppuload(ppufile:tcompilerppufile);
  153. function getcopy : tstoreddef;override;
  154. function GetTypeName:string;override;
  155. { do not override this routine in platform-specific subclasses,
  156. override ppuwrite_platform instead }
  157. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  158. function getvardef:longint;override;
  159. procedure setsize;
  160. function is_publishable : boolean;override;
  161. function needs_inittable : boolean;override;
  162. end;
  163. tvariantdefclass = class of tvariantdef;
  164. tformaldef = class(tstoreddef)
  165. typed:boolean;
  166. constructor create(Atyped:boolean);virtual;
  167. constructor ppuload(ppufile:tcompilerppufile);
  168. { do not override this routine in platform-specific subclasses,
  169. override ppuwrite_platform instead }
  170. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  171. function GetTypeName:string;override;
  172. end;
  173. tformaldefclass = class of tformaldef;
  174. tforwarddef = class(tstoreddef)
  175. tosymname : pshortstring;
  176. forwardpos : tfileposinfo;
  177. constructor create(const s:string;const pos:tfileposinfo);virtual;
  178. destructor destroy;override;
  179. function getcopy:tstoreddef;override;
  180. function GetTypeName:string;override;
  181. end;
  182. tforwarddefclass = class of tforwarddef;
  183. tundefineddef = class(tstoreddef)
  184. constructor create(doregister:boolean);virtual;
  185. constructor ppuload(ppufile:tcompilerppufile);
  186. { do not override this routine in platform-specific subclasses,
  187. override ppuwrite_platform instead }
  188. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  189. function GetTypeName:string;override;
  190. end;
  191. tundefineddefclass = class of tundefineddef;
  192. terrordef = class(tstoreddef)
  193. constructor create;virtual;
  194. { do not override this routine in platform-specific subclasses,
  195. override ppuwrite_platform instead }
  196. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  197. function GetTypeName:string;override;
  198. function getmangledparaname : TSymStr;override;
  199. end;
  200. terrordefclass = class of terrordef;
  201. tabstractpointerdef = class(tstoreddef)
  202. pointeddef : tdef;
  203. pointeddefderef : tderef;
  204. constructor create(dt:tdeftyp;def:tdef);
  205. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  206. procedure ppuwrite(ppufile:tcompilerppufile);override;
  207. procedure buildderef;override;
  208. procedure deref;override;
  209. function size:asizeint;override;
  210. function alignment:shortint;override;
  211. end;
  212. tpointerdef = class(tabstractpointerdef)
  213. has_pointer_math : boolean;
  214. constructor create(def:tdef);virtual;
  215. { returns a pointerdef for def, reusing an existing one in case it
  216. exists in the current module }
  217. class function getreusable(def: tdef): tpointerdef; virtual;
  218. { same as above, but in case the def must never be freed after the
  219. current module has been compiled -- even if the def was not written
  220. to the ppu file (for defs in para locations, as we don't reset them
  221. so we don't have to recalculate them all the time) }
  222. class function getreusable_no_free(def: tdef): tpointerdef;
  223. function size:asizeint;override;
  224. function getcopy:tstoreddef;override;
  225. constructor ppuload(ppufile:tcompilerppufile);
  226. { do not override this routine in platform-specific subclasses,
  227. override ppuwrite_platform instead }
  228. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  229. function GetTypeName:string;override;
  230. {# returns the appropriate int type for pointer arithmetic with the given pointer type.
  231. When adding or subtracting a number to/from a pointer, this function returns the
  232. int type to which that number has to be converted, before the operation can be performed.
  233. Normally, this is sinttype, except on i8086, where it takes into account the
  234. special i8086 pointer types (near, far, huge). }
  235. function pointer_arithmetic_int_type:tdef;virtual;
  236. {# the unsigned version of pointer_arithmetic_int_type. Used with inc/dec. }
  237. function pointer_arithmetic_uint_type:tdef;virtual;
  238. {# returns the int type produced when subtracting two pointers of the given type.
  239. Normally, this is sinttype, except on i8086, where it takes into account the
  240. special i8086 pointer types (near, far, huge). }
  241. function pointer_subtraction_result_type:tdef;virtual;
  242. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  243. {# the integer index type used to convert the pointer to array (i.e. denotes int_type in: ptr[int_type]) }
  244. function converted_pointer_to_array_range_type:tdef;virtual;
  245. end;
  246. tpointerdefclass = class of tpointerdef;
  247. tprocdef = class;
  248. tabstractrecorddef= class(tstoreddef)
  249. private
  250. rttistring : string;
  251. public
  252. objname,
  253. objrealname : PShortString;
  254. { for C++ classes: name of the library this class is imported from }
  255. { for Java classes/records: package name }
  256. import_lib : PShortString;
  257. symtable : TSymtable;
  258. cloneddef : tabstractrecorddef;
  259. cloneddefderef : tderef;
  260. objectoptions : tobjectoptions;
  261. { for targets that initialise typed constants via explicit assignments
  262. instead of by generating an initialised data sectino }
  263. tcinitcode : tnode;
  264. constructor create(const n:string; dt:tdeftyp;doregister:boolean);
  265. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  266. procedure ppuwrite(ppufile:tcompilerppufile);override;
  267. destructor destroy; override;
  268. procedure buildderefimpl;override;
  269. procedure derefimpl;override;
  270. procedure check_forwards; virtual;
  271. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  272. function GetSymtable(t:tGetSymtable):TSymtable;override;
  273. function is_packed:boolean;
  274. function RttiName: string;
  275. { enumerator support }
  276. function search_enumerator_get: tprocdef; virtual;
  277. function search_enumerator_move: tprocdef; virtual;
  278. function search_enumerator_current: tsym; virtual;
  279. { JVM }
  280. function jvm_full_typename(with_package_name: boolean): string;
  281. { check if the symtable contains a float field }
  282. function contains_float_field : boolean;
  283. { check if the symtable contains a field that spans an aword boundary }
  284. function contains_cross_aword_field: boolean;
  285. end;
  286. pvariantrecdesc = ^tvariantrecdesc;
  287. tvariantrecbranch = record
  288. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  289. which does not support range expressions in variant record definitions }
  290. values : array of Tconstexprint;
  291. nestedvariant : pvariantrecdesc;
  292. end;
  293. ppvariantrecdesc = ^pvariantrecdesc;
  294. tvariantrecdesc = record
  295. variantselector : tsym;
  296. variantselectorderef : tderef;
  297. branches : array of tvariantrecbranch;
  298. end;
  299. trecorddef = class(tabstractrecorddef)
  300. public
  301. variantrecdesc : pvariantrecdesc;
  302. isunion : boolean;
  303. constructor create(const n:string; p:TSymtable);virtual;
  304. constructor create_global_internal(n: string; packrecords, recordalignmin, maxCrecordalign: shortint); virtual;
  305. function add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  306. procedure add_fields_from_deflist(fieldtypes: tfplist);
  307. constructor ppuload(ppufile:tcompilerppufile);
  308. destructor destroy;override;
  309. function getcopy : tstoreddef;override;
  310. { do not override this routine in platform-specific subclasses,
  311. override ppuwrite_platform instead }
  312. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  313. procedure buildderef;override;
  314. procedure deref;override;
  315. function size:asizeint;override;
  316. function alignment : shortint;override;
  317. function padalignment: shortint;
  318. function GetTypeName:string;override;
  319. { debug }
  320. function needs_inittable : boolean;override;
  321. function needs_separate_initrtti:boolean;override;
  322. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  323. end;
  324. trecorddefclass = class of trecorddef;
  325. tobjectdef = class;
  326. { TImplementedInterface }
  327. TImplementedInterface = class
  328. private
  329. fIOffset : longint;
  330. function GetIOffset: longint;
  331. public
  332. IntfDef : tobjectdef;
  333. IntfDefDeref : tderef;
  334. IType : tinterfaceentrytype;
  335. VtblImplIntf : TImplementedInterface;
  336. NameMappings : TFPHashList;
  337. ProcDefs : TFPObjectList;
  338. ImplementsGetter : tsym;
  339. ImplementsGetterDeref : tderef;
  340. ImplementsField : tsym;
  341. constructor create(aintf: tobjectdef);virtual;
  342. constructor create_deref(intfd,getterd:tderef);virtual;
  343. destructor destroy; override;
  344. function getcopy:TImplementedInterface;
  345. procedure buildderef;
  346. procedure deref;
  347. procedure AddMapping(const origname, newname: string);
  348. function GetMapping(const origname: string):string;
  349. procedure AddImplProc(pd:tprocdef);
  350. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  351. property IOffset: longint read GetIOffset write fIOffset;
  352. end;
  353. timplementedinterfaceclass = class of timplementedinterface;
  354. { tvmtentry }
  355. tvmtentry = record
  356. procdef : tprocdef;
  357. procdefderef : tderef;
  358. visibility : tvisibility;
  359. end;
  360. pvmtentry = ^tvmtentry;
  361. { tobjectdef }
  362. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  363. pmvcallstaticinfo = ^tmvcallstaticinfo;
  364. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  365. tobjectdef = class(tabstractrecorddef)
  366. private
  367. fcurrent_dispid: longint;
  368. public
  369. childof : tobjectdef;
  370. childofderef : tderef;
  371. { for Object Pascal helpers }
  372. extendeddef : tdef;
  373. extendeddefderef: tderef;
  374. helpertype : thelpertype;
  375. { for Objective-C: protocols and classes can have the same name there }
  376. objextname : pshortstring;
  377. { to be able to have a variable vmt position }
  378. { and no vmt field for objects without virtuals }
  379. vmtentries : TFPList;
  380. vmcallstaticinfo : pmvcallstaticinfo;
  381. vmt_field : tsym;
  382. vmt_fieldderef : tderef;
  383. iidguid : pguid;
  384. iidstr : pshortstring;
  385. { store implemented interfaces defs and name mappings }
  386. ImplementedInterfaces : TFPObjectList;
  387. { number of abstract methods (used by JVM target to determine whether
  388. or not the class should be marked as abstract: must be done if 1 or
  389. more abstract methods) }
  390. abstractcnt : longint;
  391. writing_class_record_dbginfo,
  392. { a class of this type has been created in this module }
  393. created_in_current_module,
  394. { a loadvmtnode for this class has been created in this
  395. module, so if a classrefdef variable of this or a parent
  396. class is used somewhere to instantiate a class, then this
  397. class may be instantiated
  398. }
  399. maybe_created_in_current_module,
  400. { a "class of" this particular class has been created in
  401. this module
  402. }
  403. classref_created_in_current_module : boolean;
  404. objecttype : tobjecttyp;
  405. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);virtual;
  406. constructor ppuload(ppufile:tcompilerppufile);
  407. destructor destroy;override;
  408. function getcopy : tstoreddef;override;
  409. { do not override this routine in platform-specific subclasses,
  410. override ppuwrite_platform instead }
  411. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  412. function GetTypeName:string;override;
  413. procedure buildderef;override;
  414. procedure deref;override;
  415. procedure derefimpl;override;
  416. procedure resetvmtentries;
  417. procedure copyvmtentries(objdef:tobjectdef);
  418. function getparentdef:tdef;override;
  419. function size : asizeint;override;
  420. function alignment:shortint;override;
  421. function vmtmethodoffset(index:longint):longint;
  422. function members_need_inittable : boolean;
  423. { this should be called when this class implements an interface }
  424. procedure prepareguid;
  425. function is_publishable : boolean;override;
  426. function needs_inittable : boolean;override;
  427. function needs_separate_initrtti : boolean;override;
  428. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  429. function rtti_mangledname(rt:trttitype):TSymStr;override;
  430. function vmt_mangledname : TSymStr;
  431. function vmt_def: trecorddef;
  432. procedure check_forwards; override;
  433. procedure insertvmt;
  434. function vmt_offset: asizeint;
  435. procedure set_parent(c : tobjectdef);
  436. function find_destructor: tprocdef;
  437. function implements_any_interfaces: boolean;
  438. { dispinterface support }
  439. function get_next_dispid: longint;
  440. { enumerator support }
  441. function search_enumerator_get: tprocdef; override;
  442. function search_enumerator_move: tprocdef; override;
  443. function search_enumerator_current: tsym; override;
  444. { WPO }
  445. procedure register_created_object_type;override;
  446. procedure register_maybe_created_object_type;
  447. procedure register_created_classref_type;
  448. procedure register_vmt_call(index:longint);
  449. { ObjC }
  450. procedure finish_objc_data;
  451. function check_objc_types: boolean;
  452. { C++ }
  453. procedure finish_cpp_data;
  454. end;
  455. tobjectdefclass = class of tobjectdef;
  456. tclassrefdef = class(tabstractpointerdef)
  457. constructor create(def:tdef);virtual;
  458. constructor ppuload(ppufile:tcompilerppufile);
  459. { do not override this routine in platform-specific subclasses,
  460. override ppuwrite_platform instead }
  461. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  462. function getcopy:tstoreddef;override;
  463. function GetTypeName:string;override;
  464. function is_publishable : boolean;override;
  465. function rtti_mangledname(rt:trttitype):TSymStr;override;
  466. procedure register_created_object_type;override;
  467. end;
  468. tclassrefdefclass = class of tclassrefdef;
  469. tarraydef = class(tstoreddef)
  470. lowrange,
  471. highrange : asizeint;
  472. rangedef : tdef;
  473. rangedefderef : tderef;
  474. arrayoptions : tarraydefoptions;
  475. symtable : TSymtable;
  476. protected
  477. _elementdef : tdef;
  478. _elementdefderef : tderef;
  479. procedure setelementdef(def:tdef);
  480. public
  481. function elesize : asizeint;
  482. function elepackedbitsize : asizeint;
  483. function elecount : asizeuint;
  484. constructor create_from_pointer(def:tpointerdef);virtual;
  485. constructor create(l,h:asizeint;def:tdef);virtual;
  486. constructor create_openarray;virtual;
  487. class function getreusable(def: tdef; elems: asizeint): tarraydef; virtual;
  488. { same as above, but in case the def must never be freed after the
  489. current module has been compiled -- even if the def was not written
  490. to the ppu file (for defs in para locations, as we don't reset them
  491. so we don't have to recalculate them all the time) }
  492. class function getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  493. constructor ppuload(ppufile:tcompilerppufile);
  494. destructor destroy; override;
  495. function getcopy : tstoreddef;override;
  496. { do not override this routine in platform-specific subclasses,
  497. override ppuwrite_platform instead }
  498. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  499. function GetTypeName:string;override;
  500. function getmangledparaname : TSymStr;override;
  501. procedure buildderef;override;
  502. procedure deref;override;
  503. function size : asizeint;override;
  504. function alignment : shortint;override;
  505. { returns the label of the range check string }
  506. function needs_inittable : boolean;override;
  507. function needs_separate_initrtti : boolean;override;
  508. property elementdef : tdef read _elementdef write setelementdef;
  509. function is_publishable : boolean;override;
  510. end;
  511. tarraydefclass = class of tarraydef;
  512. torddef = class(tstoreddef)
  513. low,high : TConstExprInt;
  514. ordtype : tordtype;
  515. constructor create(t : tordtype;v,b : TConstExprInt; doregister: boolean);virtual;
  516. constructor ppuload(ppufile:tcompilerppufile);
  517. function getcopy : tstoreddef;override;
  518. { do not override this routine in platform-specific subclasses,
  519. override ppuwrite_platform instead }
  520. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  521. function is_publishable : boolean;override;
  522. function GetTypeName:string;override;
  523. function alignment:shortint;override;
  524. procedure setsize;
  525. function packedbitsize: asizeint; override;
  526. function getvardef : longint;override;
  527. end;
  528. torddefclass = class of torddef;
  529. tfloatdef = class(tstoreddef)
  530. floattype : tfloattype;
  531. constructor create(t: tfloattype; doregister: boolean);virtual;
  532. constructor ppuload(ppufile:tcompilerppufile);
  533. function getcopy : tstoreddef;override;
  534. { do not override this routine in platform-specific subclasses,
  535. override ppuwrite_platform instead }
  536. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  537. function GetTypeName:string;override;
  538. function is_publishable : boolean;override;
  539. function alignment:shortint;override;
  540. function structalignment: shortint;override;
  541. procedure setsize;
  542. function getvardef:longint;override;
  543. end;
  544. tfloatdefclass = class of tfloatdef;
  545. { tabstractprocdef }
  546. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  547. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  548. pno_mangledname, pno_noparams);
  549. tprocnameoptions = set of tprocnameoption;
  550. tproccopytyp = (pc_normal,
  551. { always creates a top-level function, removes all
  552. special parameters (self, vmt, parentfp, ...) }
  553. pc_bareproc,
  554. { creates a procvardef describing only the code pointer
  555. of a method/netsted function/... }
  556. pc_address_only
  557. );
  558. tabstractprocdef = class(tstoreddef)
  559. { saves a definition to the return type }
  560. returndef : tdef;
  561. returndefderef : tderef;
  562. parast : TSymtable;
  563. paras : tparalist;
  564. proctypeoption : tproctypeoption;
  565. proccalloption : tproccalloption;
  566. procoptions : tprocoptions;
  567. callerargareasize,
  568. calleeargareasize: pint;
  569. {$ifdef m68k}
  570. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  571. {$endif}
  572. funcretloc : array[callerside..calleeside] of TCGPara;
  573. has_paraloc_info : tcallercallee; { paraloc info is available }
  574. { number of user visible parameters }
  575. maxparacount,
  576. minparacount : byte;
  577. constructor create(dt:tdeftyp;level:byte;doregister:boolean);
  578. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  579. destructor destroy;override;
  580. procedure ppuwrite(ppufile:tcompilerppufile);override;
  581. procedure buildderef;override;
  582. procedure deref;override;
  583. procedure calcparas;
  584. function mangledprocparanames(oldlen : longint) : string;
  585. function typename_paras(pno: tprocnameoptions): ansistring;
  586. function is_methodpointer:boolean;virtual;
  587. function is_addressonly:boolean;virtual;
  588. function no_self_node:boolean;
  589. { get either a copy as a procdef or procvardef }
  590. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; virtual;
  591. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  592. procedure check_mark_as_nested;
  593. procedure init_paraloc_info(side: tcallercallee);
  594. procedure done_paraloc_info(side: tcallercallee);
  595. function stack_tainting_parameter(side: tcallercallee): boolean;
  596. function is_pushleftright: boolean;virtual;
  597. function address_type:tdef;virtual;
  598. { address type, generated for ofs() }
  599. function ofs_address_type:tdef;virtual;
  600. procedure declared_far;virtual;
  601. procedure declared_near;virtual;
  602. private
  603. procedure count_para(p:TObject;arg:pointer);
  604. procedure insert_para(p:TObject;arg:pointer);
  605. end;
  606. tprocvardef = class(tabstractprocdef)
  607. constructor create(level:byte);virtual;
  608. { returns a procvardef that represents the address of a proc(var)def }
  609. class function getreusableprocaddr(def: tabstractprocdef): tprocvardef; virtual;
  610. { same as above, but in case the def must never be freed after the
  611. current module has been compiled -- even if the def was not written
  612. to the ppu file (for defs in para locations, as we don't reset them
  613. so we don't have to recalculate them all the time) }
  614. class function getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  615. constructor ppuload(ppufile:tcompilerppufile);
  616. function getcopy : tstoreddef;override;
  617. { do not override this routine in platform-specific subclasses,
  618. override ppuwrite_platform instead }
  619. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  620. function GetSymtable(t:tGetSymtable):TSymtable;override;
  621. function size : asizeint;override;
  622. function GetTypeName:string;override;
  623. function is_publishable : boolean;override;
  624. function is_methodpointer:boolean;override;
  625. function is_addressonly:boolean;override;
  626. function getmangledparaname:TSymStr;override;
  627. function getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef; override;
  628. end;
  629. tprocvardefclass = class of tprocvardef;
  630. tmessageinf = record
  631. case integer of
  632. 0 : (str : pshortstring);
  633. 1 : (i : longint);
  634. end;
  635. tinlininginfo = record
  636. { node tree }
  637. code : tnode;
  638. flags : tprocinfoflags;
  639. end;
  640. pinlininginfo = ^tinlininginfo;
  641. {$ifdef oldregvars}
  642. { register variables }
  643. pregvarinfo = ^tregvarinfo;
  644. tregvarinfo = record
  645. regvars : array[1..maxvarregs] of tsym;
  646. regvars_para : array[1..maxvarregs] of boolean;
  647. regvars_refs : array[1..maxvarregs] of longint;
  648. fpuregvars : array[1..maxfpuvarregs] of tsym;
  649. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  650. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  651. end;
  652. {$endif oldregvars}
  653. timplprocdefinfo = record
  654. resultname : pshortstring;
  655. parentfpstruct: tsym;
  656. parentfpstructptrtype: tdef;
  657. parentfpinitblock: tnode;
  658. procstarttai,
  659. procendtai : tai;
  660. skpara: pointer;
  661. personality: tprocdef;
  662. forwarddef,
  663. interfacedef : boolean;
  664. hasforward : boolean;
  665. end;
  666. pimplprocdefinfo = ^timplprocdefinfo;
  667. { tprocdef }
  668. tprocdef = class(tabstractprocdef)
  669. protected
  670. {$ifdef symansistr}
  671. _mangledname : ansistring;
  672. {$else symansistr}
  673. _mangledname : pshortstring;
  674. {$endif}
  675. { information that is only required until the implementation of the
  676. procdef has been handled }
  677. implprocdefinfo : pimplprocdefinfo;
  678. function GetResultName: PShortString;
  679. procedure SetResultName(AValue: PShortString);
  680. function GetParentFPStruct: tsym;
  681. procedure SetParentFPStruct(AValue: tsym);
  682. function GetParentFPStructPtrType: tdef;
  683. procedure SetParentFPStructPtrType(AValue: tdef);
  684. function GetParentFPInitBlock: tnode;
  685. procedure SetParentFPInitBlock(AValue: tnode);
  686. function Getprocstarttai: tai;
  687. procedure Setprocstarttai(AValue: tai);
  688. function Getprocendtai: tai;
  689. procedure Setprocendtai(AValue: tai);
  690. function Getskpara: pointer;
  691. procedure Setskpara(AValue: pointer);
  692. function Getpersonality: tprocdef;
  693. procedure Setpersonality(AValue: tprocdef);
  694. function Getforwarddef: boolean;
  695. procedure Setforwarddef(AValue: boolean);
  696. function Getinterfacedef: boolean;
  697. procedure Setinterfacedef(AValue: boolean);virtual;
  698. function Gethasforward: boolean;
  699. procedure Sethasforward(AValue: boolean);
  700. function GetIsEmpty: boolean;
  701. procedure SetIsEmpty(AValue: boolean);
  702. function GetHasInliningInfo: boolean;
  703. procedure SetHasInliningInfo(AValue: boolean);
  704. public
  705. messageinf : tmessageinf;
  706. dispid : longint;
  707. {$ifndef EXTDEBUG}
  708. { where is this function defined and what were the symbol
  709. flags, needed here because there
  710. is only one symbol for all overloaded functions
  711. EXTDEBUG has fileinfo in tdef (PFV) }
  712. fileinfo : tfileposinfo;
  713. {$endif}
  714. symoptions : tsymoptions;
  715. deprecatedmsg : pshortstring;
  716. { generic support }
  717. genericdecltokenbuf : tdynamicarray;
  718. { symbol owning this definition }
  719. procsym : tsym;
  720. procsymderef : tderef;
  721. { alias names }
  722. aliasnames : TCmdStrList;
  723. { symtables }
  724. localst : TSymtable;
  725. funcretsym : tsym;
  726. funcretsymderef : tderef;
  727. struct : tabstractrecorddef;
  728. structderef : tderef;
  729. implprocoptions: timplprocoptions;
  730. { import info }
  731. import_dll,
  732. import_name : pshortstring;
  733. { info for inlining the subroutine, if this pointer is nil,
  734. the procedure can't be inlined }
  735. inlininginfo : pinlininginfo;
  736. {$ifdef oldregvars}
  737. regvarinfo: pregvarinfo;
  738. {$endif oldregvars}
  739. import_nr : word;
  740. extnumber : word;
  741. { set to a value different from tsk_none in case this procdef is for
  742. a routine that has to be internally generated by the compiler }
  743. synthetickind: tsynthetickind;
  744. visibility : tvisibility;
  745. constructor create(level:byte;doregister:boolean);virtual;
  746. constructor ppuload(ppufile:tcompilerppufile);
  747. destructor destroy;override;
  748. procedure freeimplprocdefinfo;
  749. { do not override this routine in platform-specific subclasses,
  750. override ppuwrite_platform instead }
  751. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  752. procedure buildderef;override;
  753. procedure buildderefimpl;override;
  754. procedure deref;override;
  755. procedure derefimpl;override;
  756. function GetSymtable(t:tGetSymtable):TSymtable;override;
  757. { warnings:
  758. * the symtablestack top has to be the symtable to which the copy
  759. should be added
  760. * getcopy does not create a finished/ready-to-use procdef; it
  761. needs to be finalised afterwards by calling
  762. symcreat.finish_copied_procdef() afterwards
  763. }
  764. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; override;
  765. function getcopy: tstoreddef; override;
  766. function GetTypeName : string;override;
  767. function mangledname : TSymStr; virtual;
  768. procedure setmangledname(const s : TSymStr);
  769. function fullprocname(showhidden:boolean):string;
  770. function customprocname(pno: tprocnameoptions):ansistring;
  771. function defaultmangledname: TSymStr;
  772. function cplusplusmangledname : TSymStr;
  773. function objcmangledname : TSymStr;
  774. function is_methodpointer:boolean;override;
  775. function is_addressonly:boolean;override;
  776. procedure make_external;
  777. procedure init_genericdecl;
  778. { returns whether the mangled name or any of its aliases is equal to
  779. s }
  780. function has_alias_name(const s: TSymStr):boolean;
  781. { aliases to fields only required when a function is implemented in
  782. the current unit }
  783. property resultname: PShortString read GetResultName write SetResultName;
  784. { temporary reference to structure containing copies of all local
  785. variables and parameters accessed by nested routines; reference to
  786. this structure is passed as "parent frame pointer" on targets that
  787. lack this concept (at least JVM and LLVM); no need to save to/
  788. restore from ppu, since nested routines are always in the same
  789. unit }
  790. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  791. { pointer to parentfpstruct's type (not yet valid during parsing, so
  792. cannot be used for $parentfp parameter) (no need to save to ppu) }
  793. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  794. { code to copy the parameters accessed from nested routines into the
  795. parentfpstruct (no need to save to ppu) }
  796. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  797. { First/last assembler symbol/instruction in aasmoutput list.
  798. Note: initialised after compiling the code for the procdef, but
  799. not saved to/restored from ppu. Used when inserting debug info }
  800. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  801. property procendtai: tai read Getprocendtai write Setprocendtai;
  802. { optional parameter for the synthetic routine generation logic }
  803. property skpara: pointer read Getskpara write Setskpara;
  804. { ABI-conformant exception handling personality function }
  805. property personality: tprocdef read Getpersonality write Setpersonality;
  806. { true, if the procedure is only declared
  807. (forward procedure) }
  808. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  809. { true if the procedure is declared in the interface }
  810. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  811. { true if the procedure has a forward declaration }
  812. property hasforward: boolean read Gethasforward write Sethasforward;
  813. { true if the routine's body is empty }
  814. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  815. { true if all information required to inline this routine is available }
  816. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  817. end;
  818. tprocdefclass = class of tprocdef;
  819. { single linked list of overloaded procs }
  820. pprocdeflist = ^tprocdeflist;
  821. tprocdeflist = record
  822. def : tprocdef;
  823. defderef : tderef;
  824. next : pprocdeflist;
  825. end;
  826. tstringdef = class(tstoreddef)
  827. encoding : tstringencoding;
  828. stringtype : tstringtype;
  829. len : asizeint;
  830. constructor createshort(l: byte; doregister: boolean);virtual;
  831. constructor loadshort(ppufile:tcompilerppufile);
  832. constructor createlong(l: asizeint; doregister: boolean);virtual;
  833. constructor loadlong(ppufile:tcompilerppufile);
  834. constructor createansi(aencoding: tstringencoding; doregister: boolean);virtual;
  835. constructor loadansi(ppufile:tcompilerppufile);
  836. constructor createwide(doregister: boolean);virtual;
  837. constructor loadwide(ppufile:tcompilerppufile);
  838. constructor createunicode(doregister: boolean);virtual;
  839. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  840. function getcopy : tstoreddef;override;
  841. function stringtypname:string;
  842. { do not override this routine in platform-specific subclasses,
  843. override ppuwrite_platform instead }
  844. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  845. function GetTypeName:string;override;
  846. function getmangledparaname:TSymStr;override;
  847. function is_publishable : boolean;override;
  848. function size:asizeint;override;
  849. function alignment : shortint;override;
  850. function needs_inittable : boolean;override;
  851. function getvardef:longint;override;
  852. end;
  853. tstringdefclass = class of tstringdef;
  854. { tenumdef }
  855. tenumdef = class(tstoreddef)
  856. minval,
  857. maxval : asizeint;
  858. basedef : tenumdef;
  859. basedefderef : tderef;
  860. symtable : TSymtable;
  861. has_jumps : boolean;
  862. constructor create;virtual;
  863. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  864. constructor ppuload(ppufile:tcompilerppufile);
  865. destructor destroy;override;
  866. function getcopy : tstoreddef;override;
  867. { do not override this routine in platform-specific subclasses,
  868. override ppuwrite_platform instead }
  869. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  870. procedure buildderef;override;
  871. procedure deref;override;
  872. function GetTypeName:string;override;
  873. function is_publishable : boolean;override;
  874. procedure calcsavesize(packenum: shortint);
  875. function packedbitsize: asizeint; override;
  876. procedure setmax(_max:asizeint);
  877. procedure setmin(_min:asizeint);
  878. function min:asizeint;
  879. function max:asizeint;
  880. function getfirstsym:tsym;
  881. function int2enumsym(l: asizeint): tsym;
  882. { returns basedef if assigned, otherwise self }
  883. function getbasedef: tenumdef;
  884. end;
  885. tenumdefclass = class of tenumdef;
  886. tsetdef = class(tstoreddef)
  887. elementdef : tdef;
  888. elementdefderef : tderef;
  889. setbase,
  890. setmax : asizeint;
  891. constructor create(def: tdef; low, high: asizeint; doregister: boolean);virtual;
  892. constructor ppuload(ppufile:tcompilerppufile);
  893. function getcopy : tstoreddef;override;
  894. { do not override this routine in platform-specific subclasses,
  895. override ppuwrite_platform instead }
  896. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  897. procedure buildderef;override;
  898. procedure deref;override;
  899. function GetTypeName:string;override;
  900. function is_publishable : boolean;override;
  901. end;
  902. tsetdefclass = class of tsetdef;
  903. tgenericdummyentry = class
  904. dummysym : tsym;
  905. resolvedsym : tsym;
  906. end;
  907. tdefawaresymtablestack = class(TSymtablestack)
  908. private
  909. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  910. procedure remove_helpers_and_generics(st:tsymtable);inline;
  911. procedure remove_helpers(st:tsymtable);
  912. procedure remove_generics(st:tsymtable);
  913. procedure pushcommon(st:tsymtable);inline;
  914. public
  915. procedure push(st: TSymtable); override;
  916. procedure pushafter(st,afterst:TSymtable); override;
  917. procedure pop(st: TSymtable); override;
  918. end;
  919. var
  920. current_structdef: tabstractrecorddef; { used for private functions check !! }
  921. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  922. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  923. cfiledef: tfiledefclass;
  924. cvariantdef: tvariantdefclass;
  925. cformaldef: tformaldefclass;
  926. cforwarddef: tforwarddefclass;
  927. cundefineddef: tundefineddefclass;
  928. cerrordef: terrordefclass;
  929. cpointerdef: tpointerdefclass;
  930. crecorddef: trecorddefclass;
  931. cimplementedinterface: timplementedinterfaceclass;
  932. cobjectdef: tobjectdefclass;
  933. cclassrefdef: tclassrefdefclass;
  934. carraydef: tarraydefclass;
  935. corddef: torddefclass;
  936. cfloatdef: tfloatdefclass;
  937. cprocvardef: tprocvardefclass;
  938. cprocdef: tprocdefclass;
  939. cstringdef: tstringdefclass;
  940. cenumdef: tenumdefclass;
  941. csetdef: tsetdefclass;
  942. { default types }
  943. generrordef, { error in definition }
  944. voidpointertype, { pointer for Void-pointeddef }
  945. charpointertype, { pointer for Char-pointeddef }
  946. widecharpointertype, { pointer for WideChar-pointeddef }
  947. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  948. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  949. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  950. {$ifdef x86}
  951. voidnearpointertype,
  952. voidnearcspointertype,
  953. voidneardspointertype,
  954. voidnearsspointertype,
  955. voidnearespointertype,
  956. voidnearfspointertype,
  957. voidneargspointertype,
  958. {$ifdef i8086}
  959. voidfarpointertype,
  960. voidhugepointertype,
  961. charnearpointertype,
  962. charfarpointertype,
  963. charhugepointertype,
  964. bytefarpointertype, { used for Mem[] }
  965. wordfarpointertype, { used for MemW[] }
  966. longintfarpointertype, { used for MemL[] }
  967. {$endif i8086}
  968. {$endif x86}
  969. cundefinedtype,
  970. cformaltype, { unique formal definition }
  971. ctypedformaltype, { unique typed formal definition }
  972. voidtype, { Void (procedure) }
  973. cansichartype, { Char }
  974. cwidechartype, { WideChar }
  975. pasbool1type, { boolean type }
  976. pasbool8type,
  977. pasbool16type,
  978. pasbool32type,
  979. pasbool64type,
  980. bool8type,
  981. bool16type,
  982. bool32type,
  983. bool64type, { implement me }
  984. {$ifdef llvm}
  985. llvmbool1type, { LLVM i1 type }
  986. {$endif llvm}
  987. u8inttype, { 8-Bit unsigned integer }
  988. s8inttype, { 8-Bit signed integer }
  989. u16inttype, { 16-Bit unsigned integer }
  990. s16inttype, { 16-Bit signed integer }
  991. u32inttype, { 32-Bit unsigned integer }
  992. s32inttype, { 32-Bit signed integer }
  993. u64inttype, { 64-bit unsigned integer }
  994. s64inttype, { 64-bit signed integer }
  995. u128inttype, { 128-bit unsigned integer }
  996. s128inttype, { 128-bit signed integer }
  997. s32floattype, { 32 bit floating point number }
  998. s64floattype, { 64 bit floating point number }
  999. s80floattype, { 80 bit floating point number }
  1000. sc80floattype, { 80 bit floating point number but stored like in C }
  1001. s64currencytype, { pointer to a currency type }
  1002. cshortstringtype, { pointer to type of short string const }
  1003. clongstringtype, { pointer to type of long string const }
  1004. cansistringtype, { pointer to type of ansi string const }
  1005. cwidestringtype, { pointer to type of wide string const }
  1006. cunicodestringtype,
  1007. openshortstringtype, { pointer to type of an open shortstring,
  1008. needed for readln() }
  1009. openchararraytype, { pointer to type of an open array of char,
  1010. needed for readln() }
  1011. cfiletype, { get the same definition for all file }
  1012. { used for stabs }
  1013. methodpointertype, { typecasting of methodpointers to extract self }
  1014. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  1015. hresultdef,
  1016. typekindtype, { def of TTypeKind for correct handling of GetTypeKind parameters }
  1017. { we use only one variant def for every variant class }
  1018. cvarianttype,
  1019. colevarianttype,
  1020. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  1021. sinttype,
  1022. uinttype,
  1023. { integer types corresponding to OS_SINT/OS_INT }
  1024. ossinttype,
  1025. osuinttype,
  1026. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  1027. alusinttype,
  1028. aluuinttype,
  1029. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  1030. sizeuinttype,
  1031. sizesinttype,
  1032. { unsigned and signed ord type with the same size as a pointer }
  1033. ptruinttype,
  1034. ptrsinttype,
  1035. { unsigned and signed ord type with the same size as a codepointer }
  1036. codeptruinttype,
  1037. codeptrsinttype,
  1038. { several types to simulate more or less C++ objects for GDB }
  1039. vmttype,
  1040. vmtarraytype,
  1041. pvmttype : tdef; { type of classrefs, used for stabs }
  1042. { pointer to the anchestor of all classes }
  1043. class_tobject : tobjectdef;
  1044. { pointer to the ancestor of all COM interfaces }
  1045. interface_iunknown : tobjectdef;
  1046. { pointer to the ancestor of all dispinterfaces }
  1047. interface_idispatch : tobjectdef;
  1048. { pointer to the TGUID type
  1049. of all interfaces }
  1050. rec_tguid : trecorddef;
  1051. { jump buffer type, used by setjmp }
  1052. rec_jmp_buf : trecorddef;
  1053. { system.texceptaddr type, used by fpc_pushexceptaddr }
  1054. rec_exceptaddr: trecorddef;
  1055. { Objective-C base types }
  1056. objc_metaclasstype,
  1057. objc_superclasstype,
  1058. objc_idtype,
  1059. objc_seltype : tpointerdef;
  1060. objc_objecttype : trecorddef;
  1061. { base type of @protocol(protocolname) Objective-C statements }
  1062. objc_protocoltype : tobjectdef;
  1063. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  1064. objc_fastenumeration : tobjectdef;
  1065. objc_fastenumerationstate : trecorddef;
  1066. { Java base types }
  1067. { java.lang.Object }
  1068. java_jlobject : tobjectdef;
  1069. { java.lang.Throwable }
  1070. java_jlthrowable : tobjectdef;
  1071. { FPC base type for records }
  1072. java_fpcbaserecordtype : tobjectdef;
  1073. { java.lang.String }
  1074. java_jlstring : tobjectdef;
  1075. { java.lang.Enum }
  1076. java_jlenum : tobjectdef;
  1077. { java.util.EnumSet }
  1078. java_juenumset : tobjectdef;
  1079. { java.util.BitSet }
  1080. java_jubitset : tobjectdef;
  1081. { FPC java implementation of ansistrings }
  1082. java_ansistring : tobjectdef;
  1083. { FPC java implementation of shortstrings }
  1084. java_shortstring : tobjectdef;
  1085. { FPC java procvar base class }
  1086. java_procvarbase : tobjectdef;
  1087. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1088. function make_dllmangledname(const dllname,importname:TSymStr;
  1089. import_nr : word; pco : tproccalloption):TSymStr;
  1090. { should be in the types unit, but the types unit uses the node stuff :( }
  1091. function is_interfacecom(def: tdef): boolean;
  1092. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  1093. function is_any_interface_kind(def: tdef): boolean;
  1094. function is_interfacecorba(def: tdef): boolean;
  1095. function is_interface(def: tdef): boolean;
  1096. function is_dispinterface(def: tdef): boolean;
  1097. function is_object(def: tdef): boolean;
  1098. function is_class(def: tdef): boolean;
  1099. function is_cppclass(def: tdef): boolean;
  1100. function is_objectpascal_helper(def: tdef): boolean;
  1101. function is_objcclass(def: tdef): boolean;
  1102. function is_objcclassref(def: tdef): boolean;
  1103. function is_objcprotocol(def: tdef): boolean;
  1104. function is_objccategory(def: tdef): boolean;
  1105. function is_objc_class_or_protocol(def: tdef): boolean;
  1106. function is_objc_protocol_or_category(def: tdef): boolean;
  1107. function is_classhelper(def: tdef): boolean;
  1108. function is_class_or_interface(def: tdef): boolean;
  1109. function is_class_or_interface_or_objc(def: tdef): boolean;
  1110. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1111. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1112. function is_class_or_interface_or_object(def: tdef): boolean;
  1113. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1114. function is_implicit_pointer_object_type(def: tdef): boolean;
  1115. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1116. function is_implicit_array_pointer(def: tdef): boolean;
  1117. function is_class_or_object(def: tdef): boolean;
  1118. function is_record(def: tdef): boolean;
  1119. function is_javaclass(def: tdef): boolean;
  1120. function is_javaclassref(def: tdef): boolean;
  1121. function is_javainterface(def: tdef): boolean;
  1122. function is_java_class_or_interface(def: tdef): boolean;
  1123. procedure loadobjctypes;
  1124. procedure maybeloadcocoatypes;
  1125. function use_vectorfpu(def : tdef) : boolean;
  1126. function getansistringcodepage:tstringencoding; inline;
  1127. function getansistringdef:tstringdef;
  1128. function getparaencoding(def:tdef):tstringencoding; inline;
  1129. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1130. function get_recorddef(prefix:tinternaltypeprefix;const fields:array of tdef; packrecords:shortint): trecorddef;
  1131. { get a table def of the form
  1132. record
  1133. count: countdef;
  1134. elements: array[0..count-1] of elementdef
  1135. end;
  1136. Returns both the outer record and the inner arraydef
  1137. }
  1138. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1139. implementation
  1140. uses
  1141. SysUtils,
  1142. cutils,
  1143. { global }
  1144. verbose,
  1145. { target }
  1146. systems,paramgr,
  1147. { symtable }
  1148. symsym,symtable,defutil,objcdef,
  1149. { parser }
  1150. pgenutil,
  1151. { module }
  1152. fmodule,
  1153. { other }
  1154. gendef,
  1155. fpccrc,
  1156. entfile
  1157. ;
  1158. {****************************************************************************
  1159. Helpers
  1160. ****************************************************************************}
  1161. function getansistringcodepage:tstringencoding; inline;
  1162. begin
  1163. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1164. result:=current_settings.sourcecodepage
  1165. else
  1166. result:=0;
  1167. end;
  1168. function getansistringdef:tstringdef;
  1169. var
  1170. symtable:tsymtable;
  1171. oldstack : tsymtablestack;
  1172. begin
  1173. { if a codepage is explicitly defined in this mudule we need to return
  1174. a replacement for ansistring def }
  1175. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1176. begin
  1177. if not assigned(current_module) then
  1178. internalerror(2011101301);
  1179. { codepage can be redeclared only once per unit so we don't need a list of
  1180. redefined ansistring but only one pointer }
  1181. if not assigned(current_module.ansistrdef) then
  1182. begin
  1183. { if we did not create it yet we need to do this now }
  1184. if current_module.in_interface then
  1185. symtable:=current_module.globalsymtable
  1186. else
  1187. symtable:=current_module.localsymtable;
  1188. { create a temporary stack as it's not good (TM) to mess around
  1189. with the order if the unit contains generics or helpers; don't
  1190. use a def aware symtablestack though }
  1191. oldstack:=symtablestack;
  1192. symtablestack:=tsymtablestack.create;
  1193. symtablestack.push(symtable);
  1194. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage,true);
  1195. symtablestack.pop(symtable);
  1196. symtablestack.free;
  1197. symtablestack:=oldstack;
  1198. end;
  1199. result:=tstringdef(current_module.ansistrdef);
  1200. end
  1201. else
  1202. result:=tstringdef(cansistringtype);
  1203. end;
  1204. function getparaencoding(def:tdef):tstringencoding; inline;
  1205. begin
  1206. { don't pass CP_NONE encoding to internal functions
  1207. they expect 0 encoding instead
  1208. exception: result of string concatenation, because if you pass the
  1209. result of a string concatenation to a rawbytestring, the result of
  1210. that concatenation shouldn't be converted to defaultsystemcodepage
  1211. if all strings have the same type }
  1212. result:=tstringdef(def).encoding;
  1213. if result=globals.CP_NONE then
  1214. result:=0
  1215. end;
  1216. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1217. var
  1218. typ: ttypesym;
  1219. name: string;
  1220. begin
  1221. name:=internaltypeprefixName[itp_threadvar_record]+def.unique_id_str;
  1222. typ:=try_search_current_module_type(name);
  1223. if assigned(typ) then
  1224. begin
  1225. result:=trecorddef(ttypesym(typ).typedef);
  1226. index_field:=tsym(result.symtable.symlist[0]);
  1227. non_mt_data_field:=tsym(result.symtable.symlist[1]);
  1228. exit;
  1229. end;
  1230. { set recordalinmin to sizeof(pint), so the second field gets put at
  1231. offset = sizeof(pint) as expected }
  1232. result:=crecorddef.create_global_internal(
  1233. name,sizeof(pint),sizeof(pint),
  1234. init_settings.alignment.maxCrecordalign);
  1235. {$ifdef cpu16bitaddr}
  1236. index_field:=result.add_field_by_def('',u16inttype);
  1237. {$else cpu16bitaddr}
  1238. index_field:=result.add_field_by_def('',u32inttype);
  1239. {$endif cpu16bitaddr}
  1240. non_mt_data_field:=result.add_field_by_def('',def);
  1241. { no need to add alignment padding, we won't create arrays of these }
  1242. end;
  1243. function get_recorddef(prefix:tinternaltypeprefix; const fields:array of tdef; packrecords:shortint): trecorddef;
  1244. var
  1245. fieldlist: tfplist;
  1246. srsym: tsym;
  1247. srsymtable: tsymtable;
  1248. i: longint;
  1249. name : TIDString;
  1250. begin
  1251. name:=copy(internaltypeprefixName[prefix],2,length(internaltypeprefixName[prefix]));
  1252. if searchsym_type(name,srsym,srsymtable) then
  1253. begin
  1254. result:=trecorddef(ttypesym(srsym).typedef);
  1255. exit
  1256. end;
  1257. { also always search in the current module (symtables are popped for
  1258. RTTI related code already) }
  1259. if searchsym_in_module(pointer(current_module),name,srsym,srsymtable) then
  1260. begin
  1261. result:=trecorddef(ttypesym(srsym).typedef);
  1262. exit;
  1263. end;
  1264. fieldlist:=tfplist.create;
  1265. for i:=low(fields) to high(fields) do
  1266. fieldlist.add(fields[i]);
  1267. result:=crecorddef.create_global_internal(internaltypeprefixName[prefix],packrecords,
  1268. targetinfos[target_info.system]^.alignment.recordalignmin,
  1269. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1270. result.add_fields_from_deflist(fieldlist);
  1271. fieldlist.free;
  1272. end;
  1273. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1274. var
  1275. fields: tfplist;
  1276. name: TIDString;
  1277. srsym: tsym;
  1278. srsymtable: tsymtable;
  1279. begin
  1280. { already created a message string table with this number of elements
  1281. in this unit -> reuse the def }
  1282. name:=internaltypeprefixName[prefix]+tostr(count);
  1283. if searchsym_type(copy(name,2,length(name)),srsym,srsymtable) then
  1284. begin
  1285. recdef:=trecorddef(ttypesym(srsym).typedef);
  1286. arrdef:=tarraydef(trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size).vardef);
  1287. exit
  1288. end;
  1289. { also always search in the current module (symtables are popped for
  1290. RTTI related code already) }
  1291. if searchsym_in_module(pointer(current_module),copy(name,2,length(name)),srsym,srsymtable) then
  1292. begin
  1293. recdef:=trecorddef(ttypesym(srsym).typedef);
  1294. arrdef:=tarraydef(trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size).vardef);
  1295. exit;
  1296. end;
  1297. recdef:=crecorddef.create_global_internal(name,packrecords,
  1298. targetinfos[target_info.system]^.alignment.recordalignmin,
  1299. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1300. fields:=tfplist.create;
  1301. fields.add(countdef);
  1302. if count>0 then
  1303. begin
  1304. arrdef:=carraydef.create(0,count-1,sizeuinttype);
  1305. arrdef.elementdef:=elementdef;
  1306. fields.add(arrdef);
  1307. end
  1308. else
  1309. arrdef:=nil;
  1310. recdef.add_fields_from_deflist(fields);
  1311. fields.free;
  1312. end;
  1313. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1314. var
  1315. s,
  1316. prefix : TSymStr;
  1317. crc : dword;
  1318. begin
  1319. prefix:='';
  1320. if not assigned(st) then
  1321. internalerror(200204212);
  1322. { sub procedures }
  1323. while (st.symtabletype in [localsymtable,parasymtable]) do
  1324. begin
  1325. if st.defowner.typ<>procdef then
  1326. internalerror(200204173);
  1327. { Add the full mangledname of procedure to prevent
  1328. conflicts with 2 overloads having both a nested procedure
  1329. with the same name, see tb0314 (PFV) }
  1330. s:=tprocdef(st.defowner).procsym.name;
  1331. s:=s+tprocdef(st.defowner).mangledprocparanames(Length(s));
  1332. if prefix<>'' then
  1333. prefix:=s+'_'+prefix
  1334. else
  1335. prefix:=s;
  1336. if length(prefix)>100 then
  1337. begin
  1338. crc:=0;
  1339. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  1340. prefix:='$CRC'+hexstr(crc,8);
  1341. end;
  1342. st:=st.defowner.owner;
  1343. end;
  1344. { object/classes symtable, nested type definitions in classes require the while loop }
  1345. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1346. begin
  1347. if not (st.defowner.typ in [objectdef,recorddef]) then
  1348. internalerror(200204174);
  1349. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1350. st:=st.defowner.owner;
  1351. end;
  1352. { symtable must now be static or global }
  1353. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1354. internalerror(200204175);
  1355. { The mangled name is made out of at most 4 parts:
  1356. 1) Optional typeprefix given as first parameter
  1357. with '_$' appended if not empty
  1358. 2) Unit name or 'P$'+program name (never empty)
  1359. 3) optional prefix variable that contains a unique
  1360. name for the local symbol table (prepended with '$_$'
  1361. if not empty)
  1362. 4) suffix as given as third parameter,
  1363. also optional (i.e. can be empty)
  1364. prepended by '_$$_' if not empty }
  1365. result:='';
  1366. if typeprefix<>'' then
  1367. result:=result+typeprefix+'_$';
  1368. { Add P$ for program, which can have the same name as
  1369. a unit }
  1370. if (TSymtable(main_module.localsymtable)=st) and
  1371. (not main_module.is_unit) then
  1372. result:=result+'P$'+st.name^
  1373. else
  1374. result:=result+st.name^;
  1375. if prefix<>'' then
  1376. result:=result+'$_$'+prefix;
  1377. if suffix<>'' then
  1378. result:=result+'_$$_'+suffix;
  1379. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1380. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1381. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1382. { those in the debug map, leading to troubles with dsymutil). So always }
  1383. { add an underscore on darwin. }
  1384. if (target_info.system in systems_darwin) then
  1385. result := '_' + result;
  1386. end;
  1387. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1388. var
  1389. crc : cardinal;
  1390. i : longint;
  1391. use_crc : boolean;
  1392. dllprefix : TSymStr;
  1393. begin
  1394. if (target_info.system in (systems_all_windows + systems_nativent +
  1395. [system_i386_emx, system_i386_os2]))
  1396. and (dllname <> '') then
  1397. begin
  1398. dllprefix:=lower(ExtractFileName(dllname));
  1399. { Remove .dll suffix if present }
  1400. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1401. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1402. use_crc:=false;
  1403. for i:=1 to length(dllprefix) do
  1404. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1405. begin
  1406. use_crc:=true;
  1407. break;
  1408. end;
  1409. if use_crc then
  1410. begin
  1411. crc:=0;
  1412. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1413. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1414. end
  1415. else
  1416. dllprefix:='_$dll$'+dllprefix+'$';
  1417. if importname<>'' then
  1418. result:=dllprefix+importname
  1419. else
  1420. result:=dllprefix+'_index_'+tostr(import_nr);
  1421. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1422. { This allows to import VC++ mangled names from DLLs. }
  1423. { Do not perform replacement, if external symbol is not imported from DLL. }
  1424. if (dllname<>'') then
  1425. begin
  1426. Replace(result,'?','__q$$');
  1427. {$ifdef arm}
  1428. { @ symbol is not allowed in ARM assembler only }
  1429. Replace(result,'@','__a$$');
  1430. {$endif arm}
  1431. end;
  1432. end
  1433. else
  1434. begin
  1435. if importname<>'' then
  1436. begin
  1437. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1438. result:=importname
  1439. else
  1440. result:=target_info.Cprefix+importname;
  1441. end
  1442. else
  1443. result:='_index_'+tostr(import_nr);
  1444. end;
  1445. end;
  1446. {****************************************************************************
  1447. TDEFAWARESYMTABLESTACK
  1448. (symtablestack descendant that does some special actions on
  1449. the pushed/popped symtables)
  1450. ****************************************************************************}
  1451. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1452. var
  1453. i: integer;
  1454. s: string;
  1455. list: TFPObjectList;
  1456. def: tdef;
  1457. sym,srsym : tsym;
  1458. srsymtable : tsymtable;
  1459. entry : tgenericdummyentry;
  1460. begin
  1461. { search the symtable from first to last; the helper to use will be the
  1462. last one in the list }
  1463. for i:=0 to st.symlist.count-1 do
  1464. begin
  1465. sym:=tsym(st.symlist[i]);
  1466. if not (sym.typ in [typesym,procsym]) then
  1467. continue;
  1468. if sym.typ=typesym then
  1469. def:=ttypesym(st.SymList[i]).typedef
  1470. else
  1471. def:=nil;
  1472. if is_objectpascal_helper(def) then
  1473. begin
  1474. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1475. Message1(sym_d_adding_helper_for,s);
  1476. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1477. if not assigned(list) then
  1478. begin
  1479. list:=TFPObjectList.Create(false);
  1480. current_module.extendeddefs.Add(s,list);
  1481. end;
  1482. list.Add(def);
  1483. end
  1484. else
  1485. begin
  1486. if addgenerics then
  1487. add_generic_dummysym(sym);
  1488. { add nested helpers as well }
  1489. if assigned(def) and
  1490. (def.typ in [recorddef,objectdef]) and
  1491. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) then
  1492. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1493. end;
  1494. end;
  1495. end;
  1496. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1497. begin
  1498. if sto_has_helper in st.tableoptions then
  1499. remove_helpers(st);
  1500. if sto_has_generic in st.tableoptions then
  1501. remove_generics(st);
  1502. end;
  1503. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1504. var
  1505. i, j: integer;
  1506. tmpst: TSymtable;
  1507. list: TFPObjectList;
  1508. begin
  1509. for i:=current_module.extendeddefs.count-1 downto 0 do
  1510. begin
  1511. list:=TFPObjectList(current_module.extendeddefs[i]);
  1512. for j:=list.count-1 downto 0 do
  1513. begin
  1514. if not (list[j] is tobjectdef) then
  1515. Internalerror(2011031501);
  1516. tmpst:=tobjectdef(list[j]).owner;
  1517. repeat
  1518. if tmpst=st then
  1519. begin
  1520. list.delete(j);
  1521. break;
  1522. end
  1523. else
  1524. begin
  1525. if assigned(tmpst.defowner) then
  1526. tmpst:=tmpst.defowner.owner
  1527. else
  1528. tmpst:=nil;
  1529. end;
  1530. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1531. end;
  1532. if list.count=0 then
  1533. current_module.extendeddefs.delete(i);
  1534. end;
  1535. end;
  1536. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1537. var
  1538. i,j : longint;
  1539. entry : tgenericdummyentry;
  1540. list : tfpobjectlist;
  1541. begin
  1542. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1543. begin
  1544. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1545. if not assigned(list) then
  1546. continue;
  1547. for j:=list.count-1 downto 0 do
  1548. begin
  1549. entry:=tgenericdummyentry(list[j]);
  1550. if entry.dummysym.owner=st then
  1551. list.delete(j);
  1552. end;
  1553. if list.count=0 then
  1554. current_module.genericdummysyms.delete(i);
  1555. end;
  1556. end;
  1557. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1558. begin
  1559. if (sto_has_generic in st.tableoptions) or
  1560. (
  1561. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1562. (sto_has_helper in st.tableoptions)
  1563. ) then
  1564. { nested helpers will be added as well }
  1565. add_helpers_and_generics(st,true);
  1566. end;
  1567. procedure tdefawaresymtablestack.push(st: TSymtable);
  1568. begin
  1569. pushcommon(st);
  1570. inherited push(st);
  1571. end;
  1572. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1573. begin
  1574. pushcommon(st);
  1575. inherited pushafter(st,afterst);
  1576. end;
  1577. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1578. begin
  1579. inherited pop(st);
  1580. if (sto_has_generic in st.tableoptions) or
  1581. (
  1582. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1583. (sto_has_helper in st.tableoptions)
  1584. ) then
  1585. { nested helpers will be removed as well }
  1586. remove_helpers_and_generics(st);
  1587. end;
  1588. {****************************************************************************
  1589. TDEF (base class for definitions)
  1590. ****************************************************************************}
  1591. constructor tgenericconstraintdata.create;
  1592. begin
  1593. interfaces:=tfpobjectlist.create(false);
  1594. interfacesderef:=tfplist.create;
  1595. end;
  1596. destructor tgenericconstraintdata.destroy;
  1597. var
  1598. i : longint;
  1599. begin
  1600. for i:=0 to interfacesderef.count-1 do
  1601. dispose(pderef(interfacesderef[i]));
  1602. interfacesderef.free;
  1603. interfaces.free;
  1604. inherited destroy;
  1605. end;
  1606. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1607. var
  1608. cnt,i : longint;
  1609. intfderef : pderef;
  1610. begin
  1611. ppufile.getsmallset(flags);
  1612. cnt:=ppufile.getlongint;
  1613. for i:=0 to cnt-1 do
  1614. begin
  1615. new(intfderef);
  1616. ppufile.getderef(intfderef^);
  1617. interfacesderef.add(intfderef);
  1618. end;
  1619. end;
  1620. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1621. var
  1622. i : longint;
  1623. begin
  1624. ppufile.putsmallset(flags);
  1625. ppufile.putlongint(interfacesderef.count);
  1626. for i:=0 to interfacesderef.count-1 do
  1627. ppufile.putderef(pderef(interfacesderef[i])^);
  1628. end;
  1629. procedure tgenericconstraintdata.buildderef;
  1630. var
  1631. intfderef : pderef;
  1632. i : longint;
  1633. begin
  1634. for i:=0 to interfaces.count-1 do
  1635. begin
  1636. new(intfderef);
  1637. intfderef^.build(tobjectdef(interfaces[i]));
  1638. interfacesderef.add(intfderef);
  1639. end;
  1640. end;
  1641. procedure tgenericconstraintdata.deref;
  1642. var
  1643. i : longint;
  1644. begin
  1645. for i:=0 to interfacesderef.count-1 do
  1646. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1647. end;
  1648. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1649. begin
  1650. ppuwrite_platform(ppufile);
  1651. ppufile.writeentry(ibnr);
  1652. end;
  1653. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1654. begin
  1655. { by default: do nothing }
  1656. end;
  1657. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1658. begin
  1659. { by default: do nothing }
  1660. end;
  1661. class procedure tstoreddef.setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  1662. var
  1663. reusablesymtab: tsymtable;
  1664. begin
  1665. { must not yet belong to a symtable }
  1666. if assigned(newdef.owner) then
  1667. internalerror(2015111503);
  1668. reusablesymtab:=origdef.getreusablesymtab;
  1669. res^.Data:=newdef;
  1670. reusablesymtab.insertdef(newdef);
  1671. symtablestack:=oldsymtablestack;
  1672. end;
  1673. constructor tstoreddef.create(dt:tdeftyp;doregister:boolean);
  1674. begin
  1675. inherited create(dt);
  1676. savesize := 0;
  1677. {$ifdef EXTDEBUG}
  1678. fileinfo := current_filepos;
  1679. {$endif}
  1680. generictokenbuf:=nil;
  1681. genericdef:=nil;
  1682. { Don't register forwarddefs, they are disposed at the
  1683. end of an type block }
  1684. if (dt=forwarddef) then
  1685. exit;
  1686. { Register in symtable stack }
  1687. if doregister then
  1688. begin
  1689. { immediately register interface defs, as they will always be
  1690. written to the ppu, their defid inlfuences the interface crc and
  1691. if we wait, depending on e.g. compiler defines they may get a
  1692. different defid (e.g. when a function is called, its procdef is
  1693. registered, so depending on whether or not, or when, an interface
  1694. procedure is called in the implementation, that may change its
  1695. defid otherwise) }
  1696. if assigned(current_module) and
  1697. current_module.in_interface then
  1698. register_def
  1699. else
  1700. maybe_put_in_symtable_stack;
  1701. end;
  1702. end;
  1703. destructor tstoreddef.destroy;
  1704. var
  1705. i : longint;
  1706. begin
  1707. { Direct calls are not allowed, use symtable.deletedef() }
  1708. if assigned(owner) then
  1709. internalerror(200612311);
  1710. if assigned(generictokenbuf) then
  1711. begin
  1712. generictokenbuf.free;
  1713. generictokenbuf:=nil;
  1714. end;
  1715. genericparas.free;
  1716. if assigned(genericparaderefs) then
  1717. for i:=0 to genericparaderefs.count-1 do
  1718. dispose(pderef(genericparaderefs[i]));
  1719. genericparaderefs.free;
  1720. genconstraintdata.free;
  1721. {$ifndef symansistr}
  1722. stringdispose(_fullownerhierarchyname);
  1723. {$endif not symansistr}
  1724. inherited destroy;
  1725. end;
  1726. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1727. var
  1728. sizeleft,i,cnt : longint;
  1729. buf : array[0..255] of byte;
  1730. symderef : pderef;
  1731. begin
  1732. inherited create(dt);
  1733. DefId:=ppufile.getlongint;
  1734. current_module.deflist[DefId]:=self;
  1735. {$ifdef EXTDEBUG}
  1736. fillchar(fileinfo,sizeof(fileinfo),0);
  1737. {$endif}
  1738. { load }
  1739. ppufile.getderef(typesymderef);
  1740. ppufile.getsmallset(defoptions);
  1741. ppufile.getsmallset(defstates);
  1742. if df_genconstraint in defoptions then
  1743. begin
  1744. genconstraintdata:=tgenericconstraintdata.create;
  1745. genconstraintdata.ppuload(ppufile);
  1746. end;
  1747. if [df_generic,df_specialization]*defoptions<>[] then
  1748. begin
  1749. cnt:=ppufile.getlongint;
  1750. if cnt>0 then
  1751. begin
  1752. genericparas:=tfphashobjectlist.create(false);
  1753. genericparaderefs:=tfplist.create;
  1754. for i:=0 to cnt-1 do
  1755. begin
  1756. genericparas.add(ppufile.getstring,nil);
  1757. New(symderef);
  1758. ppufile.getderef(symderef^);
  1759. genericparaderefs.add(symderef);
  1760. end;
  1761. end;
  1762. end;
  1763. if df_generic in defoptions then
  1764. begin
  1765. sizeleft:=ppufile.getlongint;
  1766. initgeneric;
  1767. while sizeleft>0 do
  1768. begin
  1769. if sizeleft>sizeof(buf) then
  1770. i:=sizeof(buf)
  1771. else
  1772. i:=sizeleft;
  1773. ppufile.getdata(buf,i);
  1774. generictokenbuf.write(buf,i);
  1775. dec(sizeleft,i);
  1776. end;
  1777. end;
  1778. if df_specialization in defoptions then
  1779. ppufile.getderef(genericdefderef);
  1780. end;
  1781. function tstoreddef.needs_separate_initrtti:boolean;
  1782. begin
  1783. result:=false;
  1784. end;
  1785. function tstoreddef.rtti_mangledname(rt : trttitype) : TSymStr;
  1786. var
  1787. prefix : string[4];
  1788. begin
  1789. if (rt=fullrtti) or (not needs_separate_initrtti) then
  1790. begin
  1791. prefix:='RTTI';
  1792. include(defstates,ds_rtti_table_used);
  1793. end
  1794. else
  1795. begin
  1796. prefix:='INIT';
  1797. include(defstates,ds_init_table_used);
  1798. end;
  1799. if assigned(typesym) and
  1800. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1801. result:=make_mangledname(prefix,typesym.owner,typesym.name)
  1802. else
  1803. result:=make_mangledname(prefix,findunitsymtable(owner),'def'+unique_id_str)
  1804. end;
  1805. function tstoreddef.OwnerHierarchyName: string;
  1806. var
  1807. tmp: tdef;
  1808. begin
  1809. tmp:=self;
  1810. result:='';
  1811. repeat
  1812. { can be not assigned in case of a forwarddef }
  1813. if assigned(tmp.owner) and
  1814. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1815. tmp:=tdef(tmp.owner.defowner)
  1816. else
  1817. break;
  1818. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1819. until tmp=nil;
  1820. end;
  1821. function tstoreddef.fullownerhierarchyname(skipprocparams:boolean): TSymStr;
  1822. var
  1823. lastowner: tsymtable;
  1824. tmp: tdef;
  1825. pno: tprocnameoptions;
  1826. begin
  1827. {$ifdef symansistr}
  1828. if not skipprocparams and (_fullownerhierarchyname<>'') then
  1829. exit(_fullownerhierarchyname);
  1830. {$else symansistr}
  1831. if not skipprocparams and assigned(_fullownerhierarchyname) then
  1832. exit(_fullownerhierarchyname^);
  1833. {$endif symansistr}
  1834. { the def can only reside inside structured types or
  1835. procedures/functions/methods }
  1836. tmp:=self;
  1837. result:='';
  1838. repeat
  1839. lastowner:=tmp.owner;
  1840. { can be not assigned in case of a forwarddef }
  1841. if not assigned(lastowner) then
  1842. break
  1843. else
  1844. tmp:=tdef(lastowner.defowner);
  1845. if not assigned(tmp) then
  1846. break;
  1847. if tmp.typ in [recorddef,objectdef] then
  1848. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  1849. else
  1850. if tmp.typ=procdef then
  1851. begin
  1852. pno:=[pno_paranames,pno_proctypeoption];
  1853. if skipprocparams then
  1854. include(pno,pno_noparams);
  1855. result:=tprocdef(tmp).customprocname(pno)+'.'+result;
  1856. end;
  1857. until tmp=nil;
  1858. { add the unit name }
  1859. if assigned(lastowner) and
  1860. assigned(lastowner.realname) then
  1861. result:=lastowner.realname^+'.'+result;
  1862. if not skipprocparams then
  1863. { don't store the name in this case }
  1864. {$ifdef symansistr}
  1865. _fullownerhierarchyname:=result;
  1866. {$else symansistr}
  1867. _fullownerhierarchyname:=stringdup(result);
  1868. {$endif symansistr}
  1869. end;
  1870. function tstoreddef.in_currentunit: boolean;
  1871. var
  1872. st: tsymtable;
  1873. begin
  1874. st:=owner;
  1875. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1876. st:=st.defowner.owner;
  1877. result:=st.iscurrentunit;
  1878. end;
  1879. function tstoreddef.getcopy : tstoreddef;
  1880. begin
  1881. Message(sym_e_cant_create_unique_type);
  1882. getcopy:=cerrordef.create;
  1883. end;
  1884. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1885. var
  1886. sizeleft,i : longint;
  1887. buf : array[0..255] of byte;
  1888. oldintfcrc : boolean;
  1889. begin
  1890. if defid<0 then
  1891. internalerror(2015101401);
  1892. ppufile.putlongint(DefId);
  1893. ppufile.putderef(typesymderef);
  1894. ppufile.putsmallset(defoptions);
  1895. oldintfcrc:=ppufile.do_crc;
  1896. ppufile.do_crc:=false;
  1897. ppufile.putsmallset(defstates);
  1898. if df_genconstraint in defoptions then
  1899. genconstraintdata.ppuwrite(ppufile);
  1900. if [df_generic,df_specialization]*defoptions<>[] then
  1901. begin
  1902. if not assigned(genericparas) then
  1903. ppufile.putlongint(0)
  1904. else
  1905. begin
  1906. if not assigned(genericparaderefs) then
  1907. internalerror(2014052305);
  1908. ppufile.putlongint(genericparas.count);
  1909. for i:=0 to genericparas.count-1 do
  1910. begin
  1911. ppufile.putstring(genericparas.nameofindex(i));
  1912. ppufile.putderef(pderef(genericparaderefs[i])^);
  1913. end;
  1914. end;
  1915. end;
  1916. if df_generic in defoptions then
  1917. begin
  1918. if assigned(generictokenbuf) then
  1919. begin
  1920. sizeleft:=generictokenbuf.size;
  1921. generictokenbuf.seek(0);
  1922. end
  1923. else
  1924. sizeleft:=0;
  1925. ppufile.putlongint(sizeleft);
  1926. while sizeleft>0 do
  1927. begin
  1928. if sizeleft>sizeof(buf) then
  1929. i:=sizeof(buf)
  1930. else
  1931. i:=sizeleft;
  1932. generictokenbuf.read(buf,i);
  1933. ppufile.putdata(buf,i);
  1934. dec(sizeleft,i);
  1935. end;
  1936. end;
  1937. ppufile.do_crc:=oldintfcrc;
  1938. if df_specialization in defoptions then
  1939. ppufile.putderef(genericdefderef);
  1940. end;
  1941. procedure tstoreddef.buildderef;
  1942. var
  1943. i : longint;
  1944. sym : tsym;
  1945. symderef : pderef;
  1946. begin
  1947. if not registered then
  1948. register_def;
  1949. typesymderef.build(typesym);
  1950. genericdefderef.build(genericdef);
  1951. if assigned(genconstraintdata) then
  1952. genconstraintdata.buildderef;
  1953. if assigned(genericparas) then
  1954. begin
  1955. if not assigned(genericparaderefs) then
  1956. genericparaderefs:=tfplist.create;
  1957. for i:=0 to genericparas.count-1 do
  1958. begin
  1959. sym:=tsym(genericparas.items[i]);
  1960. new(symderef);
  1961. symderef^.build(sym);
  1962. genericparaderefs.add(symderef);
  1963. end;
  1964. end;
  1965. end;
  1966. procedure tstoreddef.buildderefimpl;
  1967. begin
  1968. end;
  1969. procedure tstoreddef.deref;
  1970. var
  1971. symderef : pderef;
  1972. i : longint;
  1973. begin
  1974. typesym:=ttypesym(typesymderef.resolve);
  1975. if df_specialization in defoptions then
  1976. genericdef:=tstoreddef(genericdefderef.resolve);
  1977. if assigned(genconstraintdata) then
  1978. genconstraintdata.deref;
  1979. if assigned(genericparas) then
  1980. begin
  1981. if not assigned(genericparaderefs) then
  1982. internalerror(2014052302);
  1983. if genericparas.count<>genericparaderefs.count then
  1984. internalerror(2014052303);
  1985. for i:=0 to genericparaderefs.count-1 do
  1986. begin
  1987. symderef:=pderef(genericparaderefs[i]);
  1988. genericparas.items[i]:=symderef^.resolve;
  1989. end;
  1990. end;
  1991. end;
  1992. procedure tstoreddef.derefimpl;
  1993. begin
  1994. end;
  1995. function tstoreddef.size : asizeint;
  1996. begin
  1997. size:=savesize;
  1998. end;
  1999. function tstoreddef.getvardef:longint;
  2000. begin
  2001. result:=varUndefined;
  2002. end;
  2003. function tstoreddef.alignment : shortint;
  2004. begin
  2005. { natural alignment by default }
  2006. alignment:=size_2_align(savesize);
  2007. { can happen if savesize = 0, e.g. for voiddef or
  2008. an empty record
  2009. }
  2010. if (alignment=0) then
  2011. alignment:=1;
  2012. end;
  2013. { returns true, if the definition can be published }
  2014. function tstoreddef.is_publishable : boolean;
  2015. begin
  2016. is_publishable:=false;
  2017. end;
  2018. { needs an init table }
  2019. function tstoreddef.needs_inittable : boolean;
  2020. begin
  2021. needs_inittable:=false;
  2022. end;
  2023. function tstoreddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  2024. begin
  2025. result:=false;
  2026. end;
  2027. function tstoreddef.is_intregable : boolean;
  2028. var
  2029. recsize,temp: longint;
  2030. begin
  2031. is_intregable:=false;
  2032. case typ of
  2033. orddef,
  2034. pointerdef,
  2035. enumdef,
  2036. classrefdef:
  2037. is_intregable:=true;
  2038. procvardef :
  2039. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  2040. objectdef:
  2041. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  2042. setdef:
  2043. is_intregable:=is_smallset(self);
  2044. recorddef:
  2045. begin
  2046. {$ifdef llvm}
  2047. is_intregable:=false;
  2048. {$else llvm}
  2049. recsize:=size;
  2050. is_intregable:=
  2051. ispowerof2(recsize,temp) and
  2052. ((recsize<=sizeof(aint)*2) and
  2053. not trecorddef(self).contains_cross_aword_field and
  2054. { records cannot go into registers on 16 bit targets for now }
  2055. (sizeof(aint)>2) and
  2056. (not trecorddef(self).contains_float_field) or
  2057. (recsize <= sizeof(aint))
  2058. ) and
  2059. not needs_inittable;
  2060. {$endif llvm}
  2061. end;
  2062. end;
  2063. end;
  2064. function tstoreddef.is_fpuregable : boolean;
  2065. begin
  2066. {$ifdef x86}
  2067. result:=use_vectorfpu(self);
  2068. {$else x86}
  2069. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  2070. {$endif x86}
  2071. end;
  2072. function tstoreddef.is_const_intregable : boolean;
  2073. begin
  2074. case typ of
  2075. stringdef:
  2076. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  2077. arraydef:
  2078. result:=is_dynamic_array(self);
  2079. objectdef:
  2080. result:=is_interface(self);
  2081. else
  2082. result:=false;
  2083. end;
  2084. end;
  2085. procedure tstoreddef.initgeneric;
  2086. begin
  2087. if assigned(generictokenbuf) then
  2088. internalerror(200512131);
  2089. generictokenbuf:=tdynamicarray.create(256);
  2090. end;
  2091. function tstoreddef.is_generic: boolean;
  2092. var
  2093. sym: tsym;
  2094. i: longint;
  2095. begin
  2096. result:=assigned(genericparas) and
  2097. (genericparas.count>0) and
  2098. (df_generic in defoptions);
  2099. if result then
  2100. { if any of the type parameters does *not* belong to as (meaning it was passed
  2101. in from outside) then we aren't a generic, but a specialization }
  2102. for i:=0 to genericparas.count-1 do
  2103. begin
  2104. sym:=tsym(genericparas[i]);
  2105. if sym.typ<>symconst.typesym then
  2106. internalerror(2014050903);
  2107. if sym.owner.defowner<>self then
  2108. exit(false);
  2109. end;
  2110. end;
  2111. function tstoreddef.is_specialization: boolean;
  2112. var
  2113. i : longint;
  2114. sym : tsym;
  2115. begin
  2116. result:=assigned(genericparas) and
  2117. (genericparas.count>0) and
  2118. (df_specialization in defoptions);
  2119. if result then
  2120. begin
  2121. { if at least one of the generic parameters is not owned by us (meaning it was
  2122. passed in from outside) then we have a specialization, otherwise we have a generic }
  2123. for i:=0 to genericparas.count-1 do
  2124. begin
  2125. sym:=tsym(genericparas[i]);
  2126. if sym.typ<>symconst.typesym then
  2127. internalerror(2014050904);
  2128. if sym.owner.defowner<>self then
  2129. exit(true);
  2130. end;
  2131. result:=false;
  2132. end;
  2133. end;
  2134. procedure tstoreddef.register_def;
  2135. var
  2136. gst : tgetsymtable;
  2137. st : tsymtable;
  2138. begin
  2139. if registered then
  2140. exit;
  2141. { Register in current_module }
  2142. if assigned(current_module) then
  2143. begin
  2144. exclude(defoptions,df_not_registered_no_free);
  2145. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  2146. begin
  2147. st:=getsymtable(gst);
  2148. if assigned(st) then
  2149. tstoredsymtable(st).register_children;
  2150. end;
  2151. if defid<defid_not_registered then
  2152. defid:=deflist_index
  2153. else
  2154. begin
  2155. current_module.deflist.Add(self);
  2156. defid:=current_module.deflist.Count-1;
  2157. end;
  2158. maybe_put_in_symtable_stack;
  2159. end
  2160. else
  2161. DefId:=defid_registered_nost;
  2162. end;
  2163. procedure tstoreddef.maybe_put_in_symtable_stack;
  2164. var
  2165. insertstack: psymtablestackitem;
  2166. begin
  2167. if assigned(symtablestack) and
  2168. not assigned(self.owner) then
  2169. begin
  2170. insertstack:=symtablestack.stack;
  2171. { don't insert defs in exception symtables, as they are freed before
  2172. the module is compiled, so we can get crashes on high level targets
  2173. if they still need it while e.g. writing assembler code }
  2174. while assigned(insertstack) and
  2175. (insertstack^.symtable.symtabletype in [stt_exceptsymtable,withsymtable]) do
  2176. insertstack:=insertstack^.next;
  2177. if not assigned(insertstack) then
  2178. internalerror(200602044);
  2179. if insertstack^.symtable.sealed then
  2180. internalerror(2015022301);
  2181. insertstack^.symtable.insertdef(self);
  2182. end;
  2183. end;
  2184. {****************************************************************************
  2185. Tstringdef
  2186. ****************************************************************************}
  2187. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2188. begin
  2189. inherited create(stringdef,doregister);
  2190. stringtype:=st_shortstring;
  2191. encoding:=0;
  2192. len:=l;
  2193. end;
  2194. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2195. begin
  2196. inherited ppuload(stringdef,ppufile);
  2197. stringtype:=st_shortstring;
  2198. encoding:=0;
  2199. len:=ppufile.getbyte;
  2200. ppuload_platform(ppufile);
  2201. end;
  2202. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2203. begin
  2204. inherited create(stringdef,doregister);
  2205. stringtype:=st_longstring;
  2206. encoding:=0;
  2207. len:=l;
  2208. end;
  2209. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2210. begin
  2211. inherited ppuload(stringdef,ppufile);
  2212. stringtype:=st_longstring;
  2213. encoding:=0;
  2214. len:=ppufile.getasizeint;
  2215. ppuload_platform(ppufile);
  2216. end;
  2217. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2218. begin
  2219. inherited create(stringdef,doregister);
  2220. stringtype:=st_ansistring;
  2221. encoding:=aencoding;
  2222. len:=-1;
  2223. end;
  2224. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2225. begin
  2226. inherited ppuload(stringdef,ppufile);
  2227. stringtype:=st_ansistring;
  2228. len:=ppufile.getasizeint;
  2229. encoding:=ppufile.getword;
  2230. ppuload_platform(ppufile);
  2231. end;
  2232. constructor tstringdef.createwide(doregister: boolean);
  2233. begin
  2234. inherited create(stringdef,doregister);
  2235. stringtype:=st_widestring;
  2236. if target_info.endian=endian_little then
  2237. encoding:=CP_UTF16LE
  2238. else
  2239. encoding:=CP_UTF16BE;
  2240. len:=-1;
  2241. end;
  2242. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2243. begin
  2244. inherited ppuload(stringdef,ppufile);
  2245. stringtype:=st_widestring;
  2246. if target_info.endian=endian_little then
  2247. encoding:=CP_UTF16LE
  2248. else
  2249. encoding:=CP_UTF16BE;
  2250. len:=ppufile.getasizeint;
  2251. ppuload_platform(ppufile);
  2252. end;
  2253. constructor tstringdef.createunicode(doregister: boolean);
  2254. begin
  2255. inherited create(stringdef,doregister);
  2256. stringtype:=st_unicodestring;
  2257. if target_info.endian=endian_little then
  2258. encoding:=CP_UTF16LE
  2259. else
  2260. encoding:=CP_UTF16BE;
  2261. len:=-1;
  2262. end;
  2263. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2264. begin
  2265. inherited ppuload(stringdef,ppufile);
  2266. stringtype:=st_unicodestring;
  2267. len:=ppufile.getasizeint;
  2268. encoding:=ppufile.getword;
  2269. ppuload_platform(ppufile);
  2270. end;
  2271. function tstringdef.getcopy : tstoreddef;
  2272. begin
  2273. result:=cstringdef.create(typ,true);
  2274. result.typ:=stringdef;
  2275. tstringdef(result).stringtype:=stringtype;
  2276. tstringdef(result).encoding:=encoding;
  2277. tstringdef(result).len:=len;
  2278. end;
  2279. function tstringdef.stringtypname:string;
  2280. const
  2281. typname:array[tstringtype] of string[10]=(
  2282. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2283. );
  2284. begin
  2285. stringtypname:=typname[stringtype];
  2286. end;
  2287. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2288. begin
  2289. inherited ppuwrite(ppufile);
  2290. if stringtype=st_shortstring then
  2291. begin
  2292. {$ifdef extdebug}
  2293. if len > 255 then internalerror(12122002);
  2294. {$endif}
  2295. ppufile.putbyte(byte(len))
  2296. end
  2297. else
  2298. ppufile.putasizeint(len);
  2299. if stringtype in [st_ansistring,st_unicodestring] then
  2300. ppufile.putword(encoding);
  2301. case stringtype of
  2302. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2303. st_longstring : writeentry(ppufile,iblongstringdef);
  2304. st_ansistring : writeentry(ppufile,ibansistringdef);
  2305. st_widestring : writeentry(ppufile,ibwidestringdef);
  2306. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2307. end;
  2308. end;
  2309. function tstringdef.needs_inittable : boolean;
  2310. begin
  2311. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2312. end;
  2313. function tstringdef.GetTypeName : string;
  2314. const
  2315. names : array[tstringtype] of string[15] = (
  2316. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2317. begin
  2318. GetTypeName:=names[stringtype];
  2319. end;
  2320. function tstringdef.getvardef : longint;
  2321. const
  2322. vardef : array[tstringtype] of longint = (
  2323. varUndefined,varUndefined,varString,varOleStr,varUString);
  2324. begin
  2325. result:=vardef[stringtype];
  2326. end;
  2327. function tstringdef.alignment : shortint;
  2328. begin
  2329. case stringtype of
  2330. st_unicodestring,
  2331. st_widestring,
  2332. st_ansistring:
  2333. alignment:=voidpointertype.alignment;
  2334. st_longstring,
  2335. st_shortstring:
  2336. { char to string accesses byte 0 and 1 with one word access }
  2337. if (tf_requires_proper_alignment in target_info.flags) or
  2338. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2339. (m_mac in current_settings.modeswitches) then
  2340. alignment:=size_2_align(2)
  2341. else
  2342. alignment:=size_2_align(1);
  2343. else
  2344. internalerror(200412301);
  2345. end;
  2346. end;
  2347. function tstringdef.getmangledparaname : TSymStr;
  2348. begin
  2349. getmangledparaname:='STRING';
  2350. end;
  2351. function tstringdef.is_publishable : boolean;
  2352. begin
  2353. is_publishable:=true;
  2354. end;
  2355. function tstringdef.size: asizeint;
  2356. begin
  2357. case stringtype of
  2358. st_shortstring:
  2359. Result:=len+1;
  2360. st_longstring,
  2361. st_ansistring,
  2362. st_widestring,
  2363. st_unicodestring:
  2364. Result:=voidpointertype.size;
  2365. else
  2366. internalerror(2014032301);
  2367. end;
  2368. end;
  2369. {****************************************************************************
  2370. TENUMDEF
  2371. ****************************************************************************}
  2372. constructor tenumdef.create;
  2373. begin
  2374. inherited create(enumdef,true);
  2375. minval:=0;
  2376. maxval:=0;
  2377. calcsavesize(current_settings.packenum);
  2378. has_jumps:=false;
  2379. basedef:=nil;
  2380. symtable:=tenumsymtable.create(self);
  2381. end;
  2382. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2383. begin
  2384. inherited create(enumdef,true);
  2385. minval:=_min;
  2386. maxval:=_max;
  2387. basedef:=_basedef;
  2388. calcsavesize(current_settings.packenum);
  2389. has_jumps:=false;
  2390. symtable:=basedef.symtable.getcopy;
  2391. include(defoptions, df_copied_def);
  2392. end;
  2393. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2394. begin
  2395. inherited ppuload(enumdef,ppufile);
  2396. minval:=ppufile.getaint;
  2397. maxval:=ppufile.getaint;
  2398. savesize:=ppufile.getaint;
  2399. has_jumps:=false;
  2400. if df_copied_def in defoptions then
  2401. begin
  2402. symtable:=nil;
  2403. ppufile.getderef(basedefderef);
  2404. ppuload_platform(ppufile);
  2405. end
  2406. else
  2407. begin
  2408. ppuload_platform(ppufile);
  2409. // create with nil defowner first to prevent values changes on insert
  2410. symtable:=tenumsymtable.create(nil);
  2411. tenumsymtable(symtable).ppuload(ppufile);
  2412. symtable.defowner:=self;
  2413. end;
  2414. end;
  2415. destructor tenumdef.destroy;
  2416. begin
  2417. symtable.free;
  2418. symtable:=nil;
  2419. inherited destroy;
  2420. end;
  2421. function tenumdef.getcopy : tstoreddef;
  2422. begin
  2423. if assigned(basedef) then
  2424. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2425. else
  2426. begin
  2427. result:=cenumdef.create;
  2428. tenumdef(result).minval:=minval;
  2429. tenumdef(result).maxval:=maxval;
  2430. tenumdef(result).symtable.free;
  2431. tenumdef(result).symtable:=symtable.getcopy;
  2432. tenumdef(result).basedef:=self;
  2433. end;
  2434. tenumdef(result).has_jumps:=has_jumps;
  2435. tenumdef(result).basedefderef:=basedefderef;
  2436. include(tenumdef(result).defoptions,df_copied_def);
  2437. end;
  2438. procedure tenumdef.calcsavesize(packenum: shortint);
  2439. begin
  2440. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2441. if (packenum=8) or (int64(min)<low(longint)) or (int64(max)>high(cardinal)) then
  2442. savesize:=8
  2443. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2444. else
  2445. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2446. if (packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2447. savesize:=4
  2448. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2449. else
  2450. if (packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2451. savesize:=2
  2452. else
  2453. savesize:=1;
  2454. end;
  2455. function tenumdef.packedbitsize: asizeint;
  2456. var
  2457. sizeval: tconstexprint;
  2458. power: longint;
  2459. begin
  2460. result := 0;
  2461. if (minval >= 0) and
  2462. (maxval <= 1) then
  2463. result := 1
  2464. else
  2465. begin
  2466. if (minval>=0) then
  2467. sizeval:=maxval
  2468. else
  2469. { don't count 0 twice }
  2470. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  2471. { 256 must become 512 etc. }
  2472. nextpowerof2(sizeval+1,power);
  2473. result := power;
  2474. end;
  2475. end;
  2476. procedure tenumdef.setmax(_max:asizeint);
  2477. begin
  2478. maxval:=_max;
  2479. calcsavesize(current_settings.packenum);
  2480. end;
  2481. procedure tenumdef.setmin(_min:asizeint);
  2482. begin
  2483. minval:=_min;
  2484. calcsavesize(current_settings.packenum);
  2485. end;
  2486. function tenumdef.min:asizeint;
  2487. begin
  2488. min:=minval;
  2489. end;
  2490. function tenumdef.max:asizeint;
  2491. begin
  2492. max:=maxval;
  2493. end;
  2494. function tenumdef.getfirstsym: tsym;
  2495. var
  2496. i:integer;
  2497. begin
  2498. for i := 0 to symtable.SymList.Count - 1 do
  2499. begin
  2500. result:=tsym(symtable.SymList[i]);
  2501. if tenumsym(result).value=minval then
  2502. exit;
  2503. end;
  2504. result:=nil;
  2505. end;
  2506. function tenumdef.int2enumsym(l: asizeint): tsym;
  2507. var
  2508. i: longint;
  2509. sym: tsym;
  2510. bdef: tenumdef;
  2511. begin
  2512. result:=nil;
  2513. if (l<minval) or
  2514. (l>maxval) then
  2515. exit;
  2516. bdef:=getbasedef;
  2517. for i:=0 to bdef.symtable.symlist.count-1 do
  2518. begin
  2519. sym:=tsym(bdef.symtable.symlist[i]);
  2520. if (sym.typ=enumsym) and
  2521. (tenumsym(sym).value=l) then
  2522. begin
  2523. result:=sym;
  2524. exit;
  2525. end;
  2526. end;
  2527. end;
  2528. function tenumdef.getbasedef: tenumdef;
  2529. begin
  2530. if not assigned(basedef) then
  2531. result:=self
  2532. else
  2533. result:=basedef;
  2534. end;
  2535. procedure tenumdef.buildderef;
  2536. begin
  2537. inherited buildderef;
  2538. if df_copied_def in defoptions then
  2539. basedefderef.build(basedef)
  2540. else
  2541. tenumsymtable(symtable).buildderef;
  2542. end;
  2543. procedure tenumdef.deref;
  2544. begin
  2545. inherited deref;
  2546. if df_copied_def in defoptions then
  2547. begin
  2548. basedef:=tenumdef(basedefderef.resolve);
  2549. symtable:=basedef.symtable.getcopy;
  2550. end
  2551. else
  2552. tenumsymtable(symtable).deref(false);
  2553. end;
  2554. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2555. begin
  2556. inherited ppuwrite(ppufile);
  2557. ppufile.putaint(min);
  2558. ppufile.putaint(max);
  2559. ppufile.putaint(savesize);
  2560. if df_copied_def in defoptions then
  2561. ppufile.putderef(basedefderef);
  2562. writeentry(ppufile,ibenumdef);
  2563. if not (df_copied_def in defoptions) then
  2564. tenumsymtable(symtable).ppuwrite(ppufile);
  2565. end;
  2566. function tenumdef.is_publishable : boolean;
  2567. begin
  2568. is_publishable:=true;
  2569. end;
  2570. function tenumdef.GetTypeName : string;
  2571. begin
  2572. GetTypeName:='<enumeration type>';
  2573. end;
  2574. {****************************************************************************
  2575. TORDDEF
  2576. ****************************************************************************}
  2577. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  2578. begin
  2579. inherited create(orddef,doregister);
  2580. low:=v;
  2581. high:=b;
  2582. ordtype:=t;
  2583. setsize;
  2584. end;
  2585. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2586. begin
  2587. inherited ppuload(orddef,ppufile);
  2588. ordtype:=tordtype(ppufile.getbyte);
  2589. low:=ppufile.getexprint;
  2590. high:=ppufile.getexprint;
  2591. setsize;
  2592. ppuload_platform(ppufile);
  2593. end;
  2594. function torddef.getcopy : tstoreddef;
  2595. begin
  2596. result:=corddef.create(ordtype,low,high,true);
  2597. result.typ:=orddef;
  2598. torddef(result).low:=low;
  2599. torddef(result).high:=high;
  2600. torddef(result).ordtype:=ordtype;
  2601. torddef(result).savesize:=savesize;
  2602. end;
  2603. function torddef.alignment:shortint;
  2604. begin
  2605. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  2606. (ordtype in [s64bit,u64bit]) then
  2607. result := 4
  2608. else
  2609. result := inherited alignment;
  2610. end;
  2611. procedure torddef.setsize;
  2612. const
  2613. sizetbl : array[tordtype] of longint = (
  2614. 0,
  2615. 1,2,4,8,16,
  2616. 1,2,4,8,16,
  2617. 1,1,2,4,8,
  2618. 1,2,4,8,
  2619. 1,2,8
  2620. );
  2621. begin
  2622. savesize:=sizetbl[ordtype];
  2623. end;
  2624. function torddef.packedbitsize: asizeint;
  2625. var
  2626. sizeval: tconstexprint;
  2627. power: longint;
  2628. begin
  2629. result := 0;
  2630. if ordtype = uvoid then
  2631. exit;
  2632. {$ifndef cpu64bitalu}
  2633. if (ordtype in [s64bit,u64bit]) then
  2634. {$else not cpu64bitalu}
  2635. if (ordtype = u64bit) or
  2636. ((ordtype = s64bit) and
  2637. ((low <= (system.low(int64) div 2)) or
  2638. (high > (system.high(int64) div 2)))) then
  2639. {$endif cpu64bitalu}
  2640. result := 64
  2641. else if (
  2642. (low >= 0) and
  2643. (high <= 1)
  2644. ) or (
  2645. ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]
  2646. ) then
  2647. result := 1
  2648. else
  2649. begin
  2650. if (low>=0) then
  2651. sizeval:=high
  2652. else
  2653. { don't count 0 twice }
  2654. sizeval:=(cutils.max(-low,high)*2)-1;
  2655. { 256 must become 512 etc. }
  2656. nextpowerof2(sizeval+1,power);
  2657. result := power;
  2658. end;
  2659. end;
  2660. function torddef.getvardef : longint;
  2661. const
  2662. basetype2vardef : array[tordtype] of longint = (
  2663. varUndefined,
  2664. varbyte,varword,varlongword,varqword,varUndefined,
  2665. varshortint,varsmallint,varinteger,varint64,varUndefined,
  2666. varboolean,varboolean,varboolean,varboolean,varboolean,
  2667. varboolean,varboolean,varUndefined,varUndefined,
  2668. varUndefined,varUndefined,varCurrency);
  2669. begin
  2670. result:=basetype2vardef[ordtype];
  2671. end;
  2672. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2673. begin
  2674. inherited ppuwrite(ppufile);
  2675. ppufile.putbyte(byte(ordtype));
  2676. ppufile.putexprint(low);
  2677. ppufile.putexprint(high);
  2678. writeentry(ppufile,iborddef);
  2679. end;
  2680. function torddef.is_publishable : boolean;
  2681. begin
  2682. is_publishable:=(ordtype<>uvoid);
  2683. end;
  2684. function torddef.GetTypeName : string;
  2685. const
  2686. names : array[tordtype] of string[20] = (
  2687. 'untyped',
  2688. 'Byte','Word','DWord','QWord','UInt128',
  2689. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  2690. 'Boolean','Boolean8','Boolean16','Boolean32','Boolean64',
  2691. 'ByteBool','WordBool','LongBool','QWordBool',
  2692. 'Char','WideChar','Currency');
  2693. begin
  2694. GetTypeName:=names[ordtype];
  2695. end;
  2696. {****************************************************************************
  2697. TFLOATDEF
  2698. ****************************************************************************}
  2699. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  2700. begin
  2701. inherited create(floatdef,doregister);
  2702. floattype:=t;
  2703. setsize;
  2704. end;
  2705. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2706. begin
  2707. inherited ppuload(floatdef,ppufile);
  2708. floattype:=tfloattype(ppufile.getbyte);
  2709. setsize;
  2710. ppuload_platform(ppufile);
  2711. end;
  2712. function tfloatdef.getcopy : tstoreddef;
  2713. begin
  2714. result:=cfloatdef.create(floattype,true);
  2715. result.typ:=floatdef;
  2716. tfloatdef(result).savesize:=savesize;
  2717. end;
  2718. function tfloatdef.alignment:shortint;
  2719. begin
  2720. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2721. case floattype of
  2722. sc80real,
  2723. s80real: result:=16;
  2724. s64real,
  2725. s64currency,
  2726. s64comp : result:=4;
  2727. else
  2728. result := inherited alignment;
  2729. end
  2730. else
  2731. result := inherited alignment;
  2732. end;
  2733. function tfloatdef.structalignment: shortint;
  2734. begin
  2735. { aix is really annoying: the recommended scalar alignment for both
  2736. int64 and double is 64 bits, but in structs int64 has to be aligned
  2737. to 8 bytes and double to 4 bytes }
  2738. if (target_info.system in systems_aix) and
  2739. (floattype=s64real) then
  2740. result:=4
  2741. else
  2742. result:=alignment;
  2743. end;
  2744. procedure tfloatdef.setsize;
  2745. begin
  2746. case floattype of
  2747. s32real : savesize:=4;
  2748. s80real : savesize:=10;
  2749. sc80real:
  2750. if target_info.system in [system_i386_darwin,
  2751. system_i386_iphonesim,system_x86_64_darwin,
  2752. system_x86_64_iphonesim,
  2753. system_x86_64_linux,system_x86_64_freebsd,
  2754. system_x86_64_openbsd,system_x86_64_netbsd,
  2755. system_x86_64_solaris,system_x86_64_embedded,
  2756. system_x86_64_dragonfly] then
  2757. savesize:=16
  2758. else
  2759. savesize:=12;
  2760. s64real,
  2761. s64currency,
  2762. s64comp : savesize:=8;
  2763. else
  2764. savesize:=0;
  2765. end;
  2766. end;
  2767. function tfloatdef.getvardef : longint;
  2768. const
  2769. floattype2vardef : array[tfloattype] of longint = (
  2770. varSingle,varDouble,varUndefined,varUndefined,
  2771. varUndefined,varCurrency,varUndefined);
  2772. begin
  2773. if (upper(typename)='TDATETIME') and
  2774. assigned(owner) and
  2775. assigned(owner.name) and
  2776. (owner.name^='SYSTEM') then
  2777. result:=varDate
  2778. else
  2779. result:=floattype2vardef[floattype];
  2780. end;
  2781. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2782. begin
  2783. inherited ppuwrite(ppufile);
  2784. ppufile.putbyte(byte(floattype));
  2785. writeentry(ppufile,ibfloatdef);
  2786. end;
  2787. function tfloatdef.is_publishable : boolean;
  2788. begin
  2789. is_publishable:=true;
  2790. end;
  2791. function tfloatdef.GetTypeName : string;
  2792. const
  2793. names : array[tfloattype] of string[20] = (
  2794. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2795. begin
  2796. GetTypeName:=names[floattype];
  2797. end;
  2798. {****************************************************************************
  2799. TFILEDEF
  2800. ****************************************************************************}
  2801. constructor tfiledef.createtext;
  2802. begin
  2803. inherited create(filedef,true);
  2804. filetyp:=ft_text;
  2805. typedfiledef:=nil;
  2806. end;
  2807. constructor tfiledef.createuntyped;
  2808. begin
  2809. inherited create(filedef,true);
  2810. filetyp:=ft_untyped;
  2811. typedfiledef:=nil;
  2812. end;
  2813. constructor tfiledef.createtyped(def:tdef);
  2814. begin
  2815. inherited create(filedef,true);
  2816. filetyp:=ft_typed;
  2817. typedfiledef:=def;
  2818. end;
  2819. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2820. begin
  2821. inherited ppuload(filedef,ppufile);
  2822. filetyp:=tfiletyp(ppufile.getbyte);
  2823. if filetyp=ft_typed then
  2824. ppufile.getderef(typedfiledefderef)
  2825. else
  2826. typedfiledef:=nil;
  2827. ppuload_platform(ppufile);
  2828. end;
  2829. function tfiledef.getcopy : tstoreddef;
  2830. begin
  2831. case filetyp of
  2832. ft_typed:
  2833. result:=cfiledef.createtyped(typedfiledef);
  2834. ft_untyped:
  2835. result:=cfiledef.createuntyped;
  2836. ft_text:
  2837. result:=cfiledef.createtext;
  2838. else
  2839. internalerror(2004121201);
  2840. end;
  2841. end;
  2842. procedure tfiledef.buildderef;
  2843. begin
  2844. inherited buildderef;
  2845. if filetyp=ft_typed then
  2846. typedfiledefderef.build(typedfiledef);
  2847. end;
  2848. procedure tfiledef.deref;
  2849. begin
  2850. inherited deref;
  2851. if filetyp=ft_typed then
  2852. typedfiledef:=tdef(typedfiledefderef.resolve);
  2853. end;
  2854. function tfiledef.size:asizeint;
  2855. begin
  2856. if savesize=0 then
  2857. setsize;
  2858. size:=savesize;
  2859. end;
  2860. procedure tfiledef.setsize;
  2861. begin
  2862. case filetyp of
  2863. ft_text:
  2864. savesize:=search_system_type('TEXTREC').typedef.size;
  2865. ft_typed:
  2866. begin
  2867. savesize:=search_system_type('FILEREC').typedef.size;
  2868. { allocate put/get buffer in iso mode }
  2869. if m_isolike_io in current_settings.modeswitches then
  2870. inc(savesize,typedfiledef.size);
  2871. end;
  2872. ft_untyped:
  2873. savesize:=search_system_type('FILEREC').typedef.size;
  2874. else
  2875. internalerror(2013113001);
  2876. end;
  2877. end;
  2878. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2879. begin
  2880. inherited ppuwrite(ppufile);
  2881. ppufile.putbyte(byte(filetyp));
  2882. if filetyp=ft_typed then
  2883. ppufile.putderef(typedfiledefderef);
  2884. writeentry(ppufile,ibfiledef);
  2885. end;
  2886. function tfiledef.GetTypeName : string;
  2887. begin
  2888. case filetyp of
  2889. ft_untyped:
  2890. GetTypeName:='File';
  2891. ft_typed:
  2892. GetTypeName:='File Of '+typedfiledef.typename;
  2893. ft_text:
  2894. GetTypeName:='Text'
  2895. else
  2896. internalerror(2013113002);
  2897. end;
  2898. end;
  2899. function tfiledef.getmangledparaname : TSymStr;
  2900. begin
  2901. case filetyp of
  2902. ft_untyped:
  2903. getmangledparaname:='FILE';
  2904. ft_typed:
  2905. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2906. ft_text:
  2907. getmangledparaname:='TEXT'
  2908. else
  2909. internalerror(2013113003);
  2910. end;
  2911. end;
  2912. {****************************************************************************
  2913. TVARIANTDEF
  2914. ****************************************************************************}
  2915. constructor tvariantdef.create(v : tvarianttype);
  2916. begin
  2917. inherited create(variantdef,true);
  2918. varianttype:=v;
  2919. setsize;
  2920. end;
  2921. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2922. begin
  2923. inherited ppuload(variantdef,ppufile);
  2924. varianttype:=tvarianttype(ppufile.getbyte);
  2925. setsize;
  2926. ppuload_platform(ppufile);
  2927. end;
  2928. function tvariantdef.getcopy : tstoreddef;
  2929. begin
  2930. result:=cvariantdef.create(varianttype);
  2931. end;
  2932. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2933. begin
  2934. inherited ppuwrite(ppufile);
  2935. ppufile.putbyte(byte(varianttype));
  2936. writeentry(ppufile,ibvariantdef);
  2937. end;
  2938. function tvariantdef.getvardef : longint;
  2939. begin
  2940. Result:=varVariant;
  2941. end;
  2942. procedure tvariantdef.setsize;
  2943. begin
  2944. {$ifdef cpu64bitaddr}
  2945. savesize:=24;
  2946. {$else cpu64bitaddr}
  2947. savesize:=16;
  2948. {$endif cpu64bitaddr}
  2949. end;
  2950. function tvariantdef.GetTypeName : string;
  2951. begin
  2952. case varianttype of
  2953. vt_normalvariant:
  2954. GetTypeName:='Variant';
  2955. vt_olevariant:
  2956. GetTypeName:='OleVariant';
  2957. else
  2958. internalerror(2013113004);
  2959. end;
  2960. end;
  2961. function tvariantdef.needs_inittable : boolean;
  2962. begin
  2963. needs_inittable:=true;
  2964. end;
  2965. function tvariantdef.is_publishable : boolean;
  2966. begin
  2967. is_publishable:=true;
  2968. end;
  2969. {****************************************************************************
  2970. TABSTRACtpointerdef
  2971. ****************************************************************************}
  2972. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2973. begin
  2974. inherited create(dt,true);
  2975. pointeddef:=def;
  2976. if df_generic in pointeddef.defoptions then
  2977. include(defoptions,df_generic);
  2978. if df_specialization in pointeddef.defoptions then
  2979. include(defoptions,df_specialization);
  2980. end;
  2981. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2982. begin
  2983. inherited ppuload(dt,ppufile);
  2984. ppufile.getderef(pointeddefderef);
  2985. end;
  2986. procedure tabstractpointerdef.buildderef;
  2987. begin
  2988. inherited buildderef;
  2989. pointeddefderef.build(pointeddef);
  2990. end;
  2991. procedure tabstractpointerdef.deref;
  2992. begin
  2993. inherited deref;
  2994. pointeddef:=tdef(pointeddefderef.resolve);
  2995. end;
  2996. function tabstractpointerdef.size: asizeint;
  2997. begin
  2998. Result:=voidpointertype.size;
  2999. end;
  3000. function tabstractpointerdef.alignment: shortint;
  3001. begin
  3002. alignment:=size_2_align(voidpointertype.size);
  3003. end;
  3004. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3005. begin
  3006. inherited ppuwrite(ppufile);
  3007. ppufile.putderef(pointeddefderef);
  3008. end;
  3009. {****************************************************************************
  3010. tpointerdef
  3011. ****************************************************************************}
  3012. constructor tpointerdef.create(def:tdef);
  3013. begin
  3014. inherited create(pointerdef,def);
  3015. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  3016. if df_specialization in tstoreddef(def).defoptions then
  3017. genericdef:=cpointerdef.getreusable(tstoreddef(def).genericdef);
  3018. end;
  3019. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  3020. var
  3021. res: PHashSetItem;
  3022. oldsymtablestack: tsymtablestack;
  3023. begin
  3024. if not assigned(current_module) then
  3025. internalerror(2011071101);
  3026. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  3027. if not assigned(res^.Data) then
  3028. begin
  3029. { since these pointerdefs can be reused anywhere in the current
  3030. unit, add them to the global/staticsymtable (or local symtable
  3031. if they're a local def, because otherwise they'll be saved
  3032. to the ppu referencing a local symtable entry that doesn't
  3033. exist in the ppu) }
  3034. oldsymtablestack:=symtablestack;
  3035. { do not simply push/pop current_module.localsymtable, because
  3036. that can have side-effects (e.g., it removes helpers) }
  3037. symtablestack:=nil;
  3038. result:=cpointerdef.create(def);
  3039. setup_reusable_def(def,result,res,oldsymtablestack);
  3040. { res^.Data may still be nil -> don't overwrite result }
  3041. exit;
  3042. end;
  3043. result:=tpointerdef(res^.Data);
  3044. end;
  3045. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  3046. begin
  3047. result:=getreusable(def);
  3048. if not result.is_registered then
  3049. include(result.defoptions,df_not_registered_no_free);
  3050. end;
  3051. function tpointerdef.size: asizeint;
  3052. begin
  3053. result:=sizeof(pint);
  3054. end;
  3055. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  3056. begin
  3057. inherited ppuload(pointerdef,ppufile);
  3058. has_pointer_math:=(ppufile.getbyte<>0);
  3059. ppuload_platform(ppufile);
  3060. end;
  3061. function tpointerdef.getcopy : tstoreddef;
  3062. begin
  3063. { don't use direct pointeddef if it is a forwarddef because in other case
  3064. one of them will be destroyed on forward type resolve and the second will
  3065. point to garbage }
  3066. if pointeddef.typ=forwarddef then
  3067. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  3068. else
  3069. result:=cpointerdef.create(pointeddef);
  3070. tpointerdef(result).has_pointer_math:=has_pointer_math;
  3071. tpointerdef(result).savesize:=savesize;
  3072. end;
  3073. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3074. begin
  3075. inherited ppuwrite(ppufile);
  3076. ppufile.putbyte(byte(has_pointer_math));
  3077. writeentry(ppufile,ibpointerdef);
  3078. end;
  3079. function tpointerdef.GetTypeName : string;
  3080. begin
  3081. { parameter types and the resultdef of a procvardef can contain a
  3082. pointer to this procvardef itself, resulting in endless recursion ->
  3083. use the typesym's name instead if it exists (if it doesn't, such as
  3084. for anynonymous procedure types in macpas/iso mode, then there cannot
  3085. be any recursive references to it either) }
  3086. if (pointeddef.typ<>procvardef) or
  3087. not assigned(pointeddef.typesym) then
  3088. GetTypeName:='^'+pointeddef.typename
  3089. else
  3090. GetTypeName:='^'+pointeddef.typesym.realname;
  3091. end;
  3092. function tpointerdef.pointer_arithmetic_int_type:tdef;
  3093. begin
  3094. result:=ptrsinttype;
  3095. end;
  3096. function tpointerdef.pointer_arithmetic_uint_type:tdef;
  3097. begin
  3098. result:=ptruinttype;
  3099. end;
  3100. function tpointerdef.pointer_subtraction_result_type:tdef;
  3101. begin
  3102. result:=ptrsinttype;
  3103. end;
  3104. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  3105. begin
  3106. result:=true;
  3107. end;
  3108. function tpointerdef.converted_pointer_to_array_range_type:tdef;
  3109. begin
  3110. result:=ptrsinttype;
  3111. end;
  3112. {****************************************************************************
  3113. TCLASSREFDEF
  3114. ****************************************************************************}
  3115. constructor tclassrefdef.create(def:tdef);
  3116. begin
  3117. inherited create(classrefdef,def);
  3118. if df_specialization in tstoreddef(def).defoptions then
  3119. genericdef:=cclassrefdef.create(tstoreddef(def).genericdef);
  3120. end;
  3121. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  3122. begin
  3123. inherited ppuload(classrefdef,ppufile);
  3124. ppuload_platform(ppufile);
  3125. end;
  3126. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  3127. begin
  3128. inherited ppuwrite(ppufile);
  3129. writeentry(ppufile,ibclassrefdef);
  3130. end;
  3131. function tclassrefdef.getcopy:tstoreddef;
  3132. begin
  3133. if pointeddef.typ=forwarddef then
  3134. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3135. else
  3136. result:=cclassrefdef.create(pointeddef);
  3137. end;
  3138. function tclassrefdef.GetTypeName : string;
  3139. begin
  3140. GetTypeName:='Class Of '+pointeddef.typename;
  3141. end;
  3142. function tclassrefdef.is_publishable : boolean;
  3143. begin
  3144. result:=true;
  3145. end;
  3146. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3147. begin
  3148. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3149. result:=inherited rtti_mangledname(rt)
  3150. else
  3151. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3152. end;
  3153. procedure tclassrefdef.register_created_object_type;
  3154. begin
  3155. tobjectdef(pointeddef).register_created_classref_type;
  3156. end;
  3157. {***************************************************************************
  3158. TSETDEF
  3159. ***************************************************************************}
  3160. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3161. var
  3162. setallocbits: aint;
  3163. packedsavesize: aint;
  3164. actual_setalloc: ShortInt;
  3165. begin
  3166. inherited create(setdef,doregister);
  3167. elementdef:=def;
  3168. setmax:=high;
  3169. actual_setalloc:=current_settings.setalloc;
  3170. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3171. if actual_setalloc=0 then
  3172. actual_setalloc:=1;
  3173. {$endif}
  3174. if (actual_setalloc=0) then
  3175. begin
  3176. setbase:=0;
  3177. if (high<32) then
  3178. savesize:=Sizeof(longint)
  3179. else if (high<256) then
  3180. savesize:=32
  3181. else
  3182. savesize:=(high+7) div 8
  3183. end
  3184. else
  3185. begin
  3186. setallocbits:=actual_setalloc*8;
  3187. setbase:=low and not(setallocbits-1);
  3188. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3189. savesize:=packedsavesize;
  3190. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3191. if savesize=3 then
  3192. savesize:=4;
  3193. {$endif}
  3194. end;
  3195. end;
  3196. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3197. begin
  3198. inherited ppuload(setdef,ppufile);
  3199. ppufile.getderef(elementdefderef);
  3200. savesize:=ppufile.getasizeint;
  3201. setbase:=ppufile.getasizeint;
  3202. setmax:=ppufile.getasizeint;
  3203. ppuload_platform(ppufile);
  3204. end;
  3205. function tsetdef.getcopy : tstoreddef;
  3206. begin
  3207. result:=csetdef.create(elementdef,setbase,setmax,true);
  3208. { the copy might have been created with a different setalloc setting }
  3209. tsetdef(result).savesize:=savesize;
  3210. end;
  3211. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3212. begin
  3213. inherited ppuwrite(ppufile);
  3214. ppufile.putderef(elementdefderef);
  3215. ppufile.putasizeint(savesize);
  3216. ppufile.putasizeint(setbase);
  3217. ppufile.putasizeint(setmax);
  3218. writeentry(ppufile,ibsetdef);
  3219. end;
  3220. procedure tsetdef.buildderef;
  3221. begin
  3222. inherited buildderef;
  3223. elementdefderef.build(elementdef);
  3224. end;
  3225. procedure tsetdef.deref;
  3226. begin
  3227. inherited deref;
  3228. elementdef:=tdef(elementdefderef.resolve);
  3229. end;
  3230. function tsetdef.is_publishable : boolean;
  3231. begin
  3232. is_publishable:=savesize in [1,2,4];
  3233. end;
  3234. function tsetdef.GetTypeName : string;
  3235. begin
  3236. if assigned(elementdef) then
  3237. GetTypeName:='Set Of '+elementdef.typename
  3238. else
  3239. GetTypeName:='Empty Set';
  3240. end;
  3241. {***************************************************************************
  3242. TFORMALDEF
  3243. ***************************************************************************}
  3244. constructor tformaldef.create(Atyped:boolean);
  3245. begin
  3246. inherited create(formaldef,true);
  3247. typed:=Atyped;
  3248. savesize:=0;
  3249. end;
  3250. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3251. begin
  3252. inherited ppuload(formaldef,ppufile);
  3253. typed:=boolean(ppufile.getbyte);
  3254. savesize:=0;
  3255. ppuload_platform(ppufile);
  3256. end;
  3257. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3258. begin
  3259. inherited ppuwrite(ppufile);
  3260. ppufile.putbyte(byte(typed));
  3261. writeentry(ppufile,ibformaldef);
  3262. end;
  3263. function tformaldef.GetTypeName : string;
  3264. begin
  3265. if typed then
  3266. GetTypeName:='<Typed formal type>'
  3267. else
  3268. GetTypeName:='<Formal type>';
  3269. end;
  3270. {***************************************************************************
  3271. TARRAYDEF
  3272. ***************************************************************************}
  3273. constructor tarraydef.create(l,h:asizeint;def:tdef);
  3274. begin
  3275. inherited create(arraydef,true);
  3276. lowrange:=l;
  3277. highrange:=h;
  3278. rangedef:=def;
  3279. _elementdef:=nil;
  3280. arrayoptions:=[];
  3281. symtable:=tarraysymtable.create(self);
  3282. end;
  3283. constructor tarraydef.create_openarray;
  3284. begin
  3285. self.create(0,-1,sizesinttype)
  3286. end;
  3287. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3288. var
  3289. res: PHashSetItem;
  3290. oldsymtablestack: tsymtablestack;
  3291. arrdesc: packed record
  3292. def: tdef;
  3293. elecount: asizeint;
  3294. end;
  3295. begin
  3296. if not assigned(current_module) then
  3297. internalerror(2011081301);
  3298. arrdesc.def:=def;
  3299. arrdesc.elecount:=elems;
  3300. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3301. if not assigned(res^.Data) then
  3302. begin
  3303. { since these pointerdefs can be reused anywhere in the current
  3304. unit, add them to the global/staticsymtable (or local symtable
  3305. if they're a local def, because otherwise they'll be saved
  3306. to the ppu referencing a local symtable entry that doesn't
  3307. exist in the ppu) }
  3308. oldsymtablestack:=symtablestack;
  3309. { do not simply push/pop current_module.localsymtable, because
  3310. that can have side-effects (e.g., it removes helpers) }
  3311. symtablestack:=nil;
  3312. result:=carraydef.create(0,elems-1,sizesinttype);
  3313. result.elementdef:=def;
  3314. setup_reusable_def(def,result,res,oldsymtablestack);
  3315. { res^.Data may still be nil -> don't overwrite result }
  3316. exit;
  3317. end;
  3318. result:=tarraydef(res^.Data);
  3319. end;
  3320. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3321. begin
  3322. result:=getreusable(def,elems);
  3323. if not result.is_registered then
  3324. include(result.defoptions,df_not_registered_no_free);
  3325. end;
  3326. destructor tarraydef.destroy;
  3327. begin
  3328. symtable.free;
  3329. symtable:=nil;
  3330. inherited;
  3331. end;
  3332. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3333. begin
  3334. { divide by the element size and do -1 so the array will have a valid size,
  3335. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3336. to avoid a division by zero }
  3337. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,
  3338. def.converted_pointer_to_array_range_type);
  3339. arrayoptions:=[ado_IsConvertedPointer];
  3340. setelementdef(def.pointeddef);
  3341. end;
  3342. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3343. begin
  3344. inherited ppuload(arraydef,ppufile);
  3345. { the addresses are calculated later }
  3346. ppufile.getderef(_elementdefderef);
  3347. ppufile.getderef(rangedefderef);
  3348. lowrange:=ppufile.getasizeint;
  3349. highrange:=ppufile.getasizeint;
  3350. ppufile.getsmallset(arrayoptions);
  3351. ppuload_platform(ppufile);
  3352. symtable:=tarraysymtable.create(self);
  3353. tarraysymtable(symtable).ppuload(ppufile)
  3354. end;
  3355. function tarraydef.getcopy : tstoreddef;
  3356. begin
  3357. result:=carraydef.create(lowrange,highrange,rangedef);
  3358. tarraydef(result).arrayoptions:=arrayoptions;
  3359. tarraydef(result)._elementdef:=_elementdef;
  3360. end;
  3361. procedure tarraydef.buildderef;
  3362. begin
  3363. inherited buildderef;
  3364. tarraysymtable(symtable).buildderef;
  3365. _elementdefderef.build(_elementdef);
  3366. rangedefderef.build(rangedef);
  3367. end;
  3368. procedure tarraydef.deref;
  3369. begin
  3370. inherited deref;
  3371. tarraysymtable(symtable).deref(false);
  3372. _elementdef:=tdef(_elementdefderef.resolve);
  3373. rangedef:=tdef(rangedefderef.resolve);
  3374. end;
  3375. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3376. begin
  3377. inherited ppuwrite(ppufile);
  3378. ppufile.putderef(_elementdefderef);
  3379. ppufile.putderef(rangedefderef);
  3380. ppufile.putasizeint(lowrange);
  3381. ppufile.putasizeint(highrange);
  3382. ppufile.putsmallset(arrayoptions);
  3383. writeentry(ppufile,ibarraydef);
  3384. tarraysymtable(symtable).ppuwrite(ppufile);
  3385. end;
  3386. function tarraydef.elesize : asizeint;
  3387. begin
  3388. if (ado_IsBitPacked in arrayoptions) then
  3389. internalerror(2006080101);
  3390. if assigned(_elementdef) then
  3391. result:=_elementdef.size
  3392. else
  3393. result:=0;
  3394. end;
  3395. function tarraydef.elepackedbitsize : asizeint;
  3396. begin
  3397. if not(ado_IsBitPacked in arrayoptions) then
  3398. internalerror(2006080102);
  3399. if assigned(_elementdef) then
  3400. result:=_elementdef.packedbitsize
  3401. else
  3402. result:=0;
  3403. end;
  3404. function tarraydef.elecount : asizeuint;
  3405. var
  3406. qhigh,qlow : qword;
  3407. begin
  3408. if ado_IsDynamicArray in arrayoptions then
  3409. begin
  3410. result:=0;
  3411. exit;
  3412. end;
  3413. if (highrange>0) and (lowrange<0) then
  3414. begin
  3415. qhigh:=highrange;
  3416. qlow:=qword(-lowrange);
  3417. { prevent overflow, return 0 to indicate overflow }
  3418. if qhigh+qlow>qword(high(asizeint)-1) then
  3419. result:=0
  3420. else
  3421. result:=qhigh+qlow+1;
  3422. end
  3423. else
  3424. result:=int64(highrange)-lowrange+1;
  3425. end;
  3426. function tarraydef.size : asizeint;
  3427. var
  3428. cachedelecount : asizeuint;
  3429. cachedelesize : asizeint;
  3430. begin
  3431. if ado_IsDynamicArray in arrayoptions then
  3432. begin
  3433. size:=voidpointertype.size;
  3434. exit;
  3435. end;
  3436. { Tarraydef.size may never be called for an open array! }
  3437. if highrange<lowrange then
  3438. internalerror(99080501);
  3439. if not (ado_IsBitPacked in arrayoptions) then
  3440. cachedelesize:=elesize
  3441. else
  3442. cachedelesize := elepackedbitsize;
  3443. cachedelecount:=elecount;
  3444. if (cachedelesize = 0) then
  3445. begin
  3446. size := 0;
  3447. exit;
  3448. end;
  3449. if (cachedelecount = 0) then
  3450. begin
  3451. size := -1;
  3452. exit;
  3453. end;
  3454. { prevent overflow, return -1 to indicate overflow }
  3455. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3456. if (cachedelecount > asizeuint(high(asizeint))) or
  3457. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3458. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3459. accessing the array, see ncgmem (PFV) }
  3460. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3461. begin
  3462. result:=-1;
  3463. exit;
  3464. end;
  3465. result:=cachedelesize*asizeint(cachedelecount);
  3466. if (ado_IsBitPacked in arrayoptions) then
  3467. { can't just add 7 and divide by 8, because that may overflow }
  3468. result:=result div 8 + ord((result mod 8)<>0);
  3469. {$ifdef cpu16bitaddr}
  3470. if result>65535 then
  3471. begin
  3472. result:=-1;
  3473. exit;
  3474. end;
  3475. {$endif cpu16bitaddr}
  3476. end;
  3477. procedure tarraydef.setelementdef(def:tdef);
  3478. begin
  3479. _elementdef:=def;
  3480. if not(
  3481. (ado_IsDynamicArray in arrayoptions) or
  3482. (ado_IsConvertedPointer in arrayoptions) or
  3483. (ado_IsConstructor in arrayoptions) or
  3484. (highrange<lowrange)
  3485. ) and
  3486. (size=-1) then
  3487. Message(sym_e_segment_too_large);
  3488. end;
  3489. function tarraydef.alignment : shortint;
  3490. begin
  3491. { alignment of dyn. arrays doesn't depend on the element size }
  3492. if (ado_IsDynamicArray in arrayoptions) then
  3493. alignment:=voidpointertype.alignment
  3494. { alignment is the target alignment for the used load size }
  3495. else if (ado_IsBitPacked in arrayoptions) and
  3496. (elementdef.typ in [enumdef,orddef]) then
  3497. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3498. { alignment is the alignment of the elements }
  3499. else
  3500. alignment:=elementdef.alignment
  3501. end;
  3502. function tarraydef.needs_inittable : boolean;
  3503. begin
  3504. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3505. end;
  3506. function tarraydef.needs_separate_initrtti : boolean;
  3507. begin
  3508. if ado_IsBitPacked in arrayoptions then
  3509. result:=false
  3510. else
  3511. result:=elementdef.needs_separate_initrtti;
  3512. end;
  3513. function tarraydef.GetTypeName : string;
  3514. begin
  3515. if (ado_IsConstString in arrayoptions) then
  3516. result:='Constant String'
  3517. else if (ado_isarrayofconst in arrayoptions) or
  3518. (ado_isConstructor in arrayoptions) then
  3519. begin
  3520. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3521. GetTypeName:='Array Of Const'
  3522. else
  3523. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3524. end
  3525. else if (ado_IsDynamicArray in arrayoptions) then
  3526. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3527. else if ((highrange=-1) and (lowrange=0)) then
  3528. GetTypeName:='{Open} Array Of '+elementdef.typename
  3529. else
  3530. begin
  3531. result := '';
  3532. if (ado_IsBitPacked in arrayoptions) then
  3533. result:='BitPacked ';
  3534. if rangedef.typ=enumdef then
  3535. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3536. else
  3537. result:=result+'Array['+tostr(lowrange)+'..'+
  3538. tostr(highrange)+'] Of '+elementdef.typename
  3539. end;
  3540. end;
  3541. function tarraydef.getmangledparaname : TSymStr;
  3542. begin
  3543. if ado_isarrayofconst in arrayoptions then
  3544. getmangledparaname:='array_of_const'
  3545. else
  3546. if ((highrange=-1) and (lowrange=0)) then
  3547. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3548. else
  3549. internalerror(200204176);
  3550. end;
  3551. function tarraydef.is_publishable : boolean;
  3552. begin
  3553. Result:=ado_IsDynamicArray in arrayoptions;
  3554. end;
  3555. {***************************************************************************
  3556. tabstractrecorddef
  3557. ***************************************************************************}
  3558. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  3559. begin
  3560. inherited create(dt,doregister);
  3561. objname:=stringdup(upper(n));
  3562. objrealname:=stringdup(n);
  3563. objectoptions:=[];
  3564. if assigned(current_module.namespace) then
  3565. begin
  3566. import_lib:=stringdup(current_module.namespace^);
  3567. replace(import_lib^,'.','/');
  3568. end;
  3569. end;
  3570. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3571. begin
  3572. inherited ppuload(dt,ppufile);
  3573. objrealname:=ppufile.getpshortstring;
  3574. objname:=stringdup(upper(objrealname^));
  3575. import_lib:=ppufile.getpshortstring;
  3576. { only used for external C++ classes and Java classes/records }
  3577. if (import_lib^='') then
  3578. stringdispose(import_lib);
  3579. ppufile.getsmallset(objectoptions);
  3580. end;
  3581. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3582. begin
  3583. inherited ppuwrite(ppufile);
  3584. ppufile.putstring(objrealname^);
  3585. if assigned(import_lib) then
  3586. ppufile.putstring(import_lib^)
  3587. else
  3588. ppufile.putstring('');
  3589. ppufile.putsmallset(objectoptions);
  3590. end;
  3591. destructor tabstractrecorddef.destroy;
  3592. begin
  3593. stringdispose(objname);
  3594. stringdispose(objrealname);
  3595. stringdispose(import_lib);
  3596. tcinitcode.free;
  3597. inherited destroy;
  3598. end;
  3599. procedure tabstractrecorddef.buildderefimpl;
  3600. begin
  3601. inherited buildderefimpl;
  3602. if not (df_copied_def in defoptions) then
  3603. tstoredsymtable(symtable).buildderefimpl;
  3604. end;
  3605. procedure tabstractrecorddef.derefimpl;
  3606. begin
  3607. inherited derefimpl;
  3608. if not (df_copied_def in defoptions) then
  3609. tstoredsymtable(symtable).derefimpl(false);
  3610. end;
  3611. procedure tabstractrecorddef.check_forwards;
  3612. begin
  3613. { the defs of a copied def are defined for the original type only }
  3614. if not(df_copied_def in defoptions) then
  3615. tstoredsymtable(symtable).check_forwards;
  3616. end;
  3617. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3618. var
  3619. i: longint;
  3620. sym: tsym;
  3621. begin
  3622. for i:=0 to symtable.SymList.Count-1 do
  3623. begin
  3624. sym:=tsym(symtable.SymList[i]);
  3625. if sym.typ=procsym then
  3626. begin
  3627. result:=tprocsym(sym).find_procdef_bytype(pt);
  3628. if assigned(result) then
  3629. exit;
  3630. end;
  3631. end;
  3632. result:=nil;
  3633. end;
  3634. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3635. begin
  3636. if t=gs_record then
  3637. GetSymtable:=symtable
  3638. else
  3639. GetSymtable:=nil;
  3640. end;
  3641. function tabstractrecorddef.is_packed:boolean;
  3642. begin
  3643. result:=tabstractrecordsymtable(symtable).is_packed;
  3644. end;
  3645. function tabstractrecorddef.RttiName: string;
  3646. function generate_full_paramname(maxlength:longint):string;
  3647. const
  3648. commacount : array[boolean] of longint = (0,1);
  3649. var
  3650. fullparas,
  3651. paramname : ansistring;
  3652. module : tmodule;
  3653. sym : ttypesym;
  3654. i : longint;
  3655. begin
  3656. { we want at least enough space for an ellipsis }
  3657. if maxlength<3 then
  3658. internalerror(2014121203);
  3659. fullparas:='';
  3660. for i:=0 to genericparas.count-1 do
  3661. begin
  3662. sym:=ttypesym(genericparas[i]);
  3663. module:=find_module_from_symtable(sym.owner);
  3664. if not assigned(module) then
  3665. internalerror(2014121202);
  3666. paramname:=module.realmodulename^;
  3667. if sym.typedef.typ in [objectdef,recorddef] then
  3668. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3669. else
  3670. paramname:=paramname+'.'+sym.typedef.typename;
  3671. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3672. begin
  3673. if i>0 then
  3674. fullparas:=fullparas+',...'
  3675. else
  3676. fullparas:=fullparas+'...';
  3677. break;
  3678. end;
  3679. { could we fit an ellipsis after this parameter if it should be too long? }
  3680. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3681. begin
  3682. { then omit already this parameter }
  3683. if i>0 then
  3684. fullparas:=fullparas+',...'
  3685. else
  3686. fullparas:=fullparas+'...';
  3687. break;
  3688. end;
  3689. if i>0 then
  3690. fullparas:=fullparas+',';
  3691. fullparas:=fullparas+paramname;
  3692. end;
  3693. result:=fullparas;
  3694. end;
  3695. var
  3696. nongeneric,
  3697. basename : string;
  3698. i,
  3699. remlength,
  3700. paramcount,
  3701. crcidx : longint;
  3702. begin
  3703. if rttistring='' then
  3704. begin
  3705. if is_specialization then
  3706. begin
  3707. rttistring:=OwnerHierarchyName;
  3708. { there should be two $ characters, one before the CRC and one before the count }
  3709. crcidx:=-1;
  3710. for i:=length(objrealname^) downto 1 do
  3711. if objrealname^[i]='$' then
  3712. begin
  3713. crcidx:=i;
  3714. break;
  3715. end;
  3716. if crcidx<0 then
  3717. internalerror(2014121201);
  3718. basename:=copy(objrealname^,1,crcidx-1);
  3719. split_generic_name(basename,nongeneric,paramcount);
  3720. rttistring:=rttistring+nongeneric+'<';
  3721. remlength:=255-length(rttistring)-1;
  3722. if remlength<4 then
  3723. rttistring:=rttistring+'>'
  3724. else
  3725. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  3726. end
  3727. else
  3728. if is_generic then
  3729. begin
  3730. rttistring:=OwnerHierarchyName;
  3731. split_generic_name(objrealname^,nongeneric,paramcount);
  3732. rttistring:=rttistring+nongeneric+'<';
  3733. { we don't want any ',' if there is only one parameter }
  3734. for i:=0 to paramcount-0 do
  3735. rttistring:=rttistring+',';
  3736. rttistring:=rttistring+'>';
  3737. end
  3738. else
  3739. rttistring:=OwnerHierarchyName+objrealname^;
  3740. end;
  3741. result:=rttistring;
  3742. end;
  3743. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3744. var
  3745. sym : tsym;
  3746. i : integer;
  3747. pd : tprocdef;
  3748. hashedid : THashedIDString;
  3749. begin
  3750. result:=nil;
  3751. hashedid.id:='GETENUMERATOR';
  3752. sym:=tsym(symtable.FindWithHash(hashedid));
  3753. if assigned(sym) and (sym.typ=procsym) then
  3754. begin
  3755. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3756. begin
  3757. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3758. if (pd.proctypeoption = potype_function) and
  3759. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3760. (pd.visibility >= vis_public) then
  3761. begin
  3762. result:=pd;
  3763. exit;
  3764. end;
  3765. end;
  3766. end;
  3767. end;
  3768. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3769. var
  3770. sym : tsym;
  3771. i : integer;
  3772. pd : tprocdef;
  3773. hashedid : THashedIDString;
  3774. begin
  3775. result:=nil;
  3776. // first search for po_enumerator_movenext method modifier
  3777. // then search for public function MoveNext: Boolean
  3778. for i:=0 to symtable.SymList.Count-1 do
  3779. begin
  3780. sym:=TSym(symtable.SymList[i]);
  3781. if (sym.typ=procsym) then
  3782. begin
  3783. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3784. if assigned(pd) then
  3785. begin
  3786. result:=pd;
  3787. exit;
  3788. end;
  3789. end;
  3790. end;
  3791. hashedid.id:='MOVENEXT';
  3792. sym:=tsym(symtable.FindWithHash(hashedid));
  3793. if assigned(sym) and (sym.typ=procsym) then
  3794. begin
  3795. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3796. begin
  3797. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3798. if (pd.proctypeoption = potype_function) and
  3799. is_boolean(pd.returndef) and
  3800. (pd.minparacount = 0) and
  3801. (pd.visibility >= vis_public) then
  3802. begin
  3803. result:=pd;
  3804. exit;
  3805. end;
  3806. end;
  3807. end;
  3808. end;
  3809. function tabstractrecorddef.search_enumerator_current: tsym;
  3810. var
  3811. sym: tsym;
  3812. i: integer;
  3813. hashedid : THashedIDString;
  3814. begin
  3815. result:=nil;
  3816. // first search for ppo_enumerator_current property modifier
  3817. // then search for public property Current
  3818. for i:=0 to symtable.SymList.Count-1 do
  3819. begin
  3820. sym:=TSym(symtable.SymList[i]);
  3821. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3822. begin
  3823. result:=sym;
  3824. exit;
  3825. end;
  3826. end;
  3827. hashedid.id:='CURRENT';
  3828. sym:=tsym(symtable.FindWithHash(hashedid));
  3829. if assigned(sym) and (sym.typ=propertysym) and
  3830. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3831. begin
  3832. result:=sym;
  3833. exit;
  3834. end;
  3835. end;
  3836. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3837. var
  3838. st: tsymtable;
  3839. enclosingdef: tdef;
  3840. begin
  3841. if typ=objectdef then
  3842. result:=tobjectdef(self).objextname^
  3843. else if assigned(typesym) then
  3844. result:=typesym.realname
  3845. { have to generate anonymous nested type in current unit/class/record }
  3846. else
  3847. internalerror(2011032601);
  3848. { in case of specializations, add some extras to prevent name conflicts
  3849. with nested classes }
  3850. if df_specialization in defoptions then
  3851. result:='$'+result+'$specialization$';
  3852. st:=owner;
  3853. while assigned(st) and
  3854. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3855. begin
  3856. { nested classes are named as "OuterClass$InnerClass" }
  3857. enclosingdef:=tdef(st.defowner);
  3858. if enclosingdef.typ=procdef then
  3859. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3860. else if enclosingdef.typ=objectdef then
  3861. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3862. else if assigned(enclosingdef.typesym) then
  3863. result:=enclosingdef.typesym.realname+'$'+result
  3864. else
  3865. internalerror(2011060305);
  3866. st:=enclosingdef.owner;
  3867. end;
  3868. if with_package_name and
  3869. assigned(import_lib) then
  3870. result:=import_lib^+'/'+result;
  3871. end;
  3872. function tabstractrecorddef.contains_float_field: boolean;
  3873. var
  3874. i : longint;
  3875. begin
  3876. result:=true;
  3877. for i:=0 to symtable.symlist.count-1 do
  3878. begin
  3879. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  3880. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  3881. continue;
  3882. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  3883. begin
  3884. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3885. exit;
  3886. { search recursively }
  3887. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3888. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  3889. exit;
  3890. end;
  3891. end;
  3892. result:=false;
  3893. end;
  3894. function tabstractrecorddef.contains_cross_aword_field: boolean;
  3895. var
  3896. i : longint;
  3897. foffset, fsize: aword;
  3898. begin
  3899. result:=true;
  3900. for i:=0 to symtable.symlist.count-1 do
  3901. begin
  3902. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  3903. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  3904. continue;
  3905. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  3906. begin
  3907. if is_packed then
  3908. begin
  3909. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  3910. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  3911. end
  3912. else
  3913. begin
  3914. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  3915. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  3916. end;
  3917. if (foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8)) then
  3918. exit;
  3919. { search recursively }
  3920. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3921. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  3922. exit;
  3923. end;
  3924. end;
  3925. result:=false;
  3926. end;
  3927. {***************************************************************************
  3928. trecorddef
  3929. ***************************************************************************}
  3930. constructor trecorddef.create(const n:string; p:TSymtable);
  3931. begin
  3932. inherited create(n,recorddef,true);
  3933. symtable:=p;
  3934. { we can own the symtable only if nobody else owns a copy so far }
  3935. if symtable.refcount=1 then
  3936. symtable.defowner:=self;
  3937. isunion:=false;
  3938. end;
  3939. constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin, maxCrecordalign: shortint);
  3940. var
  3941. oldsymtablestack: tsymtablestack;
  3942. ts: ttypesym;
  3943. definedname: boolean;
  3944. begin
  3945. { construct name }
  3946. definedname:=n<>'';
  3947. if not definedname then
  3948. n:='$InternalRec'+tostr(current_module.deflist.count);
  3949. oldsymtablestack:=symtablestack;
  3950. { do not simply push/pop current_module.localsymtable, because
  3951. that can have side-effects (e.g., it removes helpers) }
  3952. symtablestack:=nil;
  3953. symtable:=trecordsymtable.create(n,packrecords,recordalignmin,maxCrecordalign);
  3954. symtable.defowner:=self;
  3955. isunion:=false;
  3956. inherited create(n,recorddef,true);
  3957. { if we specified a name, then we'll probably want to look up the
  3958. type again by name too -> create typesym }
  3959. ts:=nil;
  3960. if definedname then
  3961. begin
  3962. ts:=ctypesym.create(n,self,true);
  3963. { avoid hints about unused types (these may only be used for
  3964. typed constant data) }
  3965. ts.increfcount;
  3966. end;
  3967. if assigned(current_module.localsymtable) then
  3968. begin
  3969. current_module.localsymtable.insertdef(self);
  3970. if definedname then
  3971. current_module.localsymtable.insert(ts);
  3972. end
  3973. else
  3974. begin
  3975. current_module.globalsymtable.insertdef(self);
  3976. if definedname then
  3977. current_module.globalsymtable.insert(ts);
  3978. end;
  3979. symtablestack:=oldsymtablestack;
  3980. { don't create RTTI for internal types, these are not exported }
  3981. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  3982. include(defoptions,df_internal);
  3983. end;
  3984. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  3985. var
  3986. sym: tfieldvarsym;
  3987. name: TIDString;
  3988. pname: ^TIDString;
  3989. begin
  3990. if optionalname='' then
  3991. begin
  3992. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  3993. pname:=@name
  3994. end
  3995. else
  3996. pname:=@optionalname;
  3997. sym:=cfieldvarsym.create(pname^,vs_value,def,[],true);
  3998. symtable.insert(sym);
  3999. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4000. result:=sym;
  4001. end;
  4002. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4003. var
  4004. i: longint;
  4005. begin
  4006. for i:=0 to fieldtypes.count-1 do
  4007. add_field_by_def('',tdef(fieldtypes[i]));
  4008. end;
  4009. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4010. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4011. var
  4012. i,j : longint;
  4013. begin
  4014. if ppufile.getbyte=1 then
  4015. begin
  4016. new(variantrecdesc);
  4017. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4018. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4019. for i:=0 to high(variantrecdesc^.branches) do
  4020. begin
  4021. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4022. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4023. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4024. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4025. end;
  4026. end
  4027. else
  4028. variantrecdesc:=nil;
  4029. end;
  4030. begin
  4031. inherited ppuload(recorddef,ppufile);
  4032. if df_copied_def in defoptions then
  4033. begin
  4034. ppufile.getderef(cloneddefderef);
  4035. ppuload_platform(ppufile);
  4036. end
  4037. else
  4038. begin
  4039. symtable:=trecordsymtable.create(objrealname^,0,0,0);
  4040. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4041. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4042. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4043. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4044. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4045. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4046. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4047. ppufile.getsmallset(trecordsymtable(symtable).managementoperators);
  4048. { position of ppuload_platform call must correspond
  4049. to position of writeentry in ppuwrite method }
  4050. ppuload_platform(ppufile);
  4051. trecordsymtable(symtable).ppuload(ppufile);
  4052. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4053. but because iso mode supports no units, there is no need to store the variantrecdesc
  4054. in the ppu
  4055. }
  4056. // readvariantrecdesc(variantrecdesc);
  4057. { requires usefieldalignment to be set }
  4058. symtable.defowner:=self;
  4059. end;
  4060. isunion:=false;
  4061. end;
  4062. destructor trecorddef.destroy;
  4063. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4064. var
  4065. i : longint;
  4066. begin
  4067. while assigned(variantrecdesc) do
  4068. begin
  4069. for i:=0 to high(variantrecdesc^.branches) do
  4070. begin
  4071. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4072. SetLength(variantrecdesc^.branches[i].values,0);
  4073. end;
  4074. SetLength(variantrecdesc^.branches,0);
  4075. dispose(variantrecdesc);
  4076. variantrecdesc:=nil;
  4077. end;
  4078. end;
  4079. begin
  4080. if assigned(variantrecdesc) then
  4081. free_variantrecdesc(variantrecdesc);
  4082. if assigned(symtable) then
  4083. begin
  4084. symtable.free;
  4085. symtable:=nil;
  4086. end;
  4087. inherited destroy;
  4088. end;
  4089. function trecorddef.getcopy : tstoreddef;
  4090. begin
  4091. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4092. trecorddef(result).isunion:=isunion;
  4093. include(trecorddef(result).defoptions,df_copied_def);
  4094. if assigned(tcinitcode) then
  4095. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4096. if assigned(import_lib) then
  4097. trecorddef(result).import_lib:=stringdup(import_lib^);
  4098. end;
  4099. function trecorddef.needs_inittable : boolean;
  4100. begin
  4101. { each record with managed field or with any management operator needs
  4102. init table }
  4103. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4104. trecordsymtable(symtable).needs_init_final
  4105. end;
  4106. function trecorddef.needs_separate_initrtti : boolean;
  4107. begin
  4108. result:=true;
  4109. end;
  4110. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4111. begin
  4112. result:=trecordsymtable(symtable).has_non_trivial_init;
  4113. end;
  4114. procedure trecorddef.buildderef;
  4115. begin
  4116. inherited buildderef;
  4117. if df_copied_def in defoptions then
  4118. cloneddefderef.build(symtable.defowner)
  4119. else
  4120. tstoredsymtable(symtable).buildderef;
  4121. end;
  4122. procedure trecorddef.deref;
  4123. begin
  4124. inherited deref;
  4125. { now dereference the definitions }
  4126. if df_copied_def in defoptions then
  4127. begin
  4128. cloneddef:=trecorddef(cloneddefderef.resolve);
  4129. symtable:=cloneddef.symtable.getcopy;
  4130. end
  4131. else
  4132. tstoredsymtable(symtable).deref(false);
  4133. { internal types, only load from the system unit }
  4134. if assigned(owner) and
  4135. assigned(owner.name) and
  4136. (owner.name^='SYSTEM') then
  4137. begin
  4138. { TGUID }
  4139. if not assigned(rec_tguid) and
  4140. (upper(typename)='TGUID') then
  4141. rec_tguid:=self
  4142. { JMP_BUF }
  4143. else if not assigned(rec_jmp_buf) and
  4144. (upper(typename)='JMP_BUF') then
  4145. rec_jmp_buf:=self
  4146. else if not assigned(rec_exceptaddr) and
  4147. (upper(typename)='TEXCEPTADDR') then
  4148. rec_exceptaddr:=self;
  4149. end;
  4150. end;
  4151. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4152. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4153. var
  4154. i,j : longint;
  4155. begin
  4156. if assigned(variantrecdesc) then
  4157. begin
  4158. ppufile.putbyte(1);
  4159. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4160. ppufile.putasizeint(length(variantrecdesc^.branches));
  4161. for i:=0 to high(variantrecdesc^.branches) do
  4162. begin
  4163. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4164. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4165. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4166. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4167. end;
  4168. end
  4169. else
  4170. ppufile.putbyte(0);
  4171. end;
  4172. begin
  4173. inherited ppuwrite(ppufile);
  4174. if df_copied_def in defoptions then
  4175. ppufile.putderef(cloneddefderef)
  4176. else
  4177. begin
  4178. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4179. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4180. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4181. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4182. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4183. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4184. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4185. ppufile.putsmallset(trecordsymtable(symtable).managementoperators);
  4186. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4187. but because iso mode supports no units, there is no need to store the variantrecdesc
  4188. in the ppu
  4189. }
  4190. // writevariantrecdesc(variantrecdesc);
  4191. end;
  4192. writeentry(ppufile,ibrecorddef);
  4193. if not(df_copied_def in defoptions) then
  4194. trecordsymtable(symtable).ppuwrite(ppufile);
  4195. end;
  4196. function trecorddef.size:asizeint;
  4197. begin
  4198. result:=trecordsymtable(symtable).datasize;
  4199. end;
  4200. function trecorddef.alignment:shortint;
  4201. begin
  4202. alignment:=trecordsymtable(symtable).recordalignment;
  4203. end;
  4204. function trecorddef.padalignment:shortint;
  4205. begin
  4206. padalignment := trecordsymtable(symtable).padalignment;
  4207. end;
  4208. function trecorddef.GetTypeName : string;
  4209. begin
  4210. GetTypeName:='<record type>'
  4211. end;
  4212. {***************************************************************************
  4213. TABSTRACTPROCDEF
  4214. ***************************************************************************}
  4215. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  4216. begin
  4217. inherited create(dt,doregister);
  4218. parast:=tparasymtable.create(self,level);
  4219. paras:=nil;
  4220. minparacount:=0;
  4221. maxparacount:=0;
  4222. proctypeoption:=potype_none;
  4223. proccalloption:=pocall_none;
  4224. procoptions:=[];
  4225. returndef:=voidtype;
  4226. savesize:=sizeof(pint);
  4227. callerargareasize:=0;
  4228. calleeargareasize:=0;
  4229. has_paraloc_info:=callnoside;
  4230. funcretloc[callerside].init;
  4231. funcretloc[calleeside].init;
  4232. check_mark_as_nested;
  4233. end;
  4234. destructor tabstractprocdef.destroy;
  4235. begin
  4236. if assigned(paras) then
  4237. begin
  4238. {$ifdef MEMDEBUG}
  4239. memprocpara.start;
  4240. {$endif MEMDEBUG}
  4241. paras.free;
  4242. paras:=nil;
  4243. {$ifdef MEMDEBUG}
  4244. memprocpara.stop;
  4245. {$endif MEMDEBUG}
  4246. end;
  4247. if assigned(parast) then
  4248. begin
  4249. {$ifdef MEMDEBUG}
  4250. memprocparast.start;
  4251. {$endif MEMDEBUG}
  4252. parast.free;
  4253. parast:=nil;
  4254. {$ifdef MEMDEBUG}
  4255. memprocparast.stop;
  4256. {$endif MEMDEBUG}
  4257. end;
  4258. funcretloc[callerside].done;
  4259. funcretloc[calleeside].done;
  4260. inherited destroy;
  4261. end;
  4262. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  4263. begin
  4264. if (tsym(p).typ<>paravarsym) then
  4265. exit;
  4266. inc(plongint(arg)^);
  4267. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  4268. begin
  4269. if not assigned(tparavarsym(p).defaultconstsym) then
  4270. inc(minparacount);
  4271. inc(maxparacount);
  4272. end;
  4273. end;
  4274. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  4275. begin
  4276. if (tsym(p).typ<>paravarsym) then
  4277. exit;
  4278. paras.add(p);
  4279. end;
  4280. procedure tabstractprocdef.calcparas;
  4281. var
  4282. paracount : longint;
  4283. begin
  4284. { This can already be assigned when
  4285. we need to reresolve this unit (PFV) }
  4286. if assigned(paras) then
  4287. paras.free;
  4288. paras:=tparalist.create(false);
  4289. paracount:=0;
  4290. minparacount:=0;
  4291. maxparacount:=0;
  4292. parast.SymList.ForEachCall(@count_para,@paracount);
  4293. paras.capacity:=paracount;
  4294. { Insert parameters in table }
  4295. parast.SymList.ForEachCall(@insert_para,nil);
  4296. { Order parameters }
  4297. paras.sortparas;
  4298. end;
  4299. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  4300. var
  4301. crc : dword;
  4302. hp : TParavarsym;
  4303. hs : TSymStr;
  4304. newlen,
  4305. i : integer;
  4306. begin
  4307. result:='';
  4308. hp:=nil;
  4309. { add parameter types }
  4310. for i:=0 to paras.count-1 do
  4311. begin
  4312. hp:=tparavarsym(paras[i]);
  4313. if not(vo_is_hidden_para in hp.varoptions) then
  4314. result:=result+'$'+hp.vardef.mangledparaname;
  4315. end;
  4316. { add resultdef, add $$ as separator to make it unique from a
  4317. parameter separator }
  4318. if not is_void(returndef) then
  4319. result:=result+'$$'+returndef.mangledparaname;
  4320. newlen:=length(result)+oldlen;
  4321. { Replace with CRC if the parameter line is very long }
  4322. if (newlen-oldlen>12) and
  4323. ((newlen>100) or (newlen-oldlen>64)) then
  4324. begin
  4325. crc:=0;
  4326. for i:=0 to paras.count-1 do
  4327. begin
  4328. hp:=tparavarsym(paras[i]);
  4329. if not(vo_is_hidden_para in hp.varoptions) then
  4330. begin
  4331. hs:=hp.vardef.mangledparaname;
  4332. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4333. end;
  4334. end;
  4335. if not is_void(returndef) then
  4336. begin
  4337. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4338. hs:='$$'+returndef.mangledparaname;
  4339. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4340. end;
  4341. result:='$crc'+hexstr(crc,8);
  4342. end;
  4343. end;
  4344. procedure tabstractprocdef.buildderef;
  4345. begin
  4346. { released procdef? }
  4347. if not assigned(parast) then
  4348. exit;
  4349. inherited buildderef;
  4350. returndefderef.build(returndef);
  4351. if po_explicitparaloc in procoptions then
  4352. funcretloc[callerside].buildderef;
  4353. { parast }
  4354. tparasymtable(parast).buildderef;
  4355. end;
  4356. procedure tabstractprocdef.deref;
  4357. begin
  4358. inherited deref;
  4359. returndef:=tdef(returndefderef.resolve);
  4360. if po_explicitparaloc in procoptions then
  4361. begin
  4362. funcretloc[callerside].deref;
  4363. has_paraloc_info:=callerside;
  4364. end
  4365. else
  4366. begin
  4367. { deref is called after loading from a ppu, but also after another
  4368. unit has been reloaded/recompiled and all references must be
  4369. re-resolved. Since the funcretloc contains a reference to a tdef,
  4370. reset it so that we won't try to access the stale def }
  4371. funcretloc[callerside].init;
  4372. has_paraloc_info:=callnoside;
  4373. end;
  4374. { parast }
  4375. tparasymtable(parast).deref(false);
  4376. { recalculated parameters }
  4377. calcparas;
  4378. end;
  4379. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4380. begin
  4381. inherited ppuload(dt,ppufile);
  4382. parast:=nil;
  4383. Paras:=nil;
  4384. minparacount:=0;
  4385. maxparacount:=0;
  4386. ppufile.getderef(returndefderef);
  4387. proctypeoption:=tproctypeoption(ppufile.getbyte);
  4388. proccalloption:=tproccalloption(ppufile.getbyte);
  4389. ppufile.getnormalset(procoptions);
  4390. funcretloc[callerside].init;
  4391. if po_explicitparaloc in procoptions then
  4392. funcretloc[callerside].ppuload(ppufile);
  4393. savesize:=sizeof(pint);
  4394. if (po_explicitparaloc in procoptions) then
  4395. has_paraloc_info:=callerside;
  4396. end;
  4397. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  4398. var
  4399. oldintfcrc : boolean;
  4400. begin
  4401. { released procdef? }
  4402. if not assigned(parast) then
  4403. exit;
  4404. inherited ppuwrite(ppufile);
  4405. ppufile.putderef(returndefderef);
  4406. oldintfcrc:=ppufile.do_interface_crc;
  4407. ppufile.do_interface_crc:=false;
  4408. ppufile.putbyte(ord(proctypeoption));
  4409. ppufile.putbyte(ord(proccalloption));
  4410. ppufile.putnormalset(procoptions);
  4411. ppufile.do_interface_crc:=oldintfcrc;
  4412. if (po_explicitparaloc in procoptions) then
  4413. funcretloc[callerside].ppuwrite(ppufile);
  4414. end;
  4415. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  4416. var
  4417. hs,s : ansistring;
  4418. hp : TParavarsym;
  4419. hpc : tconstsym;
  4420. first : boolean;
  4421. i : integer;
  4422. begin
  4423. s:='';
  4424. first:=true;
  4425. for i:=0 to paras.count-1 do
  4426. begin
  4427. hp:=tparavarsym(paras[i]);
  4428. if not(vo_is_hidden_para in hp.varoptions) or
  4429. (pno_showhidden in pno) then
  4430. begin
  4431. if first then
  4432. begin
  4433. s:=s+'(';
  4434. first:=false;
  4435. end
  4436. else
  4437. s:=s+';';
  4438. if vo_is_hidden_para in hp.varoptions then
  4439. s:=s+'<';
  4440. case hp.varspez of
  4441. vs_var :
  4442. s:=s+'var ';
  4443. vs_const :
  4444. s:=s+'const ';
  4445. vs_out :
  4446. s:=s+'out ';
  4447. vs_constref :
  4448. s:=s+'constref ';
  4449. end;
  4450. if (pno_paranames in pno) then
  4451. s:=s+hp.realname+':';
  4452. if hp.univpara then
  4453. s:=s+'univ ';
  4454. if assigned(hp.vardef.typesym) then
  4455. begin
  4456. hs:=hp.vardef.typesym.realname;
  4457. if hs[1]<>'$' then
  4458. s:=s+hp.vardef.OwnerHierarchyName+hs
  4459. else
  4460. s:=s+hp.vardef.GetTypeName;
  4461. end
  4462. else
  4463. s:=s+hp.vardef.GetTypeName;
  4464. { default value }
  4465. if assigned(hp.defaultconstsym) then
  4466. begin
  4467. hpc:=tconstsym(hp.defaultconstsym);
  4468. hs:='';
  4469. case hpc.consttyp of
  4470. conststring,
  4471. constresourcestring :
  4472. begin
  4473. If hpc.value.len>0 then
  4474. begin
  4475. setLength(hs,hpc.value.len);
  4476. { don't write past the end of hs if the constant
  4477. is > 255 chars }
  4478. move(hpc.value.valueptr^,hs[1],length(hs));
  4479. { make sure that constant strings with newline chars
  4480. don't create a linebreak in the assembler code,
  4481. since comments are line-based. Also remove nulls
  4482. because the comments are written as a pchar. }
  4483. ReplaceCase(hs,#0,'.');
  4484. ReplaceCase(hs,#10,'.');
  4485. ReplaceCase(hs,#13,'.');
  4486. end;
  4487. end;
  4488. constreal :
  4489. str(pbestreal(hpc.value.valueptr)^,hs);
  4490. constpointer :
  4491. hs:=tostr(hpc.value.valueordptr);
  4492. constord :
  4493. begin
  4494. if is_boolean(hpc.constdef) then
  4495. begin
  4496. if hpc.value.valueord<>0 then
  4497. hs:='TRUE'
  4498. else
  4499. hs:='FALSE';
  4500. end
  4501. else
  4502. hs:=tostr(hpc.value.valueord);
  4503. end;
  4504. constnil :
  4505. hs:='nil';
  4506. constset :
  4507. hs:='<set>';
  4508. end;
  4509. if hs<>'' then
  4510. s:=s+'=`'+hs+'`';
  4511. end;
  4512. if vo_is_hidden_para in hp.varoptions then
  4513. s:=s+'>';
  4514. end;
  4515. end;
  4516. if not first then
  4517. s:=s+')';
  4518. if (po_varargs in procoptions) then
  4519. s:=s+';VarArgs';
  4520. typename_paras:=s;
  4521. end;
  4522. function tabstractprocdef.is_methodpointer:boolean;
  4523. begin
  4524. result:=false;
  4525. end;
  4526. function tabstractprocdef.is_addressonly:boolean;
  4527. begin
  4528. result:=true;
  4529. end;
  4530. function tabstractprocdef.no_self_node: boolean;
  4531. begin
  4532. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4533. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4534. end;
  4535. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  4536. var
  4537. j, nestinglevel: longint;
  4538. pvs, npvs: tparavarsym;
  4539. begin
  4540. nestinglevel:=parast.symtablelevel;
  4541. if newtyp=procdef then
  4542. begin
  4543. if (copytyp<>pc_bareproc) then
  4544. result:=cprocdef.create(nestinglevel,true)
  4545. else
  4546. result:=cprocdef.create(normal_function_level,true);
  4547. tprocdef(result).visibility:=vis_public;
  4548. end
  4549. else
  4550. begin
  4551. result:=cprocvardef.create(nestinglevel);
  4552. end;
  4553. tabstractprocdef(result).returndef:=returndef;
  4554. tabstractprocdef(result).returndefderef:=returndefderef;
  4555. pvs:=nil;
  4556. npvs:=nil;
  4557. for j:=0 to parast.symlist.count-1 do
  4558. begin
  4559. case tsym(parast.symlist[j]).typ of
  4560. paravarsym:
  4561. begin
  4562. pvs:=tparavarsym(parast.symlist[j]);
  4563. { in case of bare proc, don't copy self, vmt or framepointer
  4564. parameters }
  4565. if (copytyp=pc_bareproc) and
  4566. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4567. continue;
  4568. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4569. pvs.vardef,pvs.varoptions);
  4570. npvs.defaultconstsym:=pvs.defaultconstsym;
  4571. tabstractprocdef(result).parast.insert(npvs);
  4572. end;
  4573. constsym:
  4574. begin
  4575. // ignore, reuse original constym. Should also be duplicated
  4576. // be safe though
  4577. end;
  4578. symconst.typesym:
  4579. begin
  4580. // reuse original, part of generic declaration
  4581. end
  4582. else
  4583. internalerror(201160604);
  4584. end;
  4585. end;
  4586. tabstractprocdef(result).savesize:=savesize;
  4587. if (typ<>procvardef) and
  4588. (newtyp=procvardef) then
  4589. begin
  4590. { procvars can't be (class)constructures/destructors etc }
  4591. if proctypeoption=potype_constructor then
  4592. begin
  4593. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  4594. if not(is_implicit_pointer_object_type(returndef) or
  4595. (returndef.typ<>objectdef)) then
  4596. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  4597. tabstractprocdef(result).proctypeoption:=potype_function;
  4598. end
  4599. else if is_void(returndef) then
  4600. tabstractprocdef(result).proctypeoption:=potype_procedure
  4601. else
  4602. tabstractprocdef(result).proctypeoption:=potype_function;
  4603. end
  4604. else
  4605. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4606. tabstractprocdef(result).proccalloption:=proccalloption;
  4607. tabstractprocdef(result).procoptions:=procoptions;
  4608. if (copytyp=pc_bareproc) then
  4609. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4610. if newtyp=procvardef then
  4611. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4612. if copytyp=pc_address_only then
  4613. include(tabstractprocdef(result).procoptions,po_addressonly);
  4614. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4615. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4616. tabstractprocdef(result).maxparacount:=maxparacount;
  4617. tabstractprocdef(result).minparacount:=minparacount;
  4618. if po_explicitparaloc in procoptions then
  4619. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4620. { recalculate parameter info }
  4621. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4622. {$ifdef m68k}
  4623. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4624. {$endif}
  4625. if (typ=procdef) and
  4626. (newtyp=procvardef) and
  4627. (owner.symtabletype=ObjectSymtable) then
  4628. include(tprocvardef(result).procoptions,po_methodpointer);
  4629. end;
  4630. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  4631. begin
  4632. result:=is_addressonly;
  4633. end;
  4634. procedure tabstractprocdef.check_mark_as_nested;
  4635. begin
  4636. { nested procvars require that nested functions use the Delphi-style
  4637. nested procedure calling convention }
  4638. if (parast.symtablelevel>normal_function_level) and
  4639. (m_nested_procvars in current_settings.modeswitches) then
  4640. include(procoptions,po_delphi_nested_cc);
  4641. end;
  4642. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4643. begin
  4644. if (side in [callerside,callbothsides]) and
  4645. not(has_paraloc_info in [callerside,callbothsides]) then
  4646. begin
  4647. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  4648. if has_paraloc_info in [calleeside,callbothsides] then
  4649. has_paraloc_info:=callbothsides
  4650. else
  4651. has_paraloc_info:=callerside;
  4652. end;
  4653. if (side in [calleeside,callbothsides]) and
  4654. not(has_paraloc_info in [calleeside,callbothsides]) then
  4655. begin
  4656. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  4657. if has_paraloc_info in [callerside,callbothsides] then
  4658. has_paraloc_info:=callbothsides
  4659. else
  4660. has_paraloc_info:=calleeside;
  4661. end;
  4662. end;
  4663. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  4664. var
  4665. i: longint;
  4666. begin
  4667. if (side in [callerside,callbothsides]) and
  4668. (has_paraloc_info in [callerside,callbothsides]) then
  4669. begin
  4670. funcretloc[callerside].done;
  4671. for i:=0 to paras.count-1 do
  4672. tparavarsym(paras[i]).paraloc[callerside].done;
  4673. if has_paraloc_info=callerside then
  4674. has_paraloc_info:=callnoside
  4675. else
  4676. has_paraloc_info:=calleeside;
  4677. end;
  4678. if (side in [calleeside,callbothsides]) and
  4679. (has_paraloc_info in [calleeside,callbothsides]) then
  4680. begin
  4681. funcretloc[calleeside].done;
  4682. for i:=0 to paras.count-1 do
  4683. tparavarsym(paras[i]).paraloc[calleeside].done;
  4684. if has_paraloc_info=calleeside then
  4685. has_paraloc_info:=callnoside
  4686. else
  4687. has_paraloc_info:=callerside;
  4688. end;
  4689. end;
  4690. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4691. var
  4692. p: tparavarsym;
  4693. ploc: PCGParalocation;
  4694. i: longint;
  4695. begin
  4696. result:=false;
  4697. init_paraloc_info(side);
  4698. for i:=0 to parast.SymList.Count-1 do
  4699. if tsym(parast.SymList[i]).typ=paravarsym then
  4700. begin
  4701. p:=tparavarsym(parast.SymList[i]);
  4702. { check if no parameter is located on the stack }
  4703. if is_open_array(p.vardef) or
  4704. is_array_of_const(p.vardef) then
  4705. begin
  4706. result:=true;
  4707. exit;
  4708. end;
  4709. ploc:=p.paraloc[side].location;
  4710. while assigned(ploc) do
  4711. begin
  4712. if (ploc^.loc=LOC_REFERENCE) then
  4713. begin
  4714. result:=true;
  4715. exit
  4716. end;
  4717. ploc:=ploc^.next;
  4718. end;
  4719. end;
  4720. end;
  4721. function tabstractprocdef.is_pushleftright: boolean;
  4722. begin
  4723. result:=false;
  4724. end;
  4725. function tabstractprocdef.address_type: tdef;
  4726. begin
  4727. result:=voidcodepointertype;
  4728. end;
  4729. function tabstractprocdef.ofs_address_type:tdef;
  4730. begin
  4731. result:=address_type;
  4732. end;
  4733. procedure tabstractprocdef.declared_far;
  4734. begin
  4735. Message1(parser_w_proc_directive_ignored,'FAR');
  4736. end;
  4737. procedure tabstractprocdef.declared_near;
  4738. begin
  4739. Message1(parser_w_proc_directive_ignored,'NEAR');
  4740. end;
  4741. {***************************************************************************
  4742. TPROCDEF
  4743. ***************************************************************************}
  4744. function tprocdef.GetResultName: PShortString;
  4745. begin
  4746. if not assigned(implprocdefinfo) then
  4747. internalerror(2014010301);
  4748. result:=implprocdefinfo^.resultname;
  4749. end;
  4750. procedure tprocdef.SetResultName(AValue: PShortString);
  4751. begin
  4752. if not assigned(implprocdefinfo) then
  4753. internalerror(2014010302);
  4754. implprocdefinfo^.resultname:=AValue;
  4755. end;
  4756. function tprocdef.GetParentFPInitBlock: tnode;
  4757. begin
  4758. if not assigned(implprocdefinfo) then
  4759. internalerror(2014010303);
  4760. result:=implprocdefinfo^.parentfpinitblock;
  4761. end;
  4762. function tprocdef.GetParentFPStruct: tsym;
  4763. begin
  4764. if not assigned(implprocdefinfo) then
  4765. internalerror(2014010304);
  4766. result:=implprocdefinfo^.parentfpstruct;
  4767. end;
  4768. function tprocdef.GetParentFPStructPtrType: tdef;
  4769. begin
  4770. if not assigned(implprocdefinfo) then
  4771. internalerror(2014010305);
  4772. result:=implprocdefinfo^.parentfpstructptrtype;
  4773. end;
  4774. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4775. begin
  4776. if not assigned(implprocdefinfo) then
  4777. internalerror(2014010306);
  4778. implprocdefinfo^.parentfpinitblock:=AValue;
  4779. end;
  4780. function tprocdef.Getprocendtai: tai;
  4781. begin
  4782. if not assigned(implprocdefinfo) then
  4783. internalerror(2014010307);
  4784. result:=implprocdefinfo^.procendtai;
  4785. end;
  4786. function tprocdef.Getprocstarttai: tai;
  4787. begin
  4788. if not assigned(implprocdefinfo) then
  4789. internalerror(2014010308);
  4790. result:=implprocdefinfo^.procstarttai;
  4791. end;
  4792. procedure tprocdef.Setprocendtai(AValue: tai);
  4793. begin
  4794. if not assigned(implprocdefinfo) then
  4795. internalerror(2014010309);
  4796. implprocdefinfo^.procendtai:=AValue;
  4797. end;
  4798. function tprocdef.Getskpara: pointer;
  4799. begin
  4800. if not assigned(implprocdefinfo) then
  4801. internalerror(2014010310);
  4802. result:=implprocdefinfo^.skpara;
  4803. end;
  4804. procedure tprocdef.Setskpara(AValue: pointer);
  4805. begin
  4806. if not assigned(implprocdefinfo) then
  4807. internalerror(2014010311);
  4808. implprocdefinfo^.skpara:=AValue;
  4809. end;
  4810. function tprocdef.Getpersonality: tprocdef;
  4811. begin
  4812. if not assigned(implprocdefinfo) then
  4813. internalerror(2016121701);
  4814. result:=implprocdefinfo^.personality;
  4815. end;
  4816. procedure tprocdef.Setpersonality(AValue: tprocdef);
  4817. begin
  4818. if not assigned(implprocdefinfo) then
  4819. internalerror(2016121702);
  4820. implprocdefinfo^.personality:=AValue;
  4821. end;
  4822. function tprocdef.Getforwarddef: boolean;
  4823. begin
  4824. if not assigned(implprocdefinfo) then
  4825. internalerror(2014010312);
  4826. result:=implprocdefinfo^.forwarddef;
  4827. end;
  4828. function tprocdef.Gethasforward: boolean;
  4829. begin
  4830. if not assigned(implprocdefinfo) then
  4831. internalerror(2014010313);
  4832. result:=implprocdefinfo^.hasforward;
  4833. end;
  4834. function tprocdef.Getinterfacedef: boolean;
  4835. begin
  4836. if not assigned(implprocdefinfo) then
  4837. internalerror(2014010314);
  4838. result:=implprocdefinfo^.interfacedef;
  4839. end;
  4840. procedure tprocdef.Setforwarddef(AValue: boolean);
  4841. begin
  4842. if not assigned(implprocdefinfo) then
  4843. internalerror(2014010315);
  4844. implprocdefinfo^.forwarddef:=AValue;
  4845. end;
  4846. procedure tprocdef.Sethasforward(AValue: boolean);
  4847. begin
  4848. if not assigned(implprocdefinfo) then
  4849. internalerror(2014010316);
  4850. implprocdefinfo^.hasforward:=AValue;
  4851. end;
  4852. function tprocdef.GetIsEmpty: boolean;
  4853. begin
  4854. result:=pio_empty in implprocoptions;
  4855. end;
  4856. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4857. begin
  4858. if AValue then
  4859. include(implprocoptions,pio_empty)
  4860. else
  4861. include(implprocoptions,pio_empty);
  4862. end;
  4863. function tprocdef.GetHasInliningInfo: boolean;
  4864. begin
  4865. result:=pio_has_inlininginfo in implprocoptions;
  4866. end;
  4867. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4868. begin
  4869. if AValue then
  4870. include(implprocoptions,pio_has_inlininginfo)
  4871. else
  4872. exclude(implprocoptions,pio_has_inlininginfo);
  4873. end;
  4874. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4875. begin
  4876. if not assigned(implprocdefinfo) then
  4877. internalerror(2014010317);
  4878. implprocdefinfo^.interfacedef:=AValue;
  4879. end;
  4880. procedure tprocdef.Setprocstarttai(AValue: tai);
  4881. begin
  4882. if not assigned(implprocdefinfo) then
  4883. internalerror(2014010318);
  4884. implprocdefinfo^.procstarttai:=AValue;
  4885. end;
  4886. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4887. begin
  4888. if not assigned(implprocdefinfo) then
  4889. internalerror(2014010319);
  4890. implprocdefinfo^.parentfpstruct:=AValue;
  4891. end;
  4892. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4893. begin
  4894. if not assigned(implprocdefinfo) then
  4895. internalerror(2014010320);
  4896. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4897. end;
  4898. constructor tprocdef.create(level:byte;doregister:boolean);
  4899. begin
  4900. inherited create(procdef,level,doregister);
  4901. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4902. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4903. {$ifdef symansistr}
  4904. _mangledname:='';
  4905. {$else symansistr}
  4906. _mangledname:=nil;
  4907. {$endif symansistr}
  4908. fileinfo:=current_filepos;
  4909. extnumber:=$ffff;
  4910. aliasnames:=TCmdStrList.create;
  4911. funcretsym:=nil;
  4912. forwarddef:=true;
  4913. interfacedef:=false;
  4914. hasforward:=false;
  4915. struct := nil;
  4916. import_dll:=nil;
  4917. import_name:=nil;
  4918. import_nr:=0;
  4919. inlininginfo:=nil;
  4920. deprecatedmsg:=nil;
  4921. end;
  4922. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4923. var
  4924. i,aliasnamescount,sizeleft : longint;
  4925. level : byte;
  4926. buf : array[0..255] of byte;
  4927. begin
  4928. inherited ppuload(procdef,ppufile);
  4929. {$ifdef symansistr}
  4930. if po_has_mangledname in procoptions then
  4931. _mangledname:=ppufile.getansistring
  4932. else
  4933. _mangledname:='';
  4934. {$else symansistr}
  4935. if po_has_mangledname in procoptions then
  4936. _mangledname:=ppufile.getpshortstring
  4937. else
  4938. _mangledname:=nil;
  4939. {$endif symansistr}
  4940. extnumber:=ppufile.getword;
  4941. level:=ppufile.getbyte;
  4942. ppufile.getderef(structderef);
  4943. ppufile.getderef(procsymderef);
  4944. ppufile.getposinfo(fileinfo);
  4945. visibility:=tvisibility(ppufile.getbyte);
  4946. ppufile.getsmallset(symoptions);
  4947. if sp_has_deprecated_msg in symoptions then
  4948. deprecatedmsg:=ppufile.getpshortstring
  4949. else
  4950. deprecatedmsg:=nil;
  4951. { import stuff }
  4952. if po_has_importdll in procoptions then
  4953. import_dll:=ppufile.getpshortstring
  4954. else
  4955. import_dll:=nil;
  4956. if po_has_importname in procoptions then
  4957. import_name:=ppufile.getpshortstring
  4958. else
  4959. import_name:=nil;
  4960. import_nr:=ppufile.getword;
  4961. if (po_msgint in procoptions) then
  4962. messageinf.i:=ppufile.getlongint;
  4963. if (po_msgstr in procoptions) then
  4964. messageinf.str:=ppufile.getpshortstring;
  4965. if (po_dispid in procoptions) then
  4966. dispid:=ppufile.getlongint;
  4967. { inline stuff }
  4968. ppufile.getsmallset(implprocoptions);
  4969. if has_inlininginfo then
  4970. begin
  4971. ppufile.getderef(funcretsymderef);
  4972. new(inlininginfo);
  4973. ppufile.getsmallset(inlininginfo^.flags);
  4974. end
  4975. else
  4976. begin
  4977. inlininginfo:=nil;
  4978. funcretsym:=nil;
  4979. end;
  4980. aliasnames:=TCmdStrList.create;
  4981. { count alias names }
  4982. aliasnamescount:=ppufile.getbyte;
  4983. for i:=1 to aliasnamescount do
  4984. aliasnames.insert(ppufile.getstring);
  4985. { load the token stream containing the declaration }
  4986. sizeleft:=ppufile.getlongint;
  4987. if sizeleft>0 then
  4988. begin
  4989. init_genericdecl;
  4990. while sizeleft>0 do
  4991. begin
  4992. if sizeleft>sizeof(buf) then
  4993. i:=sizeof(buf)
  4994. else
  4995. i:=sizeleft;
  4996. ppufile.getdata(buf,i);
  4997. genericdecltokenbuf.write(buf,i);
  4998. dec(sizeleft,i);
  4999. end;
  5000. end;
  5001. ppuload_platform(ppufile);
  5002. { load para symtable }
  5003. parast:=tparasymtable.create(self,level);
  5004. tparasymtable(parast).ppuload(ppufile);
  5005. { load local symtable }
  5006. if has_inlininginfo then
  5007. begin
  5008. localst:=tlocalsymtable.create(self,level);
  5009. tlocalsymtable(localst).ppuload(ppufile);
  5010. end
  5011. else
  5012. localst:=nil;
  5013. { inline stuff }
  5014. if has_inlininginfo then
  5015. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5016. { default values for no persistent data }
  5017. if (cs_link_deffile in current_settings.globalswitches) and
  5018. (tf_need_export in target_info.flags) and
  5019. (po_exports in procoptions) then
  5020. deffile.AddExport(mangledname);
  5021. { Disable po_has_inlining until the derefimpl is done }
  5022. has_inlininginfo:=false;
  5023. end;
  5024. destructor tprocdef.destroy;
  5025. begin
  5026. aliasnames.free;
  5027. aliasnames:=nil;
  5028. if assigned(localst) and
  5029. (localst.symtabletype<>staticsymtable) then
  5030. begin
  5031. {$ifdef MEMDEBUG}
  5032. memproclocalst.start;
  5033. {$endif MEMDEBUG}
  5034. localst.free;
  5035. localst:=nil;
  5036. {$ifdef MEMDEBUG}
  5037. memproclocalst.start;
  5038. {$endif MEMDEBUG}
  5039. end;
  5040. if assigned(inlininginfo) then
  5041. begin
  5042. {$ifdef MEMDEBUG}
  5043. memprocnodetree.start;
  5044. {$endif MEMDEBUG}
  5045. tnode(inlininginfo^.code).free;
  5046. {$ifdef MEMDEBUG}
  5047. memprocnodetree.start;
  5048. {$endif MEMDEBUG}
  5049. dispose(inlininginfo);
  5050. inlininginfo:=nil;
  5051. end;
  5052. freeimplprocdefinfo;
  5053. genericdecltokenbuf.free;
  5054. genericdecltokenbuf:=nil;
  5055. stringdispose(import_dll);
  5056. stringdispose(import_name);
  5057. stringdispose(deprecatedmsg);
  5058. if (po_msgstr in procoptions) then
  5059. stringdispose(messageinf.str);
  5060. {$ifndef symansistr}
  5061. if assigned(_mangledname) then
  5062. begin
  5063. {$ifdef MEMDEBUG}
  5064. memmanglednames.start;
  5065. {$endif MEMDEBUG}
  5066. stringdispose(_mangledname);
  5067. {$ifdef MEMDEBUG}
  5068. memmanglednames.stop;
  5069. {$endif MEMDEBUG}
  5070. end;
  5071. {$endif symansistr}
  5072. inherited destroy;
  5073. end;
  5074. procedure tprocdef.freeimplprocdefinfo;
  5075. begin
  5076. if assigned(implprocdefinfo) then
  5077. begin
  5078. stringdispose(implprocdefinfo^.resultname);
  5079. freemem(implprocdefinfo);
  5080. implprocdefinfo:=nil;
  5081. end;
  5082. end;
  5083. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  5084. var
  5085. oldintfcrc : boolean;
  5086. aliasnamescount,i,sizeleft : longint;
  5087. item : TCmdStrListItem;
  5088. buf : array[0..255] of byte;
  5089. begin
  5090. { released procdef? }
  5091. if not assigned(parast) then
  5092. exit;
  5093. inherited ppuwrite(ppufile);
  5094. {$ifdef symansistr}
  5095. if po_has_mangledname in procoptions then
  5096. ppufile.putansistring(_mangledname);
  5097. {$else symansistr}
  5098. if po_has_mangledname in procoptions then
  5099. ppufile.putstring(_mangledname^);
  5100. {$endif symansistr}
  5101. ppufile.putword(extnumber);
  5102. ppufile.putbyte(parast.symtablelevel);
  5103. ppufile.putderef(structderef);
  5104. ppufile.putderef(procsymderef);
  5105. ppufile.putposinfo(fileinfo);
  5106. ppufile.putbyte(byte(visibility));
  5107. ppufile.putsmallset(symoptions);
  5108. if sp_has_deprecated_msg in symoptions then
  5109. ppufile.putstring(deprecatedmsg^);
  5110. { import }
  5111. if po_has_importdll in procoptions then
  5112. ppufile.putstring(import_dll^);
  5113. if po_has_importname in procoptions then
  5114. ppufile.putstring(import_name^);
  5115. ppufile.putword(import_nr);
  5116. if (po_msgint in procoptions) then
  5117. ppufile.putlongint(messageinf.i);
  5118. if (po_msgstr in procoptions) then
  5119. ppufile.putstring(messageinf.str^);
  5120. if (po_dispid in procoptions) then
  5121. ppufile.putlongint(dispid);
  5122. { inline stuff }
  5123. oldintfcrc:=ppufile.do_crc;
  5124. ppufile.do_crc:=false;
  5125. ppufile.putsmallset(implprocoptions);
  5126. if has_inlininginfo then
  5127. begin
  5128. ppufile.putderef(funcretsymderef);
  5129. ppufile.putsmallset(inlininginfo^.flags);
  5130. end;
  5131. { count alias names }
  5132. aliasnamescount:=0;
  5133. item:=TCmdStrListItem(aliasnames.first);
  5134. while assigned(item) do
  5135. begin
  5136. inc(aliasnamescount);
  5137. item:=TCmdStrListItem(item.next);
  5138. end;
  5139. if aliasnamescount>255 then
  5140. internalerror(200711021);
  5141. ppufile.putbyte(aliasnamescount);
  5142. item:=TCmdStrListItem(aliasnames.first);
  5143. while assigned(item) do
  5144. begin
  5145. ppufile.putstring(item.str);
  5146. item:=TCmdStrListItem(item.next);
  5147. end;
  5148. ppufile.do_crc:=oldintfcrc;
  5149. { generic tokens for the declaration }
  5150. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  5151. begin
  5152. sizeleft:=genericdecltokenbuf.size;
  5153. genericdecltokenbuf.seek(0);
  5154. ppufile.putlongint(sizeleft);
  5155. while sizeleft>0 do
  5156. begin
  5157. if sizeleft>sizeof(buf) then
  5158. i:=sizeof(buf)
  5159. else
  5160. i:=sizeleft;
  5161. genericdecltokenbuf.read(buf,i);
  5162. ppufile.putdata(buf,i);
  5163. dec(sizeleft,i);
  5164. end;
  5165. end
  5166. else
  5167. ppufile.putlongint(0);
  5168. { write this entry }
  5169. writeentry(ppufile,ibprocdef);
  5170. { Save the para symtable, this is taken from the interface }
  5171. tparasymtable(parast).ppuwrite(ppufile);
  5172. { save localsymtable for inline procedures or when local
  5173. browser info is requested, this has no influence on the crc }
  5174. if has_inlininginfo then
  5175. begin
  5176. oldintfcrc:=ppufile.do_crc;
  5177. ppufile.do_crc:=false;
  5178. tlocalsymtable(localst).ppuwrite(ppufile);
  5179. ppufile.do_crc:=oldintfcrc;
  5180. end;
  5181. { node tree for inlining }
  5182. oldintfcrc:=ppufile.do_crc;
  5183. ppufile.do_crc:=false;
  5184. if has_inlininginfo then
  5185. ppuwritenodetree(ppufile,inlininginfo^.code);
  5186. ppufile.do_crc:=oldintfcrc;
  5187. end;
  5188. function tprocdef.fullprocname(showhidden:boolean):string;
  5189. var
  5190. pno: tprocnameoptions;
  5191. begin
  5192. pno:=[];
  5193. if showhidden then
  5194. include(pno,pno_showhidden);
  5195. result:=customprocname(pno);
  5196. end;
  5197. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  5198. var
  5199. s, rn : ansistring;
  5200. t : ttoken;
  5201. syssym : tsyssym;
  5202. begin
  5203. {$ifdef EXTDEBUG}
  5204. include(pno,pno_showhidden);
  5205. {$endif EXTDEBUG}
  5206. s:='';
  5207. if proctypeoption=potype_operator then
  5208. begin
  5209. for t:=NOTOKEN to last_overloaded do
  5210. if procsym.realname='$'+overloaded_names[t] then
  5211. begin
  5212. s:='operator ';
  5213. if (pno_ownername in pno) and
  5214. assigned(struct) then
  5215. s:=s+struct.RttiName+'.';
  5216. s:=s+arraytokeninfo[t].str;
  5217. if not (pno_noparams in pno) then
  5218. s:=s+typename_paras(pno);
  5219. break;
  5220. end;
  5221. end
  5222. else
  5223. begin
  5224. if (po_classmethod in procoptions) and
  5225. not(pno_noclassmarker in pno) then
  5226. s:='class ';
  5227. case proctypeoption of
  5228. potype_constructor,
  5229. potype_class_constructor:
  5230. s:=s+'constructor ';
  5231. potype_class_destructor,
  5232. potype_destructor:
  5233. s:=s+'destructor ';
  5234. else
  5235. if (pno_proctypeoption in pno) then
  5236. begin
  5237. if assigned(returndef) and
  5238. not(is_void(returndef)) then
  5239. s:=s+'function '
  5240. else
  5241. s:=s+'procedure ';
  5242. end;
  5243. end;
  5244. if (pno_ownername in pno) and
  5245. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  5246. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  5247. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  5248. begin
  5249. syssym:=tsyssym.find_by_number(extnumber);
  5250. if not assigned(syssym) then
  5251. internalerror(2016060305);
  5252. rn:=syssym.realname;
  5253. end
  5254. else
  5255. rn:=procsym.realname;
  5256. if (pno_noleadingdollar in pno) and
  5257. (rn[1]='$') then
  5258. delete(rn,1,1);
  5259. s:=s+rn;
  5260. if not (pno_noparams in pno) then
  5261. s:=s+typename_paras(pno);
  5262. end;
  5263. if not(proctypeoption in [potype_constructor,potype_destructor,
  5264. potype_class_constructor,potype_class_destructor]) and
  5265. assigned(returndef) and
  5266. not(is_void(returndef)) then
  5267. s:=s+':'+returndef.GetTypeName;
  5268. if assigned(owner) and (owner.symtabletype=localsymtable) then
  5269. s:=s+' is nested'
  5270. else if po_is_block in procoptions then
  5271. s:=s+' is block';
  5272. s:=s+';';
  5273. if po_far in procoptions then
  5274. s:=s+' far;';
  5275. { forced calling convention? }
  5276. if (po_hascallingconvention in procoptions) then
  5277. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  5278. if (po_staticmethod in procoptions) and
  5279. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  5280. s:=s+' Static;';
  5281. if pno_mangledname in pno then
  5282. s:=s+' -- mangled name: '+mangledname;
  5283. customprocname:=s;
  5284. end;
  5285. function tprocdef.is_methodpointer:boolean;
  5286. begin
  5287. { don't check assigned(_class), that's also the case for nested
  5288. procedures inside methods }
  5289. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  5290. end;
  5291. function tprocdef.is_addressonly:boolean;
  5292. begin
  5293. result:=assigned(owner) and
  5294. not is_methodpointer and
  5295. (not(m_nested_procvars in current_settings.modeswitches) or
  5296. not is_nested_pd(self));
  5297. end;
  5298. procedure tprocdef.make_external;
  5299. begin
  5300. include(procoptions,po_external);
  5301. forwarddef:=false;
  5302. end;
  5303. procedure tprocdef.init_genericdecl;
  5304. begin
  5305. if assigned(genericdecltokenbuf) then
  5306. internalerror(2015061901);
  5307. genericdecltokenbuf:=tdynamicarray.create(256);
  5308. end;
  5309. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  5310. var
  5311. item : TCmdStrListItem;
  5312. begin
  5313. result:=true;
  5314. if mangledname=s then
  5315. exit;
  5316. item:=TCmdStrListItem(aliasnames.first);
  5317. while assigned(item) do
  5318. begin
  5319. if item.str=s then
  5320. exit;
  5321. item:=TCmdStrListItem(item.next);
  5322. end;
  5323. result:=false;
  5324. end;
  5325. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  5326. begin
  5327. case t of
  5328. gs_local :
  5329. GetSymtable:=localst;
  5330. gs_para :
  5331. GetSymtable:=parast;
  5332. else
  5333. GetSymtable:=nil;
  5334. end;
  5335. end;
  5336. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  5337. var
  5338. j : longint;
  5339. begin
  5340. result:=inherited getcopyas(newtyp,copytyp);
  5341. if newtyp=procvardef then
  5342. begin
  5343. { create new paralist }
  5344. tprocvardef(result).calcparas;
  5345. exit;
  5346. end;
  5347. { don't copy mangled name, can be different }
  5348. tprocdef(result).messageinf:=messageinf;
  5349. tprocdef(result).dispid:=dispid;
  5350. if po_msgstr in procoptions then
  5351. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  5352. tprocdef(result).symoptions:=symoptions;
  5353. if assigned(deprecatedmsg) then
  5354. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  5355. { will have to be associated with appropriate procsym }
  5356. tprocdef(result).procsym:=nil;
  5357. { don't create aliases for bare copies, nor copy the funcretsym as
  5358. the function result parameter will be inserted again if necessary
  5359. (e.g. if the calling convention is changed) }
  5360. if copytyp<>pc_bareproc then
  5361. begin
  5362. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  5363. if assigned(funcretsym) then
  5364. begin
  5365. if funcretsym.owner=parast then
  5366. begin
  5367. j:=parast.symlist.indexof(funcretsym);
  5368. if j<0 then
  5369. internalerror(2011040606);
  5370. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  5371. end
  5372. else if funcretsym.owner=localst then
  5373. begin
  5374. { nothing to do, will be inserted for the new procdef while
  5375. parsing its body (by pdecsub.insert_funcret_local) }
  5376. end
  5377. else
  5378. internalerror(2011040605);
  5379. end;
  5380. end;
  5381. { will have to be associated with a new struct }
  5382. tprocdef(result).struct:=nil;
  5383. if assigned(implprocdefinfo) then
  5384. begin
  5385. if assigned(resultname) then
  5386. tprocdef(result).resultname:=stringdup(resultname^);
  5387. tprocdef(result).synthetickind:=synthetickind;
  5388. end;
  5389. if assigned(import_dll) then
  5390. tprocdef(result).import_dll:=stringdup(import_dll^);
  5391. if assigned(import_name) then
  5392. tprocdef(result).import_name:=stringdup(import_name^);
  5393. tprocdef(result).import_nr:=import_nr;
  5394. tprocdef(result).extnumber:=$ffff;
  5395. tprocdef(result).visibility:=visibility;
  5396. { we need a separate implementation for the copied def }
  5397. tprocdef(result).forwarddef:=true;
  5398. tprocdef(result).interfacedef:=true;
  5399. { create new paralist }
  5400. tprocdef(result).calcparas;
  5401. end;
  5402. function tprocdef.getcopy: tstoreddef;
  5403. begin
  5404. result:=getcopyas(procdef,pc_normal);
  5405. end;
  5406. procedure tprocdef.buildderef;
  5407. begin
  5408. inherited buildderef;
  5409. structderef.build(struct);
  5410. { procsym that originaly defined this definition, should be in the
  5411. same symtable }
  5412. procsymderef.build(procsym);
  5413. end;
  5414. procedure tprocdef.buildderefimpl;
  5415. begin
  5416. inherited buildderefimpl;
  5417. { inline tree }
  5418. if has_inlininginfo then
  5419. begin
  5420. { Localst is not available for main/unit init }
  5421. if assigned(localst) then
  5422. begin
  5423. tlocalsymtable(localst).buildderef;
  5424. tlocalsymtable(localst).buildderefimpl;
  5425. end;
  5426. funcretsymderef.build(funcretsym);
  5427. inlininginfo^.code.buildderefimpl;
  5428. end;
  5429. end;
  5430. procedure tprocdef.deref;
  5431. begin
  5432. inherited deref;
  5433. struct:=tabstractrecorddef(structderef.resolve);
  5434. { procsym that originaly defined this definition, should be in the
  5435. same symtable }
  5436. procsym:=tprocsym(procsymderef.resolve);
  5437. end;
  5438. procedure tprocdef.derefimpl;
  5439. begin
  5440. { Enable has_inlininginfo when the inlininginfo
  5441. structure is available. The has_inlininginfo was disabled
  5442. after the load, since the data was invalid }
  5443. if assigned(inlininginfo) then
  5444. has_inlininginfo:=true;
  5445. { Inline }
  5446. if has_inlininginfo then
  5447. begin
  5448. { Locals }
  5449. if assigned(localst) then
  5450. begin
  5451. tlocalsymtable(localst).deref(false);
  5452. tlocalsymtable(localst).derefimpl(false);
  5453. end;
  5454. inlininginfo^.code.derefimpl;
  5455. { funcretsym, this is always located in the localst }
  5456. funcretsym:=tsym(funcretsymderef.resolve);
  5457. end
  5458. else
  5459. begin
  5460. { safety }
  5461. { Not safe! A unit may be reresolved after its interface has been
  5462. parsed but before its implementation has been parsed, and in that
  5463. case the funcretsym is still required!
  5464. funcretsym:=nil; }
  5465. end;
  5466. end;
  5467. function tprocdef.GetTypeName : string;
  5468. begin
  5469. GetTypeName := FullProcName(false);
  5470. end;
  5471. function tprocdef.mangledname : TSymStr;
  5472. begin
  5473. {$ifdef symansistr}
  5474. if _mangledname='' then
  5475. begin
  5476. result:=defaultmangledname;
  5477. _mangledname:=result;
  5478. end
  5479. else
  5480. result:=_mangledname;
  5481. {$else symansistr}
  5482. if not assigned(_mangledname) then
  5483. begin
  5484. result:=defaultmangledname;
  5485. _mangledname:=stringdup(mangledname);
  5486. end
  5487. else
  5488. result:=_mangledname^;
  5489. {$endif symansistr}
  5490. end;
  5491. function tprocdef.defaultmangledname: TSymStr;
  5492. begin
  5493. { we need to use the symtable where the procsym is inserted,
  5494. because that is visible to the world }
  5495. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  5496. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  5497. end;
  5498. function tprocdef.cplusplusmangledname : TSymStr;
  5499. function getcppparaname(p : tdef) : TSymStr;
  5500. const
  5501. {$ifdef NAMEMANGLING_GCC2}
  5502. ordtype2str : array[tordtype] of string[2] = (
  5503. '',
  5504. 'Uc','Us','Ui','Us','',
  5505. 'Sc','s','i','x','',
  5506. 'b','b','b','b','b','b',
  5507. 'c','w','x');
  5508. {$else NAMEMANGLING_GCC2}
  5509. ordtype2str : array[tordtype] of string[1] = (
  5510. 'v',
  5511. 'h','t','j','y','',
  5512. 'a','s','i','x','',
  5513. 'b','b','b','b','b',
  5514. 'b','b','b','b',
  5515. 'c','w','x');
  5516. floattype2str : array[tfloattype] of string[1] = (
  5517. 'f','d','e','e',
  5518. 'd','d','g');
  5519. {$endif NAMEMANGLING_GCC2}
  5520. var
  5521. s : TSymStr;
  5522. begin
  5523. case p.typ of
  5524. orddef:
  5525. s:=ordtype2str[torddef(p).ordtype];
  5526. pointerdef:
  5527. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  5528. {$ifndef NAMEMANGLING_GCC2}
  5529. floatdef:
  5530. s:=floattype2str[tfloatdef(p).floattype];
  5531. {$endif NAMEMANGLING_GCC2}
  5532. else
  5533. internalerror(2103001);
  5534. end;
  5535. getcppparaname:=s;
  5536. end;
  5537. var
  5538. s,s2 : TSymStr;
  5539. hp : TParavarsym;
  5540. i : integer;
  5541. begin
  5542. {$ifdef NAMEMANGLING_GCC2}
  5543. { outdated gcc 2.x name mangling scheme }
  5544. s := procsym.realname;
  5545. if procsym.owner.symtabletype=ObjectSymtable then
  5546. begin
  5547. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  5548. case proctypeoption of
  5549. potype_destructor:
  5550. s:='_$_'+tostr(length(s2))+s2;
  5551. potype_constructor:
  5552. s:='___'+tostr(length(s2))+s2;
  5553. else
  5554. s:='_'+s+'__'+tostr(length(s2))+s2;
  5555. end;
  5556. end
  5557. else s:=s+'__';
  5558. s:=s+'F';
  5559. { concat modifiers }
  5560. { !!!!! }
  5561. { now we handle the parameters }
  5562. if maxparacount>0 then
  5563. begin
  5564. for i:=0 to paras.count-1 do
  5565. begin
  5566. hp:=tparavarsym(paras[i]);
  5567. { no hidden parameters form part of a C++ mangled name:
  5568. a) self is not included
  5569. b) there are no "high" or other hidden parameters
  5570. }
  5571. if vo_is_hidden_para in hp.varoptions then
  5572. continue;
  5573. s2:=getcppparaname(hp.vardef);
  5574. if hp.varspez in [vs_var,vs_out] then
  5575. s2:='R'+s2;
  5576. s:=s+s2;
  5577. end;
  5578. end
  5579. else
  5580. s:=s+'v';
  5581. cplusplusmangledname:=s;
  5582. {$else NAMEMANGLING_GCC2}
  5583. { gcc 3.x and 4.x name mangling scheme }
  5584. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  5585. if procsym.owner.symtabletype=ObjectSymtable then
  5586. begin
  5587. s:='_ZN';
  5588. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  5589. s:=s+tostr(length(s2))+s2;
  5590. case proctypeoption of
  5591. potype_constructor:
  5592. s:=s+'C1';
  5593. potype_destructor:
  5594. s:=s+'D1';
  5595. else
  5596. s:=s+tostr(length(procsym.realname))+procsym.realname;
  5597. end;
  5598. s:=s+'E';
  5599. end
  5600. else
  5601. s:=procsym.realname;
  5602. { now we handle the parameters }
  5603. if maxparacount>0 then
  5604. begin
  5605. for i:=0 to paras.count-1 do
  5606. begin
  5607. hp:=tparavarsym(paras[i]);
  5608. { no hidden parameters form part of a C++ mangled name:
  5609. a) self is not included
  5610. b) there are no "high" or other hidden parameters
  5611. }
  5612. if vo_is_hidden_para in hp.varoptions then
  5613. continue;
  5614. s2:=getcppparaname(hp.vardef);
  5615. if hp.varspez in [vs_var,vs_out] then
  5616. s2:='R'+s2;
  5617. s:=s+s2;
  5618. end;
  5619. end
  5620. else
  5621. s:=s+'v';
  5622. cplusplusmangledname:=s;
  5623. {$endif NAMEMANGLING_GCC2}
  5624. end;
  5625. function tprocdef.objcmangledname : TSymStr;
  5626. var
  5627. manglednamelen: longint;
  5628. iscatmethod : boolean;
  5629. begin
  5630. if not (po_msgstr in procoptions) then
  5631. internalerror(2009030901);
  5632. { we may very well need longer strings to handle these... }
  5633. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5634. length('+"[ ]"')+length(messageinf.str^);
  5635. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5636. if (iscatmethod) then
  5637. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5638. if manglednamelen>255 then
  5639. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5640. if not(po_classmethod in procoptions) then
  5641. result:='"-['
  5642. else
  5643. result:='"+[';
  5644. { quotes are necessary because the +/- otherwise confuse the assembler
  5645. into expecting a number
  5646. }
  5647. if iscatmethod then
  5648. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5649. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5650. if iscatmethod then
  5651. result:=result+')';
  5652. result:=result+' '+messageinf.str^+']"';
  5653. end;
  5654. procedure tprocdef.setmangledname(const s : TSymStr);
  5655. begin
  5656. { This is not allowed anymore, the forward declaration
  5657. already needs to create the correct mangledname, no changes
  5658. afterwards are allowed (PFV) }
  5659. { Exception: interface definitions in mode macpas, since in that }
  5660. { case no reference to the old name can exist yet (JM) }
  5661. {$ifdef symansistr}
  5662. if _mangledname<>'' then
  5663. if ((m_mac in current_settings.modeswitches) and
  5664. (interfacedef)) then
  5665. _mangledname:=''
  5666. else
  5667. internalerror(200411171);
  5668. {$else symansistr}
  5669. if assigned(_mangledname) then
  5670. if ((m_mac in current_settings.modeswitches) and
  5671. (interfacedef)) then
  5672. stringdispose(_mangledname)
  5673. else
  5674. internalerror(200411171);
  5675. {$endif symansistr}
  5676. {$ifdef jvm}
  5677. { this routine can be called for compilerproces. can't set mangled
  5678. name since it must be calculated, but it uses import_name when set
  5679. -> set that one }
  5680. import_name:=stringdup(s);
  5681. include(procoptions,po_has_importname);
  5682. include(procoptions,po_has_mangledname);
  5683. {$else}
  5684. {$ifdef symansistr}
  5685. _mangledname:=s;
  5686. {$else symansistr}
  5687. _mangledname:=stringdup(s);
  5688. {$endif symansistr}
  5689. {$endif jvm}
  5690. include(procoptions,po_has_mangledname);
  5691. end;
  5692. {***************************************************************************
  5693. TPROCVARDEF
  5694. ***************************************************************************}
  5695. constructor tprocvardef.create(level:byte);
  5696. begin
  5697. inherited create(procvardef,level,true);
  5698. end;
  5699. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef): tprocvardef;
  5700. var
  5701. res: PHashSetItem;
  5702. oldsymtablestack: tsymtablestack;
  5703. begin
  5704. if not assigned(current_module) then
  5705. internalerror(2011081301);
  5706. res:=current_module.procaddrdefs.FindOrAdd(@def,sizeof(def));
  5707. if not assigned(res^.Data) then
  5708. begin
  5709. { since these pointerdefs can be reused anywhere in the current
  5710. unit, add them to the global/staticsymtable (or local symtable
  5711. if they're a local def, because otherwise they'll be saved
  5712. to the ppu referencing a local symtable entry that doesn't
  5713. exist in the ppu) }
  5714. oldsymtablestack:=symtablestack;
  5715. { do not simply push/pop current_module.localsymtable, because
  5716. that can have side-effects (e.g., it removes helpers) }
  5717. symtablestack:=nil;
  5718. result:=tprocvardef(def.getcopyas(procvardef,pc_address_only));
  5719. setup_reusable_def(def,result,res,oldsymtablestack);
  5720. { res^.Data may still be nil -> don't overwrite result }
  5721. exit;
  5722. end;
  5723. result:=tprocvardef(res^.Data);
  5724. end;
  5725. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  5726. begin
  5727. result:=getreusableprocaddr(def);
  5728. if not result.is_registered then
  5729. include(result.defoptions,df_not_registered_no_free);
  5730. end;
  5731. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5732. begin
  5733. inherited ppuload(procvardef,ppufile);
  5734. { load para symtable }
  5735. parast:=tparasymtable.create(self,ppufile.getbyte);
  5736. ppuload_platform(ppufile);
  5737. tparasymtable(parast).ppuload(ppufile);
  5738. end;
  5739. function tprocvardef.getcopy : tstoreddef;
  5740. var
  5741. i : tcallercallee;
  5742. j : longint;
  5743. begin
  5744. result:=cprocvardef.create(parast.symtablelevel);
  5745. tprocvardef(result).returndef:=returndef;
  5746. tprocvardef(result).returndefderef:=returndefderef;
  5747. tprocvardef(result).parast:=parast.getcopy;
  5748. tprocvardef(result).savesize:=savesize;
  5749. { create paralist copy }
  5750. calcparas;
  5751. tprocvardef(result).paras:=tparalist.create(false);
  5752. tprocvardef(result).paras.count:=paras.count;
  5753. for j:=0 to paras.count-1 do
  5754. tprocvardef(result).paras[j]:=paras[j];
  5755. tprocvardef(result).proctypeoption:=proctypeoption;
  5756. tprocvardef(result).proccalloption:=proccalloption;
  5757. tprocvardef(result).procoptions:=procoptions;
  5758. tprocvardef(result).callerargareasize:=callerargareasize;
  5759. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5760. tprocvardef(result).maxparacount:=maxparacount;
  5761. tprocvardef(result).minparacount:=minparacount;
  5762. for i:=low(funcretloc) to high(funcretloc) do
  5763. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5764. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5765. {$ifdef m68k}
  5766. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5767. {$endif}
  5768. end;
  5769. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5770. begin
  5771. inherited ppuwrite(ppufile);
  5772. { Save the para symtable level (necessary to distinguish nested
  5773. procvars) }
  5774. ppufile.putbyte(parast.symtablelevel);
  5775. { Write this entry }
  5776. writeentry(ppufile,ibprocvardef);
  5777. { Save the para symtable, this is taken from the interface }
  5778. tparasymtable(parast).ppuwrite(ppufile);
  5779. end;
  5780. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5781. begin
  5782. case t of
  5783. gs_para :
  5784. GetSymtable:=parast;
  5785. else
  5786. GetSymtable:=nil;
  5787. end;
  5788. end;
  5789. function tprocvardef.size : asizeint;
  5790. begin
  5791. { we return false for is_addressonly for a block (because it's not a
  5792. simple pointer to a function), but they are handled as implicit
  5793. pointers to a datastructure that contains everything ->
  5794. voidpointertype.size instead of voidcodepointertype.size }
  5795. if po_is_block in procoptions then
  5796. size:=voidpointertype.size
  5797. else if not is_addressonly then
  5798. begin
  5799. if is_nested_pd(self) then
  5800. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5801. else
  5802. size:=voidcodepointertype.size+voidpointertype.size;
  5803. end
  5804. else
  5805. size:=voidcodepointertype.size;
  5806. end;
  5807. function tprocvardef.is_methodpointer:boolean;
  5808. begin
  5809. result:=(po_methodpointer in procoptions);
  5810. end;
  5811. function tprocvardef.is_addressonly:boolean;
  5812. begin
  5813. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  5814. not(po_is_block in procoptions) and
  5815. not is_nested_pd(self)) or
  5816. (po_addressonly in procoptions);
  5817. end;
  5818. function tprocvardef.getmangledparaname:TSymStr;
  5819. begin
  5820. if not(po_methodpointer in procoptions) then
  5821. if not is_nested_pd(self) then
  5822. result:='procvar'
  5823. else
  5824. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  5825. having not a type name or not an unique one, see webtbs/tw27515.pp
  5826. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  5827. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  5828. else
  5829. result:='procvarofobj'
  5830. end;
  5831. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  5832. begin
  5833. result:=inherited;
  5834. tabstractprocdef(result).calcparas;
  5835. end;
  5836. function tprocvardef.is_publishable : boolean;
  5837. begin
  5838. is_publishable:=(po_methodpointer in procoptions);
  5839. end;
  5840. function tprocvardef.GetTypeName : string;
  5841. var
  5842. s: string;
  5843. pno : tprocnameoptions;
  5844. begin
  5845. {$ifdef EXTDEBUG}
  5846. pno:=[pno_showhidden];
  5847. {$else EXTDEBUG}
  5848. pno:=[];
  5849. {$endif EXTDEBUG}
  5850. s:='<';
  5851. if po_is_block in procoptions then
  5852. s := s+'reference to'
  5853. else if po_classmethod in procoptions then
  5854. s := s+'class method type of'
  5855. else
  5856. if po_addressonly in procoptions then
  5857. s := s+'address of'
  5858. else
  5859. s := s+'procedure variable type of';
  5860. if assigned(returndef) and
  5861. (returndef<>voidtype) then
  5862. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5863. else
  5864. s:=s+' procedure'+typename_paras(pno);
  5865. if po_methodpointer in procoptions then
  5866. s := s+' of object';
  5867. if is_nested_pd(self) then
  5868. s := s+' is nested';
  5869. if po_far in procoptions then
  5870. s := s+';far';
  5871. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5872. end;
  5873. {***************************************************************************
  5874. TOBJECTDEF
  5875. ***************************************************************************}
  5876. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  5877. begin
  5878. inherited create(n,objectdef,doregister);
  5879. fcurrent_dispid:=0;
  5880. objecttype:=ot;
  5881. childof:=nil;
  5882. if objecttype=odt_helper then
  5883. owner.includeoption(sto_has_helper);
  5884. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  5885. current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  5886. { create space for vmt !! }
  5887. vmtentries:=TFPList.Create;
  5888. set_parent(c);
  5889. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5890. prepareguid;
  5891. { setup implemented interfaces }
  5892. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5893. ImplementedInterfaces:=TFPObjectList.Create(true)
  5894. else
  5895. ImplementedInterfaces:=nil;
  5896. writing_class_record_dbginfo:=false;
  5897. end;
  5898. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5899. var
  5900. i,
  5901. implintfcount : longint;
  5902. d, getterd : tderef;
  5903. ImplIntf : TImplementedInterface;
  5904. vmtentry : pvmtentry;
  5905. begin
  5906. inherited ppuload(objectdef,ppufile);
  5907. objecttype:=tobjecttyp(ppufile.getbyte);
  5908. helpertype:=thelpertype(ppufile.getbyte);
  5909. objextname:=ppufile.getpshortstring;
  5910. { only used for external Objective-C classes/protocols }
  5911. if (objextname^='') then
  5912. stringdispose(objextname);
  5913. symtable:=tObjectSymtable.create(self,objrealname^,0,0,0);
  5914. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5915. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5916. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5917. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5918. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  5919. ppufile.getderef(vmt_fieldderef);
  5920. ppufile.getderef(childofderef);
  5921. { load guid }
  5922. iidstr:=nil;
  5923. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5924. begin
  5925. new(iidguid);
  5926. ppufile.getguid(iidguid^);
  5927. iidstr:=ppufile.getpshortstring;
  5928. end;
  5929. abstractcnt:=ppufile.getlongint;
  5930. if objecttype=odt_helper then
  5931. ppufile.getderef(extendeddefderef);
  5932. vmtentries:=TFPList.Create;
  5933. vmtentries.count:=ppufile.getlongint;
  5934. for i:=0 to vmtentries.count-1 do
  5935. begin
  5936. ppufile.getderef(d);
  5937. new(vmtentry);
  5938. vmtentry^.procdef:=nil;
  5939. vmtentry^.procdefderef:=d;
  5940. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5941. vmtentries[i]:=vmtentry;
  5942. end;
  5943. { load implemented interfaces }
  5944. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5945. begin
  5946. ImplementedInterfaces:=TFPObjectList.Create(true);
  5947. implintfcount:=ppufile.getlongint;
  5948. for i:=0 to implintfcount-1 do
  5949. begin
  5950. ppufile.getderef(d);
  5951. ppufile.getderef(getterd);
  5952. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5953. ImplIntf.IOffset:=ppufile.getlongint;
  5954. byte(ImplIntf.IType):=ppufile.getbyte;
  5955. ImplementedInterfaces.Add(ImplIntf);
  5956. end;
  5957. end
  5958. else
  5959. ImplementedInterfaces:=nil;
  5960. if df_copied_def in defoptions then
  5961. begin
  5962. ppufile.getderef(cloneddefderef);
  5963. ppuload_platform(ppufile);
  5964. end
  5965. else
  5966. begin
  5967. ppuload_platform(ppufile);
  5968. tObjectSymtable(symtable).ppuload(ppufile);
  5969. end;
  5970. { handles the predefined class tobject }
  5971. { the last TOBJECT which is loaded gets }
  5972. { it ! }
  5973. if (childof=nil) and
  5974. (objecttype in [odt_class,odt_javaclass]) and
  5975. (objname^='TOBJECT') then
  5976. class_tobject:=self;
  5977. if (childof=nil) and
  5978. (objecttype=odt_interfacecom) then
  5979. if (objname^='IUNKNOWN') then
  5980. interface_iunknown:=self
  5981. else
  5982. if (objname^='IDISPATCH') then
  5983. interface_idispatch:=self;
  5984. if (childof=nil) and
  5985. (objecttype=odt_objcclass) and
  5986. (objname^='PROTOCOL') then
  5987. objc_protocoltype:=self;
  5988. if (objecttype=odt_javaclass) and
  5989. not(oo_is_formal in objectoptions) then
  5990. begin
  5991. if (objname^='JLOBJECT') then
  5992. java_jlobject:=self
  5993. else if (objname^='JLTHROWABLE') then
  5994. java_jlthrowable:=self
  5995. else if (objname^='FPCBASERECORDTYPE') then
  5996. java_fpcbaserecordtype:=self
  5997. else if (objname^='JLSTRING') then
  5998. java_jlstring:=self
  5999. else if (objname^='ANSISTRINGCLASS') then
  6000. java_ansistring:=self
  6001. else if (objname^='SHORTSTRINGCLASS') then
  6002. java_shortstring:=self
  6003. else if (objname^='JLENUM') then
  6004. java_jlenum:=self
  6005. else if (objname^='JUENUMSET') then
  6006. java_juenumset:=self
  6007. else if (objname^='FPCBITSET') then
  6008. java_jubitset:=self
  6009. else if (objname^='FPCBASEPROCVARTYPE') then
  6010. java_procvarbase:=self;
  6011. end;
  6012. writing_class_record_dbginfo:=false;
  6013. end;
  6014. destructor tobjectdef.destroy;
  6015. begin
  6016. if assigned(symtable) then
  6017. begin
  6018. symtable.free;
  6019. symtable:=nil;
  6020. end;
  6021. stringdispose(objextname);
  6022. stringdispose(iidstr);
  6023. if assigned(ImplementedInterfaces) then
  6024. begin
  6025. ImplementedInterfaces.free;
  6026. ImplementedInterfaces:=nil;
  6027. end;
  6028. if assigned(iidguid) then
  6029. begin
  6030. dispose(iidguid);
  6031. iidguid:=nil;
  6032. end;
  6033. if assigned(vmtentries) then
  6034. begin
  6035. resetvmtentries;
  6036. vmtentries.free;
  6037. vmtentries:=nil;
  6038. end;
  6039. if assigned(vmcallstaticinfo) then
  6040. begin
  6041. freemem(vmcallstaticinfo);
  6042. vmcallstaticinfo:=nil;
  6043. end;
  6044. inherited destroy;
  6045. end;
  6046. function tobjectdef.getcopy : tstoreddef;
  6047. var
  6048. i : longint;
  6049. begin
  6050. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  6051. { the constructor allocates a symtable which we release to avoid memory leaks }
  6052. tobjectdef(result).symtable.free;
  6053. tobjectdef(result).symtable:=symtable.getcopy;
  6054. if assigned(objextname) then
  6055. tobjectdef(result).objextname:=stringdup(objextname^);
  6056. if assigned(import_lib) then
  6057. tobjectdef(result).import_lib:=stringdup(import_lib^);
  6058. tobjectdef(result).objectoptions:=objectoptions;
  6059. include(tobjectdef(result).defoptions,df_copied_def);
  6060. tobjectdef(result).extendeddef:=extendeddef;
  6061. if assigned(tcinitcode) then
  6062. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  6063. tobjectdef(result).vmt_field:=vmt_field;
  6064. if assigned(iidguid) then
  6065. begin
  6066. new(tobjectdef(result).iidguid);
  6067. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  6068. end;
  6069. if assigned(iidstr) then
  6070. tobjectdef(result).iidstr:=stringdup(iidstr^);
  6071. tobjectdef(result).abstractcnt:=abstractcnt;
  6072. if assigned(ImplementedInterfaces) then
  6073. begin
  6074. for i:=0 to ImplementedInterfaces.count-1 do
  6075. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  6076. end;
  6077. if assigned(vmtentries) then
  6078. begin
  6079. tobjectdef(result).vmtentries:=TFPList.Create;
  6080. tobjectdef(result).copyvmtentries(self);
  6081. end;
  6082. end;
  6083. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  6084. var
  6085. i : longint;
  6086. vmtentry : pvmtentry;
  6087. ImplIntf : TImplementedInterface;
  6088. old_do_indirect_crc: boolean;
  6089. begin
  6090. { if class1 in unit A changes, and class2 in unit B inherits from it
  6091. (so unit B uses unit A), then unit B with class2 will be recompiled.
  6092. However, if there is also a class3 in unit C that only depends on
  6093. unit B, then unit C will not be recompiled because nothing changed
  6094. to the interface of unit B. Nevertheless, unit C can indirectly
  6095. depend on unit A via derefs, and these must be updated -> the
  6096. indirect crc keeps track of such changes. }
  6097. old_do_indirect_crc:=ppufile.do_indirect_crc;
  6098. ppufile.do_indirect_crc:=true;
  6099. inherited ppuwrite(ppufile);
  6100. ppufile.putbyte(byte(objecttype));
  6101. ppufile.putbyte(byte(helpertype));
  6102. if assigned(objextname) then
  6103. ppufile.putstring(objextname^)
  6104. else
  6105. ppufile.putstring('');
  6106. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  6107. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  6108. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  6109. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  6110. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  6111. ppufile.putderef(vmt_fieldderef);
  6112. ppufile.putderef(childofderef);
  6113. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6114. begin
  6115. ppufile.putguid(iidguid^);
  6116. ppufile.putstring(iidstr^);
  6117. end;
  6118. ppufile.putlongint(abstractcnt);
  6119. if objecttype=odt_helper then
  6120. ppufile.putderef(extendeddefderef);
  6121. ppufile.putlongint(vmtentries.count);
  6122. for i:=0 to vmtentries.count-1 do
  6123. begin
  6124. vmtentry:=pvmtentry(vmtentries[i]);
  6125. ppufile.putderef(vmtentry^.procdefderef);
  6126. ppufile.putbyte(byte(vmtentry^.visibility));
  6127. end;
  6128. if assigned(ImplementedInterfaces) then
  6129. begin
  6130. ppufile.putlongint(ImplementedInterfaces.Count);
  6131. for i:=0 to ImplementedInterfaces.Count-1 do
  6132. begin
  6133. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  6134. ppufile.putderef(ImplIntf.intfdefderef);
  6135. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  6136. ppufile.putlongint(ImplIntf.Ioffset);
  6137. ppufile.putbyte(byte(ImplIntf.IType));
  6138. end;
  6139. end;
  6140. if df_copied_def in defoptions then
  6141. ppufile.putderef(cloneddefderef);
  6142. writeentry(ppufile,ibobjectdef);
  6143. if not(df_copied_def in defoptions) then
  6144. tObjectSymtable(symtable).ppuwrite(ppufile);
  6145. ppufile.do_indirect_crc:=old_do_indirect_crc;
  6146. end;
  6147. function tobjectdef.GetTypeName:string;
  6148. begin
  6149. { in this case we will go in endless recursion, because then }
  6150. { there is no tsym associated yet with the def. It can occur }
  6151. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  6152. { instead of the actual type name }
  6153. if not assigned(typesym) then
  6154. result:='<Currently Parsed Class>'
  6155. else
  6156. result:=typesymbolprettyname;
  6157. end;
  6158. procedure tobjectdef.buildderef;
  6159. var
  6160. i : longint;
  6161. vmtentry : pvmtentry;
  6162. begin
  6163. inherited buildderef;
  6164. vmt_fieldderef.build(vmt_field);
  6165. childofderef.build(childof);
  6166. if df_copied_def in defoptions then
  6167. cloneddefderef.build(symtable.defowner)
  6168. else
  6169. tstoredsymtable(symtable).buildderef;
  6170. if objecttype=odt_helper then
  6171. extendeddefderef.build(extendeddef);
  6172. for i:=0 to vmtentries.count-1 do
  6173. begin
  6174. vmtentry:=pvmtentry(vmtentries[i]);
  6175. vmtentry^.procdefderef.build(vmtentry^.procdef);
  6176. end;
  6177. if assigned(ImplementedInterfaces) then
  6178. begin
  6179. for i:=0 to ImplementedInterfaces.count-1 do
  6180. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  6181. end;
  6182. end;
  6183. procedure tobjectdef.deref;
  6184. var
  6185. i : longint;
  6186. vmtentry : pvmtentry;
  6187. begin
  6188. inherited deref;
  6189. vmt_field:=tsym(vmt_fieldderef.resolve);
  6190. childof:=tobjectdef(childofderef.resolve);
  6191. if df_copied_def in defoptions then
  6192. begin
  6193. cloneddef:=tobjectdef(cloneddefderef.resolve);
  6194. symtable:=cloneddef.symtable.getcopy;
  6195. end
  6196. else
  6197. tstoredsymtable(symtable).deref(false);
  6198. if objecttype=odt_helper then
  6199. extendeddef:=tdef(extendeddefderef.resolve);
  6200. for i:=0 to vmtentries.count-1 do
  6201. begin
  6202. vmtentry:=pvmtentry(vmtentries[i]);
  6203. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  6204. end;
  6205. if assigned(ImplementedInterfaces) then
  6206. begin
  6207. for i:=0 to ImplementedInterfaces.count-1 do
  6208. TImplementedInterface(ImplementedInterfaces[i]).deref;
  6209. end;
  6210. end;
  6211. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  6212. var
  6213. pd: tprocdef absolute def;
  6214. st: tsymtable;
  6215. psym: tsym;
  6216. nname: TIDString;
  6217. begin
  6218. if (tdef(def).typ<>procdef) then
  6219. exit;
  6220. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  6221. owner = symtable in which objcclassdef is defined
  6222. }
  6223. st:=pd.owner.defowner.owner;
  6224. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  6225. { check for an existing procsym with our special name }
  6226. psym:=tsym(st.find(nname));
  6227. if not assigned(psym) then
  6228. begin
  6229. psym:=cprocsym.create(nname);
  6230. { avoid warning about this symbol being unused }
  6231. psym.IncRefCount;
  6232. { don't check for duplicates:
  6233. a) we checked above
  6234. b) in case we are in the implementation section of a unit, this
  6235. will also check for this symbol in the interface section
  6236. (since you normally cannot have symbols with the same name
  6237. both interface and implementation), and it's possible to
  6238. have class helpers for the same class in the interface and
  6239. in the implementation, and they cannot be merged since only
  6240. the once in the interface must be saved to the ppu/visible
  6241. from other units }
  6242. st.insert(psym,false);
  6243. end
  6244. else if (psym.typ<>procsym) then
  6245. internalerror(2009111501);
  6246. { add ourselves to this special procsym }
  6247. tprocsym(psym).procdeflist.add(def);
  6248. end;
  6249. procedure tobjectdef.derefimpl;
  6250. begin
  6251. inherited derefimpl;
  6252. { the procdefs are not owned by the class helper procsyms, so they
  6253. are not stored/restored either -> re-add them here }
  6254. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  6255. (oo_is_classhelper in objectoptions) then
  6256. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  6257. end;
  6258. procedure tobjectdef.resetvmtentries;
  6259. var
  6260. i : longint;
  6261. begin
  6262. for i:=0 to vmtentries.Count-1 do
  6263. Dispose(pvmtentry(vmtentries[i]));
  6264. vmtentries.clear;
  6265. end;
  6266. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  6267. var
  6268. i : longint;
  6269. vmtentry : pvmtentry;
  6270. begin
  6271. resetvmtentries;
  6272. vmtentries.count:=objdef.vmtentries.count;
  6273. for i:=0 to objdef.vmtentries.count-1 do
  6274. begin
  6275. new(vmtentry);
  6276. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  6277. vmtentries[i]:=vmtentry;
  6278. end;
  6279. end;
  6280. function tobjectdef.getparentdef:tdef;
  6281. begin
  6282. { TODO: Remove getparentdef hack}
  6283. { With 2 forward declared classes with the child class before the
  6284. parent class the child class is written earlier to the ppu. Leaving it
  6285. possible to have a reference to the parent class for property overriding,
  6286. but the parent class still has the childof not resolved yet (PFV) }
  6287. if childof=nil then
  6288. childof:=tobjectdef(childofderef.resolve);
  6289. result:=childof;
  6290. end;
  6291. procedure tobjectdef.prepareguid;
  6292. begin
  6293. { set up guid }
  6294. if not assigned(iidguid) then
  6295. begin
  6296. new(iidguid);
  6297. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  6298. end;
  6299. { setup iidstring }
  6300. if not assigned(iidstr) then
  6301. iidstr:=stringdup(''); { default is empty string }
  6302. end;
  6303. procedure tobjectdef.set_parent( c : tobjectdef);
  6304. begin
  6305. if assigned(childof) then
  6306. exit;
  6307. childof:=c;
  6308. if not assigned(c) then
  6309. exit;
  6310. { inherit options and status }
  6311. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  6312. { initially has the same number of abstract methods as the parent }
  6313. abstractcnt:=c.abstractcnt;
  6314. { add the data of the anchestor class/object }
  6315. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  6316. begin
  6317. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  6318. { inherit recordalignment }
  6319. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  6320. { if both the parent and this record use C-alignment, also inherit
  6321. the current field alignment }
  6322. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  6323. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  6324. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  6325. { the padding is not inherited for Objective-C classes (maybe not
  6326. for cppclass either?) }
  6327. if objecttype=odt_objcclass then
  6328. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  6329. if (oo_has_vmt in objectoptions) and
  6330. (oo_has_vmt in c.objectoptions) then
  6331. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  6332. { if parent has a vmt field then the offset is the same for the child PM }
  6333. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  6334. begin
  6335. vmt_field:=c.vmt_field;
  6336. include(objectoptions,oo_has_vmt);
  6337. end;
  6338. end;
  6339. end;
  6340. procedure tobjectdef.insertvmt;
  6341. begin
  6342. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6343. exit;
  6344. if (oo_has_vmt in objectoptions) then
  6345. internalerror(12345)
  6346. else
  6347. begin
  6348. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  6349. tObjectSymtable(symtable).fieldalignment);
  6350. if (tf_requires_proper_alignment in target_info.flags) then
  6351. begin
  6352. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  6353. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  6354. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  6355. end;
  6356. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[],true);
  6357. hidesym(vmt_field);
  6358. tObjectSymtable(symtable).insert(vmt_field);
  6359. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  6360. include(objectoptions,oo_has_vmt);
  6361. end;
  6362. end;
  6363. function tobjectdef.vmt_offset: asizeint;
  6364. begin
  6365. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6366. result:=0
  6367. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  6368. result:=tfieldvarsym(vmt_field).fieldoffset
  6369. else
  6370. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  6371. end;
  6372. procedure tobjectdef.check_forwards;
  6373. begin
  6374. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  6375. inherited;
  6376. if (oo_is_forward in objectoptions) then
  6377. begin
  6378. { ok, in future, the forward can be resolved }
  6379. Message1(sym_e_class_forward_not_resolved,objrealname^);
  6380. exclude(objectoptions,oo_is_forward);
  6381. end;
  6382. end;
  6383. function tobjectdef.find_destructor: tprocdef;
  6384. var
  6385. objdef: tobjectdef;
  6386. begin
  6387. objdef:=self;
  6388. while assigned(objdef) do
  6389. begin
  6390. result:=objdef.find_procdef_bytype(potype_destructor);
  6391. if assigned(result) then
  6392. exit;
  6393. objdef:=objdef.childof;
  6394. end;
  6395. result:=nil;
  6396. end;
  6397. function tobjectdef.implements_any_interfaces: boolean;
  6398. begin
  6399. result := (ImplementedInterfaces.Count > 0) or
  6400. (assigned(childof) and childof.implements_any_interfaces);
  6401. end;
  6402. function tobjectdef.size : asizeint;
  6403. begin
  6404. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6405. result:=voidpointertype.size
  6406. else
  6407. result:=tObjectSymtable(symtable).datasize;
  6408. end;
  6409. function tobjectdef.alignment:shortint;
  6410. begin
  6411. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6412. alignment:=voidpointertype.alignment
  6413. else
  6414. alignment:=tObjectSymtable(symtable).recordalignment;
  6415. end;
  6416. function tobjectdef.vmtmethodoffset(index:longint):longint;
  6417. begin
  6418. { for offset of methods for classes, see rtl/inc/objpash.inc }
  6419. case objecttype of
  6420. odt_class:
  6421. { the +2*sizeof(pint) is size and -size }
  6422. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  6423. odt_helper,
  6424. odt_objcclass,
  6425. odt_objcprotocol:
  6426. vmtmethodoffset:=0;
  6427. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  6428. vmtmethodoffset:=index*voidcodepointertype.size;
  6429. odt_javaclass,
  6430. odt_interfacejava:
  6431. { invalid }
  6432. vmtmethodoffset:=-1;
  6433. else
  6434. { the +2*sizeof(pint) is size and -size }
  6435. {$ifdef WITHDMT}
  6436. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  6437. {$else WITHDMT}
  6438. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  6439. {$endif WITHDMT}
  6440. end;
  6441. end;
  6442. function tobjectdef.vmt_mangledname : TSymStr;
  6443. begin
  6444. if not(oo_has_vmt in objectoptions) then
  6445. Message1(parser_n_object_has_no_vmt,objrealname^);
  6446. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  6447. end;
  6448. function tobjectdef.vmt_def: trecorddef;
  6449. var
  6450. vmttypesym: tsym;
  6451. begin
  6452. vmttypesym:=tsym(get_top_level_symtable.Find('vmtdef$'+mangledparaname));
  6453. if not assigned(vmttypesym) or
  6454. (vmttypesym.typ<>symconst.typesym) or
  6455. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  6456. internalerror(2015052501);
  6457. result:=trecorddef(ttypesym(vmttypesym).typedef);
  6458. end;
  6459. function tobjectdef.needs_inittable : boolean;
  6460. var
  6461. hp : tobjectdef;
  6462. begin
  6463. case objecttype of
  6464. odt_helper,
  6465. odt_class :
  6466. needs_inittable:=false;
  6467. odt_dispinterface,
  6468. odt_interfacecom:
  6469. needs_inittable:=true;
  6470. odt_interfacecorba:
  6471. begin
  6472. hp:=childof;
  6473. while assigned(hp) do
  6474. begin
  6475. if hp=interface_iunknown then
  6476. begin
  6477. needs_inittable:=true;
  6478. exit;
  6479. end;
  6480. hp:=hp.childof;
  6481. end;
  6482. needs_inittable:=false;
  6483. end;
  6484. odt_object:
  6485. needs_inittable:=
  6486. tObjectSymtable(symtable).needs_init_final or
  6487. (assigned(childof) and
  6488. childof.needs_inittable);
  6489. odt_cppclass,
  6490. odt_objcclass,
  6491. odt_objcprotocol,
  6492. odt_javaclass,
  6493. odt_interfacejava:
  6494. needs_inittable:=false;
  6495. else
  6496. internalerror(200108267);
  6497. end;
  6498. end;
  6499. function tobjectdef.needs_separate_initrtti : boolean;
  6500. begin
  6501. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6502. end;
  6503. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  6504. begin
  6505. if objecttype in [odt_class,odt_object] then
  6506. begin
  6507. result:=tobjectsymtable(symtable).has_non_trivial_init or
  6508. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  6509. end
  6510. else
  6511. result:=false;
  6512. end;
  6513. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  6514. begin
  6515. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  6516. result:=inherited rtti_mangledname(rt)
  6517. else
  6518. begin
  6519. { necessary in case of a dynamic array of nsobject, or
  6520. if an nsobject field appears in a record that needs
  6521. init/finalisation }
  6522. if rt=initrtti then
  6523. begin
  6524. result:=voidpointertype.rtti_mangledname(rt);
  6525. exit;
  6526. end;
  6527. if not(target_info.system in systems_objc_nfabi) then
  6528. begin
  6529. result:=target_asm.labelprefix;
  6530. case objecttype of
  6531. odt_objcclass:
  6532. begin
  6533. case rt of
  6534. objcclassrtti:
  6535. if not(oo_is_classhelper in objectoptions) then
  6536. result:=result+'_OBJC_CLASS_'
  6537. else
  6538. result:=result+'_OBJC_CATEGORY_';
  6539. objcmetartti:
  6540. if not(oo_is_classhelper in objectoptions) then
  6541. result:=result+'_OBJC_METACLASS_'
  6542. else
  6543. internalerror(2009111511);
  6544. else
  6545. internalerror(2009092302);
  6546. end;
  6547. end;
  6548. odt_objcprotocol:
  6549. result:=result+'_OBJC_PROTOCOL_';
  6550. end;
  6551. end
  6552. else
  6553. begin
  6554. case objecttype of
  6555. odt_objcclass:
  6556. begin
  6557. if (oo_is_classhelper in objectoptions) and
  6558. (rt<>objcclassrtti) then
  6559. internalerror(2009111512);
  6560. case rt of
  6561. objcclassrtti:
  6562. if not(oo_is_classhelper in objectoptions) then
  6563. result:='_OBJC_CLASS_$_'
  6564. else
  6565. result:='_OBJC_$_CATEGORY_';
  6566. objcmetartti:
  6567. result:='_OBJC_METACLASS_$_';
  6568. objcclassrortti:
  6569. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  6570. objcmetarortti:
  6571. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  6572. else
  6573. internalerror(2009092303);
  6574. end;
  6575. end;
  6576. odt_objcprotocol:
  6577. begin
  6578. result:=lower(target_asm.labelprefix);
  6579. case rt of
  6580. objcclassrtti:
  6581. result:=result+'_OBJC_PROTOCOL_$_';
  6582. objcmetartti:
  6583. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  6584. else
  6585. internalerror(2009092501);
  6586. end;
  6587. end;
  6588. else
  6589. internalerror(2013113005);
  6590. end;
  6591. end;
  6592. result:=result+objextname^;
  6593. end;
  6594. end;
  6595. function tobjectdef.members_need_inittable : boolean;
  6596. begin
  6597. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  6598. end;
  6599. function tobjectdef.is_publishable : boolean;
  6600. begin
  6601. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  6602. end;
  6603. function tobjectdef.get_next_dispid: longint;
  6604. begin
  6605. inc(fcurrent_dispid);
  6606. result:=fcurrent_dispid;
  6607. end;
  6608. function tobjectdef.search_enumerator_get: tprocdef;
  6609. begin
  6610. result:=inherited;
  6611. if not assigned(result) and assigned(childof) then
  6612. result:=childof.search_enumerator_get;
  6613. end;
  6614. function tobjectdef.search_enumerator_move: tprocdef;
  6615. begin
  6616. result:=inherited;
  6617. if not assigned(result) and assigned(childof) then
  6618. result:=childof.search_enumerator_move;
  6619. end;
  6620. function tobjectdef.search_enumerator_current: tsym;
  6621. begin
  6622. result:=inherited;
  6623. if not assigned(result) and assigned(childof) then
  6624. result:=childof.search_enumerator_current;
  6625. end;
  6626. procedure tobjectdef.register_created_classref_type;
  6627. begin
  6628. if not classref_created_in_current_module then
  6629. begin
  6630. classref_created_in_current_module:=true;
  6631. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  6632. end;
  6633. end;
  6634. procedure tobjectdef.register_created_object_type;
  6635. begin
  6636. if not created_in_current_module then
  6637. begin
  6638. created_in_current_module:=true;
  6639. current_module.wpoinfo.addcreatedobjtype(self);
  6640. end;
  6641. end;
  6642. procedure tobjectdef.register_maybe_created_object_type;
  6643. begin
  6644. { if we know it has been created for sure, no need
  6645. to also record that it maybe can be created in
  6646. this module
  6647. }
  6648. if not (created_in_current_module) and
  6649. not (maybe_created_in_current_module) then
  6650. begin
  6651. maybe_created_in_current_module:=true;
  6652. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  6653. end;
  6654. end;
  6655. procedure tobjectdef.register_vmt_call(index: longint);
  6656. begin
  6657. if (is_object(self) or is_class(self)) then
  6658. current_module.wpoinfo.addcalledvmtentry(self,index);
  6659. end;
  6660. procedure check_and_finish_msg(data: tobject; arg: pointer);
  6661. var
  6662. def: tdef absolute data;
  6663. pd: tprocdef absolute data;
  6664. i,
  6665. paracount: longint;
  6666. begin
  6667. if (def.typ=procdef) then
  6668. begin
  6669. { add all messages also under a dummy name to the symtable in
  6670. which the objcclass/protocol/category is declared, so they can
  6671. be called via id.<name>
  6672. }
  6673. create_class_helper_for_procdef(pd,nil);
  6674. { we have to wait until now to set the mangled name because it
  6675. depends on the (possibly external) class name, which is defined
  6676. at the very end. }
  6677. if not(po_msgstr in pd.procoptions) then
  6678. begin
  6679. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  6680. { recover to avoid internalerror later on }
  6681. include(pd.procoptions,po_msgstr);
  6682. pd.messageinf.str:=stringdup('MissingDeclaration');
  6683. end;
  6684. { Mangled name is already set in case this is a copy of
  6685. another type. }
  6686. if not(po_has_mangledname in pd.procoptions) then
  6687. begin
  6688. { check whether the number of formal parameters is correct,
  6689. and whether they have valid Objective-C types }
  6690. paracount:=0;
  6691. for i:=1 to length(pd.messageinf.str^) do
  6692. if pd.messageinf.str^[i]=':' then
  6693. inc(paracount);
  6694. for i:=0 to pd.paras.count-1 do
  6695. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  6696. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  6697. dec(paracount);
  6698. if (paracount<>0) then
  6699. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  6700. pd.setmangledname(pd.objcmangledname);
  6701. end
  6702. else
  6703. { all checks already done }
  6704. exit;
  6705. if not(oo_is_external in pd.struct.objectoptions) then
  6706. begin
  6707. if (po_varargs in pd.procoptions) then
  6708. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  6709. else
  6710. begin
  6711. { check for "array of const" parameters }
  6712. for i:=0 to pd.parast.symlist.count-1 do
  6713. begin
  6714. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6715. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6716. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6717. end;
  6718. end;
  6719. end;
  6720. end;
  6721. end;
  6722. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6723. var
  6724. sym: tsym absolute data;
  6725. begin
  6726. if (sym.typ=fieldvarsym) and
  6727. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6728. sym.IncRefCount;
  6729. end;
  6730. procedure tobjectdef.finish_objc_data;
  6731. begin
  6732. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6733. if (oo_is_external in objectoptions) then
  6734. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6735. end;
  6736. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6737. var
  6738. sym: tabstractvarsym absolute data;
  6739. res: pboolean absolute arg;
  6740. founderrordef: tdef;
  6741. begin
  6742. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6743. exit;
  6744. if (sym.typ=paravarsym) and
  6745. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6746. is_array_of_const(tparavarsym(sym).vardef)) then
  6747. exit;
  6748. if not objcchecktype(sym.vardef,founderrordef) then
  6749. begin
  6750. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6751. res^:=false;
  6752. end;
  6753. end;
  6754. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6755. var
  6756. def: tdef absolute data;
  6757. res: pboolean absolute arg;
  6758. founderrordef: tdef;
  6759. begin
  6760. if (def.typ<>procdef) then
  6761. exit;
  6762. { check parameter types for validity }
  6763. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6764. { check the result type for validity }
  6765. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6766. begin
  6767. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6768. res^:=false;
  6769. end;
  6770. end;
  6771. function tobjectdef.check_objc_types: boolean;
  6772. begin
  6773. { done in separate step from finish_objc_data, because when
  6774. finish_objc_data is called, not all forwarddefs have been resolved
  6775. yet and we need to know all types here }
  6776. result:=true;
  6777. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6778. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6779. end;
  6780. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6781. var
  6782. def: tdef absolute data;
  6783. pd: tprocdef absolute data;
  6784. begin
  6785. if (def.typ=procdef) then
  6786. begin
  6787. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6788. if (oo_is_external in pd.struct.objectoptions) then
  6789. begin
  6790. { copied from psub.read_proc }
  6791. if assigned(tobjectdef(pd.struct).import_lib) then
  6792. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6793. else
  6794. begin
  6795. { add import name to external list for DLL scanning }
  6796. if tf_has_dllscanner in target_info.flags then
  6797. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6798. end;
  6799. end;
  6800. end;
  6801. end;
  6802. procedure tobjectdef.finish_cpp_data;
  6803. begin
  6804. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6805. end;
  6806. {****************************************************************************
  6807. TImplementedInterface
  6808. ****************************************************************************}
  6809. function TImplementedInterface.GetIOffset: longint;
  6810. begin
  6811. if (fIOffset=-1) and
  6812. (IType in [etFieldValue,etFieldValueClass]) then
  6813. result:=tfieldvarsym(ImplementsField).fieldoffset
  6814. else
  6815. result:=fIOffset;
  6816. end;
  6817. constructor TImplementedInterface.create(aintf: tobjectdef);
  6818. begin
  6819. inherited create;
  6820. intfdef:=aintf;
  6821. IOffset:=-1;
  6822. IType:=etStandard;
  6823. NameMappings:=nil;
  6824. procdefs:=nil;
  6825. end;
  6826. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6827. begin
  6828. inherited create;
  6829. intfdef:=nil;
  6830. intfdefderef:=intfd;
  6831. ImplementsGetterDeref:=getterd;
  6832. IOffset:=-1;
  6833. IType:=etStandard;
  6834. NameMappings:=nil;
  6835. procdefs:=nil;
  6836. end;
  6837. destructor TImplementedInterface.destroy;
  6838. var
  6839. i : longint;
  6840. mappedname : pshortstring;
  6841. begin
  6842. if assigned(NameMappings) then
  6843. begin
  6844. for i:=0 to NameMappings.Count-1 do
  6845. begin
  6846. mappedname:=pshortstring(NameMappings[i]);
  6847. stringdispose(mappedname);
  6848. end;
  6849. NameMappings.free;
  6850. NameMappings:=nil;
  6851. end;
  6852. if assigned(procdefs) then
  6853. begin
  6854. procdefs.free;
  6855. procdefs:=nil;
  6856. end;
  6857. inherited destroy;
  6858. end;
  6859. procedure TImplementedInterface.buildderef;
  6860. begin
  6861. intfdefderef.build(intfdef);
  6862. ImplementsGetterDeref.build(ImplementsGetter);
  6863. end;
  6864. procedure TImplementedInterface.deref;
  6865. begin
  6866. intfdef:=tobjectdef(intfdefderef.resolve);
  6867. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6868. end;
  6869. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6870. begin
  6871. if not assigned(NameMappings) then
  6872. NameMappings:=TFPHashList.Create;
  6873. NameMappings.Add(origname,stringdup(newname));
  6874. end;
  6875. function TImplementedInterface.GetMapping(const origname: string):string;
  6876. var
  6877. mappedname : pshortstring;
  6878. begin
  6879. result:='';
  6880. if not assigned(NameMappings) then
  6881. exit;
  6882. mappedname:=PShortstring(NameMappings.Find(origname));
  6883. if assigned(mappedname) then
  6884. result:=mappedname^;
  6885. end;
  6886. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6887. begin
  6888. if not assigned(procdefs) then
  6889. procdefs:=TFPObjectList.Create(false);
  6890. { duplicate entries must be stored, because multiple }
  6891. { interfaces can declare methods with the same name }
  6892. { and all of these get their own VMT entry }
  6893. procdefs.Add(pd);
  6894. end;
  6895. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6896. var
  6897. i : longint;
  6898. begin
  6899. result:=false;
  6900. { interfaces being implemented through delegation are not mergable (FK) }
  6901. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6902. exit;
  6903. weight:=0;
  6904. { empty interface is mergeable }
  6905. if ProcDefs.Count=0 then
  6906. begin
  6907. result:=true;
  6908. exit;
  6909. end;
  6910. { The interface to merge must at least the number of
  6911. procedures of this interface }
  6912. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6913. exit;
  6914. for i:=0 to ProcDefs.Count-1 do
  6915. begin
  6916. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6917. exit;
  6918. end;
  6919. weight:=ProcDefs.Count;
  6920. result:=true;
  6921. end;
  6922. function TImplementedInterface.getcopy:TImplementedInterface;
  6923. begin
  6924. Result:=TImplementedInterface.Create(nil);
  6925. { 1) the procdefs list will be freed once for each copy
  6926. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6927. 3) idem for the name mappings
  6928. }
  6929. { warning: this is completely wrong on so many levels...
  6930. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6931. We need to make clean copies of the different fields
  6932. this is not implemented yet, and thus we generate an internal
  6933. error instead PM 2011-06-14 }
  6934. internalerror(2011061401);
  6935. end;
  6936. {****************************************************************************
  6937. TFORWARDDEF
  6938. ****************************************************************************}
  6939. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6940. begin
  6941. inherited create(forwarddef,true);
  6942. tosymname:=stringdup(s);
  6943. forwardpos:=pos;
  6944. end;
  6945. function tforwarddef.GetTypeName:string;
  6946. begin
  6947. GetTypeName:='unresolved forward to '+tosymname^;
  6948. end;
  6949. destructor tforwarddef.destroy;
  6950. begin
  6951. stringdispose(tosymname);
  6952. inherited destroy;
  6953. end;
  6954. function tforwarddef.getcopy:tstoreddef;
  6955. begin
  6956. result:=cforwarddef.create(tosymname^, forwardpos);
  6957. end;
  6958. {****************************************************************************
  6959. TUNDEFINEDDEF
  6960. ****************************************************************************}
  6961. constructor tundefineddef.create(doregister:boolean);
  6962. begin
  6963. inherited create(undefineddef,doregister);
  6964. end;
  6965. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6966. begin
  6967. inherited ppuload(undefineddef,ppufile);
  6968. ppuload_platform(ppufile);
  6969. end;
  6970. function tundefineddef.GetTypeName:string;
  6971. begin
  6972. GetTypeName:='<undefined type>';
  6973. end;
  6974. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6975. begin
  6976. inherited ppuwrite(ppufile);
  6977. writeentry(ppufile,ibundefineddef);
  6978. end;
  6979. {****************************************************************************
  6980. TERRORDEF
  6981. ****************************************************************************}
  6982. constructor terrordef.create;
  6983. begin
  6984. inherited create(errordef,true);
  6985. { prevent consecutive faults }
  6986. savesize:=1;
  6987. end;
  6988. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6989. begin
  6990. { Can't write errordefs to ppu }
  6991. internalerror(200411063);
  6992. end;
  6993. function terrordef.GetTypeName:string;
  6994. begin
  6995. GetTypeName:='<erroneous type>';
  6996. end;
  6997. function terrordef.getmangledparaname:TSymStr;
  6998. begin
  6999. getmangledparaname:='error';
  7000. end;
  7001. {****************************************************************************
  7002. Definition Helpers
  7003. ****************************************************************************}
  7004. function is_interfacecom(def: tdef): boolean;
  7005. begin
  7006. is_interfacecom:=
  7007. assigned(def) and
  7008. (def.typ=objectdef) and
  7009. (tobjectdef(def).objecttype=odt_interfacecom);
  7010. end;
  7011. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  7012. begin
  7013. is_interfacecom_or_dispinterface:=
  7014. assigned(def) and
  7015. (def.typ=objectdef) and
  7016. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  7017. end;
  7018. function is_any_interface_kind(def: tdef): boolean;
  7019. begin
  7020. result:=
  7021. assigned(def) and
  7022. (def.typ=objectdef) and
  7023. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  7024. is_objccategory(def));
  7025. end;
  7026. function is_interfacecorba(def: tdef): boolean;
  7027. begin
  7028. is_interfacecorba:=
  7029. assigned(def) and
  7030. (def.typ=objectdef) and
  7031. (tobjectdef(def).objecttype=odt_interfacecorba);
  7032. end;
  7033. function is_interface(def: tdef): boolean;
  7034. begin
  7035. is_interface:=
  7036. assigned(def) and
  7037. (def.typ=objectdef) and
  7038. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  7039. end;
  7040. function is_dispinterface(def: tdef): boolean;
  7041. begin
  7042. result:=
  7043. assigned(def) and
  7044. (def.typ=objectdef) and
  7045. (tobjectdef(def).objecttype=odt_dispinterface);
  7046. end;
  7047. function is_class(def: tdef): boolean;
  7048. begin
  7049. is_class:=
  7050. assigned(def) and
  7051. (def.typ=objectdef) and
  7052. (tobjectdef(def).objecttype=odt_class);
  7053. end;
  7054. function is_object(def: tdef): boolean;
  7055. begin
  7056. is_object:=
  7057. assigned(def) and
  7058. (def.typ=objectdef) and
  7059. (tobjectdef(def).objecttype=odt_object);
  7060. end;
  7061. function is_cppclass(def: tdef): boolean;
  7062. begin
  7063. is_cppclass:=
  7064. assigned(def) and
  7065. (def.typ=objectdef) and
  7066. (tobjectdef(def).objecttype=odt_cppclass);
  7067. end;
  7068. function is_objcclass(def: tdef): boolean;
  7069. begin
  7070. is_objcclass:=
  7071. assigned(def) and
  7072. (def.typ=objectdef) and
  7073. (tobjectdef(def).objecttype=odt_objcclass);
  7074. end;
  7075. function is_objectpascal_helper(def: tdef): boolean;
  7076. begin
  7077. result:=
  7078. assigned(def) and
  7079. (def.typ=objectdef) and
  7080. (tobjectdef(def).objecttype=odt_helper);
  7081. end;
  7082. function is_objcclassref(def: tdef): boolean;
  7083. begin
  7084. is_objcclassref:=
  7085. assigned(def) and
  7086. (def.typ=classrefdef) and
  7087. is_objcclass(tclassrefdef(def).pointeddef);
  7088. end;
  7089. function is_objcprotocol(def: tdef): boolean;
  7090. begin
  7091. result:=
  7092. assigned(def) and
  7093. (def.typ=objectdef) and
  7094. (tobjectdef(def).objecttype=odt_objcprotocol);
  7095. end;
  7096. function is_objccategory(def: tdef): boolean;
  7097. begin
  7098. result:=
  7099. assigned(def) and
  7100. (def.typ=objectdef) and
  7101. { if used as a forward type }
  7102. ((tobjectdef(def).objecttype=odt_objccategory) or
  7103. { if used as after it has been resolved }
  7104. ((tobjectdef(def).objecttype=odt_objcclass) and
  7105. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7106. end;
  7107. function is_objc_class_or_protocol(def: tdef): boolean;
  7108. begin
  7109. result:=
  7110. assigned(def) and
  7111. (def.typ=objectdef) and
  7112. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  7113. end;
  7114. function is_objc_protocol_or_category(def: tdef): boolean;
  7115. begin
  7116. result:=
  7117. assigned(def) and
  7118. (def.typ=objectdef) and
  7119. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  7120. ((tobjectdef(def).objecttype = odt_objcclass) and
  7121. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7122. end;
  7123. function is_classhelper(def: tdef): boolean;
  7124. begin
  7125. result:=
  7126. is_objectpascal_helper(def) or
  7127. is_objccategory(def);
  7128. end;
  7129. function is_class_or_interface(def: tdef): boolean;
  7130. begin
  7131. result:=
  7132. assigned(def) and
  7133. (def.typ=objectdef) and
  7134. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  7135. end;
  7136. function is_class_or_interface_or_objc(def: tdef): boolean;
  7137. begin
  7138. result:=
  7139. assigned(def) and
  7140. (def.typ=objectdef) and
  7141. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  7142. end;
  7143. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  7144. begin
  7145. result:=
  7146. assigned(def) and
  7147. (def.typ=objectdef) and
  7148. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7149. end;
  7150. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  7151. begin
  7152. result:=
  7153. assigned(def) and
  7154. (def.typ=objectdef) and
  7155. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7156. end;
  7157. function is_class_or_interface_or_object(def: tdef): boolean;
  7158. begin
  7159. result:=
  7160. assigned(def) and
  7161. (def.typ=objectdef) and
  7162. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  7163. end;
  7164. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  7165. begin
  7166. result:=
  7167. assigned(def) and
  7168. (def.typ=objectdef) and
  7169. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7170. end;
  7171. function is_implicit_pointer_object_type(def: tdef): boolean;
  7172. begin
  7173. result:=
  7174. assigned(def) and
  7175. (((def.typ=objectdef) and
  7176. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  7177. ((target_info.system in systems_jvm) and
  7178. (def.typ=recorddef)));
  7179. end;
  7180. function is_implicit_array_pointer(def: tdef): boolean;
  7181. begin
  7182. result:=is_dynamic_array(def) or is_dynamicstring(def);
  7183. end;
  7184. function is_class_or_object(def: tdef): boolean;
  7185. begin
  7186. result:=
  7187. assigned(def) and
  7188. (def.typ=objectdef) and
  7189. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  7190. end;
  7191. function is_record(def: tdef): boolean;
  7192. begin
  7193. result:=
  7194. assigned(def) and
  7195. (def.typ=recorddef);
  7196. end;
  7197. function is_javaclass(def: tdef): boolean;
  7198. begin
  7199. result:=
  7200. assigned(def) and
  7201. (def.typ=objectdef) and
  7202. (tobjectdef(def).objecttype=odt_javaclass);
  7203. end;
  7204. function is_javaclassref(def: tdef): boolean;
  7205. begin
  7206. is_javaclassref:=
  7207. assigned(def) and
  7208. (def.typ=classrefdef) and
  7209. is_javaclass(tclassrefdef(def).pointeddef);
  7210. end;
  7211. function is_javainterface(def: tdef): boolean;
  7212. begin
  7213. result:=
  7214. assigned(def) and
  7215. (def.typ=objectdef) and
  7216. (tobjectdef(def).objecttype=odt_interfacejava);
  7217. end;
  7218. function is_java_class_or_interface(def: tdef): boolean;
  7219. begin
  7220. result:=
  7221. assigned(def) and
  7222. (def.typ=objectdef) and
  7223. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  7224. end;
  7225. procedure loadobjctypes;
  7226. begin
  7227. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  7228. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  7229. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  7230. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  7231. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  7232. end;
  7233. procedure maybeloadcocoatypes;
  7234. var
  7235. tsym: ttypesym;
  7236. cocoaunit: string[15];
  7237. begin
  7238. if assigned(objc_fastenumeration) then
  7239. exit;
  7240. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim]) then
  7241. cocoaunit:='COCOAALL'
  7242. else
  7243. cocoaunit:='IPHONEALL';
  7244. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  7245. if assigned(tsym) then
  7246. objc_fastenumeration:=tobjectdef(tsym.typedef)
  7247. else
  7248. objc_fastenumeration:=nil;
  7249. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  7250. if assigned(tsym) then
  7251. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  7252. else
  7253. objc_fastenumerationstate:=nil;
  7254. end;
  7255. function use_vectorfpu(def : tdef) : boolean;
  7256. begin
  7257. {$ifdef x86}
  7258. {$define use_vectorfpuimplemented}
  7259. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  7260. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  7261. {$endif x86}
  7262. {$ifdef arm}
  7263. {$define use_vectorfpuimplemented}
  7264. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  7265. {$endif arm}
  7266. {$ifdef aarch64}
  7267. {$define use_vectorfpuimplemented}
  7268. use_vectorfpu:=true;
  7269. {$endif aarch64}
  7270. {$ifndef use_vectorfpuimplemented}
  7271. use_vectorfpu:=false;
  7272. {$endif}
  7273. end;
  7274. end.