symdef.pas 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252
  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. procedure reset;virtual;
  57. function getcopy : tstoreddef;virtual;
  58. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  59. procedure buildderef;override;
  60. procedure buildderefimpl;override;
  61. procedure deref;override;
  62. procedure derefimpl;override;
  63. function size:aint;override;
  64. function getvardef:longint;override;
  65. function alignment:shortint;override;
  66. function is_publishable : boolean;override;
  67. function needs_inittable : boolean;override;
  68. function rtti_mangledname(rt:trttitype):string;override;
  69. { regvars }
  70. function is_intregable : boolean;
  71. function is_fpuregable : boolean;
  72. { generics }
  73. procedure initgeneric;
  74. private
  75. savesize : aint;
  76. end;
  77. tfiletyp = (ft_text,ft_typed,ft_untyped);
  78. tfiledef = class(tstoreddef)
  79. filetyp : tfiletyp;
  80. typedfiledef : tdef;
  81. typedfiledefderef : tderef;
  82. constructor createtext;
  83. constructor createuntyped;
  84. constructor createtyped(def : tdef);
  85. constructor ppuload(ppufile:tcompilerppufile);
  86. function getcopy : tstoreddef;override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderef;override;
  89. procedure deref;override;
  90. function GetTypeName:string;override;
  91. function getmangledparaname:string;override;
  92. procedure setsize;
  93. end;
  94. tvariantdef = class(tstoreddef)
  95. varianttype : tvarianttype;
  96. constructor create(v : tvarianttype);
  97. constructor ppuload(ppufile:tcompilerppufile);
  98. function getcopy : tstoreddef;override;
  99. function GetTypeName:string;override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. function getvardef:longint;override;
  102. procedure setsize;
  103. function is_publishable : boolean;override;
  104. function needs_inittable : boolean;override;
  105. end;
  106. tformaldef = class(tstoreddef)
  107. typed:boolean;
  108. constructor create(Atyped:boolean);
  109. constructor ppuload(ppufile:tcompilerppufile);
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. function GetTypeName:string;override;
  112. end;
  113. tforwarddef = class(tstoreddef)
  114. tosymname : pshortstring;
  115. forwardpos : tfileposinfo;
  116. constructor create(const s:string;const pos : tfileposinfo);
  117. destructor destroy;override;
  118. function GetTypeName:string;override;
  119. end;
  120. tundefineddef = class(tstoreddef)
  121. constructor create;
  122. constructor ppuload(ppufile:tcompilerppufile);
  123. procedure ppuwrite(ppufile:tcompilerppufile);override;
  124. function GetTypeName:string;override;
  125. end;
  126. terrordef = class(tstoreddef)
  127. constructor create;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function GetTypeName:string;override;
  130. function getmangledparaname : string;override;
  131. end;
  132. tabstractpointerdef = class(tstoreddef)
  133. pointeddef : tdef;
  134. pointeddefderef : tderef;
  135. constructor create(dt:tdeftyp;def:tdef);
  136. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure buildderef;override;
  139. procedure deref;override;
  140. end;
  141. tpointerdef = class(tabstractpointerdef)
  142. is_far : boolean;
  143. constructor create(def:tdef);
  144. constructor createfar(def:tdef);
  145. function getcopy : tstoreddef;override;
  146. constructor ppuload(ppufile:tcompilerppufile);
  147. procedure ppuwrite(ppufile:tcompilerppufile);override;
  148. function GetTypeName:string;override;
  149. end;
  150. tabstractrecorddef= class(tstoreddef)
  151. symtable : TSymtable;
  152. cloneddef : tabstractrecorddef;
  153. cloneddefderef : tderef;
  154. procedure reset;override;
  155. function GetSymtable(t:tGetSymtable):TSymtable;override;
  156. function is_packed:boolean;
  157. end;
  158. trecorddef = class(tabstractrecorddef)
  159. public
  160. isunion : boolean;
  161. constructor create(p : TSymtable);
  162. constructor ppuload(ppufile:tcompilerppufile);
  163. destructor destroy;override;
  164. function getcopy : tstoreddef;override;
  165. procedure ppuwrite(ppufile:tcompilerppufile);override;
  166. procedure buildderef;override;
  167. procedure deref;override;
  168. function size:aint;override;
  169. function alignment : shortint;override;
  170. function padalignment: shortint;
  171. function GetTypeName:string;override;
  172. { debug }
  173. function needs_inittable : boolean;override;
  174. end;
  175. tprocdef = class;
  176. tobjectdef = class;
  177. { TImplementedInterface }
  178. TImplementedInterface = class
  179. IntfDef : tobjectdef;
  180. IntfDefDeref : tderef;
  181. IType : tinterfaceentrytype;
  182. IOffset : longint;
  183. VtblImplIntf : TImplementedInterface;
  184. NameMappings : TFPHashList;
  185. ProcDefs : TFPObjectList;
  186. ImplementsGetter : tsym;
  187. constructor create(aintf: tobjectdef);
  188. constructor create_deref(d:tderef);
  189. destructor destroy; override;
  190. function getcopy:TImplementedInterface;
  191. procedure buildderef;
  192. procedure deref;
  193. procedure AddMapping(const origname, newname: string);
  194. function GetMapping(const origname: string):string;
  195. procedure AddImplProc(pd:tprocdef);
  196. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  197. end;
  198. { tvmtentry }
  199. tvmtentry = record
  200. procdef : tprocdef;
  201. procdefderef : tderef;
  202. visibility : tvisibility;
  203. end;
  204. pvmtentry = ^tvmtentry;
  205. { tobjectdef }
  206. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  207. pmvcallstaticinfo = ^tmvcallstaticinfo;
  208. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  209. tobjectdef = class(tabstractrecorddef)
  210. public
  211. dwarf_struct_lab : tasmsymbol;
  212. childof : tobjectdef;
  213. childofderef : tderef;
  214. { for C++ classes: name of the library this class is imported from }
  215. import_lib,
  216. objname,
  217. objrealname,
  218. { for Objective-C: protocols and classes can have the same name there }
  219. objextname : pshortstring;
  220. objectoptions : tobjectoptions;
  221. { to be able to have a variable vmt position }
  222. { and no vmt field for objects without virtuals }
  223. vmtentries : TFPList;
  224. vmcallstaticinfo : pmvcallstaticinfo;
  225. vmt_offset : longint;
  226. objecttype : tobjecttyp;
  227. iidguid : pguid;
  228. iidstr : pshortstring;
  229. writing_class_record_dbginfo,
  230. { a class of this type has been created in this module }
  231. created_in_current_module,
  232. { a loadvmtnode for this class has been created in this
  233. module, so if a classrefdef variable of this or a parent
  234. class is used somewhere to instantiate a class, then this
  235. class may be instantiated
  236. }
  237. maybe_created_in_current_module,
  238. { a "class of" this particular class has been created in
  239. this module
  240. }
  241. classref_created_in_current_module : boolean;
  242. { store implemented interfaces defs and name mappings }
  243. ImplementedInterfaces : TFPObjectList;
  244. constructor create(ot : tobjecttyp;const n : string;c : tobjectdef);
  245. constructor ppuload(ppufile:tcompilerppufile);
  246. destructor destroy;override;
  247. function getcopy : tstoreddef;override;
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;
  249. function GetTypeName:string;override;
  250. procedure buildderef;override;
  251. procedure deref;override;
  252. procedure buildderefimpl;override;
  253. procedure derefimpl;override;
  254. procedure resetvmtentries;
  255. procedure copyvmtentries(objdef:tobjectdef);
  256. function getparentdef:tdef;override;
  257. function size : aint;override;
  258. function alignment:shortint;override;
  259. function vmtmethodoffset(index:longint):longint;
  260. function members_need_inittable : boolean;
  261. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  262. { this should be called when this class implements an interface }
  263. procedure prepareguid;
  264. function is_publishable : boolean;override;
  265. function needs_inittable : boolean;override;
  266. function rtti_mangledname(rt:trttitype):string;override;
  267. function vmt_mangledname : string;
  268. procedure check_forwards;
  269. function is_related(d : tdef) : boolean;override;
  270. procedure insertvmt;
  271. procedure set_parent(c : tobjectdef);
  272. function FindDestructor : tprocdef;
  273. function implements_any_interfaces: boolean;
  274. procedure reset; override;
  275. { enumerator support }
  276. function search_enumerator_get: tprocdef;
  277. function search_enumerator_move: tprocdef;
  278. function search_enumerator_current: tsym;
  279. { WPO }
  280. procedure register_created_object_type;override;
  281. procedure register_maybe_created_object_type;
  282. procedure register_created_classref_type;
  283. procedure register_vmt_call(index:longint);
  284. { ObjC & C++ }
  285. procedure make_all_methods_external;
  286. { ObjC }
  287. procedure finish_objc_data;
  288. { C++ }
  289. procedure finish_cpp_data;
  290. end;
  291. tclassrefdef = class(tabstractpointerdef)
  292. constructor create(def:tdef);
  293. constructor ppuload(ppufile:tcompilerppufile);
  294. procedure ppuwrite(ppufile:tcompilerppufile);override;
  295. function GetTypeName:string;override;
  296. function is_publishable : boolean;override;
  297. function rtti_mangledname(rt:trttitype):string;override;
  298. procedure register_created_object_type;override;
  299. procedure reset;override;
  300. end;
  301. tarraydef = class(tstoreddef)
  302. lowrange,
  303. highrange : aint;
  304. rangedef : tdef;
  305. rangedefderef : tderef;
  306. arrayoptions : tarraydefoptions;
  307. protected
  308. _elementdef : tdef;
  309. _elementdefderef : tderef;
  310. procedure setelementdef(def:tdef);
  311. public
  312. function elesize : aint;
  313. function elepackedbitsize : aint;
  314. function elecount : aword;
  315. constructor create_from_pointer(def:tdef);
  316. constructor create(l,h : aint;def:tdef);
  317. constructor ppuload(ppufile:tcompilerppufile);
  318. function getcopy : tstoreddef;override;
  319. procedure ppuwrite(ppufile:tcompilerppufile);override;
  320. function GetTypeName:string;override;
  321. function getmangledparaname : string;override;
  322. procedure buildderef;override;
  323. procedure deref;override;
  324. function size : aint;override;
  325. function alignment : shortint;override;
  326. { returns the label of the range check string }
  327. function needs_inittable : boolean;override;
  328. property elementdef : tdef read _elementdef write setelementdef;
  329. function is_publishable : boolean;override;
  330. end;
  331. torddef = class(tstoreddef)
  332. low,high : TConstExprInt;
  333. ordtype : tordtype;
  334. constructor create(t : tordtype;v,b : TConstExprInt);
  335. constructor ppuload(ppufile:tcompilerppufile);
  336. function getcopy : tstoreddef;override;
  337. procedure ppuwrite(ppufile:tcompilerppufile);override;
  338. function is_publishable : boolean;override;
  339. function GetTypeName:string;override;
  340. function alignment:shortint;override;
  341. procedure setsize;
  342. function packedbitsize: aint; override;
  343. function getvardef : longint;override;
  344. end;
  345. tfloatdef = class(tstoreddef)
  346. floattype : tfloattype;
  347. constructor create(t : tfloattype);
  348. constructor ppuload(ppufile:tcompilerppufile);
  349. function getcopy : tstoreddef;override;
  350. procedure ppuwrite(ppufile:tcompilerppufile);override;
  351. function GetTypeName:string;override;
  352. function is_publishable : boolean;override;
  353. function alignment:shortint;override;
  354. procedure setsize;
  355. function getvardef:longint;override;
  356. end;
  357. tabstractprocdef = class(tstoreddef)
  358. { saves a definition to the return type }
  359. returndef : tdef;
  360. returndefderef : tderef;
  361. parast : TSymtable;
  362. paras : tparalist;
  363. proctypeoption : tproctypeoption;
  364. proccalloption : tproccalloption;
  365. procoptions : tprocoptions;
  366. requiredargarea : aint;
  367. { number of user visibile parameters }
  368. maxparacount,
  369. minparacount : byte;
  370. {$ifdef m68k}
  371. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  372. {$endif}
  373. funcretloc : array[tcallercallee] of TLocation;
  374. has_paraloc_info : boolean; { paraloc info is available }
  375. constructor create(dt:tdeftyp;level:byte);
  376. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  377. destructor destroy;override;
  378. procedure ppuwrite(ppufile:tcompilerppufile);override;
  379. procedure buildderef;override;
  380. procedure deref;override;
  381. procedure calcparas;
  382. function typename_paras(showhidden:boolean): string;
  383. function is_methodpointer:boolean;virtual;
  384. function is_addressonly:boolean;virtual;
  385. private
  386. procedure count_para(p:TObject;arg:pointer);
  387. procedure insert_para(p:TObject;arg:pointer);
  388. end;
  389. tprocvardef = class(tabstractprocdef)
  390. constructor create(level:byte);
  391. constructor ppuload(ppufile:tcompilerppufile);
  392. function getcopy : tstoreddef;override;
  393. procedure ppuwrite(ppufile:tcompilerppufile);override;
  394. function GetSymtable(t:tGetSymtable):TSymtable;override;
  395. function size : aint;override;
  396. function GetTypeName:string;override;
  397. function is_publishable : boolean;override;
  398. function is_methodpointer:boolean;override;
  399. function is_addressonly:boolean;override;
  400. function getmangledparaname:string;override;
  401. end;
  402. tmessageinf = record
  403. case integer of
  404. 0 : (str : pshortstring);
  405. 1 : (i : longint);
  406. end;
  407. tinlininginfo = record
  408. { node tree }
  409. code : tnode;
  410. flags : tprocinfoflags;
  411. end;
  412. pinlininginfo = ^tinlininginfo;
  413. {$ifdef oldregvars}
  414. { register variables }
  415. pregvarinfo = ^tregvarinfo;
  416. tregvarinfo = record
  417. regvars : array[1..maxvarregs] of tsym;
  418. regvars_para : array[1..maxvarregs] of boolean;
  419. regvars_refs : array[1..maxvarregs] of longint;
  420. fpuregvars : array[1..maxfpuvarregs] of tsym;
  421. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  422. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  423. end;
  424. {$endif oldregvars}
  425. tprocdef = class(tabstractprocdef)
  426. private
  427. _mangledname : pshortstring;
  428. public
  429. messageinf : tmessageinf;
  430. dispid : longint;
  431. {$ifndef EXTDEBUG}
  432. { where is this function defined and what were the symbol
  433. flags, needed here because there
  434. is only one symbol for all overloaded functions
  435. EXTDEBUG has fileinfo in tdef (PFV) }
  436. fileinfo : tfileposinfo;
  437. {$endif}
  438. symoptions : tsymoptions;
  439. deprecatedmsg : pshortstring;
  440. { symbol owning this definition }
  441. procsym : tsym;
  442. procsymderef : tderef;
  443. { alias names }
  444. aliasnames : TCmdStrList;
  445. { symtables }
  446. localst : TSymtable;
  447. funcretsym : tsym;
  448. funcretsymderef : tderef;
  449. _class : tobjectdef;
  450. _classderef : tderef;
  451. {$if defined(powerpc) or defined(m68k)}
  452. { library symbol for AmigaOS/MorphOS }
  453. libsym : tsym;
  454. libsymderef : tderef;
  455. {$endif powerpc or m68k}
  456. { name of the result variable to insert in the localsymtable }
  457. resultname : pshortstring;
  458. { import info }
  459. import_dll,
  460. import_name : pshortstring;
  461. { info for inlining the subroutine, if this pointer is nil,
  462. the procedure can't be inlined }
  463. inlininginfo : pinlininginfo;
  464. {$ifdef oldregvars}
  465. regvarinfo: pregvarinfo;
  466. {$endif oldregvars}
  467. { position in aasmoutput list }
  468. procstarttai,
  469. procendtai : tai;
  470. import_nr : word;
  471. extnumber : word;
  472. {$ifdef i386}
  473. fpu_used : byte;
  474. {$endif i386}
  475. visibility : tvisibility;
  476. { true, if the procedure is only declared
  477. (forward procedure) }
  478. forwarddef,
  479. { true if the procedure is declared in the interface }
  480. interfacedef : boolean;
  481. { true if the procedure has a forward declaration }
  482. hasforward : boolean;
  483. constructor create(level:byte);
  484. constructor ppuload(ppufile:tcompilerppufile);
  485. destructor destroy;override;
  486. procedure ppuwrite(ppufile:tcompilerppufile);override;
  487. procedure buildderef;override;
  488. procedure buildderefimpl;override;
  489. procedure deref;override;
  490. procedure derefimpl;override;
  491. procedure reset;override;
  492. function GetSymtable(t:tGetSymtable):TSymtable;override;
  493. function GetTypeName : string;override;
  494. function mangledname : string;
  495. procedure setmangledname(const s : string);
  496. function fullprocname(showhidden:boolean):string;
  497. function cplusplusmangledname : string;
  498. function objcmangledname : string;
  499. function is_methodpointer:boolean;override;
  500. function is_addressonly:boolean;override;
  501. end;
  502. { single linked list of overloaded procs }
  503. pprocdeflist = ^tprocdeflist;
  504. tprocdeflist = record
  505. def : tprocdef;
  506. defderef : tderef;
  507. next : pprocdeflist;
  508. end;
  509. tstringdef = class(tstoreddef)
  510. stringtype : tstringtype;
  511. len : aint;
  512. constructor createshort(l : byte);
  513. constructor loadshort(ppufile:tcompilerppufile);
  514. constructor createlong(l : aint);
  515. constructor loadlong(ppufile:tcompilerppufile);
  516. constructor createansi;
  517. constructor loadansi(ppufile:tcompilerppufile);
  518. constructor createwide;
  519. constructor loadwide(ppufile:tcompilerppufile);
  520. constructor createunicode;
  521. constructor loadunicode(ppufile:tcompilerppufile);
  522. function getcopy : tstoreddef;override;
  523. function stringtypname:string;
  524. procedure ppuwrite(ppufile:tcompilerppufile);override;
  525. function GetTypeName:string;override;
  526. function getmangledparaname:string;override;
  527. function is_publishable : boolean;override;
  528. function alignment : shortint;override;
  529. function needs_inittable : boolean;override;
  530. function getvardef:longint;override;
  531. end;
  532. tenumdef = class(tstoreddef)
  533. minval,
  534. maxval : aint;
  535. has_jumps : boolean;
  536. firstenum : tsym; {tenumsym}
  537. basedef : tenumdef;
  538. basedefderef : tderef;
  539. constructor create;
  540. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  541. constructor ppuload(ppufile:tcompilerppufile);
  542. function getcopy : tstoreddef;override;
  543. procedure ppuwrite(ppufile:tcompilerppufile);override;
  544. procedure buildderef;override;
  545. procedure deref;override;
  546. procedure derefimpl;override;
  547. function GetTypeName:string;override;
  548. function is_publishable : boolean;override;
  549. procedure calcsavesize;
  550. function packedbitsize: aint; override;
  551. procedure setmax(_max:aint);
  552. procedure setmin(_min:aint);
  553. function min:aint;
  554. function max:aint;
  555. end;
  556. tsetdef = class(tstoreddef)
  557. elementdef : tdef;
  558. elementdefderef : tderef;
  559. setbase,
  560. setmax : aword;
  561. constructor create(def:tdef;low, high : aint);
  562. constructor ppuload(ppufile:tcompilerppufile);
  563. function getcopy : tstoreddef;override;
  564. procedure ppuwrite(ppufile:tcompilerppufile);override;
  565. procedure buildderef;override;
  566. procedure deref;override;
  567. function GetTypeName:string;override;
  568. function is_publishable : boolean;override;
  569. end;
  570. var
  571. current_objectdef : tobjectdef; { used for private functions check !! }
  572. { default types }
  573. generrordef, { error in definition }
  574. voidpointertype, { pointer for Void-pointeddef }
  575. charpointertype, { pointer for Char-pointeddef }
  576. widecharpointertype, { pointer for WideChar-pointeddef }
  577. voidfarpointertype,
  578. cundefinedtype,
  579. cformaltype, { unique formal definition }
  580. ctypedformaltype, { unique typed formal definition }
  581. voidtype, { Void (procedure) }
  582. cchartype, { Char }
  583. cwidechartype, { WideChar }
  584. booltype, { boolean type }
  585. bool8type,
  586. bool16type,
  587. bool32type,
  588. bool64type, { implement me }
  589. u8inttype, { 8-Bit unsigned integer }
  590. s8inttype, { 8-Bit signed integer }
  591. u16inttype, { 16-Bit unsigned integer }
  592. s16inttype, { 16-Bit signed integer }
  593. u32inttype, { 32-Bit unsigned integer }
  594. s32inttype, { 32-Bit signed integer }
  595. u64inttype, { 64-bit unsigned integer }
  596. s64inttype, { 64-bit signed integer }
  597. s32floattype, { pointer for realconstn }
  598. s64floattype, { pointer for realconstn }
  599. s80floattype, { pointer to type of temp. floats }
  600. s64currencytype, { pointer to a currency type }
  601. cshortstringtype, { pointer to type of short string const }
  602. clongstringtype, { pointer to type of long string const }
  603. cansistringtype, { pointer to type of ansi string const }
  604. cwidestringtype, { pointer to type of wide string const }
  605. cunicodestringtype,
  606. openshortstringtype, { pointer to type of an open shortstring,
  607. needed for readln() }
  608. openchararraytype, { pointer to type of an open array of char,
  609. needed for readln() }
  610. cfiletype, { get the same definition for all file }
  611. { used for stabs }
  612. methodpointertype, { typecasting of methodpointers to extract self }
  613. hresultdef,
  614. { we use only one variant def for every variant class }
  615. cvarianttype,
  616. colevarianttype,
  617. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  618. sinttype,
  619. uinttype,
  620. { unsigned and signed ord type with the same size as a pointer }
  621. ptruinttype,
  622. ptrsinttype,
  623. { several types to simulate more or less C++ objects for GDB }
  624. vmttype,
  625. vmtarraytype,
  626. pvmttype : tdef; { type of classrefs, used for stabs }
  627. { pointer to the anchestor of all classes }
  628. class_tobject : tobjectdef;
  629. { pointer to the ancestor of all COM interfaces }
  630. interface_iunknown : tobjectdef;
  631. { pointer to the TGUID type
  632. of all interfaces }
  633. rec_tguid : trecorddef;
  634. { Objective-C base types }
  635. objc_metaclasstype,
  636. objc_superclasstype,
  637. objc_idtype,
  638. objc_seltype : tpointerdef;
  639. objc_objecttype : trecorddef;
  640. { base type of @protocol(protocolname) Objective-C statements }
  641. objc_protocoltype : tobjectdef;
  642. const
  643. {$ifdef i386}
  644. pbestrealtype : ^tdef = @s80floattype;
  645. {$endif}
  646. {$ifdef x86_64}
  647. pbestrealtype : ^tdef = @s80floattype;
  648. {$endif}
  649. {$ifdef m68k}
  650. pbestrealtype : ^tdef = @s64floattype;
  651. {$endif}
  652. {$ifdef alpha}
  653. pbestrealtype : ^tdef = @s64floattype;
  654. {$endif}
  655. {$ifdef powerpc}
  656. pbestrealtype : ^tdef = @s64floattype;
  657. {$endif}
  658. {$ifdef POWERPC64}
  659. pbestrealtype : ^tdef = @s64floattype;
  660. {$endif}
  661. {$ifdef ia64}
  662. pbestrealtype : ^tdef = @s64floattype;
  663. {$endif}
  664. {$ifdef SPARC}
  665. pbestrealtype : ^tdef = @s64floattype;
  666. {$endif SPARC}
  667. {$ifdef vis}
  668. pbestrealtype : ^tdef = @s64floattype;
  669. {$endif vis}
  670. {$ifdef ARM}
  671. pbestrealtype : ^tdef = @s64floattype;
  672. {$endif ARM}
  673. {$ifdef MIPS}
  674. pbestrealtype : ^tdef = @s64floattype;
  675. {$endif MIPS}
  676. {$ifdef AVR}
  677. pbestrealtype : ^tdef = @s64floattype;
  678. {$endif AVR}
  679. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  680. { should be in the types unit, but the types unit uses the node stuff :( }
  681. function is_interfacecom(def: tdef): boolean;
  682. function is_interfacecorba(def: tdef): boolean;
  683. function is_interface(def: tdef): boolean;
  684. function is_dispinterface(def: tdef): boolean;
  685. function is_object(def: tdef): boolean;
  686. function is_class(def: tdef): boolean;
  687. function is_cppclass(def: tdef): boolean;
  688. function is_objcclass(def: tdef): boolean;
  689. function is_objcclassref(def: tdef): boolean;
  690. function is_objcprotocol(def: tdef): boolean;
  691. function is_objccategory(def: tdef): boolean;
  692. function is_objc_class_or_protocol(def: tdef): boolean;
  693. function is_objc_protocol_or_category(def: tdef): boolean;
  694. function is_class_or_interface(def: tdef): boolean;
  695. function is_class_or_interface_or_objc(def: tdef): boolean;
  696. function is_class_or_interface_or_object(def: tdef): boolean;
  697. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  698. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  699. procedure loadobjctypes;
  700. function use_vectorfpu(def : tdef) : boolean;
  701. implementation
  702. uses
  703. SysUtils,
  704. cutils,
  705. { global }
  706. verbose,
  707. { target }
  708. systems,aasmcpu,paramgr,
  709. { symtable }
  710. symsym,symtable,symutil,defutil,
  711. { module }
  712. fmodule,
  713. { other }
  714. gendef,
  715. fpccrc
  716. ;
  717. {****************************************************************************
  718. Constants
  719. ****************************************************************************}
  720. const
  721. varempty = 0;
  722. varnull = 1;
  723. varsmallint = 2;
  724. varinteger = 3;
  725. varsingle = 4;
  726. vardouble = 5;
  727. varcurrency = 6;
  728. vardate = 7;
  729. varolestr = 8;
  730. vardispatch = 9;
  731. varerror = 10;
  732. varboolean = 11;
  733. varvariant = 12;
  734. varunknown = 13;
  735. vardecimal = 14;
  736. varshortint = 16;
  737. varbyte = 17;
  738. varword = 18;
  739. varlongword = 19;
  740. varint64 = 20;
  741. varqword = 21;
  742. varunicodestr = 22;
  743. varUndefined = -1;
  744. varstrarg = $48;
  745. varstring = $100;
  746. varany = $101;
  747. vardefmask = $fff;
  748. vararray = $2000;
  749. varbyref = $4000;
  750. {****************************************************************************
  751. Helpers
  752. ****************************************************************************}
  753. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  754. var
  755. s,hs,
  756. prefix : string;
  757. oldlen,
  758. newlen,
  759. i : longint;
  760. crc : dword;
  761. hp : tparavarsym;
  762. begin
  763. prefix:='';
  764. if not assigned(st) then
  765. internalerror(200204212);
  766. { sub procedures }
  767. while (st.symtabletype=localsymtable) do
  768. begin
  769. if st.defowner.typ<>procdef then
  770. internalerror(200204173);
  771. { Add the full mangledname of procedure to prevent
  772. conflicts with 2 overloads having both a nested procedure
  773. with the same name, see tb0314 (PFV) }
  774. s:=tprocdef(st.defowner).procsym.name;
  775. oldlen:=length(s);
  776. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  777. begin
  778. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  779. if not(vo_is_hidden_para in hp.varoptions) then
  780. s:=s+'$'+hp.vardef.mangledparaname;
  781. end;
  782. if not is_void(tprocdef(st.defowner).returndef) then
  783. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  784. newlen:=length(s);
  785. { Replace with CRC if the parameter line is very long }
  786. if (newlen-oldlen>12) and
  787. ((newlen+length(prefix)>128) or (newlen-oldlen>32)) then
  788. begin
  789. crc:=0;
  790. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  791. begin
  792. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  793. if not(vo_is_hidden_para in hp.varoptions) then
  794. begin
  795. hs:=hp.vardef.mangledparaname;
  796. crc:=UpdateCrc32(crc,hs[1],length(hs));
  797. end;
  798. end;
  799. hs:=hp.vardef.mangledparaname;
  800. crc:=UpdateCrc32(crc,hs[1],length(hs));
  801. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  802. end;
  803. if prefix<>'' then
  804. prefix:=s+'_'+prefix
  805. else
  806. prefix:=s;
  807. st:=st.defowner.owner;
  808. end;
  809. { object/classes symtable, nested type definitions in classes require the while loop }
  810. while st.symtabletype=ObjectSymtable do
  811. begin
  812. if st.defowner.typ<>objectdef then
  813. internalerror(200204174);
  814. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  815. st:=st.defowner.owner;
  816. end;
  817. { symtable must now be static or global }
  818. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  819. internalerror(200204175);
  820. result:='';
  821. if typeprefix<>'' then
  822. result:=result+typeprefix+'_';
  823. { Add P$ for program, which can have the same name as
  824. a unit }
  825. if (TSymtable(main_module.localsymtable)=st) and
  826. (not main_module.is_unit) then
  827. result:=result+'P$'+st.name^
  828. else
  829. result:=result+st.name^;
  830. if prefix<>'' then
  831. result:=result+'_'+prefix;
  832. if suffix<>'' then
  833. result:=result+'_'+suffix;
  834. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  835. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  836. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  837. { those in the debug map, leading to troubles with dsymutil). So always }
  838. { add an underscore on darwin. }
  839. if (target_info.system in systems_darwin) then
  840. result := '_' + result;
  841. end;
  842. {****************************************************************************
  843. TDEF (base class for definitions)
  844. ****************************************************************************}
  845. constructor tstoreddef.create(dt:tdeftyp);
  846. var
  847. insertstack : psymtablestackitem;
  848. begin
  849. inherited create(dt);
  850. savesize := 0;
  851. {$ifdef EXTDEBUG}
  852. fileinfo := current_filepos;
  853. {$endif}
  854. generictokenbuf:=nil;
  855. genericdef:=nil;
  856. { Don't register forwarddefs, they are disposed at the
  857. end of an type block }
  858. if (dt=forwarddef) then
  859. exit;
  860. { Register in current_module }
  861. if assigned(current_module) then
  862. begin
  863. current_module.deflist.Add(self);
  864. DefId:=current_module.deflist.Count-1;
  865. end;
  866. { Register in symtable stack }
  867. if assigned(symtablestack) then
  868. begin
  869. insertstack:=symtablestack.stack;
  870. while assigned(insertstack) and
  871. (insertstack^.symtable.symtabletype=withsymtable) do
  872. insertstack:=insertstack^.next;
  873. if not assigned(insertstack) then
  874. internalerror(200602044);
  875. insertstack^.symtable.insertdef(self);
  876. end;
  877. end;
  878. destructor tstoreddef.destroy;
  879. begin
  880. { Direct calls are not allowed, use symtable.deletedef() }
  881. if assigned(owner) then
  882. internalerror(200612311);
  883. if assigned(generictokenbuf) then
  884. begin
  885. generictokenbuf.free;
  886. generictokenbuf:=nil;
  887. end;
  888. inherited destroy;
  889. end;
  890. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  891. var
  892. sizeleft,i : longint;
  893. buf : array[0..255] of byte;
  894. begin
  895. inherited create(dt);
  896. DefId:=ppufile.getlongint;
  897. current_module.deflist[DefId]:=self;
  898. {$ifdef EXTDEBUG}
  899. fillchar(fileinfo,sizeof(fileinfo),0);
  900. {$endif}
  901. { load }
  902. ppufile.getderef(typesymderef);
  903. ppufile.getsmallset(defoptions);
  904. ppufile.getsmallset(defstates);
  905. if df_generic in defoptions then
  906. begin
  907. sizeleft:=ppufile.getlongint;
  908. initgeneric;
  909. while sizeleft>0 do
  910. begin
  911. if sizeleft>sizeof(buf) then
  912. i:=sizeof(buf)
  913. else
  914. i:=sizeleft;
  915. ppufile.getdata(buf,i);
  916. generictokenbuf.write(buf,i);
  917. dec(sizeleft,i);
  918. end;
  919. end;
  920. if df_specialization in defoptions then
  921. ppufile.getderef(genericdefderef);
  922. end;
  923. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  924. var
  925. prefix : string[4];
  926. begin
  927. if rt=fullrtti then
  928. begin
  929. prefix:='RTTI';
  930. include(defstates,ds_rtti_table_used);
  931. end
  932. else
  933. begin
  934. prefix:='INIT';
  935. include(defstates,ds_init_table_used);
  936. end;
  937. if assigned(typesym) and
  938. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  939. result:=make_mangledname(prefix,owner,typesym.name)
  940. else
  941. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  942. end;
  943. procedure Tstoreddef.reset;
  944. begin
  945. end;
  946. function tstoreddef.getcopy : tstoreddef;
  947. begin
  948. Message(sym_e_cant_create_unique_type);
  949. getcopy:=terrordef.create;
  950. end;
  951. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  952. var
  953. sizeleft,i : longint;
  954. buf : array[0..255] of byte;
  955. oldintfcrc : boolean;
  956. begin
  957. ppufile.putlongint(DefId);
  958. ppufile.putderef(typesymderef);
  959. ppufile.putsmallset(defoptions);
  960. oldintfcrc:=ppufile.do_crc;
  961. ppufile.do_crc:=false;
  962. ppufile.putsmallset(defstates);
  963. if df_generic in defoptions then
  964. begin
  965. if assigned(generictokenbuf) then
  966. begin
  967. sizeleft:=generictokenbuf.size;
  968. generictokenbuf.seek(0);
  969. end
  970. else
  971. sizeleft:=0;
  972. ppufile.putlongint(sizeleft);
  973. while sizeleft>0 do
  974. begin
  975. if sizeleft>sizeof(buf) then
  976. i:=sizeof(buf)
  977. else
  978. i:=sizeleft;
  979. generictokenbuf.read(buf,i);
  980. ppufile.putdata(buf,i);
  981. dec(sizeleft,i);
  982. end;
  983. end;
  984. ppufile.do_crc:=oldintfcrc;
  985. if df_specialization in defoptions then
  986. ppufile.putderef(genericdefderef);
  987. end;
  988. procedure tstoreddef.buildderef;
  989. begin
  990. typesymderef.build(typesym);
  991. genericdefderef.build(genericdef);
  992. end;
  993. procedure tstoreddef.buildderefimpl;
  994. begin
  995. end;
  996. procedure tstoreddef.deref;
  997. begin
  998. typesym:=ttypesym(typesymderef.resolve);
  999. if df_specialization in defoptions then
  1000. genericdef:=tstoreddef(genericdefderef.resolve);
  1001. end;
  1002. procedure tstoreddef.derefimpl;
  1003. begin
  1004. end;
  1005. function tstoreddef.size : aint;
  1006. begin
  1007. size:=savesize;
  1008. end;
  1009. function tstoreddef.getvardef:longint;
  1010. begin
  1011. result:=varUndefined;
  1012. end;
  1013. function tstoreddef.alignment : shortint;
  1014. begin
  1015. { natural alignment by default }
  1016. alignment:=size_2_align(savesize);
  1017. { can happen if savesize = 0, e.g. for voiddef or
  1018. an empty record
  1019. }
  1020. if (alignment=0) then
  1021. alignment:=1;
  1022. end;
  1023. { returns true, if the definition can be published }
  1024. function tstoreddef.is_publishable : boolean;
  1025. begin
  1026. is_publishable:=false;
  1027. end;
  1028. { needs an init table }
  1029. function tstoreddef.needs_inittable : boolean;
  1030. begin
  1031. needs_inittable:=false;
  1032. end;
  1033. function tstoreddef.is_intregable : boolean;
  1034. var
  1035. recsize,temp: longint;
  1036. begin
  1037. is_intregable:=false;
  1038. case typ of
  1039. orddef,
  1040. pointerdef,
  1041. enumdef,
  1042. classrefdef:
  1043. is_intregable:=true;
  1044. procvardef :
  1045. is_intregable:=not(po_methodpointer in tprocvardef(self).procoptions);
  1046. objectdef:
  1047. is_intregable:=(is_class_or_interface_or_objc(self)) and not needs_inittable;
  1048. setdef:
  1049. is_intregable:=is_smallset(self);
  1050. recorddef:
  1051. begin
  1052. recsize:=size;
  1053. is_intregable:=
  1054. ispowerof2(recsize,temp) and
  1055. (recsize <= sizeof(aint));
  1056. end;
  1057. end;
  1058. end;
  1059. function tstoreddef.is_fpuregable : boolean;
  1060. begin
  1061. {$ifdef x86}
  1062. result:=use_vectorfpu(self);
  1063. {$else x86}
  1064. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1065. {$endif x86}
  1066. end;
  1067. procedure tstoreddef.initgeneric;
  1068. begin
  1069. if assigned(generictokenbuf) then
  1070. internalerror(200512131);
  1071. generictokenbuf:=tdynamicarray.create(256);
  1072. end;
  1073. {****************************************************************************
  1074. Tstringdef
  1075. ****************************************************************************}
  1076. constructor tstringdef.createshort(l : byte);
  1077. begin
  1078. inherited create(stringdef);
  1079. stringtype:=st_shortstring;
  1080. len:=l;
  1081. savesize:=len+1;
  1082. end;
  1083. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1084. begin
  1085. inherited ppuload(stringdef,ppufile);
  1086. stringtype:=st_shortstring;
  1087. len:=ppufile.getbyte;
  1088. savesize:=len+1;
  1089. end;
  1090. constructor tstringdef.createlong(l : aint);
  1091. begin
  1092. inherited create(stringdef);
  1093. stringtype:=st_longstring;
  1094. len:=l;
  1095. savesize:=sizeof(pint);
  1096. end;
  1097. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1098. begin
  1099. inherited ppuload(stringdef,ppufile);
  1100. stringtype:=st_longstring;
  1101. len:=ppufile.getaint;
  1102. savesize:=sizeof(pint);
  1103. end;
  1104. constructor tstringdef.createansi;
  1105. begin
  1106. inherited create(stringdef);
  1107. stringtype:=st_ansistring;
  1108. len:=-1;
  1109. savesize:=sizeof(pint);
  1110. end;
  1111. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1112. begin
  1113. inherited ppuload(stringdef,ppufile);
  1114. stringtype:=st_ansistring;
  1115. len:=ppufile.getaint;
  1116. savesize:=sizeof(pint);
  1117. end;
  1118. constructor tstringdef.createwide;
  1119. begin
  1120. inherited create(stringdef);
  1121. stringtype:=st_widestring;
  1122. len:=-1;
  1123. savesize:=sizeof(pint);
  1124. end;
  1125. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1126. begin
  1127. inherited ppuload(stringdef,ppufile);
  1128. stringtype:=st_widestring;
  1129. len:=ppufile.getaint;
  1130. savesize:=sizeof(pint);
  1131. end;
  1132. constructor tstringdef.createunicode;
  1133. begin
  1134. inherited create(stringdef);
  1135. stringtype:=st_unicodestring;
  1136. len:=-1;
  1137. savesize:=sizeof(pint);
  1138. end;
  1139. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1140. begin
  1141. inherited ppuload(stringdef,ppufile);
  1142. stringtype:=st_unicodestring;
  1143. len:=ppufile.getaint;
  1144. savesize:=sizeof(pint);
  1145. end;
  1146. function tstringdef.getcopy : tstoreddef;
  1147. begin
  1148. result:=tstringdef.create(typ);
  1149. result.typ:=stringdef;
  1150. tstringdef(result).stringtype:=stringtype;
  1151. tstringdef(result).len:=len;
  1152. tstringdef(result).savesize:=savesize;
  1153. end;
  1154. function tstringdef.stringtypname:string;
  1155. const
  1156. typname:array[tstringtype] of string[10]=(
  1157. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1158. );
  1159. begin
  1160. stringtypname:=typname[stringtype];
  1161. end;
  1162. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1163. begin
  1164. inherited ppuwrite(ppufile);
  1165. if stringtype=st_shortstring then
  1166. begin
  1167. {$ifdef extdebug}
  1168. if len > 255 then internalerror(12122002);
  1169. {$endif}
  1170. ppufile.putbyte(byte(len))
  1171. end
  1172. else
  1173. ppufile.putaint(len);
  1174. case stringtype of
  1175. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1176. st_longstring : ppufile.writeentry(iblongstringdef);
  1177. st_ansistring : ppufile.writeentry(ibansistringdef);
  1178. st_widestring : ppufile.writeentry(ibwidestringdef);
  1179. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1180. end;
  1181. end;
  1182. function tstringdef.needs_inittable : boolean;
  1183. begin
  1184. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1185. end;
  1186. function tstringdef.GetTypeName : string;
  1187. const
  1188. names : array[tstringtype] of string[15] = (
  1189. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1190. begin
  1191. GetTypeName:=names[stringtype];
  1192. end;
  1193. function tstringdef.getvardef : longint;
  1194. const
  1195. vardef : array[tstringtype] of longint = (
  1196. varUndefined,varUndefined,varString,varOleStr,varUnicodeStr);
  1197. begin
  1198. result:=vardef[stringtype];
  1199. end;
  1200. function tstringdef.alignment : shortint;
  1201. begin
  1202. case stringtype of
  1203. st_unicodestring,
  1204. st_widestring,
  1205. st_ansistring:
  1206. alignment:=size_2_align(savesize);
  1207. st_longstring,
  1208. st_shortstring:
  1209. { char to string accesses byte 0 and 1 with one word access }
  1210. if (tf_requires_proper_alignment in target_info.flags) or
  1211. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1212. (m_mac in current_settings.modeswitches) then
  1213. alignment:=size_2_align(2)
  1214. else
  1215. alignment:=size_2_align(1);
  1216. else
  1217. internalerror(200412301);
  1218. end;
  1219. end;
  1220. function tstringdef.getmangledparaname : string;
  1221. begin
  1222. getmangledparaname:='STRING';
  1223. end;
  1224. function tstringdef.is_publishable : boolean;
  1225. begin
  1226. is_publishable:=true;
  1227. end;
  1228. {****************************************************************************
  1229. TENUMDEF
  1230. ****************************************************************************}
  1231. constructor tenumdef.create;
  1232. begin
  1233. inherited create(enumdef);
  1234. minval:=0;
  1235. maxval:=0;
  1236. calcsavesize;
  1237. has_jumps:=false;
  1238. basedef:=nil;
  1239. firstenum:=nil;
  1240. end;
  1241. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1242. begin
  1243. inherited create(enumdef);
  1244. minval:=_min;
  1245. maxval:=_max;
  1246. basedef:=_basedef;
  1247. calcsavesize;
  1248. has_jumps:=false;
  1249. firstenum:=basedef.firstenum;
  1250. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1251. firstenum:=tenumsym(firstenum).nextenum;
  1252. end;
  1253. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1254. begin
  1255. inherited ppuload(enumdef,ppufile);
  1256. ppufile.getderef(basedefderef);
  1257. minval:=ppufile.getaint;
  1258. maxval:=ppufile.getaint;
  1259. savesize:=ppufile.getaint;
  1260. has_jumps:=false;
  1261. firstenum:=Nil;
  1262. end;
  1263. function tenumdef.getcopy : tstoreddef;
  1264. begin
  1265. if assigned(basedef) then
  1266. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1267. else
  1268. begin
  1269. result:=tenumdef.create;
  1270. tenumdef(result).minval:=minval;
  1271. tenumdef(result).maxval:=maxval;
  1272. end;
  1273. tenumdef(result).has_jumps:=has_jumps;
  1274. tenumdef(result).firstenum:=firstenum;
  1275. tenumdef(result).basedefderef:=basedefderef;
  1276. end;
  1277. procedure tenumdef.calcsavesize;
  1278. begin
  1279. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1280. savesize:=8
  1281. else
  1282. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1283. savesize:=4
  1284. else
  1285. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1286. savesize:=2
  1287. else
  1288. savesize:=1;
  1289. end;
  1290. function tenumdef.packedbitsize: aint;
  1291. var
  1292. sizeval: tconstexprint;
  1293. power: longint;
  1294. begin
  1295. result := 0;
  1296. if (minval >= 0) and
  1297. (maxval <= 1) then
  1298. result := 1
  1299. else
  1300. begin
  1301. if (minval>=0) then
  1302. sizeval:=maxval
  1303. else
  1304. { don't count 0 twice }
  1305. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1306. { 256 must become 512 etc. }
  1307. nextpowerof2(sizeval+1,power);
  1308. result := power;
  1309. end;
  1310. end;
  1311. procedure tenumdef.setmax(_max:aint);
  1312. begin
  1313. maxval:=_max;
  1314. calcsavesize;
  1315. end;
  1316. procedure tenumdef.setmin(_min:aint);
  1317. begin
  1318. minval:=_min;
  1319. calcsavesize;
  1320. end;
  1321. function tenumdef.min:aint;
  1322. begin
  1323. min:=minval;
  1324. end;
  1325. function tenumdef.max:aint;
  1326. begin
  1327. max:=maxval;
  1328. end;
  1329. procedure tenumdef.buildderef;
  1330. begin
  1331. inherited buildderef;
  1332. basedefderef.build(basedef);
  1333. end;
  1334. procedure tenumdef.deref;
  1335. begin
  1336. inherited deref;
  1337. basedef:=tenumdef(basedefderef.resolve);
  1338. { restart ordering }
  1339. firstenum:=nil;
  1340. end;
  1341. procedure tenumdef.derefimpl;
  1342. begin
  1343. if assigned(basedef) and
  1344. (firstenum=nil) then
  1345. begin
  1346. firstenum:=basedef.firstenum;
  1347. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1348. firstenum:=tenumsym(firstenum).nextenum;
  1349. end;
  1350. end;
  1351. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1352. begin
  1353. inherited ppuwrite(ppufile);
  1354. ppufile.putderef(basedefderef);
  1355. ppufile.putaint(min);
  1356. ppufile.putaint(max);
  1357. ppufile.putaint(savesize);
  1358. ppufile.writeentry(ibenumdef);
  1359. end;
  1360. function tenumdef.is_publishable : boolean;
  1361. begin
  1362. is_publishable:=true;
  1363. end;
  1364. function tenumdef.GetTypeName : string;
  1365. begin
  1366. GetTypeName:='<enumeration type>';
  1367. end;
  1368. {****************************************************************************
  1369. TORDDEF
  1370. ****************************************************************************}
  1371. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1372. begin
  1373. inherited create(orddef);
  1374. low:=v;
  1375. high:=b;
  1376. ordtype:=t;
  1377. setsize;
  1378. end;
  1379. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1380. begin
  1381. inherited ppuload(orddef,ppufile);
  1382. ordtype:=tordtype(ppufile.getbyte);
  1383. low:=ppufile.getexprint;
  1384. high:=ppufile.getexprint;
  1385. setsize;
  1386. end;
  1387. function torddef.getcopy : tstoreddef;
  1388. begin
  1389. result:=torddef.create(ordtype,low,high);
  1390. result.typ:=orddef;
  1391. torddef(result).low:=low;
  1392. torddef(result).high:=high;
  1393. torddef(result).ordtype:=ordtype;
  1394. torddef(result).savesize:=savesize;
  1395. end;
  1396. function torddef.alignment:shortint;
  1397. begin
  1398. if (target_info.system in [system_i386_darwin,system_arm_darwin]) and
  1399. (ordtype in [s64bit,u64bit]) then
  1400. result := 4
  1401. else
  1402. result := inherited alignment;
  1403. end;
  1404. procedure torddef.setsize;
  1405. const
  1406. sizetbl : array[tordtype] of longint = (
  1407. 0,
  1408. 1,2,4,8,
  1409. 1,2,4,8,
  1410. 1,1,2,4,8,
  1411. 1,2,8
  1412. );
  1413. begin
  1414. savesize:=sizetbl[ordtype];
  1415. end;
  1416. function torddef.packedbitsize: aint;
  1417. var
  1418. sizeval: tconstexprint;
  1419. power: longint;
  1420. begin
  1421. result := 0;
  1422. if ordtype = uvoid then
  1423. exit;
  1424. if (ordtype = u64bit) or
  1425. ((ordtype = s64bit) and
  1426. ((low <= (system.low(int64) div 2)) or
  1427. (high > (system.high(int64) div 2)))) then
  1428. result := 64
  1429. else if (low >= 0) and
  1430. (high <= 1) then
  1431. result := 1
  1432. else
  1433. begin
  1434. if (low>=0) then
  1435. sizeval:=high
  1436. else
  1437. { don't count 0 twice }
  1438. sizeval:=(cutils.max(-low,high)*2)-1;
  1439. { 256 must become 512 etc. }
  1440. nextpowerof2(sizeval+1,power);
  1441. result := power;
  1442. end;
  1443. end;
  1444. function torddef.getvardef : longint;
  1445. const
  1446. basetype2vardef : array[tordtype] of longint = (
  1447. varUndefined,
  1448. varbyte,varqword,varlongword,varqword,
  1449. varshortint,varsmallint,varinteger,varint64,
  1450. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1451. varUndefined,varUndefined,varCurrency);
  1452. begin
  1453. result:=basetype2vardef[ordtype];
  1454. end;
  1455. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1456. begin
  1457. inherited ppuwrite(ppufile);
  1458. ppufile.putbyte(byte(ordtype));
  1459. ppufile.putexprint(low);
  1460. ppufile.putexprint(high);
  1461. ppufile.writeentry(iborddef);
  1462. end;
  1463. function torddef.is_publishable : boolean;
  1464. begin
  1465. is_publishable:=(ordtype<>uvoid);
  1466. end;
  1467. function torddef.GetTypeName : string;
  1468. const
  1469. names : array[tordtype] of string[20] = (
  1470. 'untyped',
  1471. 'Byte','Word','DWord','QWord',
  1472. 'ShortInt','SmallInt','LongInt','Int64',
  1473. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1474. 'Char','WideChar','Currency');
  1475. begin
  1476. GetTypeName:=names[ordtype];
  1477. end;
  1478. {****************************************************************************
  1479. TFLOATDEF
  1480. ****************************************************************************}
  1481. constructor tfloatdef.create(t : tfloattype);
  1482. begin
  1483. inherited create(floatdef);
  1484. floattype:=t;
  1485. setsize;
  1486. end;
  1487. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1488. begin
  1489. inherited ppuload(floatdef,ppufile);
  1490. floattype:=tfloattype(ppufile.getbyte);
  1491. setsize;
  1492. end;
  1493. function tfloatdef.getcopy : tstoreddef;
  1494. begin
  1495. result:=tfloatdef.create(floattype);
  1496. result.typ:=floatdef;
  1497. tfloatdef(result).savesize:=savesize;
  1498. end;
  1499. function tfloatdef.alignment:shortint;
  1500. begin
  1501. if (target_info.system in [system_i386_darwin,system_arm_darwin]) then
  1502. case floattype of
  1503. s80real : result:=16;
  1504. s64real,
  1505. s64currency,
  1506. s64comp : result:=4;
  1507. else
  1508. result := inherited alignment;
  1509. end
  1510. else
  1511. result := inherited alignment;
  1512. end;
  1513. procedure tfloatdef.setsize;
  1514. begin
  1515. case floattype of
  1516. s32real : savesize:=4;
  1517. s80real : savesize:=10;
  1518. s64real,
  1519. s64currency,
  1520. s64comp : savesize:=8;
  1521. else
  1522. savesize:=0;
  1523. end;
  1524. end;
  1525. function tfloatdef.getvardef : longint;
  1526. const
  1527. floattype2vardef : array[tfloattype] of longint = (
  1528. varSingle,varDouble,varUndefined,
  1529. varUndefined,varCurrency,varUndefined);
  1530. begin
  1531. if (upper(typename)='TDATETIME') and
  1532. assigned(owner) and
  1533. assigned(owner.name) and
  1534. (owner.name^='SYSTEM') then
  1535. result:=varDate
  1536. else
  1537. result:=floattype2vardef[floattype];
  1538. end;
  1539. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1540. begin
  1541. inherited ppuwrite(ppufile);
  1542. ppufile.putbyte(byte(floattype));
  1543. ppufile.writeentry(ibfloatdef);
  1544. end;
  1545. function tfloatdef.is_publishable : boolean;
  1546. begin
  1547. is_publishable:=true;
  1548. end;
  1549. function tfloatdef.GetTypeName : string;
  1550. const
  1551. names : array[tfloattype] of string[20] = (
  1552. 'Single','Double','Extended','Comp','Currency','Float128');
  1553. begin
  1554. GetTypeName:=names[floattype];
  1555. end;
  1556. {****************************************************************************
  1557. TFILEDEF
  1558. ****************************************************************************}
  1559. constructor tfiledef.createtext;
  1560. begin
  1561. inherited create(filedef);
  1562. filetyp:=ft_text;
  1563. typedfiledef:=nil;
  1564. setsize;
  1565. end;
  1566. constructor tfiledef.createuntyped;
  1567. begin
  1568. inherited create(filedef);
  1569. filetyp:=ft_untyped;
  1570. typedfiledef:=nil;
  1571. setsize;
  1572. end;
  1573. constructor tfiledef.createtyped(def:tdef);
  1574. begin
  1575. inherited create(filedef);
  1576. filetyp:=ft_typed;
  1577. typedfiledef:=def;
  1578. setsize;
  1579. end;
  1580. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1581. begin
  1582. inherited ppuload(filedef,ppufile);
  1583. filetyp:=tfiletyp(ppufile.getbyte);
  1584. if filetyp=ft_typed then
  1585. ppufile.getderef(typedfiledefderef)
  1586. else
  1587. typedfiledef:=nil;
  1588. setsize;
  1589. end;
  1590. function tfiledef.getcopy : tstoreddef;
  1591. begin
  1592. case filetyp of
  1593. ft_typed:
  1594. result:=tfiledef.createtyped(typedfiledef);
  1595. ft_untyped:
  1596. result:=tfiledef.createuntyped;
  1597. ft_text:
  1598. result:=tfiledef.createtext;
  1599. else
  1600. internalerror(2004121201);
  1601. end;
  1602. end;
  1603. procedure tfiledef.buildderef;
  1604. begin
  1605. inherited buildderef;
  1606. if filetyp=ft_typed then
  1607. typedfiledefderef.build(typedfiledef);
  1608. end;
  1609. procedure tfiledef.deref;
  1610. begin
  1611. inherited deref;
  1612. if filetyp=ft_typed then
  1613. typedfiledef:=tdef(typedfiledefderef.resolve);
  1614. end;
  1615. procedure tfiledef.setsize;
  1616. begin
  1617. {$ifdef cpu64bitaddr}
  1618. case filetyp of
  1619. ft_text :
  1620. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1621. savesize:=632{+8}
  1622. else
  1623. savesize:=628{+8};
  1624. ft_typed,
  1625. ft_untyped :
  1626. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1627. savesize:=372
  1628. else
  1629. savesize:=368;
  1630. end;
  1631. {$else cpu64bitaddr}
  1632. case filetyp of
  1633. ft_text :
  1634. savesize:=592{+4};
  1635. ft_typed,
  1636. ft_untyped :
  1637. savesize:=332;
  1638. end;
  1639. {$endif cpu64bitaddr}
  1640. end;
  1641. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1642. begin
  1643. inherited ppuwrite(ppufile);
  1644. ppufile.putbyte(byte(filetyp));
  1645. if filetyp=ft_typed then
  1646. ppufile.putderef(typedfiledefderef);
  1647. ppufile.writeentry(ibfiledef);
  1648. end;
  1649. function tfiledef.GetTypeName : string;
  1650. begin
  1651. case filetyp of
  1652. ft_untyped:
  1653. GetTypeName:='File';
  1654. ft_typed:
  1655. GetTypeName:='File Of '+typedfiledef.typename;
  1656. ft_text:
  1657. GetTypeName:='Text'
  1658. end;
  1659. end;
  1660. function tfiledef.getmangledparaname : string;
  1661. begin
  1662. case filetyp of
  1663. ft_untyped:
  1664. getmangledparaname:='FILE';
  1665. ft_typed:
  1666. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1667. ft_text:
  1668. getmangledparaname:='TEXT'
  1669. end;
  1670. end;
  1671. {****************************************************************************
  1672. TVARIANTDEF
  1673. ****************************************************************************}
  1674. constructor tvariantdef.create(v : tvarianttype);
  1675. begin
  1676. inherited create(variantdef);
  1677. varianttype:=v;
  1678. setsize;
  1679. end;
  1680. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1681. begin
  1682. inherited ppuload(variantdef,ppufile);
  1683. varianttype:=tvarianttype(ppufile.getbyte);
  1684. setsize;
  1685. end;
  1686. function tvariantdef.getcopy : tstoreddef;
  1687. begin
  1688. result:=tvariantdef.create(varianttype);
  1689. end;
  1690. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1691. begin
  1692. inherited ppuwrite(ppufile);
  1693. ppufile.putbyte(byte(varianttype));
  1694. ppufile.writeentry(ibvariantdef);
  1695. end;
  1696. function tvariantdef.getvardef : longint;
  1697. begin
  1698. Result:=varVariant;
  1699. end;
  1700. procedure tvariantdef.setsize;
  1701. begin
  1702. {$ifdef cpu64bitaddr}
  1703. savesize:=24;
  1704. {$else cpu64bitaddr}
  1705. savesize:=16;
  1706. {$endif cpu64bitaddr}
  1707. end;
  1708. function tvariantdef.GetTypeName : string;
  1709. begin
  1710. case varianttype of
  1711. vt_normalvariant:
  1712. GetTypeName:='Variant';
  1713. vt_olevariant:
  1714. GetTypeName:='OleVariant';
  1715. end;
  1716. end;
  1717. function tvariantdef.needs_inittable : boolean;
  1718. begin
  1719. needs_inittable:=true;
  1720. end;
  1721. function tvariantdef.is_publishable : boolean;
  1722. begin
  1723. is_publishable:=true;
  1724. end;
  1725. {****************************************************************************
  1726. TABSTRACtpointerdef
  1727. ****************************************************************************}
  1728. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1729. begin
  1730. inherited create(dt);
  1731. pointeddef:=def;
  1732. savesize:=sizeof(pint);
  1733. end;
  1734. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1735. begin
  1736. inherited ppuload(dt,ppufile);
  1737. ppufile.getderef(pointeddefderef);
  1738. savesize:=sizeof(pint);
  1739. end;
  1740. procedure tabstractpointerdef.buildderef;
  1741. begin
  1742. inherited buildderef;
  1743. pointeddefderef.build(pointeddef);
  1744. end;
  1745. procedure tabstractpointerdef.deref;
  1746. begin
  1747. inherited deref;
  1748. pointeddef:=tdef(pointeddefderef.resolve);
  1749. end;
  1750. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1751. begin
  1752. inherited ppuwrite(ppufile);
  1753. ppufile.putderef(pointeddefderef);
  1754. end;
  1755. {****************************************************************************
  1756. tpointerdef
  1757. ****************************************************************************}
  1758. constructor tpointerdef.create(def:tdef);
  1759. begin
  1760. inherited create(pointerdef,def);
  1761. is_far:=false;
  1762. end;
  1763. constructor tpointerdef.createfar(def:tdef);
  1764. begin
  1765. inherited create(pointerdef,def);
  1766. is_far:=true;
  1767. end;
  1768. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1769. begin
  1770. inherited ppuload(pointerdef,ppufile);
  1771. is_far:=(ppufile.getbyte<>0);
  1772. end;
  1773. function tpointerdef.getcopy : tstoreddef;
  1774. begin
  1775. result:=tpointerdef.create(pointeddef);
  1776. tpointerdef(result).is_far:=is_far;
  1777. tpointerdef(result).savesize:=savesize;
  1778. end;
  1779. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1780. begin
  1781. inherited ppuwrite(ppufile);
  1782. ppufile.putbyte(byte(is_far));
  1783. ppufile.writeentry(ibpointerdef);
  1784. end;
  1785. function tpointerdef.GetTypeName : string;
  1786. begin
  1787. if is_far then
  1788. GetTypeName:='^'+pointeddef.typename+';far'
  1789. else
  1790. GetTypeName:='^'+pointeddef.typename;
  1791. end;
  1792. {****************************************************************************
  1793. TCLASSREFDEF
  1794. ****************************************************************************}
  1795. constructor tclassrefdef.create(def:tdef);
  1796. begin
  1797. inherited create(classrefdef,def);
  1798. end;
  1799. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1800. begin
  1801. inherited ppuload(classrefdef,ppufile);
  1802. end;
  1803. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1804. begin
  1805. inherited ppuwrite(ppufile);
  1806. ppufile.writeentry(ibclassrefdef);
  1807. end;
  1808. function tclassrefdef.GetTypeName : string;
  1809. begin
  1810. GetTypeName:='Class Of '+pointeddef.typename;
  1811. end;
  1812. function tclassrefdef.is_publishable : boolean;
  1813. begin
  1814. result:=true;
  1815. end;
  1816. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  1817. begin
  1818. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  1819. result:=inherited rtti_mangledname(rt)
  1820. else
  1821. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  1822. end;
  1823. procedure tclassrefdef.reset;
  1824. begin
  1825. tobjectdef(pointeddef).classref_created_in_current_module:=false;
  1826. inherited reset;
  1827. end;
  1828. procedure tclassrefdef.register_created_object_type;
  1829. begin
  1830. tobjectdef(pointeddef).register_created_classref_type;
  1831. end;
  1832. {***************************************************************************
  1833. TSETDEF
  1834. ***************************************************************************}
  1835. constructor tsetdef.create(def:tdef;low, high : aint);
  1836. var
  1837. setallocbits: aint;
  1838. packedsavesize: aint;
  1839. begin
  1840. inherited create(setdef);
  1841. elementdef:=def;
  1842. setmax:=high;
  1843. if (current_settings.setalloc=0) then
  1844. begin
  1845. setbase:=0;
  1846. if (high<32) then
  1847. savesize:=Sizeof(longint)
  1848. else if (high<256) then
  1849. savesize:=32
  1850. else
  1851. savesize:=(high+7) div 8
  1852. end
  1853. else
  1854. begin
  1855. setallocbits:=current_settings.setalloc*8;
  1856. setbase:=low and not(setallocbits-1);
  1857. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1858. savesize:=packedsavesize;
  1859. if savesize=3 then
  1860. savesize:=4;
  1861. end;
  1862. end;
  1863. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1864. begin
  1865. inherited ppuload(setdef,ppufile);
  1866. ppufile.getderef(elementdefderef);
  1867. savesize:=ppufile.getaint;
  1868. setbase:=ppufile.getaint;
  1869. setmax:=ppufile.getaint;
  1870. end;
  1871. function tsetdef.getcopy : tstoreddef;
  1872. begin
  1873. result:=tsetdef.create(elementdef,setbase,setmax);
  1874. { the copy might have been created with a different setalloc setting }
  1875. tsetdef(result).savesize:=savesize;
  1876. end;
  1877. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1878. begin
  1879. inherited ppuwrite(ppufile);
  1880. ppufile.putderef(elementdefderef);
  1881. ppufile.putaint(savesize);
  1882. ppufile.putaint(setbase);
  1883. ppufile.putaint(setmax);
  1884. ppufile.writeentry(ibsetdef);
  1885. end;
  1886. procedure tsetdef.buildderef;
  1887. begin
  1888. inherited buildderef;
  1889. elementdefderef.build(elementdef);
  1890. end;
  1891. procedure tsetdef.deref;
  1892. begin
  1893. inherited deref;
  1894. elementdef:=tdef(elementdefderef.resolve);
  1895. end;
  1896. function tsetdef.is_publishable : boolean;
  1897. begin
  1898. is_publishable:=savesize in [1,2,4];
  1899. end;
  1900. function tsetdef.GetTypeName : string;
  1901. begin
  1902. if assigned(elementdef) then
  1903. GetTypeName:='Set Of '+elementdef.typename
  1904. else
  1905. GetTypeName:='Empty Set';
  1906. end;
  1907. {***************************************************************************
  1908. TFORMALDEF
  1909. ***************************************************************************}
  1910. constructor tformaldef.create(Atyped:boolean);
  1911. begin
  1912. inherited create(formaldef);
  1913. typed:=Atyped;
  1914. savesize:=0;
  1915. end;
  1916. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1917. begin
  1918. inherited ppuload(formaldef,ppufile);
  1919. typed:=boolean(ppufile.getbyte);
  1920. savesize:=0;
  1921. end;
  1922. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1923. begin
  1924. inherited ppuwrite(ppufile);
  1925. ppufile.putbyte(byte(typed));
  1926. ppufile.writeentry(ibformaldef);
  1927. end;
  1928. function tformaldef.GetTypeName : string;
  1929. begin
  1930. if typed then
  1931. GetTypeName:='<Typed formal type>'
  1932. else
  1933. GetTypeName:='<Formal type>';
  1934. end;
  1935. {***************************************************************************
  1936. TARRAYDEF
  1937. ***************************************************************************}
  1938. constructor tarraydef.create(l,h : aint;def:tdef);
  1939. begin
  1940. inherited create(arraydef);
  1941. lowrange:=l;
  1942. highrange:=h;
  1943. rangedef:=def;
  1944. _elementdef:=nil;
  1945. arrayoptions:=[];
  1946. end;
  1947. constructor tarraydef.create_from_pointer(def:tdef);
  1948. begin
  1949. { use -1 so that the elecount will not overflow }
  1950. self.create(0,high(aint)-1,s32inttype);
  1951. arrayoptions:=[ado_IsConvertedPointer];
  1952. setelementdef(def);
  1953. end;
  1954. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  1955. begin
  1956. inherited ppuload(arraydef,ppufile);
  1957. { the addresses are calculated later }
  1958. ppufile.getderef(_elementdefderef);
  1959. ppufile.getderef(rangedefderef);
  1960. lowrange:=ppufile.getaint;
  1961. highrange:=ppufile.getaint;
  1962. ppufile.getsmallset(arrayoptions);
  1963. end;
  1964. function tarraydef.getcopy : tstoreddef;
  1965. begin
  1966. result:=tarraydef.create(lowrange,highrange,rangedef);
  1967. tarraydef(result).arrayoptions:=arrayoptions;
  1968. tarraydef(result)._elementdef:=_elementdef;
  1969. end;
  1970. procedure tarraydef.buildderef;
  1971. begin
  1972. inherited buildderef;
  1973. _elementdefderef.build(_elementdef);
  1974. rangedefderef.build(rangedef);
  1975. end;
  1976. procedure tarraydef.deref;
  1977. begin
  1978. inherited deref;
  1979. _elementdef:=tdef(_elementdefderef.resolve);
  1980. rangedef:=tdef(rangedefderef.resolve);
  1981. end;
  1982. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  1983. begin
  1984. inherited ppuwrite(ppufile);
  1985. ppufile.putderef(_elementdefderef);
  1986. ppufile.putderef(rangedefderef);
  1987. ppufile.putaint(lowrange);
  1988. ppufile.putaint(highrange);
  1989. ppufile.putsmallset(arrayoptions);
  1990. ppufile.writeentry(ibarraydef);
  1991. end;
  1992. function tarraydef.elesize : aint;
  1993. begin
  1994. if (ado_IsBitPacked in arrayoptions) then
  1995. internalerror(2006080101);
  1996. if assigned(_elementdef) then
  1997. result:=_elementdef.size
  1998. else
  1999. result:=0;
  2000. end;
  2001. function tarraydef.elepackedbitsize : aint;
  2002. begin
  2003. if not(ado_IsBitPacked in arrayoptions) then
  2004. internalerror(2006080102);
  2005. if assigned(_elementdef) then
  2006. result:=_elementdef.packedbitsize
  2007. else
  2008. result:=0;
  2009. end;
  2010. function tarraydef.elecount : aword;
  2011. var
  2012. qhigh,qlow : qword;
  2013. begin
  2014. if ado_IsDynamicArray in arrayoptions then
  2015. begin
  2016. result:=0;
  2017. exit;
  2018. end;
  2019. if (highrange>0) and (lowrange<0) then
  2020. begin
  2021. qhigh:=highrange;
  2022. qlow:=qword(-lowrange);
  2023. { prevent overflow, return 0 to indicate overflow }
  2024. if qhigh+qlow>qword(high(aint)-1) then
  2025. result:=0
  2026. else
  2027. result:=qhigh+qlow+1;
  2028. end
  2029. else
  2030. result:=int64(highrange)-lowrange+1;
  2031. end;
  2032. function tarraydef.size : aint;
  2033. var
  2034. cachedelecount : aword;
  2035. cachedelesize : aint;
  2036. begin
  2037. if ado_IsDynamicArray in arrayoptions then
  2038. begin
  2039. size:=sizeof(pint);
  2040. exit;
  2041. end;
  2042. { Tarraydef.size may never be called for an open array! }
  2043. if highrange<lowrange then
  2044. internalerror(99080501);
  2045. if not (ado_IsBitPacked in arrayoptions) then
  2046. cachedelesize:=elesize
  2047. else
  2048. cachedelesize := elepackedbitsize;
  2049. cachedelecount:=elecount;
  2050. if (cachedelesize = 0) then
  2051. begin
  2052. size := 0;
  2053. exit;
  2054. end;
  2055. if (cachedelecount = 0) then
  2056. begin
  2057. size := -1;
  2058. exit;
  2059. end;
  2060. { prevent overflow, return -1 to indicate overflow }
  2061. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2062. if (cachedelecount > aword(high(aint))) or
  2063. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2064. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2065. accessing the array, see ncgmem (PFV) }
  2066. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2067. begin
  2068. result:=-1;
  2069. exit;
  2070. end;
  2071. result:=cachedelesize*aint(cachedelecount);
  2072. if (ado_IsBitPacked in arrayoptions) then
  2073. { can't just add 7 and divide by 8, because that may overflow }
  2074. result:=result div 8 + ord((result mod 8)<>0);
  2075. end;
  2076. procedure tarraydef.setelementdef(def:tdef);
  2077. begin
  2078. _elementdef:=def;
  2079. if not(
  2080. (ado_IsDynamicArray in arrayoptions) or
  2081. (ado_IsConvertedPointer in arrayoptions) or
  2082. (highrange<lowrange)
  2083. ) and
  2084. (size=-1) then
  2085. Message(sym_e_segment_too_large);
  2086. end;
  2087. function tarraydef.alignment : shortint;
  2088. begin
  2089. { alignment of dyn. arrays doesn't depend on the element size }
  2090. if (ado_IsDynamicArray in arrayoptions) then
  2091. alignment:=size_2_align(sizeof(pint))
  2092. { alignment is the alignment of the elements }
  2093. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2094. ((elementdef.typ=objectdef) and
  2095. is_object(elementdef)) then
  2096. alignment:=elementdef.alignment
  2097. { alignment is the size of the elements }
  2098. else if not (ado_IsBitPacked in arrayoptions) then
  2099. alignment:=size_2_align(elesize)
  2100. else
  2101. alignment:=packedbitsloadsize(elepackedbitsize);
  2102. end;
  2103. function tarraydef.needs_inittable : boolean;
  2104. begin
  2105. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2106. end;
  2107. function tarraydef.GetTypeName : string;
  2108. begin
  2109. if (ado_IsConstString in arrayoptions) then
  2110. result:='Constant String'
  2111. else if (ado_isarrayofconst in arrayoptions) or
  2112. (ado_isConstructor in arrayoptions) then
  2113. begin
  2114. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2115. GetTypeName:='Array Of Const'
  2116. else
  2117. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2118. end
  2119. else if (ado_IsDynamicArray in arrayoptions) then
  2120. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2121. else if ((highrange=-1) and (lowrange=0)) then
  2122. GetTypeName:='Open Array Of '+elementdef.typename
  2123. else
  2124. begin
  2125. result := '';
  2126. if (ado_IsBitPacked in arrayoptions) then
  2127. result:='Packed ';
  2128. if rangedef.typ=enumdef then
  2129. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2130. else
  2131. result:=result+'Array['+tostr(lowrange)+'..'+
  2132. tostr(highrange)+'] Of '+elementdef.typename
  2133. end;
  2134. end;
  2135. function tarraydef.getmangledparaname : string;
  2136. begin
  2137. if ado_isarrayofconst in arrayoptions then
  2138. getmangledparaname:='array_of_const'
  2139. else
  2140. if ((highrange=-1) and (lowrange=0)) then
  2141. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2142. else
  2143. internalerror(200204176);
  2144. end;
  2145. function tarraydef.is_publishable : boolean;
  2146. begin
  2147. Result:=ado_IsDynamicArray in arrayoptions;
  2148. end;
  2149. {***************************************************************************
  2150. tabstractrecorddef
  2151. ***************************************************************************}
  2152. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2153. begin
  2154. if t=gs_record then
  2155. GetSymtable:=symtable
  2156. else
  2157. GetSymtable:=nil;
  2158. end;
  2159. procedure tabstractrecorddef.reset;
  2160. begin
  2161. inherited reset;
  2162. tstoredsymtable(symtable).reset_all_defs;
  2163. end;
  2164. function tabstractrecorddef.is_packed:boolean;
  2165. begin
  2166. result:=tabstractrecordsymtable(symtable).is_packed;
  2167. end;
  2168. {***************************************************************************
  2169. trecorddef
  2170. ***************************************************************************}
  2171. constructor trecorddef.create(p : TSymtable);
  2172. begin
  2173. inherited create(recorddef);
  2174. symtable:=p;
  2175. { we can own the symtable only if nobody else owns a copy so far }
  2176. if symtable.refcount=1 then
  2177. symtable.defowner:=self;
  2178. isunion:=false;
  2179. end;
  2180. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2181. begin
  2182. inherited ppuload(recorddef,ppufile);
  2183. if df_copied_def in defoptions then
  2184. ppufile.getderef(cloneddefderef)
  2185. else
  2186. begin
  2187. symtable:=trecordsymtable.create(0);
  2188. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2189. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2190. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2191. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2192. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2193. trecordsymtable(symtable).ppuload(ppufile);
  2194. { requires usefieldalignment to be set }
  2195. symtable.defowner:=self;
  2196. end;
  2197. isunion:=false;
  2198. end;
  2199. destructor trecorddef.destroy;
  2200. begin
  2201. if assigned(symtable) then
  2202. begin
  2203. symtable.free;
  2204. symtable:=nil;
  2205. end;
  2206. inherited destroy;
  2207. end;
  2208. function trecorddef.getcopy : tstoreddef;
  2209. begin
  2210. result:=trecorddef.create(symtable.getcopy);
  2211. trecorddef(result).isunion:=isunion;
  2212. include(trecorddef(result).defoptions,df_copied_def);
  2213. end;
  2214. function trecorddef.needs_inittable : boolean;
  2215. begin
  2216. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2217. end;
  2218. procedure trecorddef.buildderef;
  2219. begin
  2220. inherited buildderef;
  2221. if df_copied_def in defoptions then
  2222. cloneddefderef.build(symtable.defowner)
  2223. else
  2224. tstoredsymtable(symtable).buildderef;
  2225. end;
  2226. procedure trecorddef.deref;
  2227. begin
  2228. inherited deref;
  2229. { now dereference the definitions }
  2230. if df_copied_def in defoptions then
  2231. begin
  2232. cloneddef:=trecorddef(cloneddefderef.resolve);
  2233. symtable:=cloneddef.symtable.getcopy;
  2234. end
  2235. else
  2236. tstoredsymtable(symtable).deref;
  2237. { assign TGUID? load only from system unit }
  2238. if not(assigned(rec_tguid)) and
  2239. (upper(typename)='TGUID') and
  2240. assigned(owner) and
  2241. assigned(owner.name) and
  2242. (owner.name^='SYSTEM') then
  2243. rec_tguid:=self;
  2244. end;
  2245. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2246. begin
  2247. inherited ppuwrite(ppufile);
  2248. if df_copied_def in defoptions then
  2249. ppufile.putderef(cloneddefderef)
  2250. else
  2251. begin
  2252. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2253. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2254. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2255. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2256. ppufile.putaint(trecordsymtable(symtable).datasize);
  2257. end;
  2258. ppufile.writeentry(ibrecorddef);
  2259. if not(df_copied_def in defoptions) then
  2260. trecordsymtable(symtable).ppuwrite(ppufile);
  2261. end;
  2262. function trecorddef.size:aint;
  2263. begin
  2264. result:=trecordsymtable(symtable).datasize;
  2265. end;
  2266. function trecorddef.alignment:shortint;
  2267. begin
  2268. alignment:=trecordsymtable(symtable).recordalignment;
  2269. end;
  2270. function trecorddef.padalignment:shortint;
  2271. begin
  2272. padalignment := trecordsymtable(symtable).padalignment;
  2273. end;
  2274. function trecorddef.GetTypeName : string;
  2275. begin
  2276. GetTypeName:='<record type>'
  2277. end;
  2278. {***************************************************************************
  2279. TABSTRACTPROCDEF
  2280. ***************************************************************************}
  2281. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2282. begin
  2283. inherited create(dt);
  2284. parast:=tparasymtable.create(self,level);
  2285. paras:=nil;
  2286. minparacount:=0;
  2287. maxparacount:=0;
  2288. proctypeoption:=potype_none;
  2289. proccalloption:=pocall_none;
  2290. procoptions:=[];
  2291. returndef:=voidtype;
  2292. savesize:=sizeof(pint);
  2293. requiredargarea:=0;
  2294. has_paraloc_info:=false;
  2295. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2296. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2297. end;
  2298. destructor tabstractprocdef.destroy;
  2299. begin
  2300. if assigned(paras) then
  2301. begin
  2302. {$ifdef MEMDEBUG}
  2303. memprocpara.start;
  2304. {$endif MEMDEBUG}
  2305. paras.free;
  2306. paras:=nil;
  2307. {$ifdef MEMDEBUG}
  2308. memprocpara.stop;
  2309. {$endif MEMDEBUG}
  2310. end;
  2311. if assigned(parast) then
  2312. begin
  2313. {$ifdef MEMDEBUG}
  2314. memprocparast.start;
  2315. {$endif MEMDEBUG}
  2316. parast.free;
  2317. parast:=nil;
  2318. {$ifdef MEMDEBUG}
  2319. memprocparast.stop;
  2320. {$endif MEMDEBUG}
  2321. end;
  2322. inherited destroy;
  2323. end;
  2324. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2325. begin
  2326. if (tsym(p).typ<>paravarsym) then
  2327. exit;
  2328. inc(plongint(arg)^);
  2329. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2330. begin
  2331. if not assigned(tparavarsym(p).defaultconstsym) then
  2332. inc(minparacount);
  2333. inc(maxparacount);
  2334. end;
  2335. end;
  2336. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2337. begin
  2338. if (tsym(p).typ<>paravarsym) then
  2339. exit;
  2340. paras.add(p);
  2341. end;
  2342. procedure tabstractprocdef.calcparas;
  2343. var
  2344. paracount : longint;
  2345. begin
  2346. { This can already be assigned when
  2347. we need to reresolve this unit (PFV) }
  2348. if assigned(paras) then
  2349. paras.free;
  2350. paras:=tparalist.create(false);
  2351. paracount:=0;
  2352. minparacount:=0;
  2353. maxparacount:=0;
  2354. parast.SymList.ForEachCall(@count_para,@paracount);
  2355. paras.capacity:=paracount;
  2356. { Insert parameters in table }
  2357. parast.SymList.ForEachCall(@insert_para,nil);
  2358. { Order parameters }
  2359. paras.sortparas;
  2360. end;
  2361. procedure tabstractprocdef.buildderef;
  2362. begin
  2363. { released procdef? }
  2364. if not assigned(parast) then
  2365. exit;
  2366. inherited buildderef;
  2367. returndefderef.build(returndef);
  2368. { parast }
  2369. tparasymtable(parast).buildderef;
  2370. end;
  2371. procedure tabstractprocdef.deref;
  2372. begin
  2373. inherited deref;
  2374. returndef:=tdef(returndefderef.resolve);
  2375. { parast }
  2376. tparasymtable(parast).deref;
  2377. { recalculated parameters }
  2378. calcparas;
  2379. end;
  2380. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2381. var
  2382. b : byte;
  2383. begin
  2384. inherited ppuload(dt,ppufile);
  2385. parast:=nil;
  2386. Paras:=nil;
  2387. minparacount:=0;
  2388. maxparacount:=0;
  2389. ppufile.getderef(returndefderef);
  2390. { TODO: remove fpu_used loading}
  2391. ppufile.getbyte;
  2392. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2393. proccalloption:=tproccalloption(ppufile.getbyte);
  2394. ppufile.getnormalset(procoptions);
  2395. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2396. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2397. if po_explicitparaloc in procoptions then
  2398. begin
  2399. b:=ppufile.getbyte;
  2400. if b<>sizeof(funcretloc[callerside]) then
  2401. internalerror(200411155);
  2402. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2403. end;
  2404. savesize:=sizeof(pint);
  2405. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2406. end;
  2407. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2408. var
  2409. oldintfcrc : boolean;
  2410. begin
  2411. { released procdef? }
  2412. if not assigned(parast) then
  2413. exit;
  2414. inherited ppuwrite(ppufile);
  2415. ppufile.putderef(returndefderef);
  2416. oldintfcrc:=ppufile.do_interface_crc;
  2417. ppufile.do_interface_crc:=false;
  2418. ppufile.putbyte(0);
  2419. ppufile.putbyte(ord(proctypeoption));
  2420. ppufile.putbyte(ord(proccalloption));
  2421. ppufile.putnormalset(procoptions);
  2422. ppufile.do_interface_crc:=oldintfcrc;
  2423. if (po_explicitparaloc in procoptions) then
  2424. begin
  2425. { Make a 'valid' funcretloc for procedures }
  2426. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2427. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2428. end;
  2429. end;
  2430. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2431. var
  2432. hs,s : string;
  2433. hp : TParavarsym;
  2434. hpc : tconstsym;
  2435. first : boolean;
  2436. i : integer;
  2437. begin
  2438. s:='';
  2439. first:=true;
  2440. for i:=0 to paras.count-1 do
  2441. begin
  2442. hp:=tparavarsym(paras[i]);
  2443. if not(vo_is_hidden_para in hp.varoptions) or
  2444. (showhidden) then
  2445. begin
  2446. if first then
  2447. begin
  2448. s:=s+'(';
  2449. first:=false;
  2450. end
  2451. else
  2452. s:=s+',';
  2453. if vo_is_hidden_para in hp.varoptions then
  2454. s:=s+'<';
  2455. case hp.varspez of
  2456. vs_var :
  2457. s:=s+'var ';
  2458. vs_const :
  2459. s:=s+'const ';
  2460. vs_out :
  2461. s:=s+'out ';
  2462. end;
  2463. if assigned(hp.vardef.typesym) then
  2464. begin
  2465. hs:=hp.vardef.typesym.realname;
  2466. if hs[1]<>'$' then
  2467. s:=s+hs
  2468. else
  2469. s:=s+hp.vardef.GetTypeName;
  2470. end
  2471. else
  2472. s:=s+hp.vardef.GetTypeName;
  2473. { default value }
  2474. if assigned(hp.defaultconstsym) then
  2475. begin
  2476. hpc:=tconstsym(hp.defaultconstsym);
  2477. hs:='';
  2478. case hpc.consttyp of
  2479. conststring,
  2480. constresourcestring :
  2481. begin
  2482. If hpc.value.len>0 then
  2483. begin
  2484. setLength(hs,hpc.value.len);
  2485. move(hpc.value.valueptr^,hs[1],hpc.value.len);
  2486. end;
  2487. end;
  2488. constreal :
  2489. str(pbestreal(hpc.value.valueptr)^,hs);
  2490. constpointer :
  2491. hs:=tostr(hpc.value.valueordptr);
  2492. constord :
  2493. begin
  2494. if is_boolean(hpc.constdef) then
  2495. begin
  2496. if hpc.value.valueord<>0 then
  2497. hs:='TRUE'
  2498. else
  2499. hs:='FALSE';
  2500. end
  2501. else
  2502. hs:=tostr(hpc.value.valueord);
  2503. end;
  2504. constnil :
  2505. hs:='nil';
  2506. constset :
  2507. hs:='<set>';
  2508. end;
  2509. if hs<>'' then
  2510. s:=s+'="'+hs+'"';
  2511. end;
  2512. if vo_is_hidden_para in hp.varoptions then
  2513. s:=s+'>';
  2514. end;
  2515. end;
  2516. if not first then
  2517. s:=s+')';
  2518. if (po_varargs in procoptions) then
  2519. s:=s+';VarArgs';
  2520. typename_paras:=s;
  2521. end;
  2522. function tabstractprocdef.is_methodpointer:boolean;
  2523. begin
  2524. result:=false;
  2525. end;
  2526. function tabstractprocdef.is_addressonly:boolean;
  2527. begin
  2528. result:=true;
  2529. end;
  2530. {***************************************************************************
  2531. TPROCDEF
  2532. ***************************************************************************}
  2533. constructor tprocdef.create(level:byte);
  2534. begin
  2535. inherited create(procdef,level);
  2536. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2537. _mangledname:=nil;
  2538. fileinfo:=current_filepos;
  2539. extnumber:=$ffff;
  2540. aliasnames:=TCmdStrList.create;
  2541. funcretsym:=nil;
  2542. forwarddef:=true;
  2543. interfacedef:=false;
  2544. hasforward:=false;
  2545. _class := nil;
  2546. import_dll:=nil;
  2547. import_name:=nil;
  2548. import_nr:=0;
  2549. inlininginfo:=nil;
  2550. deprecatedmsg:=nil;
  2551. {$ifdef i386}
  2552. fpu_used:=maxfpuregs;
  2553. {$endif i386}
  2554. end;
  2555. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2556. var
  2557. i,aliasnamescount : longint;
  2558. level : byte;
  2559. begin
  2560. inherited ppuload(procdef,ppufile);
  2561. if po_has_mangledname in procoptions then
  2562. _mangledname:=stringdup(ppufile.getstring)
  2563. else
  2564. _mangledname:=nil;
  2565. extnumber:=ppufile.getword;
  2566. level:=ppufile.getbyte;
  2567. ppufile.getderef(_classderef);
  2568. ppufile.getderef(procsymderef);
  2569. ppufile.getposinfo(fileinfo);
  2570. visibility:=tvisibility(ppufile.getbyte);
  2571. ppufile.getsmallset(symoptions);
  2572. if sp_has_deprecated_msg in symoptions then
  2573. deprecatedmsg:=stringdup(ppufile.getstring)
  2574. else
  2575. deprecatedmsg:=nil;
  2576. {$ifdef powerpc}
  2577. { library symbol for AmigaOS/MorphOS }
  2578. ppufile.getderef(libsymderef);
  2579. {$endif powerpc}
  2580. { import stuff }
  2581. if po_has_importdll in procoptions then
  2582. import_dll:=stringdup(ppufile.getstring)
  2583. else
  2584. import_dll:=nil;
  2585. if po_has_importname in procoptions then
  2586. import_name:=stringdup(ppufile.getstring)
  2587. else
  2588. import_name:=nil;
  2589. import_nr:=ppufile.getword;
  2590. if (po_msgint in procoptions) then
  2591. messageinf.i:=ppufile.getlongint;
  2592. if (po_msgstr in procoptions) then
  2593. messageinf.str:=stringdup(ppufile.getstring);
  2594. if (po_dispid in procoptions) then
  2595. dispid:=ppufile.getlongint;
  2596. { inline stuff }
  2597. if (po_has_inlininginfo in procoptions) then
  2598. begin
  2599. ppufile.getderef(funcretsymderef);
  2600. new(inlininginfo);
  2601. ppufile.getsmallset(inlininginfo^.flags);
  2602. end
  2603. else
  2604. begin
  2605. inlininginfo:=nil;
  2606. funcretsym:=nil;
  2607. end;
  2608. aliasnames:=TCmdStrList.create;
  2609. { count alias names }
  2610. aliasnamescount:=ppufile.getbyte;
  2611. for i:=1 to aliasnamescount do
  2612. aliasnames.insert(ppufile.getstring);
  2613. { load para symtable }
  2614. parast:=tparasymtable.create(self,level);
  2615. tparasymtable(parast).ppuload(ppufile);
  2616. { load local symtable }
  2617. if (po_has_inlininginfo in procoptions) then
  2618. begin
  2619. localst:=tlocalsymtable.create(self,level);
  2620. tlocalsymtable(localst).ppuload(ppufile);
  2621. end
  2622. else
  2623. localst:=nil;
  2624. { inline stuff }
  2625. if (po_has_inlininginfo in procoptions) then
  2626. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2627. { default values for no persistent data }
  2628. if (cs_link_deffile in current_settings.globalswitches) and
  2629. (tf_need_export in target_info.flags) and
  2630. (po_exports in procoptions) then
  2631. deffile.AddExport(mangledname);
  2632. forwarddef:=false;
  2633. interfacedef:=false;
  2634. hasforward:=false;
  2635. { Disable po_has_inlining until the derefimpl is done }
  2636. exclude(procoptions,po_has_inlininginfo);
  2637. {$ifdef i386}
  2638. fpu_used:=maxfpuregs;
  2639. {$endif i386}
  2640. end;
  2641. destructor tprocdef.destroy;
  2642. begin
  2643. aliasnames.free;
  2644. aliasnames:=nil;
  2645. if assigned(localst) and
  2646. (localst.symtabletype<>staticsymtable) then
  2647. begin
  2648. {$ifdef MEMDEBUG}
  2649. memproclocalst.start;
  2650. {$endif MEMDEBUG}
  2651. localst.free;
  2652. localst:=nil;
  2653. {$ifdef MEMDEBUG}
  2654. memproclocalst.start;
  2655. {$endif MEMDEBUG}
  2656. end;
  2657. if assigned(inlininginfo) then
  2658. begin
  2659. {$ifdef MEMDEBUG}
  2660. memprocnodetree.start;
  2661. {$endif MEMDEBUG}
  2662. tnode(inlininginfo^.code).free;
  2663. {$ifdef MEMDEBUG}
  2664. memprocnodetree.start;
  2665. {$endif MEMDEBUG}
  2666. dispose(inlininginfo);
  2667. inlininginfo:=nil;
  2668. end;
  2669. stringdispose(resultname);
  2670. stringdispose(import_dll);
  2671. stringdispose(import_name);
  2672. stringdispose(deprecatedmsg);
  2673. if (po_msgstr in procoptions) then
  2674. stringdispose(messageinf.str);
  2675. if assigned(_mangledname) then
  2676. begin
  2677. {$ifdef MEMDEBUG}
  2678. memmanglednames.start;
  2679. {$endif MEMDEBUG}
  2680. stringdispose(_mangledname);
  2681. {$ifdef MEMDEBUG}
  2682. memmanglednames.stop;
  2683. {$endif MEMDEBUG}
  2684. end;
  2685. inherited destroy;
  2686. end;
  2687. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2688. var
  2689. oldintfcrc : boolean;
  2690. aliasnamescount : longint;
  2691. item : TCmdStrListItem;
  2692. begin
  2693. { released procdef? }
  2694. if not assigned(parast) then
  2695. exit;
  2696. inherited ppuwrite(ppufile);
  2697. if po_has_mangledname in procoptions then
  2698. ppufile.putstring(_mangledname^);
  2699. ppufile.putword(extnumber);
  2700. ppufile.putbyte(parast.symtablelevel);
  2701. ppufile.putderef(_classderef);
  2702. ppufile.putderef(procsymderef);
  2703. ppufile.putposinfo(fileinfo);
  2704. ppufile.putbyte(byte(visibility));
  2705. ppufile.putsmallset(symoptions);
  2706. if sp_has_deprecated_msg in symoptions then
  2707. ppufile.putstring(deprecatedmsg^);
  2708. {$ifdef powerpc}
  2709. { library symbol for AmigaOS/MorphOS }
  2710. ppufile.putderef(libsymderef);
  2711. {$endif powerpc}
  2712. { import }
  2713. if po_has_importdll in procoptions then
  2714. ppufile.putstring(import_dll^);
  2715. if po_has_importname in procoptions then
  2716. ppufile.putstring(import_name^);
  2717. ppufile.putword(import_nr);
  2718. if (po_msgint in procoptions) then
  2719. ppufile.putlongint(messageinf.i);
  2720. if (po_msgstr in procoptions) then
  2721. ppufile.putstring(messageinf.str^);
  2722. if (po_dispid in procoptions) then
  2723. ppufile.putlongint(dispid);
  2724. { inline stuff }
  2725. oldintfcrc:=ppufile.do_crc;
  2726. ppufile.do_crc:=false;
  2727. if (po_has_inlininginfo in procoptions) then
  2728. begin
  2729. ppufile.putderef(funcretsymderef);
  2730. ppufile.putsmallset(inlininginfo^.flags);
  2731. end;
  2732. { count alias names }
  2733. aliasnamescount:=0;
  2734. item:=TCmdStrListItem(aliasnames.first);
  2735. while assigned(item) do
  2736. begin
  2737. inc(aliasnamescount);
  2738. item:=TCmdStrListItem(item.next);
  2739. end;
  2740. if aliasnamescount>255 then
  2741. internalerror(200711021);
  2742. ppufile.putbyte(aliasnamescount);
  2743. item:=TCmdStrListItem(aliasnames.first);
  2744. while assigned(item) do
  2745. begin
  2746. ppufile.putstring(item.str);
  2747. item:=TCmdStrListItem(item.next);
  2748. end;
  2749. ppufile.do_crc:=oldintfcrc;
  2750. { write this entry }
  2751. ppufile.writeentry(ibprocdef);
  2752. { Save the para symtable, this is taken from the interface }
  2753. tparasymtable(parast).ppuwrite(ppufile);
  2754. { save localsymtable for inline procedures or when local
  2755. browser info is requested, this has no influence on the crc }
  2756. if (po_has_inlininginfo in procoptions) then
  2757. begin
  2758. oldintfcrc:=ppufile.do_crc;
  2759. ppufile.do_crc:=false;
  2760. tlocalsymtable(localst).ppuwrite(ppufile);
  2761. ppufile.do_crc:=oldintfcrc;
  2762. end;
  2763. { node tree for inlining }
  2764. oldintfcrc:=ppufile.do_crc;
  2765. ppufile.do_crc:=false;
  2766. if (po_has_inlininginfo in procoptions) then
  2767. ppuwritenodetree(ppufile,inlininginfo^.code);
  2768. ppufile.do_crc:=oldintfcrc;
  2769. end;
  2770. procedure tprocdef.reset;
  2771. begin
  2772. inherited reset;
  2773. procstarttai:=nil;
  2774. procendtai:=nil;
  2775. end;
  2776. function tprocdef.fullprocname(showhidden:boolean):string;
  2777. var
  2778. s : string;
  2779. t : ttoken;
  2780. begin
  2781. {$ifdef EXTDEBUG}
  2782. showhidden:=true;
  2783. {$endif EXTDEBUG}
  2784. s:='';
  2785. if owner.symtabletype=localsymtable then
  2786. s:=s+'local ';
  2787. if assigned(_class) then
  2788. begin
  2789. if po_classmethod in procoptions then
  2790. s:=s+'class ';
  2791. s:=s+_class.objrealname^+'.';
  2792. end;
  2793. if proctypeoption=potype_operator then
  2794. begin
  2795. for t:=NOTOKEN to last_overloaded do
  2796. if procsym.realname='$'+overloaded_names[t] then
  2797. begin
  2798. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2799. break;
  2800. end;
  2801. end
  2802. else
  2803. s:=s+procsym.realname+typename_paras(showhidden);
  2804. case proctypeoption of
  2805. potype_constructor:
  2806. s:='constructor '+s;
  2807. potype_destructor:
  2808. s:='destructor '+s;
  2809. else
  2810. if assigned(returndef) and
  2811. not(is_void(returndef)) then
  2812. s:=s+':'+returndef.GetTypeName;
  2813. end;
  2814. s:=s+';';
  2815. { forced calling convention? }
  2816. if (po_hascallingconvention in procoptions) then
  2817. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2818. if po_staticmethod in procoptions then
  2819. s:=s+' Static;';
  2820. fullprocname:=s;
  2821. end;
  2822. function tprocdef.is_methodpointer:boolean;
  2823. begin
  2824. result:=assigned(_class);
  2825. end;
  2826. function tprocdef.is_addressonly:boolean;
  2827. begin
  2828. result:=assigned(owner) and
  2829. (owner.symtabletype<>ObjectSymtable);
  2830. end;
  2831. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2832. begin
  2833. case t of
  2834. gs_local :
  2835. GetSymtable:=localst;
  2836. gs_para :
  2837. GetSymtable:=parast;
  2838. else
  2839. GetSymtable:=nil;
  2840. end;
  2841. end;
  2842. procedure tprocdef.buildderef;
  2843. begin
  2844. inherited buildderef;
  2845. _classderef.build(_class);
  2846. { procsym that originaly defined this definition, should be in the
  2847. same symtable }
  2848. procsymderef.build(procsym);
  2849. {$ifdef powerpc}
  2850. { library symbol for AmigaOS/MorphOS }
  2851. libsymderef.build(libsym);
  2852. {$endif powerpc}
  2853. end;
  2854. procedure tprocdef.buildderefimpl;
  2855. begin
  2856. inherited buildderefimpl;
  2857. { Localst is not available for main/unit init }
  2858. if assigned(localst) then
  2859. begin
  2860. tlocalsymtable(localst).buildderef;
  2861. tlocalsymtable(localst).buildderefimpl;
  2862. end;
  2863. { inline tree }
  2864. if (po_has_inlininginfo in procoptions) then
  2865. begin
  2866. funcretsymderef.build(funcretsym);
  2867. inlininginfo^.code.buildderefimpl;
  2868. end;
  2869. end;
  2870. procedure tprocdef.deref;
  2871. begin
  2872. inherited deref;
  2873. _class:=tobjectdef(_classderef.resolve);
  2874. { procsym that originaly defined this definition, should be in the
  2875. same symtable }
  2876. procsym:=tprocsym(procsymderef.resolve);
  2877. {$ifdef powerpc}
  2878. { library symbol for AmigaOS/MorphOS }
  2879. libsym:=tsym(libsymderef.resolve);
  2880. {$endif powerpc}
  2881. end;
  2882. procedure tprocdef.derefimpl;
  2883. begin
  2884. { Enable has_inlininginfo when the inlininginfo
  2885. structure is available. The has_inlininginfo was disabled
  2886. after the load, since the data was invalid }
  2887. if assigned(inlininginfo) then
  2888. include(procoptions,po_has_inlininginfo);
  2889. { Locals }
  2890. if assigned(localst) then
  2891. begin
  2892. tlocalsymtable(localst).deref;
  2893. tlocalsymtable(localst).derefimpl;
  2894. end;
  2895. { Inline }
  2896. if (po_has_inlininginfo in procoptions) then
  2897. begin
  2898. inlininginfo^.code.derefimpl;
  2899. { funcretsym, this is always located in the localst }
  2900. funcretsym:=tsym(funcretsymderef.resolve);
  2901. end
  2902. else
  2903. begin
  2904. { safety }
  2905. { Not safe! A unit may be reresolved after its interface has been
  2906. parsed but before its implementation has been parsed, and in that
  2907. case the funcretsym is still required!
  2908. funcretsym:=nil; }
  2909. end;
  2910. end;
  2911. function tprocdef.GetTypeName : string;
  2912. begin
  2913. GetTypeName := FullProcName(false);
  2914. end;
  2915. function tprocdef.mangledname : string;
  2916. var
  2917. hp : TParavarsym;
  2918. hs : string;
  2919. crc : dword;
  2920. newlen,
  2921. oldlen,
  2922. i : integer;
  2923. begin
  2924. if assigned(_mangledname) then
  2925. begin
  2926. {$ifdef compress}
  2927. mangledname:=minilzw_decode(_mangledname^);
  2928. {$else}
  2929. mangledname:=_mangledname^;
  2930. {$endif}
  2931. exit;
  2932. end;
  2933. { we need to use the symtable where the procsym is inserted,
  2934. because that is visible to the world }
  2935. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2936. oldlen:=length(mangledname);
  2937. { add parameter types }
  2938. for i:=0 to paras.count-1 do
  2939. begin
  2940. hp:=tparavarsym(paras[i]);
  2941. if not(vo_is_hidden_para in hp.varoptions) then
  2942. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2943. end;
  2944. { add resultdef, add $$ as separator to make it unique from a
  2945. parameter separator }
  2946. if not is_void(returndef) then
  2947. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2948. newlen:=length(mangledname);
  2949. { Replace with CRC if the parameter line is very long }
  2950. if (newlen-oldlen>12) and
  2951. ((newlen>128) or (newlen-oldlen>64)) then
  2952. begin
  2953. crc:=0;
  2954. for i:=0 to paras.count-1 do
  2955. begin
  2956. hp:=tparavarsym(paras[i]);
  2957. if not(vo_is_hidden_para in hp.varoptions) then
  2958. begin
  2959. hs:=hp.vardef.mangledparaname;
  2960. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2961. end;
  2962. end;
  2963. hs:=hp.vardef.mangledparaname;
  2964. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2965. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2966. end;
  2967. {$ifdef compress}
  2968. _mangledname:=stringdup(minilzw_encode(mangledname));
  2969. {$else}
  2970. _mangledname:=stringdup(mangledname);
  2971. {$endif}
  2972. end;
  2973. function tprocdef.cplusplusmangledname : string;
  2974. function getcppparaname(p : tdef) : string;
  2975. const
  2976. {$ifdef NAMEMANGLING_GCC2}
  2977. ordtype2str : array[tordtype] of string[2] = (
  2978. '',
  2979. 'Uc','Us','Ui','Us',
  2980. 'Sc','s','i','x',
  2981. 'b','b','b','b','b',
  2982. 'c','w','x');
  2983. {$else NAMEMANGLING_GCC2}
  2984. ordtype2str : array[tordtype] of string[1] = (
  2985. 'v',
  2986. 'h','t','j','y',
  2987. 'a','s','i','x',
  2988. 'b','b','b','b','b',
  2989. 'c','w','x');
  2990. floattype2str : array[tfloattype] of string[1] = (
  2991. 'f','d','e',
  2992. 'd','d','g');
  2993. {$endif NAMEMANGLING_GCC2}
  2994. var
  2995. s : string;
  2996. begin
  2997. case p.typ of
  2998. orddef:
  2999. s:=ordtype2str[torddef(p).ordtype];
  3000. pointerdef:
  3001. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3002. {$ifndef NAMEMANGLING_GCC2}
  3003. floatdef:
  3004. s:=floattype2str[tfloatdef(p).floattype];
  3005. {$endif NAMEMANGLING_GCC2}
  3006. else
  3007. internalerror(2103001);
  3008. end;
  3009. getcppparaname:=s;
  3010. end;
  3011. var
  3012. s,s2 : string;
  3013. hp : TParavarsym;
  3014. i : integer;
  3015. begin
  3016. {$ifdef NAMEMANGLING_GCC2}
  3017. { outdated gcc 2.x name mangling scheme }
  3018. s := procsym.realname;
  3019. if procsym.owner.symtabletype=ObjectSymtable then
  3020. begin
  3021. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3022. case proctypeoption of
  3023. potype_destructor:
  3024. s:='_$_'+tostr(length(s2))+s2;
  3025. potype_constructor:
  3026. s:='___'+tostr(length(s2))+s2;
  3027. else
  3028. s:='_'+s+'__'+tostr(length(s2))+s2;
  3029. end;
  3030. end
  3031. else s:=s+'__';
  3032. s:=s+'F';
  3033. { concat modifiers }
  3034. { !!!!! }
  3035. { now we handle the parameters }
  3036. if maxparacount>0 then
  3037. begin
  3038. for i:=0 to paras.count-1 do
  3039. begin
  3040. hp:=tparavarsym(paras[i]);
  3041. { no hidden parameters form part of a C++ mangled name:
  3042. a) self is not included
  3043. b) there are no "high" or other hidden parameters
  3044. }
  3045. if vo_is_hidden_para in hp.varoptions then
  3046. continue;
  3047. s2:=getcppparaname(hp.vardef);
  3048. if hp.varspez in [vs_var,vs_out] then
  3049. s2:='R'+s2;
  3050. s:=s+s2;
  3051. end;
  3052. end
  3053. else
  3054. s:=s+'v';
  3055. cplusplusmangledname:=s;
  3056. {$else NAMEMANGLING_GCC2}
  3057. { gcc 3.x and 4.x name mangling scheme }
  3058. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3059. if procsym.owner.symtabletype=ObjectSymtable then
  3060. begin
  3061. s:='_ZN';
  3062. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3063. s:=s+tostr(length(s2))+s2;
  3064. case proctypeoption of
  3065. potype_constructor:
  3066. s:=s+'C1';
  3067. potype_destructor:
  3068. s:=s+'D1';
  3069. else
  3070. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3071. end;
  3072. s:=s+'E';
  3073. end
  3074. else
  3075. s:=procsym.realname;
  3076. { now we handle the parameters }
  3077. if maxparacount>0 then
  3078. begin
  3079. for i:=0 to paras.count-1 do
  3080. begin
  3081. hp:=tparavarsym(paras[i]);
  3082. { no hidden parameters form part of a C++ mangled name:
  3083. a) self is not included
  3084. b) there are no "high" or other hidden parameters
  3085. }
  3086. if vo_is_hidden_para in hp.varoptions then
  3087. continue;
  3088. s2:=getcppparaname(hp.vardef);
  3089. if hp.varspez in [vs_var,vs_out] then
  3090. s2:='R'+s2;
  3091. s:=s+s2;
  3092. end;
  3093. end
  3094. else
  3095. s:=s+'v';
  3096. cplusplusmangledname:=s;
  3097. {$endif NAMEMANGLING_GCC2}
  3098. end;
  3099. function tprocdef.objcmangledname : string;
  3100. var
  3101. manglednamelen: longint;
  3102. iscatmethod : boolean;
  3103. begin
  3104. if not (po_msgstr in procoptions) then
  3105. internalerror(2009030901);
  3106. { we may very well need longer strings to handle these... }
  3107. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3108. length('+"[ ]"')+length(messageinf.str^);
  3109. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3110. if (iscatmethod) then
  3111. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3112. if manglednamelen>255 then
  3113. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3114. if not(po_classmethod in procoptions) then
  3115. result:='"-['
  3116. else
  3117. result:='"+[';
  3118. { quotes are necessary because the +/- otherwise confuse the assembler
  3119. into expecting a number
  3120. }
  3121. if iscatmethod then
  3122. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3123. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3124. if iscatmethod then
  3125. result:=result+')';
  3126. result:=result+' '+messageinf.str^+']"';
  3127. end;
  3128. procedure tprocdef.setmangledname(const s : string);
  3129. begin
  3130. { This is not allowed anymore, the forward declaration
  3131. already needs to create the correct mangledname, no changes
  3132. afterwards are allowed (PFV) }
  3133. { Exception: interface definitions in mode macpas, since in that }
  3134. { case no reference to the old name can exist yet (JM) }
  3135. if assigned(_mangledname) then
  3136. if ((m_mac in current_settings.modeswitches) and
  3137. (interfacedef)) then
  3138. stringdispose(_mangledname)
  3139. else
  3140. internalerror(200411171);
  3141. {$ifdef compress}
  3142. _mangledname:=stringdup(minilzw_encode(s));
  3143. {$else}
  3144. _mangledname:=stringdup(s);
  3145. {$endif}
  3146. include(procoptions,po_has_mangledname);
  3147. end;
  3148. {***************************************************************************
  3149. TPROCVARDEF
  3150. ***************************************************************************}
  3151. constructor tprocvardef.create(level:byte);
  3152. begin
  3153. inherited create(procvardef,level);
  3154. end;
  3155. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3156. begin
  3157. inherited ppuload(procvardef,ppufile);
  3158. { load para symtable }
  3159. parast:=tparasymtable.create(self,unknown_level);
  3160. tparasymtable(parast).ppuload(ppufile);
  3161. end;
  3162. function tprocvardef.getcopy : tstoreddef;
  3163. var
  3164. i : tcallercallee;
  3165. j : longint;
  3166. begin
  3167. result:=tprocvardef.create(parast.symtablelevel);
  3168. tprocvardef(result).returndef:=returndef;
  3169. tprocvardef(result).returndefderef:=returndefderef;
  3170. tprocvardef(result).parast:=parast.getcopy;
  3171. tprocvardef(result).savesize:=savesize;
  3172. { create paralist copy }
  3173. tprocvardef(result).paras:=tparalist.create(false);
  3174. tprocvardef(result).paras.count:=paras.count;
  3175. for j:=0 to paras.count-1 do
  3176. tprocvardef(result).paras[j]:=paras[j];
  3177. tprocvardef(result).proctypeoption:=proctypeoption;
  3178. tprocvardef(result).proccalloption:=proccalloption;
  3179. tprocvardef(result).procoptions:=procoptions;
  3180. tprocvardef(result).requiredargarea:=requiredargarea;
  3181. tprocvardef(result).maxparacount:=maxparacount;
  3182. tprocvardef(result).minparacount:=minparacount;
  3183. for i:=low(tcallercallee) to high(tcallercallee) do
  3184. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3185. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3186. {$ifdef m68k}
  3187. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3188. {$endif}
  3189. end;
  3190. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3191. begin
  3192. inherited ppuwrite(ppufile);
  3193. { Write this entry }
  3194. ppufile.writeentry(ibprocvardef);
  3195. { Save the para symtable, this is taken from the interface }
  3196. tparasymtable(parast).ppuwrite(ppufile);
  3197. end;
  3198. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3199. begin
  3200. case t of
  3201. gs_para :
  3202. GetSymtable:=parast;
  3203. else
  3204. GetSymtable:=nil;
  3205. end;
  3206. end;
  3207. function tprocvardef.size : aint;
  3208. begin
  3209. if (po_methodpointer in procoptions) and
  3210. not(po_addressonly in procoptions) then
  3211. size:=2*sizeof(pint)
  3212. else
  3213. size:=sizeof(pint);
  3214. end;
  3215. function tprocvardef.is_methodpointer:boolean;
  3216. begin
  3217. result:=(po_methodpointer in procoptions);
  3218. end;
  3219. function tprocvardef.is_addressonly:boolean;
  3220. begin
  3221. result:=not(po_methodpointer in procoptions) or
  3222. (po_addressonly in procoptions);
  3223. end;
  3224. function tprocvardef.getmangledparaname:string;
  3225. begin
  3226. result:='procvar';
  3227. end;
  3228. function tprocvardef.is_publishable : boolean;
  3229. begin
  3230. is_publishable:=(po_methodpointer in procoptions);
  3231. end;
  3232. function tprocvardef.GetTypeName : string;
  3233. var
  3234. s: string;
  3235. showhidden : boolean;
  3236. begin
  3237. {$ifdef EXTDEBUG}
  3238. showhidden:=true;
  3239. {$else EXTDEBUG}
  3240. showhidden:=false;
  3241. {$endif EXTDEBUG}
  3242. s:='<';
  3243. if po_classmethod in procoptions then
  3244. s := s+'class method type of'
  3245. else
  3246. if po_addressonly in procoptions then
  3247. s := s+'address of'
  3248. else
  3249. s := s+'procedure variable type of';
  3250. if po_local in procoptions then
  3251. s := s+' local';
  3252. if assigned(returndef) and
  3253. (returndef<>voidtype) then
  3254. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3255. else
  3256. s:=s+' procedure'+typename_paras(showhidden);
  3257. if po_methodpointer in procoptions then
  3258. s := s+' of object';
  3259. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3260. end;
  3261. {***************************************************************************
  3262. TOBJECTDEF
  3263. ***************************************************************************}
  3264. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3265. begin
  3266. inherited create(objectdef);
  3267. objecttype:=ot;
  3268. objectoptions:=[];
  3269. childof:=nil;
  3270. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3271. { create space for vmt !! }
  3272. vmtentries:=TFPList.Create;
  3273. vmt_offset:=0;
  3274. set_parent(c);
  3275. objname:=stringdup(upper(n));
  3276. objrealname:=stringdup(n);
  3277. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3278. prepareguid;
  3279. { setup implemented interfaces }
  3280. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3281. ImplementedInterfaces:=TFPObjectList.Create(true)
  3282. else
  3283. ImplementedInterfaces:=nil;
  3284. writing_class_record_dbginfo:=false;
  3285. end;
  3286. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3287. var
  3288. i,
  3289. implintfcount : longint;
  3290. d : tderef;
  3291. ImplIntf : TImplementedInterface;
  3292. vmtentry : pvmtentry;
  3293. begin
  3294. inherited ppuload(objectdef,ppufile);
  3295. objecttype:=tobjecttyp(ppufile.getbyte);
  3296. objrealname:=stringdup(ppufile.getstring);
  3297. objname:=stringdup(upper(objrealname^));
  3298. objextname:=stringdup(ppufile.getstring);
  3299. { only used for external Objective-C classes/protocols }
  3300. if (objextname^='') then
  3301. stringdispose(objextname);
  3302. import_lib:=stringdup(ppufile.getstring);
  3303. { only used for external C++ classes }
  3304. if (import_lib^='') then
  3305. stringdispose(import_lib);
  3306. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3307. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3308. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3309. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3310. vmt_offset:=ppufile.getlongint;
  3311. ppufile.getderef(childofderef);
  3312. ppufile.getsmallset(objectoptions);
  3313. { load guid }
  3314. iidstr:=nil;
  3315. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3316. begin
  3317. new(iidguid);
  3318. ppufile.getguid(iidguid^);
  3319. iidstr:=stringdup(ppufile.getstring);
  3320. end;
  3321. vmtentries:=TFPList.Create;
  3322. vmtentries.count:=ppufile.getlongint;
  3323. for i:=0 to vmtentries.count-1 do
  3324. begin
  3325. ppufile.getderef(d);
  3326. new(vmtentry);
  3327. vmtentry^.procdef:=nil;
  3328. vmtentry^.procdefderef:=d;
  3329. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3330. vmtentries[i]:=vmtentry;
  3331. end;
  3332. { load implemented interfaces }
  3333. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3334. begin
  3335. ImplementedInterfaces:=TFPObjectList.Create(true);
  3336. implintfcount:=ppufile.getlongint;
  3337. for i:=0 to implintfcount-1 do
  3338. begin
  3339. ppufile.getderef(d);
  3340. ImplIntf:=TImplementedInterface.Create_deref(d);
  3341. ImplIntf.IOffset:=ppufile.getlongint;
  3342. ImplementedInterfaces.Add(ImplIntf);
  3343. end;
  3344. end
  3345. else
  3346. ImplementedInterfaces:=nil;
  3347. if df_copied_def in defoptions then
  3348. ppufile.getderef(cloneddefderef)
  3349. else
  3350. tObjectSymtable(symtable).ppuload(ppufile);
  3351. { handles the predefined class tobject }
  3352. { the last TOBJECT which is loaded gets }
  3353. { it ! }
  3354. if (childof=nil) and
  3355. (objecttype=odt_class) and
  3356. (objname^='TOBJECT') then
  3357. class_tobject:=self;
  3358. if (childof=nil) and
  3359. (objecttype=odt_interfacecom) and
  3360. (objname^='IUNKNOWN') then
  3361. interface_iunknown:=self;
  3362. if (childof=nil) and
  3363. (objecttype=odt_objcclass) and
  3364. (objname^='PROTOCOL') then
  3365. objc_protocoltype:=self;
  3366. writing_class_record_dbginfo:=false;
  3367. end;
  3368. destructor tobjectdef.destroy;
  3369. begin
  3370. if assigned(symtable) then
  3371. begin
  3372. symtable.free;
  3373. symtable:=nil;
  3374. end;
  3375. stringdispose(objname);
  3376. stringdispose(objrealname);
  3377. stringdispose(objextname);
  3378. stringdispose(import_lib);
  3379. stringdispose(iidstr);
  3380. if assigned(ImplementedInterfaces) then
  3381. begin
  3382. ImplementedInterfaces.free;
  3383. ImplementedInterfaces:=nil;
  3384. end;
  3385. if assigned(iidguid) then
  3386. begin
  3387. dispose(iidguid);
  3388. iidguid:=nil;
  3389. end;
  3390. if assigned(vmtentries) then
  3391. begin
  3392. resetvmtentries;
  3393. vmtentries.free;
  3394. vmtentries:=nil;
  3395. end;
  3396. if assigned(vmcallstaticinfo) then
  3397. begin
  3398. freemem(vmcallstaticinfo);
  3399. vmcallstaticinfo:=nil;
  3400. end;
  3401. inherited destroy;
  3402. end;
  3403. function tobjectdef.getcopy : tstoreddef;
  3404. var
  3405. i : longint;
  3406. begin
  3407. result:=tobjectdef.create(objecttype,objname^,childof);
  3408. { the constructor allocates a symtable which we release to avoid memory leaks }
  3409. tobjectdef(result).symtable.free;
  3410. tobjectdef(result).symtable:=symtable.getcopy;
  3411. if assigned(objname) then
  3412. tobjectdef(result).objname:=stringdup(objname^);
  3413. if assigned(objrealname) then
  3414. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3415. if assigned(objextname) then
  3416. tobjectdef(result).objextname:=stringdup(objextname^);
  3417. if assigned(import_lib) then
  3418. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3419. tobjectdef(result).objectoptions:=objectoptions;
  3420. include(tobjectdef(result).defoptions,df_copied_def);
  3421. tobjectdef(result).vmt_offset:=vmt_offset;
  3422. if assigned(iidguid) then
  3423. begin
  3424. new(tobjectdef(result).iidguid);
  3425. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3426. end;
  3427. if assigned(iidstr) then
  3428. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3429. if assigned(ImplementedInterfaces) then
  3430. begin
  3431. for i:=0 to ImplementedInterfaces.count-1 do
  3432. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3433. end;
  3434. if assigned(vmtentries) then
  3435. begin
  3436. tobjectdef(result).vmtentries:=TFPList.Create;
  3437. tobjectdef(result).copyvmtentries(self);
  3438. end;
  3439. end;
  3440. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3441. var
  3442. i : longint;
  3443. vmtentry : pvmtentry;
  3444. ImplIntf : TImplementedInterface;
  3445. old_do_indirect_crc: boolean;
  3446. begin
  3447. { if class1 in unit A changes, and class2 in unit B inherits from it
  3448. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3449. However, if there is also a class3 in unit C that only depends on
  3450. unit B, then unit C will not be recompiled because nothing changed
  3451. to the interface of unit B. Nevertheless, unit C can indirectly
  3452. depend on unit A via derefs, and these must be updated -> the
  3453. indirect crc keeps track of such changes. }
  3454. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3455. ppufile.do_indirect_crc:=true;
  3456. inherited ppuwrite(ppufile);
  3457. ppufile.putbyte(byte(objecttype));
  3458. ppufile.putstring(objrealname^);
  3459. if assigned(objextname) then
  3460. ppufile.putstring(objextname^)
  3461. else
  3462. ppufile.putstring('');
  3463. if assigned(import_lib) then
  3464. ppufile.putstring(import_lib^)
  3465. else
  3466. ppufile.putstring('');
  3467. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3468. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3469. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3470. ppufile.putlongint(vmt_offset);
  3471. ppufile.putderef(childofderef);
  3472. ppufile.putsmallset(objectoptions);
  3473. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3474. begin
  3475. ppufile.putguid(iidguid^);
  3476. ppufile.putstring(iidstr^);
  3477. end;
  3478. ppufile.putlongint(vmtentries.count);
  3479. for i:=0 to vmtentries.count-1 do
  3480. begin
  3481. vmtentry:=pvmtentry(vmtentries[i]);
  3482. ppufile.putderef(vmtentry^.procdefderef);
  3483. ppufile.putbyte(byte(vmtentry^.visibility));
  3484. end;
  3485. if assigned(ImplementedInterfaces) then
  3486. begin
  3487. ppufile.putlongint(ImplementedInterfaces.Count);
  3488. for i:=0 to ImplementedInterfaces.Count-1 do
  3489. begin
  3490. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3491. ppufile.putderef(ImplIntf.intfdefderef);
  3492. ppufile.putlongint(ImplIntf.Ioffset);
  3493. end;
  3494. end;
  3495. if df_copied_def in defoptions then
  3496. ppufile.putderef(cloneddefderef);
  3497. ppufile.writeentry(ibobjectdef);
  3498. if not(df_copied_def in defoptions) then
  3499. tObjectSymtable(symtable).ppuwrite(ppufile);
  3500. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3501. end;
  3502. function tobjectdef.GetTypeName:string;
  3503. begin
  3504. { in this case we will go in endless recursion, because then }
  3505. { there is no tsym associated yet with the def. It can occur }
  3506. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3507. { instead of the actual type name }
  3508. if not assigned(typesym) then
  3509. result:='<Currently Parsed Class>'
  3510. else
  3511. result:=typename;
  3512. end;
  3513. procedure tobjectdef.buildderef;
  3514. var
  3515. i : longint;
  3516. vmtentry : pvmtentry;
  3517. begin
  3518. inherited buildderef;
  3519. childofderef.build(childof);
  3520. if df_copied_def in defoptions then
  3521. cloneddefderef.build(symtable.defowner)
  3522. else
  3523. tstoredsymtable(symtable).buildderef;
  3524. for i:=0 to vmtentries.count-1 do
  3525. begin
  3526. vmtentry:=pvmtentry(vmtentries[i]);
  3527. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3528. end;
  3529. if assigned(ImplementedInterfaces) then
  3530. begin
  3531. for i:=0 to ImplementedInterfaces.count-1 do
  3532. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3533. end;
  3534. end;
  3535. procedure tobjectdef.deref;
  3536. var
  3537. i : longint;
  3538. vmtentry : pvmtentry;
  3539. begin
  3540. inherited deref;
  3541. childof:=tobjectdef(childofderef.resolve);
  3542. if df_copied_def in defoptions then
  3543. begin
  3544. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3545. symtable:=cloneddef.symtable.getcopy;
  3546. end
  3547. else
  3548. tstoredsymtable(symtable).deref;
  3549. for i:=0 to vmtentries.count-1 do
  3550. begin
  3551. vmtentry:=pvmtentry(vmtentries[i]);
  3552. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3553. end;
  3554. if assigned(ImplementedInterfaces) then
  3555. begin
  3556. for i:=0 to ImplementedInterfaces.count-1 do
  3557. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3558. end;
  3559. end;
  3560. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3561. var
  3562. pd: tprocdef absolute def;
  3563. st: tsymtable;
  3564. psym: tsym;
  3565. nname: TIDString;
  3566. begin
  3567. if (tdef(def).typ<>procdef) then
  3568. exit;
  3569. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3570. owner = symtable in which objcclassdef is defined
  3571. }
  3572. st:=pd.owner.defowner.owner;
  3573. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3574. { check for an existing procsym with our special name }
  3575. psym:=tsym(st.find(nname));
  3576. if not assigned(psym) then
  3577. begin
  3578. psym:=tprocsym.create(nname);
  3579. { avoid warning about this symbol being unused }
  3580. psym.IncRefCount;
  3581. st.insert(psym,true);
  3582. end
  3583. else if (psym.typ<>procsym) then
  3584. internalerror(2009111501);
  3585. { add ourselves to this special procsym }
  3586. tprocsym(psym).procdeflist.add(def);
  3587. end;
  3588. procedure tobjectdef.buildderefimpl;
  3589. begin
  3590. inherited buildderefimpl;
  3591. if not (df_copied_def in defoptions) then
  3592. tstoredsymtable(symtable).buildderefimpl;
  3593. end;
  3594. procedure tobjectdef.derefimpl;
  3595. begin
  3596. inherited derefimpl;
  3597. if not (df_copied_def in defoptions) then
  3598. tstoredsymtable(symtable).derefimpl;
  3599. { the procdefs are not owned by the class helper procsyms, so they
  3600. are not stored/restored either -> re-add them here }
  3601. if (objecttype=odt_objcclass) or
  3602. (oo_is_classhelper in objectoptions) then
  3603. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3604. end;
  3605. procedure tobjectdef.resetvmtentries;
  3606. var
  3607. i : longint;
  3608. begin
  3609. for i:=0 to vmtentries.Count-1 do
  3610. Dispose(pvmtentry(vmtentries[i]));
  3611. vmtentries.clear;
  3612. end;
  3613. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3614. var
  3615. i : longint;
  3616. vmtentry : pvmtentry;
  3617. begin
  3618. resetvmtentries;
  3619. vmtentries.count:=objdef.vmtentries.count;
  3620. for i:=0 to objdef.vmtentries.count-1 do
  3621. begin
  3622. new(vmtentry);
  3623. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3624. vmtentries[i]:=vmtentry;
  3625. end;
  3626. end;
  3627. function tobjectdef.getparentdef:tdef;
  3628. begin
  3629. { TODO: Remove getparentdef hack}
  3630. { With 2 forward declared classes with the child class before the
  3631. parent class the child class is written earlier to the ppu. Leaving it
  3632. possible to have a reference to the parent class for property overriding,
  3633. but the parent class still has the childof not resolved yet (PFV) }
  3634. if childof=nil then
  3635. childof:=tobjectdef(childofderef.resolve);
  3636. result:=childof;
  3637. end;
  3638. procedure tobjectdef.prepareguid;
  3639. begin
  3640. { set up guid }
  3641. if not assigned(iidguid) then
  3642. begin
  3643. new(iidguid);
  3644. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3645. end;
  3646. { setup iidstring }
  3647. if not assigned(iidstr) then
  3648. iidstr:=stringdup(''); { default is empty string }
  3649. end;
  3650. procedure tobjectdef.set_parent( c : tobjectdef);
  3651. begin
  3652. if assigned(childof) then
  3653. exit;
  3654. childof:=c;
  3655. if not assigned(c) then
  3656. exit;
  3657. { inherit options and status }
  3658. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3659. { add the data of the anchestor class/object }
  3660. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3661. begin
  3662. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3663. { inherit recordalignment }
  3664. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3665. if (oo_has_vmt in objectoptions) and
  3666. (oo_has_vmt in c.objectoptions) then
  3667. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3668. { if parent has a vmt field then the offset is the same for the child PM }
  3669. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3670. begin
  3671. vmt_offset:=c.vmt_offset;
  3672. include(objectoptions,oo_has_vmt);
  3673. end;
  3674. end;
  3675. end;
  3676. procedure tobjectdef.insertvmt;
  3677. var
  3678. vs: tfieldvarsym;
  3679. begin
  3680. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3681. exit;
  3682. if (oo_has_vmt in objectoptions) then
  3683. internalerror(12345)
  3684. else
  3685. begin
  3686. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3687. tObjectSymtable(symtable).fieldalignment);
  3688. if (tf_requires_proper_alignment in target_info.flags) then
  3689. begin
  3690. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3691. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3692. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3693. end;
  3694. vmt_offset:=tObjectSymtable(symtable).datasize;
  3695. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3696. hidesym(vs);
  3697. tObjectSymtable(symtable).insert(vs);
  3698. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3699. include(objectoptions,oo_has_vmt);
  3700. end;
  3701. end;
  3702. procedure tobjectdef.check_forwards;
  3703. begin
  3704. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3705. tstoredsymtable(symtable).check_forwards;
  3706. if (oo_is_forward in objectoptions) then
  3707. begin
  3708. { ok, in future, the forward can be resolved }
  3709. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3710. exclude(objectoptions,oo_is_forward);
  3711. end;
  3712. end;
  3713. { true if prot implements d (or if they are equal) }
  3714. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3715. var
  3716. i : longint;
  3717. begin
  3718. { objcprotocols have multiple inheritance, all protocols from which
  3719. the current protocol inherits are stored in implementedinterfaces }
  3720. result:=prot=d;
  3721. if result then
  3722. exit;
  3723. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3724. begin
  3725. result:=is_related_protocol(tobjectdef(prot.ImplementedInterfaces[i]),d);
  3726. if result then
  3727. exit;
  3728. end;
  3729. end;
  3730. { true, if self inherits from d (or if they are equal) }
  3731. function tobjectdef.is_related(d : tdef) : boolean;
  3732. var
  3733. hp : tobjectdef;
  3734. begin
  3735. if (objecttype=odt_objcprotocol) then
  3736. begin
  3737. is_related:=is_related_protocol(self,d);
  3738. exit
  3739. end;
  3740. hp:=self;
  3741. while assigned(hp) do
  3742. begin
  3743. if hp=d then
  3744. begin
  3745. is_related:=true;
  3746. exit;
  3747. end;
  3748. hp:=hp.childof;
  3749. end;
  3750. is_related:=false;
  3751. end;
  3752. function tobjectdef.FindDestructor : tprocdef;
  3753. var
  3754. objdef : tobjectdef;
  3755. i : longint;
  3756. sym : tsym;
  3757. pd : tprocdef;
  3758. begin
  3759. result:=nil;
  3760. objdef:=self;
  3761. while assigned(objdef) do
  3762. begin
  3763. for i:=0 to objdef.symtable.SymList.Count-1 do
  3764. begin
  3765. sym:=TSym(objdef.symtable.SymList[i]);
  3766. if sym.typ=procsym then
  3767. begin
  3768. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3769. if assigned(pd) then
  3770. begin
  3771. result:=pd;
  3772. exit;
  3773. end;
  3774. end;
  3775. end;
  3776. objdef:=objdef.childof;
  3777. end;
  3778. end;
  3779. function tobjectdef.implements_any_interfaces: boolean;
  3780. begin
  3781. result := (ImplementedInterfaces.Count > 0) or
  3782. (assigned(childof) and childof.implements_any_interfaces);
  3783. end;
  3784. function tobjectdef.size : aint;
  3785. begin
  3786. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3787. result:=sizeof(pint)
  3788. else
  3789. result:=tObjectSymtable(symtable).datasize;
  3790. end;
  3791. function tobjectdef.alignment:shortint;
  3792. begin
  3793. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3794. alignment:=sizeof(pint)
  3795. else
  3796. alignment:=tObjectSymtable(symtable).recordalignment;
  3797. end;
  3798. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3799. begin
  3800. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3801. case objecttype of
  3802. odt_class:
  3803. { the +2*sizeof(pint) is size and -size }
  3804. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3805. odt_objcclass,
  3806. odt_objcprotocol:
  3807. vmtmethodoffset:=0;
  3808. odt_interfacecom,odt_interfacecorba:
  3809. vmtmethodoffset:=index*sizeof(pint);
  3810. else
  3811. {$ifdef WITHDMT}
  3812. vmtmethodoffset:=(index+4)*sizeof(pint);
  3813. {$else WITHDMT}
  3814. vmtmethodoffset:=(index+3)*sizeof(pint);
  3815. {$endif WITHDMT}
  3816. end;
  3817. end;
  3818. function tobjectdef.vmt_mangledname : string;
  3819. begin
  3820. if not(oo_has_vmt in objectoptions) then
  3821. Message1(parser_n_object_has_no_vmt,objrealname^);
  3822. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3823. end;
  3824. function tobjectdef.needs_inittable : boolean;
  3825. begin
  3826. case objecttype of
  3827. odt_dispinterface,
  3828. odt_class :
  3829. needs_inittable:=false;
  3830. odt_interfacecom:
  3831. needs_inittable:=true;
  3832. odt_interfacecorba:
  3833. needs_inittable:=is_related(interface_iunknown);
  3834. odt_object:
  3835. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3836. odt_cppclass,
  3837. odt_objcclass,
  3838. odt_objcprotocol:
  3839. needs_inittable:=false;
  3840. else
  3841. internalerror(200108267);
  3842. end;
  3843. end;
  3844. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3845. begin
  3846. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3847. result:=inherited rtti_mangledname(rt)
  3848. else
  3849. begin
  3850. if not(target_info.system in system_objc_nfabi) then
  3851. begin
  3852. result:=target_asm.labelprefix;
  3853. case objecttype of
  3854. odt_objcclass:
  3855. begin
  3856. case rt of
  3857. objcclassrtti:
  3858. if not(oo_is_classhelper in objectoptions) then
  3859. result:=result+'_OBJC_CLASS_'
  3860. else
  3861. result:=result+'_OBJC_CATEGORY_';
  3862. objcmetartti:
  3863. if not(oo_is_classhelper in objectoptions) then
  3864. result:=result+'_OBJC_METACLASS_'
  3865. else
  3866. internalerror(2009111511);
  3867. else
  3868. internalerror(2009092302);
  3869. end;
  3870. end;
  3871. odt_objcprotocol:
  3872. result:=result+'_OBJC_PROTOCOL_';
  3873. end;
  3874. end
  3875. else
  3876. begin
  3877. case objecttype of
  3878. odt_objcclass:
  3879. begin
  3880. if (oo_is_classhelper in objectoptions) and
  3881. (rt<>objcclassrtti) then
  3882. internalerror(2009111512);
  3883. case rt of
  3884. objcclassrtti:
  3885. if not(oo_is_classhelper in objectoptions) then
  3886. result:='_OBJC_CLASS_$_'
  3887. else
  3888. result:='_OBJC_$_CATEGORY_';
  3889. objcmetartti:
  3890. result:='_OBJC_METACLASS_$_';
  3891. objcclassrortti:
  3892. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  3893. objcmetarortti:
  3894. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  3895. else
  3896. internalerror(2009092303);
  3897. end;
  3898. end;
  3899. odt_objcprotocol:
  3900. begin
  3901. result:=lower(target_asm.labelprefix);
  3902. case rt of
  3903. objcclassrtti:
  3904. result:=result+'_OBJC_PROTOCOL_$_';
  3905. objcmetartti:
  3906. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  3907. else
  3908. internalerror(2009092501);
  3909. end;
  3910. end;
  3911. end;
  3912. end;
  3913. result:=result+objextname^;
  3914. end;
  3915. end;
  3916. function tobjectdef.members_need_inittable : boolean;
  3917. begin
  3918. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3919. end;
  3920. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3921. var
  3922. ImplIntf : TImplementedInterface;
  3923. i : longint;
  3924. begin
  3925. result:=nil;
  3926. if not assigned(ImplementedInterfaces) then
  3927. exit;
  3928. for i:=0 to ImplementedInterfaces.Count-1 do
  3929. begin
  3930. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3931. if ImplIntf.intfdef=aintfdef then
  3932. begin
  3933. result:=ImplIntf;
  3934. exit;
  3935. end;
  3936. end;
  3937. end;
  3938. function tobjectdef.is_publishable : boolean;
  3939. begin
  3940. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3941. end;
  3942. procedure tobjectdef.reset;
  3943. begin
  3944. inherited reset;
  3945. created_in_current_module:=false;
  3946. maybe_created_in_current_module:=false;
  3947. classref_created_in_current_module:=false;
  3948. end;
  3949. function tobjectdef.search_enumerator_get: tprocdef;
  3950. var
  3951. objdef : tobjectdef;
  3952. sym : tsym;
  3953. i : integer;
  3954. pd : tprocdef;
  3955. hashedid : THashedIDString;
  3956. begin
  3957. result:=nil;
  3958. objdef:=self;
  3959. hashedid.id:='GETENUMERATOR';
  3960. while assigned(objdef) do
  3961. begin
  3962. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3963. if assigned(sym) and (sym.typ=procsym) then
  3964. begin
  3965. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3966. begin
  3967. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3968. if (pd.proctypeoption = potype_function) and
  3969. is_class_or_interface_or_object(pd.returndef) and
  3970. (pd.visibility >= vis_public) then
  3971. begin
  3972. result:=pd;
  3973. exit;
  3974. end;
  3975. end;
  3976. end;
  3977. objdef:=objdef.childof;
  3978. end;
  3979. end;
  3980. function tobjectdef.search_enumerator_move: tprocdef;
  3981. var
  3982. objdef : tobjectdef;
  3983. sym : tsym;
  3984. i : integer;
  3985. pd : tprocdef;
  3986. hashedid : THashedIDString;
  3987. begin
  3988. result:=nil;
  3989. objdef:=self;
  3990. // first search for po_enumerator_movenext method modifier
  3991. // then search for public function MoveNext: Boolean
  3992. hashedid.id:='MOVENEXT';
  3993. while assigned(objdef) do
  3994. begin
  3995. for i:=0 to objdef.symtable.SymList.Count-1 do
  3996. begin
  3997. sym:=TSym(objdef.symtable.SymList[i]);
  3998. if (sym.typ=procsym) then
  3999. begin
  4000. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4001. if assigned(pd) then
  4002. begin
  4003. result:=pd;
  4004. exit;
  4005. end;
  4006. end;
  4007. end;
  4008. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4009. if assigned(sym) and (sym.typ=procsym) then
  4010. begin
  4011. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4012. begin
  4013. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4014. if (pd.proctypeoption = potype_function) and
  4015. is_boolean(pd.returndef) and
  4016. (pd.minparacount = 0) and
  4017. (pd.visibility >= vis_public) then
  4018. begin
  4019. result:=pd;
  4020. exit;
  4021. end;
  4022. end;
  4023. end;
  4024. objdef:=objdef.childof;
  4025. end;
  4026. end;
  4027. function tobjectdef.search_enumerator_current: tsym;
  4028. var
  4029. objdef : tobjectdef;
  4030. sym: tsym;
  4031. i: integer;
  4032. hashedid : THashedIDString;
  4033. begin
  4034. result:=nil;
  4035. objdef:=self;
  4036. hashedid.id:='CURRENT';
  4037. // first search for ppo_enumerator_current property modifier
  4038. // then search for public property Current
  4039. while assigned(objdef) do
  4040. begin
  4041. for i:=0 to objdef.symtable.SymList.Count-1 do
  4042. begin
  4043. sym:=TSym(objdef.symtable.SymList[i]);
  4044. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4045. begin
  4046. result:=sym;
  4047. exit;
  4048. end;
  4049. end;
  4050. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4051. if assigned(sym) and (sym.typ=propertysym) and
  4052. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4053. begin
  4054. result:=sym;
  4055. exit;
  4056. end;
  4057. objdef:=objdef.childof;
  4058. end;
  4059. end;
  4060. procedure tobjectdef.register_created_classref_type;
  4061. begin
  4062. if not classref_created_in_current_module then
  4063. begin
  4064. classref_created_in_current_module:=true;
  4065. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4066. end;
  4067. end;
  4068. procedure tobjectdef.register_created_object_type;
  4069. begin
  4070. if not created_in_current_module then
  4071. begin
  4072. created_in_current_module:=true;
  4073. current_module.wpoinfo.addcreatedobjtype(self);
  4074. end;
  4075. end;
  4076. procedure tobjectdef.register_maybe_created_object_type;
  4077. begin
  4078. { if we know it has been created for sure, no need
  4079. to also record that it maybe can be created in
  4080. this module
  4081. }
  4082. if not (created_in_current_module) and
  4083. not (maybe_created_in_current_module) then
  4084. begin
  4085. maybe_created_in_current_module:=true;
  4086. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4087. end;
  4088. end;
  4089. procedure tobjectdef.register_vmt_call(index: longint);
  4090. begin
  4091. if (is_object(self) or is_class(self)) then
  4092. current_module.wpoinfo.addcalledvmtentry(self,index);
  4093. end;
  4094. procedure make_procdef_external(data: tobject; arg: pointer);
  4095. var
  4096. def: tdef absolute data;
  4097. begin
  4098. if (def.typ = procdef) then
  4099. begin
  4100. include(tprocdef(def).procoptions,po_external);
  4101. tprocdef(def).forwarddef:=false;
  4102. end;
  4103. end;
  4104. procedure tobjectdef.make_all_methods_external;
  4105. begin
  4106. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4107. end;
  4108. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4109. var
  4110. def: tdef absolute data;
  4111. pd: tprocdef absolute data;
  4112. i,
  4113. paracount: longint;
  4114. begin
  4115. if (def.typ=procdef) then
  4116. begin
  4117. { add all messages also under a dummy name to the symtable in
  4118. which the objcclass/protocol/category is declared, so they can
  4119. be called via id.<name>
  4120. }
  4121. create_class_helper_for_procdef(pd,nil);
  4122. { we have to wait until now to set the mangled name because it
  4123. depends on the (possibly external) class name, which is defined
  4124. at the very end. }
  4125. if not(po_msgstr in pd.procoptions) then
  4126. begin
  4127. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4128. { recover to avoid internalerror later on }
  4129. include(pd.procoptions,po_msgstr);
  4130. pd.messageinf.str:=stringdup('MissingDeclaration');
  4131. end;
  4132. { Mangled name is already set in case this is a copy of
  4133. another type. }
  4134. if not(po_has_mangledname in pd.procoptions) then
  4135. begin
  4136. { check whether the number of formal parameters is correct }
  4137. paracount:=0;
  4138. for i:=1 to length(pd.messageinf.str^) do
  4139. if pd.messageinf.str^[i]=':' then
  4140. inc(paracount);
  4141. for i:=0 to pd.paras.count-1 do
  4142. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4143. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4144. dec(paracount);
  4145. if (paracount<>0) then
  4146. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4147. pd.setmangledname(pd.objcmangledname);
  4148. end
  4149. else
  4150. { all checks already done }
  4151. exit;
  4152. if not(oo_is_external in pd._class.objectoptions) then
  4153. begin
  4154. if (po_varargs in pd.procoptions) then
  4155. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4156. else
  4157. begin
  4158. { check for "array of const" parameters }
  4159. for i:=0 to pd.parast.symlist.count-1 do
  4160. begin
  4161. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4162. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4163. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4164. end;
  4165. end;
  4166. end;
  4167. end;
  4168. end;
  4169. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4170. var
  4171. sym: tsym absolute data;
  4172. begin
  4173. if (sym.typ=fieldvarsym) and
  4174. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4175. sym.IncRefCount;
  4176. end;
  4177. procedure tobjectdef.finish_objc_data;
  4178. begin
  4179. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4180. if (oo_is_external in objectoptions) then
  4181. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4182. end;
  4183. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4184. var
  4185. def: tdef absolute data;
  4186. pd: tprocdef absolute data;
  4187. begin
  4188. if (def.typ=procdef) then
  4189. begin
  4190. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4191. if (oo_is_external in pd._class.objectoptions) then
  4192. begin
  4193. { copied from psub.read_proc }
  4194. if assigned(pd._class.import_lib) then
  4195. current_module.AddExternalImport(pd._class.import_lib^,pd.mangledname,0,false,false)
  4196. else
  4197. begin
  4198. { add import name to external list for DLL scanning }
  4199. if tf_has_dllscanner in target_info.flags then
  4200. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4201. end;
  4202. end;
  4203. end;
  4204. end;
  4205. procedure tobjectdef.finish_cpp_data;
  4206. begin
  4207. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4208. end;
  4209. {****************************************************************************
  4210. TImplementedInterface
  4211. ****************************************************************************}
  4212. constructor TImplementedInterface.create(aintf: tobjectdef);
  4213. begin
  4214. inherited create;
  4215. intfdef:=aintf;
  4216. IOffset:=-1;
  4217. IType:=etStandard;
  4218. NameMappings:=nil;
  4219. procdefs:=nil;
  4220. end;
  4221. constructor TImplementedInterface.create_deref(d:tderef);
  4222. begin
  4223. inherited create;
  4224. intfdef:=nil;
  4225. intfdefderef:=d;
  4226. IOffset:=-1;
  4227. IType:=etStandard;
  4228. NameMappings:=nil;
  4229. procdefs:=nil;
  4230. end;
  4231. destructor TImplementedInterface.destroy;
  4232. var
  4233. i : longint;
  4234. mappedname : pshortstring;
  4235. begin
  4236. if assigned(NameMappings) then
  4237. begin
  4238. for i:=0 to NameMappings.Count-1 do
  4239. begin
  4240. mappedname:=pshortstring(NameMappings[i]);
  4241. stringdispose(mappedname);
  4242. end;
  4243. NameMappings.free;
  4244. NameMappings:=nil;
  4245. end;
  4246. if assigned(procdefs) then
  4247. begin
  4248. procdefs.free;
  4249. procdefs:=nil;
  4250. end;
  4251. inherited destroy;
  4252. end;
  4253. procedure TImplementedInterface.buildderef;
  4254. begin
  4255. intfdefderef.build(intfdef);
  4256. end;
  4257. procedure TImplementedInterface.deref;
  4258. begin
  4259. intfdef:=tobjectdef(intfdefderef.resolve);
  4260. end;
  4261. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4262. begin
  4263. if not assigned(NameMappings) then
  4264. NameMappings:=TFPHashList.Create;
  4265. NameMappings.Add(origname,stringdup(newname));
  4266. end;
  4267. function TImplementedInterface.GetMapping(const origname: string):string;
  4268. var
  4269. mappedname : pshortstring;
  4270. begin
  4271. result:='';
  4272. if not assigned(NameMappings) then
  4273. exit;
  4274. mappedname:=PShortstring(NameMappings.Find(origname));
  4275. if assigned(mappedname) then
  4276. result:=mappedname^;
  4277. end;
  4278. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4279. begin
  4280. if not assigned(procdefs) then
  4281. procdefs:=TFPObjectList.Create(false);
  4282. { duplicate entries must be stored, because multiple }
  4283. { interfaces can declare methods with the same name }
  4284. { and all of these get their own VMT entry }
  4285. procdefs.Add(pd);
  4286. end;
  4287. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4288. var
  4289. i : longint;
  4290. begin
  4291. result:=false;
  4292. { interfaces being implemented through delegation are not mergable (FK) }
  4293. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4294. exit;
  4295. weight:=0;
  4296. { empty interface is mergeable }
  4297. if ProcDefs.Count=0 then
  4298. begin
  4299. result:=true;
  4300. exit;
  4301. end;
  4302. { The interface to merge must at least the number of
  4303. procedures of this interface }
  4304. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4305. exit;
  4306. for i:=0 to ProcDefs.Count-1 do
  4307. begin
  4308. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4309. exit;
  4310. end;
  4311. weight:=ProcDefs.Count;
  4312. result:=true;
  4313. end;
  4314. function TImplementedInterface.getcopy:TImplementedInterface;
  4315. begin
  4316. Result:=TImplementedInterface.Create(nil);
  4317. {$warning: this is completely wrong on so many levels...}
  4318. { 1) the procdefs list will be freed once for each copy
  4319. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4320. 3) idem for the name mappings
  4321. }
  4322. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4323. end;
  4324. {****************************************************************************
  4325. TFORWARDDEF
  4326. ****************************************************************************}
  4327. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4328. begin
  4329. inherited create(forwarddef);
  4330. tosymname:=stringdup(s);
  4331. forwardpos:=pos;
  4332. end;
  4333. function tforwarddef.GetTypeName:string;
  4334. begin
  4335. GetTypeName:='unresolved forward to '+tosymname^;
  4336. end;
  4337. destructor tforwarddef.destroy;
  4338. begin
  4339. stringdispose(tosymname);
  4340. inherited destroy;
  4341. end;
  4342. {****************************************************************************
  4343. TUNDEFINEDDEF
  4344. ****************************************************************************}
  4345. constructor tundefineddef.create;
  4346. begin
  4347. inherited create(undefineddef);
  4348. end;
  4349. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4350. begin
  4351. inherited ppuload(undefineddef,ppufile);
  4352. end;
  4353. function tundefineddef.GetTypeName:string;
  4354. begin
  4355. GetTypeName:='<undefined type>';
  4356. end;
  4357. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4358. begin
  4359. inherited ppuwrite(ppufile);
  4360. ppufile.writeentry(ibundefineddef);
  4361. end;
  4362. {****************************************************************************
  4363. TERRORDEF
  4364. ****************************************************************************}
  4365. constructor terrordef.create;
  4366. begin
  4367. inherited create(errordef);
  4368. { prevent consecutive faults }
  4369. savesize:=1;
  4370. end;
  4371. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4372. begin
  4373. { Can't write errordefs to ppu }
  4374. internalerror(200411063);
  4375. end;
  4376. function terrordef.GetTypeName:string;
  4377. begin
  4378. GetTypeName:='<erroneous type>';
  4379. end;
  4380. function terrordef.getmangledparaname:string;
  4381. begin
  4382. getmangledparaname:='error';
  4383. end;
  4384. {****************************************************************************
  4385. Definition Helpers
  4386. ****************************************************************************}
  4387. function is_interfacecom(def: tdef): boolean;
  4388. begin
  4389. is_interfacecom:=
  4390. assigned(def) and
  4391. (def.typ=objectdef) and
  4392. (tobjectdef(def).objecttype=odt_interfacecom);
  4393. end;
  4394. function is_interfacecorba(def: tdef): boolean;
  4395. begin
  4396. is_interfacecorba:=
  4397. assigned(def) and
  4398. (def.typ=objectdef) and
  4399. (tobjectdef(def).objecttype=odt_interfacecorba);
  4400. end;
  4401. function is_interface(def: tdef): boolean;
  4402. begin
  4403. is_interface:=
  4404. assigned(def) and
  4405. (def.typ=objectdef) and
  4406. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4407. end;
  4408. function is_dispinterface(def: tdef): boolean;
  4409. begin
  4410. result:=
  4411. assigned(def) and
  4412. (def.typ=objectdef) and
  4413. (tobjectdef(def).objecttype=odt_dispinterface);
  4414. end;
  4415. function is_class(def: tdef): boolean;
  4416. begin
  4417. is_class:=
  4418. assigned(def) and
  4419. (def.typ=objectdef) and
  4420. (tobjectdef(def).objecttype=odt_class);
  4421. end;
  4422. function is_object(def: tdef): boolean;
  4423. begin
  4424. is_object:=
  4425. assigned(def) and
  4426. (def.typ=objectdef) and
  4427. (tobjectdef(def).objecttype=odt_object);
  4428. end;
  4429. function is_cppclass(def: tdef): boolean;
  4430. begin
  4431. is_cppclass:=
  4432. assigned(def) and
  4433. (def.typ=objectdef) and
  4434. (tobjectdef(def).objecttype=odt_cppclass);
  4435. end;
  4436. function is_objcclass(def: tdef): boolean;
  4437. begin
  4438. is_objcclass:=
  4439. assigned(def) and
  4440. (def.typ=objectdef) and
  4441. (tobjectdef(def).objecttype=odt_objcclass);
  4442. end;
  4443. function is_objcclassref(def: tdef): boolean;
  4444. begin
  4445. is_objcclassref:=
  4446. assigned(def) and
  4447. (def.typ=classrefdef) and
  4448. is_objcclass(tclassrefdef(def).pointeddef);
  4449. end;
  4450. function is_objcprotocol(def: tdef): boolean;
  4451. begin
  4452. result:=
  4453. assigned(def) and
  4454. (def.typ=objectdef) and
  4455. (tobjectdef(def).objecttype=odt_objcprotocol);
  4456. end;
  4457. function is_objccategory(def: tdef): boolean;
  4458. begin
  4459. result:=
  4460. assigned(def) and
  4461. (def.typ=objectdef) and
  4462. { if used as a forward type }
  4463. ((tobjectdef(def).objecttype=odt_objccategory) or
  4464. { if used as after it has been resolved }
  4465. ((tobjectdef(def).objecttype=odt_objcclass) and
  4466. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4467. end;
  4468. function is_objc_class_or_protocol(def: tdef): boolean;
  4469. begin
  4470. result:=
  4471. assigned(def) and
  4472. (def.typ=objectdef) and
  4473. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4474. end;
  4475. function is_objc_protocol_or_category(def: tdef): boolean;
  4476. begin
  4477. result:=
  4478. assigned(def) and
  4479. (def.typ=objectdef) and
  4480. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4481. ((tobjectdef(def).objecttype = odt_objcclass) and
  4482. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4483. end;
  4484. function is_class_or_interface(def: tdef): boolean;
  4485. begin
  4486. result:=
  4487. assigned(def) and
  4488. (def.typ=objectdef) and
  4489. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4490. end;
  4491. function is_class_or_interface_or_objc(def: tdef): boolean;
  4492. begin
  4493. result:=
  4494. assigned(def) and
  4495. (def.typ=objectdef) and
  4496. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4497. end;
  4498. function is_class_or_interface_or_object(def: tdef): boolean;
  4499. begin
  4500. result:=
  4501. assigned(def) and
  4502. (def.typ=objectdef) and
  4503. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4504. end;
  4505. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4506. begin
  4507. result:=
  4508. assigned(def) and
  4509. (def.typ=objectdef) and
  4510. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4511. end;
  4512. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4513. begin
  4514. result:=
  4515. assigned(def) and
  4516. (def.typ=objectdef) and
  4517. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4518. end;
  4519. procedure loadobjctypes;
  4520. begin
  4521. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS').typedef);
  4522. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER').typedef);
  4523. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID').typedef);
  4524. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL').typedef);
  4525. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT').typedef);
  4526. end;
  4527. function use_vectorfpu(def : tdef) : boolean;
  4528. begin
  4529. {$ifdef x86}
  4530. {$define use_vectorfpuimplemented}
  4531. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4532. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4533. {$endif x86}
  4534. {$ifdef arm}
  4535. {$define use_vectorfpuimplemented}
  4536. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4537. {$endif arm}
  4538. {$ifndef use_vectorfpuimplemented}
  4539. use_vectorfpu:=false;
  4540. {$endif}
  4541. end;
  4542. end.