symdef.pas 220 KB

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