symdef.pas 205 KB

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