symdef.pas 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237
  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. funcretsym:=nil;
  2906. end;
  2907. end;
  2908. function tprocdef.GetTypeName : string;
  2909. begin
  2910. GetTypeName := FullProcName(false);
  2911. end;
  2912. function tprocdef.mangledname : string;
  2913. var
  2914. hp : TParavarsym;
  2915. hs : string;
  2916. crc : dword;
  2917. newlen,
  2918. oldlen,
  2919. i : integer;
  2920. begin
  2921. if assigned(_mangledname) then
  2922. begin
  2923. {$ifdef compress}
  2924. mangledname:=minilzw_decode(_mangledname^);
  2925. {$else}
  2926. mangledname:=_mangledname^;
  2927. {$endif}
  2928. exit;
  2929. end;
  2930. { we need to use the symtable where the procsym is inserted,
  2931. because that is visible to the world }
  2932. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2933. oldlen:=length(mangledname);
  2934. { add parameter types }
  2935. for i:=0 to paras.count-1 do
  2936. begin
  2937. hp:=tparavarsym(paras[i]);
  2938. if not(vo_is_hidden_para in hp.varoptions) then
  2939. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2940. end;
  2941. { add resultdef, add $$ as separator to make it unique from a
  2942. parameter separator }
  2943. if not is_void(returndef) then
  2944. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2945. newlen:=length(mangledname);
  2946. { Replace with CRC if the parameter line is very long }
  2947. if (newlen-oldlen>12) and
  2948. ((newlen>128) or (newlen-oldlen>64)) then
  2949. begin
  2950. crc:=0;
  2951. for i:=0 to paras.count-1 do
  2952. begin
  2953. hp:=tparavarsym(paras[i]);
  2954. if not(vo_is_hidden_para in hp.varoptions) then
  2955. begin
  2956. hs:=hp.vardef.mangledparaname;
  2957. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2958. end;
  2959. end;
  2960. hs:=hp.vardef.mangledparaname;
  2961. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2962. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2963. end;
  2964. {$ifdef compress}
  2965. _mangledname:=stringdup(minilzw_encode(mangledname));
  2966. {$else}
  2967. _mangledname:=stringdup(mangledname);
  2968. {$endif}
  2969. end;
  2970. function tprocdef.cplusplusmangledname : string;
  2971. function getcppparaname(p : tdef) : string;
  2972. const
  2973. {$ifdef NAMEMANGLING_GCC2}
  2974. ordtype2str : array[tordtype] of string[2] = (
  2975. '',
  2976. 'Uc','Us','Ui','Us',
  2977. 'Sc','s','i','x',
  2978. 'b','b','b','b','b',
  2979. 'c','w','x');
  2980. {$else NAMEMANGLING_GCC2}
  2981. ordtype2str : array[tordtype] of string[1] = (
  2982. 'v',
  2983. 'h','t','j','y',
  2984. 'a','s','i','x',
  2985. 'b','b','b','b','b',
  2986. 'c','w','x');
  2987. floattype2str : array[tfloattype] of string[1] = (
  2988. 'f','d','e',
  2989. 'd','d','g');
  2990. {$endif NAMEMANGLING_GCC2}
  2991. var
  2992. s : string;
  2993. begin
  2994. case p.typ of
  2995. orddef:
  2996. s:=ordtype2str[torddef(p).ordtype];
  2997. pointerdef:
  2998. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  2999. {$ifndef NAMEMANGLING_GCC2}
  3000. floatdef:
  3001. s:=floattype2str[tfloatdef(p).floattype];
  3002. {$endif NAMEMANGLING_GCC2}
  3003. else
  3004. internalerror(2103001);
  3005. end;
  3006. getcppparaname:=s;
  3007. end;
  3008. var
  3009. s,s2 : string;
  3010. hp : TParavarsym;
  3011. i : integer;
  3012. begin
  3013. {$ifdef NAMEMANGLING_GCC2}
  3014. { outdated gcc 2.x name mangling scheme }
  3015. s := procsym.realname;
  3016. if procsym.owner.symtabletype=ObjectSymtable then
  3017. begin
  3018. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3019. case proctypeoption of
  3020. potype_destructor:
  3021. s:='_$_'+tostr(length(s2))+s2;
  3022. potype_constructor:
  3023. s:='___'+tostr(length(s2))+s2;
  3024. else
  3025. s:='_'+s+'__'+tostr(length(s2))+s2;
  3026. end;
  3027. end
  3028. else s:=s+'__';
  3029. s:=s+'F';
  3030. { concat modifiers }
  3031. { !!!!! }
  3032. { now we handle the parameters }
  3033. if maxparacount>0 then
  3034. begin
  3035. for i:=0 to paras.count-1 do
  3036. begin
  3037. hp:=tparavarsym(paras[i]);
  3038. { no hidden parameters form part of a C++ mangled name:
  3039. a) self is not included
  3040. b) there are no "high" or other hidden parameters
  3041. }
  3042. if vo_is_hidden_para in hp.varoptions then
  3043. continue;
  3044. s2:=getcppparaname(hp.vardef);
  3045. if hp.varspez in [vs_var,vs_out] then
  3046. s2:='R'+s2;
  3047. s:=s+s2;
  3048. end;
  3049. end
  3050. else
  3051. s:=s+'v';
  3052. cplusplusmangledname:=s;
  3053. {$else NAMEMANGLING_GCC2}
  3054. { gcc 3.x and 4.x name mangling scheme }
  3055. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3056. if procsym.owner.symtabletype=ObjectSymtable then
  3057. begin
  3058. s:='_ZN';
  3059. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3060. s:=s+tostr(length(s2))+s2;
  3061. case proctypeoption of
  3062. potype_constructor:
  3063. s:=s+'C1';
  3064. potype_destructor:
  3065. s:=s+'D1';
  3066. else
  3067. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3068. end;
  3069. s:=s+'E';
  3070. end
  3071. else
  3072. s:=procsym.realname;
  3073. { now we handle the parameters }
  3074. if maxparacount>0 then
  3075. begin
  3076. for i:=0 to paras.count-1 do
  3077. begin
  3078. hp:=tparavarsym(paras[i]);
  3079. { no hidden parameters form part of a C++ mangled name:
  3080. a) self is not included
  3081. b) there are no "high" or other hidden parameters
  3082. }
  3083. if vo_is_hidden_para in hp.varoptions then
  3084. continue;
  3085. s2:=getcppparaname(hp.vardef);
  3086. if hp.varspez in [vs_var,vs_out] then
  3087. s2:='R'+s2;
  3088. s:=s+s2;
  3089. end;
  3090. end
  3091. else
  3092. s:=s+'v';
  3093. cplusplusmangledname:=s;
  3094. {$endif NAMEMANGLING_GCC2}
  3095. end;
  3096. function tprocdef.objcmangledname : string;
  3097. var
  3098. manglednamelen: longint;
  3099. iscatmethod : boolean;
  3100. begin
  3101. if not (po_msgstr in procoptions) then
  3102. internalerror(2009030901);
  3103. { we may very well need longer strings to handle these... }
  3104. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3105. length('+"[ ]"')+length(messageinf.str^);
  3106. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3107. if (iscatmethod) then
  3108. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3109. if manglednamelen>255 then
  3110. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3111. if not(po_classmethod in procoptions) then
  3112. result:='"-['
  3113. else
  3114. result:='"+[';
  3115. { quotes are necessary because the +/- otherwise confuse the assembler
  3116. into expecting a number
  3117. }
  3118. if iscatmethod then
  3119. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3120. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3121. if iscatmethod then
  3122. result:=result+')';
  3123. result:=result+' '+messageinf.str^+']"';
  3124. end;
  3125. procedure tprocdef.setmangledname(const s : string);
  3126. begin
  3127. { This is not allowed anymore, the forward declaration
  3128. already needs to create the correct mangledname, no changes
  3129. afterwards are allowed (PFV) }
  3130. { Exception: interface definitions in mode macpas, since in that }
  3131. { case no reference to the old name can exist yet (JM) }
  3132. if assigned(_mangledname) then
  3133. if ((m_mac in current_settings.modeswitches) and
  3134. (interfacedef)) then
  3135. stringdispose(_mangledname)
  3136. else
  3137. internalerror(200411171);
  3138. {$ifdef compress}
  3139. _mangledname:=stringdup(minilzw_encode(s));
  3140. {$else}
  3141. _mangledname:=stringdup(s);
  3142. {$endif}
  3143. include(procoptions,po_has_mangledname);
  3144. end;
  3145. {***************************************************************************
  3146. TPROCVARDEF
  3147. ***************************************************************************}
  3148. constructor tprocvardef.create(level:byte);
  3149. begin
  3150. inherited create(procvardef,level);
  3151. end;
  3152. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3153. begin
  3154. inherited ppuload(procvardef,ppufile);
  3155. { load para symtable }
  3156. parast:=tparasymtable.create(self,unknown_level);
  3157. tparasymtable(parast).ppuload(ppufile);
  3158. end;
  3159. function tprocvardef.getcopy : tstoreddef;
  3160. var
  3161. i : tcallercallee;
  3162. j : longint;
  3163. begin
  3164. result:=tprocvardef.create(parast.symtablelevel);
  3165. tprocvardef(result).returndef:=returndef;
  3166. tprocvardef(result).returndefderef:=returndefderef;
  3167. tprocvardef(result).parast:=parast.getcopy;
  3168. tprocvardef(result).savesize:=savesize;
  3169. { create paralist copy }
  3170. tprocvardef(result).paras:=tparalist.create(false);
  3171. tprocvardef(result).paras.count:=paras.count;
  3172. for j:=0 to paras.count-1 do
  3173. tprocvardef(result).paras[j]:=paras[j];
  3174. tprocvardef(result).proctypeoption:=proctypeoption;
  3175. tprocvardef(result).proccalloption:=proccalloption;
  3176. tprocvardef(result).procoptions:=procoptions;
  3177. tprocvardef(result).requiredargarea:=requiredargarea;
  3178. tprocvardef(result).maxparacount:=maxparacount;
  3179. tprocvardef(result).minparacount:=minparacount;
  3180. for i:=low(tcallercallee) to high(tcallercallee) do
  3181. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3182. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3183. {$ifdef m68k}
  3184. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3185. {$endif}
  3186. end;
  3187. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3188. begin
  3189. inherited ppuwrite(ppufile);
  3190. { Write this entry }
  3191. ppufile.writeentry(ibprocvardef);
  3192. { Save the para symtable, this is taken from the interface }
  3193. tparasymtable(parast).ppuwrite(ppufile);
  3194. end;
  3195. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3196. begin
  3197. case t of
  3198. gs_para :
  3199. GetSymtable:=parast;
  3200. else
  3201. GetSymtable:=nil;
  3202. end;
  3203. end;
  3204. function tprocvardef.size : aint;
  3205. begin
  3206. if (po_methodpointer in procoptions) and
  3207. not(po_addressonly in procoptions) then
  3208. size:=2*sizeof(pint)
  3209. else
  3210. size:=sizeof(pint);
  3211. end;
  3212. function tprocvardef.is_methodpointer:boolean;
  3213. begin
  3214. result:=(po_methodpointer in procoptions);
  3215. end;
  3216. function tprocvardef.is_addressonly:boolean;
  3217. begin
  3218. result:=not(po_methodpointer in procoptions) or
  3219. (po_addressonly in procoptions);
  3220. end;
  3221. function tprocvardef.getmangledparaname:string;
  3222. begin
  3223. result:='procvar';
  3224. end;
  3225. function tprocvardef.is_publishable : boolean;
  3226. begin
  3227. is_publishable:=(po_methodpointer in procoptions);
  3228. end;
  3229. function tprocvardef.GetTypeName : string;
  3230. var
  3231. s: string;
  3232. showhidden : boolean;
  3233. begin
  3234. {$ifdef EXTDEBUG}
  3235. showhidden:=true;
  3236. {$else EXTDEBUG}
  3237. showhidden:=false;
  3238. {$endif EXTDEBUG}
  3239. s:='<';
  3240. if po_classmethod in procoptions then
  3241. s := s+'class method type of'
  3242. else
  3243. if po_addressonly in procoptions then
  3244. s := s+'address of'
  3245. else
  3246. s := s+'procedure variable type of';
  3247. if po_local in procoptions then
  3248. s := s+' local';
  3249. if assigned(returndef) and
  3250. (returndef<>voidtype) then
  3251. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3252. else
  3253. s:=s+' procedure'+typename_paras(showhidden);
  3254. if po_methodpointer in procoptions then
  3255. s := s+' of object';
  3256. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3257. end;
  3258. {***************************************************************************
  3259. TOBJECTDEF
  3260. ***************************************************************************}
  3261. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3262. begin
  3263. inherited create(objectdef);
  3264. objecttype:=ot;
  3265. objectoptions:=[];
  3266. childof:=nil;
  3267. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3268. { create space for vmt !! }
  3269. vmtentries:=TFPList.Create;
  3270. vmt_offset:=0;
  3271. set_parent(c);
  3272. objname:=stringdup(upper(n));
  3273. objrealname:=stringdup(n);
  3274. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3275. prepareguid;
  3276. { setup implemented interfaces }
  3277. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3278. ImplementedInterfaces:=TFPObjectList.Create(true)
  3279. else
  3280. ImplementedInterfaces:=nil;
  3281. writing_class_record_dbginfo:=false;
  3282. end;
  3283. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3284. var
  3285. i,
  3286. implintfcount : longint;
  3287. d : tderef;
  3288. ImplIntf : TImplementedInterface;
  3289. vmtentry : pvmtentry;
  3290. begin
  3291. inherited ppuload(objectdef,ppufile);
  3292. objecttype:=tobjecttyp(ppufile.getbyte);
  3293. objrealname:=stringdup(ppufile.getstring);
  3294. objname:=stringdup(upper(objrealname^));
  3295. objextname:=stringdup(ppufile.getstring);
  3296. { only used for external Objective-C classes/protocols }
  3297. if (objextname^='') then
  3298. stringdispose(objextname);
  3299. import_lib:=stringdup(ppufile.getstring);
  3300. { only used for external C++ classes }
  3301. if (import_lib^='') then
  3302. stringdispose(import_lib);
  3303. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3304. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3305. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3306. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3307. vmt_offset:=ppufile.getlongint;
  3308. ppufile.getderef(childofderef);
  3309. ppufile.getsmallset(objectoptions);
  3310. { load guid }
  3311. iidstr:=nil;
  3312. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3313. begin
  3314. new(iidguid);
  3315. ppufile.getguid(iidguid^);
  3316. iidstr:=stringdup(ppufile.getstring);
  3317. end;
  3318. vmtentries:=TFPList.Create;
  3319. vmtentries.count:=ppufile.getlongint;
  3320. for i:=0 to vmtentries.count-1 do
  3321. begin
  3322. ppufile.getderef(d);
  3323. new(vmtentry);
  3324. vmtentry^.procdef:=nil;
  3325. vmtentry^.procdefderef:=d;
  3326. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3327. vmtentries[i]:=vmtentry;
  3328. end;
  3329. { load implemented interfaces }
  3330. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3331. begin
  3332. ImplementedInterfaces:=TFPObjectList.Create(true);
  3333. implintfcount:=ppufile.getlongint;
  3334. for i:=0 to implintfcount-1 do
  3335. begin
  3336. ppufile.getderef(d);
  3337. ImplIntf:=TImplementedInterface.Create_deref(d);
  3338. ImplIntf.IOffset:=ppufile.getlongint;
  3339. ImplementedInterfaces.Add(ImplIntf);
  3340. end;
  3341. end
  3342. else
  3343. ImplementedInterfaces:=nil;
  3344. if df_copied_def in defoptions then
  3345. ppufile.getderef(cloneddefderef)
  3346. else
  3347. tObjectSymtable(symtable).ppuload(ppufile);
  3348. { handles the predefined class tobject }
  3349. { the last TOBJECT which is loaded gets }
  3350. { it ! }
  3351. if (childof=nil) and
  3352. (objecttype=odt_class) and
  3353. (objname^='TOBJECT') then
  3354. class_tobject:=self;
  3355. if (childof=nil) and
  3356. (objecttype=odt_interfacecom) and
  3357. (objname^='IUNKNOWN') then
  3358. interface_iunknown:=self;
  3359. if (childof=nil) and
  3360. (objecttype=odt_objcclass) and
  3361. (objname^='PROTOCOL') then
  3362. objc_protocoltype:=self;
  3363. writing_class_record_dbginfo:=false;
  3364. end;
  3365. destructor tobjectdef.destroy;
  3366. begin
  3367. if assigned(symtable) then
  3368. begin
  3369. symtable.free;
  3370. symtable:=nil;
  3371. end;
  3372. stringdispose(objname);
  3373. stringdispose(objrealname);
  3374. stringdispose(objextname);
  3375. stringdispose(import_lib);
  3376. stringdispose(iidstr);
  3377. if assigned(ImplementedInterfaces) then
  3378. begin
  3379. ImplementedInterfaces.free;
  3380. ImplementedInterfaces:=nil;
  3381. end;
  3382. if assigned(iidguid) then
  3383. begin
  3384. dispose(iidguid);
  3385. iidguid:=nil;
  3386. end;
  3387. if assigned(vmtentries) then
  3388. begin
  3389. resetvmtentries;
  3390. vmtentries.free;
  3391. vmtentries:=nil;
  3392. end;
  3393. if assigned(vmcallstaticinfo) then
  3394. begin
  3395. freemem(vmcallstaticinfo);
  3396. vmcallstaticinfo:=nil;
  3397. end;
  3398. inherited destroy;
  3399. end;
  3400. function tobjectdef.getcopy : tstoreddef;
  3401. var
  3402. i : longint;
  3403. begin
  3404. result:=tobjectdef.create(objecttype,objname^,childof);
  3405. { the constructor allocates a symtable which we release to avoid memory leaks }
  3406. tobjectdef(result).symtable.free;
  3407. tobjectdef(result).symtable:=symtable.getcopy;
  3408. if assigned(objname) then
  3409. tobjectdef(result).objname:=stringdup(objname^);
  3410. if assigned(objrealname) then
  3411. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3412. if assigned(objextname) then
  3413. tobjectdef(result).objextname:=stringdup(objextname^);
  3414. if assigned(import_lib) then
  3415. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3416. tobjectdef(result).objectoptions:=objectoptions;
  3417. include(tobjectdef(result).defoptions,df_copied_def);
  3418. tobjectdef(result).vmt_offset:=vmt_offset;
  3419. if assigned(iidguid) then
  3420. begin
  3421. new(tobjectdef(result).iidguid);
  3422. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3423. end;
  3424. if assigned(iidstr) then
  3425. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3426. if assigned(ImplementedInterfaces) then
  3427. begin
  3428. for i:=0 to ImplementedInterfaces.count-1 do
  3429. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3430. end;
  3431. if assigned(vmtentries) then
  3432. begin
  3433. tobjectdef(result).vmtentries:=TFPList.Create;
  3434. tobjectdef(result).copyvmtentries(self);
  3435. end;
  3436. end;
  3437. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3438. var
  3439. i : longint;
  3440. vmtentry : pvmtentry;
  3441. ImplIntf : TImplementedInterface;
  3442. begin
  3443. inherited ppuwrite(ppufile);
  3444. ppufile.putbyte(byte(objecttype));
  3445. ppufile.putstring(objrealname^);
  3446. if assigned(objextname) then
  3447. ppufile.putstring(objextname^)
  3448. else
  3449. ppufile.putstring('');
  3450. if assigned(import_lib) then
  3451. ppufile.putstring(import_lib^)
  3452. else
  3453. ppufile.putstring('');
  3454. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3455. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3456. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3457. ppufile.putlongint(vmt_offset);
  3458. ppufile.putderef(childofderef);
  3459. ppufile.putsmallset(objectoptions);
  3460. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3461. begin
  3462. ppufile.putguid(iidguid^);
  3463. ppufile.putstring(iidstr^);
  3464. end;
  3465. ppufile.putlongint(vmtentries.count);
  3466. for i:=0 to vmtentries.count-1 do
  3467. begin
  3468. vmtentry:=pvmtentry(vmtentries[i]);
  3469. ppufile.putderef(vmtentry^.procdefderef);
  3470. ppufile.putbyte(byte(vmtentry^.visibility));
  3471. end;
  3472. if assigned(ImplementedInterfaces) then
  3473. begin
  3474. ppufile.putlongint(ImplementedInterfaces.Count);
  3475. for i:=0 to ImplementedInterfaces.Count-1 do
  3476. begin
  3477. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3478. ppufile.putderef(ImplIntf.intfdefderef);
  3479. ppufile.putlongint(ImplIntf.Ioffset);
  3480. end;
  3481. end;
  3482. if df_copied_def in defoptions then
  3483. ppufile.putderef(cloneddefderef);
  3484. ppufile.writeentry(ibobjectdef);
  3485. if not(df_copied_def in defoptions) then
  3486. tObjectSymtable(symtable).ppuwrite(ppufile);
  3487. end;
  3488. function tobjectdef.GetTypeName:string;
  3489. begin
  3490. { in this case we will go in endless recursion, because then }
  3491. { there is no tsym associated yet with the def. It can occur }
  3492. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3493. { instead of the actual type name }
  3494. if not assigned(typesym) then
  3495. result:='<Currently Parsed Class>'
  3496. else
  3497. result:=typename;
  3498. end;
  3499. procedure tobjectdef.buildderef;
  3500. var
  3501. i : longint;
  3502. vmtentry : pvmtentry;
  3503. begin
  3504. inherited buildderef;
  3505. childofderef.build(childof);
  3506. if df_copied_def in defoptions then
  3507. cloneddefderef.build(symtable.defowner)
  3508. else
  3509. tstoredsymtable(symtable).buildderef;
  3510. for i:=0 to vmtentries.count-1 do
  3511. begin
  3512. vmtentry:=pvmtentry(vmtentries[i]);
  3513. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3514. end;
  3515. if assigned(ImplementedInterfaces) then
  3516. begin
  3517. for i:=0 to ImplementedInterfaces.count-1 do
  3518. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3519. end;
  3520. end;
  3521. procedure tobjectdef.deref;
  3522. var
  3523. i : longint;
  3524. vmtentry : pvmtentry;
  3525. begin
  3526. inherited deref;
  3527. childof:=tobjectdef(childofderef.resolve);
  3528. if df_copied_def in defoptions then
  3529. begin
  3530. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3531. symtable:=cloneddef.symtable.getcopy;
  3532. end
  3533. else
  3534. tstoredsymtable(symtable).deref;
  3535. for i:=0 to vmtentries.count-1 do
  3536. begin
  3537. vmtentry:=pvmtentry(vmtentries[i]);
  3538. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3539. end;
  3540. if assigned(ImplementedInterfaces) then
  3541. begin
  3542. for i:=0 to ImplementedInterfaces.count-1 do
  3543. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3544. end;
  3545. end;
  3546. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3547. var
  3548. pd: tprocdef absolute def;
  3549. st: tsymtable;
  3550. psym: tsym;
  3551. nname: TIDString;
  3552. begin
  3553. if (tdef(def).typ<>procdef) then
  3554. exit;
  3555. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3556. owner = symtable in which objcclassdef is defined
  3557. }
  3558. st:=pd.owner.defowner.owner;
  3559. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3560. { check for an existing procsym with our special name }
  3561. psym:=tsym(st.find(nname));
  3562. if not assigned(psym) then
  3563. begin
  3564. psym:=tprocsym.create(nname);
  3565. { avoid warning about this symbol being unused }
  3566. psym.IncRefCount;
  3567. st.insert(psym,true);
  3568. end
  3569. else if (psym.typ<>procsym) then
  3570. internalerror(2009111501);
  3571. { add ourselves to this special procsym }
  3572. tprocsym(psym).procdeflist.add(def);
  3573. end;
  3574. procedure tobjectdef.buildderefimpl;
  3575. begin
  3576. inherited buildderefimpl;
  3577. if not (df_copied_def in defoptions) then
  3578. tstoredsymtable(symtable).buildderefimpl;
  3579. end;
  3580. procedure tobjectdef.derefimpl;
  3581. begin
  3582. inherited derefimpl;
  3583. if not (df_copied_def in defoptions) then
  3584. tstoredsymtable(symtable).derefimpl;
  3585. { the procdefs are not owned by the class helper procsyms, so they
  3586. are not stored/restored either -> re-add them here }
  3587. if (objecttype=odt_objcclass) or
  3588. (oo_is_classhelper in objectoptions) then
  3589. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3590. end;
  3591. procedure tobjectdef.resetvmtentries;
  3592. var
  3593. i : longint;
  3594. begin
  3595. for i:=0 to vmtentries.Count-1 do
  3596. Dispose(pvmtentry(vmtentries[i]));
  3597. vmtentries.clear;
  3598. end;
  3599. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3600. var
  3601. i : longint;
  3602. vmtentry : pvmtentry;
  3603. begin
  3604. resetvmtentries;
  3605. vmtentries.count:=objdef.vmtentries.count;
  3606. for i:=0 to objdef.vmtentries.count-1 do
  3607. begin
  3608. new(vmtentry);
  3609. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3610. vmtentries[i]:=vmtentry;
  3611. end;
  3612. end;
  3613. function tobjectdef.getparentdef:tdef;
  3614. begin
  3615. { TODO: Remove getparentdef hack}
  3616. { With 2 forward declared classes with the child class before the
  3617. parent class the child class is written earlier to the ppu. Leaving it
  3618. possible to have a reference to the parent class for property overriding,
  3619. but the parent class still has the childof not resolved yet (PFV) }
  3620. if childof=nil then
  3621. childof:=tobjectdef(childofderef.resolve);
  3622. result:=childof;
  3623. end;
  3624. procedure tobjectdef.prepareguid;
  3625. begin
  3626. { set up guid }
  3627. if not assigned(iidguid) then
  3628. begin
  3629. new(iidguid);
  3630. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3631. end;
  3632. { setup iidstring }
  3633. if not assigned(iidstr) then
  3634. iidstr:=stringdup(''); { default is empty string }
  3635. end;
  3636. procedure tobjectdef.set_parent( c : tobjectdef);
  3637. begin
  3638. if assigned(childof) then
  3639. exit;
  3640. childof:=c;
  3641. if not assigned(c) then
  3642. exit;
  3643. { inherit options and status }
  3644. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3645. { add the data of the anchestor class/object }
  3646. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3647. begin
  3648. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3649. { inherit recordalignment }
  3650. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3651. if (oo_has_vmt in objectoptions) and
  3652. (oo_has_vmt in c.objectoptions) then
  3653. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3654. { if parent has a vmt field then the offset is the same for the child PM }
  3655. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3656. begin
  3657. vmt_offset:=c.vmt_offset;
  3658. include(objectoptions,oo_has_vmt);
  3659. end;
  3660. end;
  3661. end;
  3662. procedure tobjectdef.insertvmt;
  3663. var
  3664. vs: tfieldvarsym;
  3665. begin
  3666. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3667. exit;
  3668. if (oo_has_vmt in objectoptions) then
  3669. internalerror(12345)
  3670. else
  3671. begin
  3672. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3673. tObjectSymtable(symtable).fieldalignment);
  3674. if (tf_requires_proper_alignment in target_info.flags) then
  3675. begin
  3676. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3677. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3678. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3679. end;
  3680. vmt_offset:=tObjectSymtable(symtable).datasize;
  3681. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3682. hidesym(vs);
  3683. tObjectSymtable(symtable).insert(vs);
  3684. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3685. include(objectoptions,oo_has_vmt);
  3686. end;
  3687. end;
  3688. procedure tobjectdef.check_forwards;
  3689. begin
  3690. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3691. tstoredsymtable(symtable).check_forwards;
  3692. if (oo_is_forward in objectoptions) then
  3693. begin
  3694. { ok, in future, the forward can be resolved }
  3695. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3696. exclude(objectoptions,oo_is_forward);
  3697. end;
  3698. end;
  3699. { true if prot implements d (or if they are equal) }
  3700. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3701. var
  3702. i : longint;
  3703. begin
  3704. { objcprotocols have multiple inheritance, all protocols from which
  3705. the current protocol inherits are stored in implementedinterfaces }
  3706. result:=prot=d;
  3707. if result then
  3708. exit;
  3709. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3710. begin
  3711. result:=is_related_protocol(tobjectdef(prot.ImplementedInterfaces[i]),d);
  3712. if result then
  3713. exit;
  3714. end;
  3715. end;
  3716. { true, if self inherits from d (or if they are equal) }
  3717. function tobjectdef.is_related(d : tdef) : boolean;
  3718. var
  3719. hp : tobjectdef;
  3720. begin
  3721. if (objecttype=odt_objcprotocol) then
  3722. begin
  3723. is_related:=is_related_protocol(self,d);
  3724. exit
  3725. end;
  3726. hp:=self;
  3727. while assigned(hp) do
  3728. begin
  3729. if hp=d then
  3730. begin
  3731. is_related:=true;
  3732. exit;
  3733. end;
  3734. hp:=hp.childof;
  3735. end;
  3736. is_related:=false;
  3737. end;
  3738. function tobjectdef.FindDestructor : tprocdef;
  3739. var
  3740. objdef : tobjectdef;
  3741. i : longint;
  3742. sym : tsym;
  3743. pd : tprocdef;
  3744. begin
  3745. result:=nil;
  3746. objdef:=self;
  3747. while assigned(objdef) do
  3748. begin
  3749. for i:=0 to objdef.symtable.SymList.Count-1 do
  3750. begin
  3751. sym:=TSym(objdef.symtable.SymList[i]);
  3752. if sym.typ=procsym then
  3753. begin
  3754. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3755. if assigned(pd) then
  3756. begin
  3757. result:=pd;
  3758. exit;
  3759. end;
  3760. end;
  3761. end;
  3762. objdef:=objdef.childof;
  3763. end;
  3764. end;
  3765. function tobjectdef.implements_any_interfaces: boolean;
  3766. begin
  3767. result := (ImplementedInterfaces.Count > 0) or
  3768. (assigned(childof) and childof.implements_any_interfaces);
  3769. end;
  3770. function tobjectdef.size : aint;
  3771. begin
  3772. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3773. result:=sizeof(pint)
  3774. else
  3775. result:=tObjectSymtable(symtable).datasize;
  3776. end;
  3777. function tobjectdef.alignment:shortint;
  3778. begin
  3779. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3780. alignment:=sizeof(pint)
  3781. else
  3782. alignment:=tObjectSymtable(symtable).recordalignment;
  3783. end;
  3784. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3785. begin
  3786. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3787. case objecttype of
  3788. odt_class:
  3789. { the +2*sizeof(pint) is size and -size }
  3790. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3791. odt_objcclass,
  3792. odt_objcprotocol:
  3793. vmtmethodoffset:=0;
  3794. odt_interfacecom,odt_interfacecorba:
  3795. vmtmethodoffset:=index*sizeof(pint);
  3796. else
  3797. {$ifdef WITHDMT}
  3798. vmtmethodoffset:=(index+4)*sizeof(pint);
  3799. {$else WITHDMT}
  3800. vmtmethodoffset:=(index+3)*sizeof(pint);
  3801. {$endif WITHDMT}
  3802. end;
  3803. end;
  3804. function tobjectdef.vmt_mangledname : string;
  3805. begin
  3806. if not(oo_has_vmt in objectoptions) then
  3807. Message1(parser_n_object_has_no_vmt,objrealname^);
  3808. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3809. end;
  3810. function tobjectdef.needs_inittable : boolean;
  3811. begin
  3812. case objecttype of
  3813. odt_dispinterface,
  3814. odt_class :
  3815. needs_inittable:=false;
  3816. odt_interfacecom:
  3817. needs_inittable:=true;
  3818. odt_interfacecorba:
  3819. needs_inittable:=is_related(interface_iunknown);
  3820. odt_object:
  3821. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3822. odt_cppclass,
  3823. odt_objcclass,
  3824. odt_objcprotocol:
  3825. needs_inittable:=false;
  3826. else
  3827. internalerror(200108267);
  3828. end;
  3829. end;
  3830. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3831. begin
  3832. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3833. result:=inherited rtti_mangledname(rt)
  3834. else
  3835. begin
  3836. if not(target_info.system in system_objc_nfabi) then
  3837. begin
  3838. result:=target_asm.labelprefix;
  3839. case objecttype of
  3840. odt_objcclass:
  3841. begin
  3842. case rt of
  3843. objcclassrtti:
  3844. if not(oo_is_classhelper in objectoptions) then
  3845. result:=result+'_OBJC_CLASS_'
  3846. else
  3847. result:=result+'_OBJC_CATEGORY_';
  3848. objcmetartti:
  3849. if not(oo_is_classhelper in objectoptions) then
  3850. result:=result+'_OBJC_METACLASS_'
  3851. else
  3852. internalerror(2009111511);
  3853. else
  3854. internalerror(2009092302);
  3855. end;
  3856. end;
  3857. odt_objcprotocol:
  3858. result:=result+'_OBJC_PROTOCOL_';
  3859. end;
  3860. end
  3861. else
  3862. begin
  3863. case objecttype of
  3864. odt_objcclass:
  3865. begin
  3866. if (oo_is_classhelper in objectoptions) and
  3867. (rt<>objcclassrtti) then
  3868. internalerror(2009111512);
  3869. case rt of
  3870. objcclassrtti:
  3871. if not(oo_is_classhelper in objectoptions) then
  3872. result:='_OBJC_CLASS_$_'
  3873. else
  3874. result:='_OBJC_$_CATEGORY_';
  3875. objcmetartti:
  3876. result:='_OBJC_METACLASS_$_';
  3877. objcclassrortti:
  3878. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  3879. objcmetarortti:
  3880. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  3881. else
  3882. internalerror(2009092303);
  3883. end;
  3884. end;
  3885. odt_objcprotocol:
  3886. begin
  3887. result:=lower(target_asm.labelprefix);
  3888. case rt of
  3889. objcclassrtti:
  3890. result:=result+'_OBJC_PROTOCOL_$_';
  3891. objcmetartti:
  3892. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  3893. else
  3894. internalerror(2009092501);
  3895. end;
  3896. end;
  3897. end;
  3898. end;
  3899. result:=result+objextname^;
  3900. end;
  3901. end;
  3902. function tobjectdef.members_need_inittable : boolean;
  3903. begin
  3904. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3905. end;
  3906. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3907. var
  3908. ImplIntf : TImplementedInterface;
  3909. i : longint;
  3910. begin
  3911. result:=nil;
  3912. if not assigned(ImplementedInterfaces) then
  3913. exit;
  3914. for i:=0 to ImplementedInterfaces.Count-1 do
  3915. begin
  3916. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3917. if ImplIntf.intfdef=aintfdef then
  3918. begin
  3919. result:=ImplIntf;
  3920. exit;
  3921. end;
  3922. end;
  3923. end;
  3924. function tobjectdef.is_publishable : boolean;
  3925. begin
  3926. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3927. end;
  3928. procedure tobjectdef.reset;
  3929. begin
  3930. inherited reset;
  3931. created_in_current_module:=false;
  3932. maybe_created_in_current_module:=false;
  3933. classref_created_in_current_module:=false;
  3934. end;
  3935. function tobjectdef.search_enumerator_get: tprocdef;
  3936. var
  3937. objdef : tobjectdef;
  3938. sym : tsym;
  3939. i : integer;
  3940. pd : tprocdef;
  3941. hashedid : THashedIDString;
  3942. begin
  3943. result:=nil;
  3944. objdef:=self;
  3945. hashedid.id:='GETENUMERATOR';
  3946. while assigned(objdef) do
  3947. begin
  3948. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3949. if assigned(sym) and (sym.typ=procsym) then
  3950. begin
  3951. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3952. begin
  3953. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3954. if (pd.proctypeoption = potype_function) and
  3955. is_class_or_interface_or_object(pd.returndef) and
  3956. (pd.visibility >= vis_public) then
  3957. begin
  3958. result:=pd;
  3959. exit;
  3960. end;
  3961. end;
  3962. end;
  3963. objdef:=objdef.childof;
  3964. end;
  3965. end;
  3966. function tobjectdef.search_enumerator_move: tprocdef;
  3967. var
  3968. objdef : tobjectdef;
  3969. sym : tsym;
  3970. i : integer;
  3971. pd : tprocdef;
  3972. hashedid : THashedIDString;
  3973. begin
  3974. result:=nil;
  3975. objdef:=self;
  3976. // first search for po_enumerator_movenext method modifier
  3977. // then search for public function MoveNext: Boolean
  3978. hashedid.id:='MOVENEXT';
  3979. while assigned(objdef) do
  3980. begin
  3981. for i:=0 to objdef.symtable.SymList.Count-1 do
  3982. begin
  3983. sym:=TSym(objdef.symtable.SymList[i]);
  3984. if (sym.typ=procsym) then
  3985. begin
  3986. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3987. if assigned(pd) then
  3988. begin
  3989. result:=pd;
  3990. exit;
  3991. end;
  3992. end;
  3993. end;
  3994. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3995. if assigned(sym) and (sym.typ=procsym) then
  3996. begin
  3997. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3998. begin
  3999. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4000. if (pd.proctypeoption = potype_function) and
  4001. is_boolean(pd.returndef) and
  4002. (pd.minparacount = 0) and
  4003. (pd.visibility >= vis_public) then
  4004. begin
  4005. result:=pd;
  4006. exit;
  4007. end;
  4008. end;
  4009. end;
  4010. objdef:=objdef.childof;
  4011. end;
  4012. end;
  4013. function tobjectdef.search_enumerator_current: tsym;
  4014. var
  4015. objdef : tobjectdef;
  4016. sym: tsym;
  4017. i: integer;
  4018. hashedid : THashedIDString;
  4019. begin
  4020. result:=nil;
  4021. objdef:=self;
  4022. hashedid.id:='CURRENT';
  4023. // first search for ppo_enumerator_current property modifier
  4024. // then search for public property Current
  4025. while assigned(objdef) do
  4026. begin
  4027. for i:=0 to objdef.symtable.SymList.Count-1 do
  4028. begin
  4029. sym:=TSym(objdef.symtable.SymList[i]);
  4030. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4031. begin
  4032. result:=sym;
  4033. exit;
  4034. end;
  4035. end;
  4036. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4037. if assigned(sym) and (sym.typ=propertysym) and
  4038. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4039. begin
  4040. result:=sym;
  4041. exit;
  4042. end;
  4043. objdef:=objdef.childof;
  4044. end;
  4045. end;
  4046. procedure tobjectdef.register_created_classref_type;
  4047. begin
  4048. if not classref_created_in_current_module then
  4049. begin
  4050. classref_created_in_current_module:=true;
  4051. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4052. end;
  4053. end;
  4054. procedure tobjectdef.register_created_object_type;
  4055. begin
  4056. if not created_in_current_module then
  4057. begin
  4058. created_in_current_module:=true;
  4059. current_module.wpoinfo.addcreatedobjtype(self);
  4060. end;
  4061. end;
  4062. procedure tobjectdef.register_maybe_created_object_type;
  4063. begin
  4064. { if we know it has been created for sure, no need
  4065. to also record that it maybe can be created in
  4066. this module
  4067. }
  4068. if not (created_in_current_module) and
  4069. not (maybe_created_in_current_module) then
  4070. begin
  4071. maybe_created_in_current_module:=true;
  4072. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4073. end;
  4074. end;
  4075. procedure tobjectdef.register_vmt_call(index: longint);
  4076. begin
  4077. if (is_object(self) or is_class(self)) then
  4078. current_module.wpoinfo.addcalledvmtentry(self,index);
  4079. end;
  4080. procedure make_procdef_external(data: tobject; arg: pointer);
  4081. var
  4082. def: tdef absolute data;
  4083. begin
  4084. if (def.typ = procdef) then
  4085. begin
  4086. include(tprocdef(def).procoptions,po_external);
  4087. tprocdef(def).forwarddef:=false;
  4088. end;
  4089. end;
  4090. procedure tobjectdef.make_all_methods_external;
  4091. begin
  4092. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4093. end;
  4094. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4095. var
  4096. def: tdef absolute data;
  4097. pd: tprocdef absolute data;
  4098. i,
  4099. paracount: longint;
  4100. begin
  4101. if (def.typ=procdef) then
  4102. begin
  4103. { add all messages also under a dummy name to the symtable in
  4104. which the objcclass/protocol/category is declared, so they can
  4105. be called via id.<name>
  4106. }
  4107. create_class_helper_for_procdef(pd,nil);
  4108. { we have to wait until now to set the mangled name because it
  4109. depends on the (possibly external) class name, which is defined
  4110. at the very end. }
  4111. if not(po_msgstr in pd.procoptions) then
  4112. begin
  4113. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4114. { recover to avoid internalerror later on }
  4115. include(pd.procoptions,po_msgstr);
  4116. pd.messageinf.str:=stringdup('MissingDeclaration');
  4117. end;
  4118. { Mangled name is already set in case this is a copy of
  4119. another type. }
  4120. if not(po_has_mangledname in pd.procoptions) then
  4121. begin
  4122. { check whether the number of formal parameters is correct }
  4123. paracount:=0;
  4124. for i:=1 to length(pd.messageinf.str^) do
  4125. if pd.messageinf.str^[i]=':' then
  4126. inc(paracount);
  4127. for i:=0 to pd.paras.count-1 do
  4128. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4129. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4130. dec(paracount);
  4131. if (paracount<>0) then
  4132. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4133. pd.setmangledname(pd.objcmangledname);
  4134. end
  4135. else
  4136. { all checks already done }
  4137. exit;
  4138. if not(oo_is_external in pd._class.objectoptions) then
  4139. begin
  4140. if (po_varargs in pd.procoptions) then
  4141. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4142. else
  4143. begin
  4144. { check for "array of const" parameters }
  4145. for i:=0 to pd.parast.symlist.count-1 do
  4146. begin
  4147. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4148. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4149. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4150. end;
  4151. end;
  4152. end;
  4153. end;
  4154. end;
  4155. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4156. var
  4157. sym: tsym absolute data;
  4158. begin
  4159. if (sym.typ=fieldvarsym) and
  4160. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4161. sym.IncRefCount;
  4162. end;
  4163. procedure tobjectdef.finish_objc_data;
  4164. begin
  4165. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4166. if (oo_is_external in objectoptions) then
  4167. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4168. end;
  4169. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4170. var
  4171. def: tdef absolute data;
  4172. pd: tprocdef absolute data;
  4173. begin
  4174. if (def.typ=procdef) then
  4175. begin
  4176. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4177. if (oo_is_external in pd._class.objectoptions) then
  4178. begin
  4179. { copied from psub.read_proc }
  4180. if assigned(pd._class.import_lib) then
  4181. current_module.AddExternalImport(pd._class.import_lib^,pd.mangledname,0,false,false)
  4182. else
  4183. begin
  4184. { add import name to external list for DLL scanning }
  4185. if tf_has_dllscanner in target_info.flags then
  4186. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4187. end;
  4188. end;
  4189. end;
  4190. end;
  4191. procedure tobjectdef.finish_cpp_data;
  4192. begin
  4193. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4194. end;
  4195. {****************************************************************************
  4196. TImplementedInterface
  4197. ****************************************************************************}
  4198. constructor TImplementedInterface.create(aintf: tobjectdef);
  4199. begin
  4200. inherited create;
  4201. intfdef:=aintf;
  4202. IOffset:=-1;
  4203. IType:=etStandard;
  4204. NameMappings:=nil;
  4205. procdefs:=nil;
  4206. end;
  4207. constructor TImplementedInterface.create_deref(d:tderef);
  4208. begin
  4209. inherited create;
  4210. intfdef:=nil;
  4211. intfdefderef:=d;
  4212. IOffset:=-1;
  4213. IType:=etStandard;
  4214. NameMappings:=nil;
  4215. procdefs:=nil;
  4216. end;
  4217. destructor TImplementedInterface.destroy;
  4218. var
  4219. i : longint;
  4220. mappedname : pshortstring;
  4221. begin
  4222. if assigned(NameMappings) then
  4223. begin
  4224. for i:=0 to NameMappings.Count-1 do
  4225. begin
  4226. mappedname:=pshortstring(NameMappings[i]);
  4227. stringdispose(mappedname);
  4228. end;
  4229. NameMappings.free;
  4230. NameMappings:=nil;
  4231. end;
  4232. if assigned(procdefs) then
  4233. begin
  4234. procdefs.free;
  4235. procdefs:=nil;
  4236. end;
  4237. inherited destroy;
  4238. end;
  4239. procedure TImplementedInterface.buildderef;
  4240. begin
  4241. intfdefderef.build(intfdef);
  4242. end;
  4243. procedure TImplementedInterface.deref;
  4244. begin
  4245. intfdef:=tobjectdef(intfdefderef.resolve);
  4246. end;
  4247. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4248. begin
  4249. if not assigned(NameMappings) then
  4250. NameMappings:=TFPHashList.Create;
  4251. NameMappings.Add(origname,stringdup(newname));
  4252. end;
  4253. function TImplementedInterface.GetMapping(const origname: string):string;
  4254. var
  4255. mappedname : pshortstring;
  4256. begin
  4257. result:='';
  4258. if not assigned(NameMappings) then
  4259. exit;
  4260. mappedname:=PShortstring(NameMappings.Find(origname));
  4261. if assigned(mappedname) then
  4262. result:=mappedname^;
  4263. end;
  4264. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4265. begin
  4266. if not assigned(procdefs) then
  4267. procdefs:=TFPObjectList.Create(false);
  4268. { duplicate entries must be stored, because multiple }
  4269. { interfaces can declare methods with the same name }
  4270. { and all of these get their own VMT entry }
  4271. procdefs.Add(pd);
  4272. end;
  4273. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4274. var
  4275. i : longint;
  4276. begin
  4277. result:=false;
  4278. { interfaces being implemented through delegation are not mergable (FK) }
  4279. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4280. exit;
  4281. weight:=0;
  4282. { empty interface is mergeable }
  4283. if ProcDefs.Count=0 then
  4284. begin
  4285. result:=true;
  4286. exit;
  4287. end;
  4288. { The interface to merge must at least the number of
  4289. procedures of this interface }
  4290. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4291. exit;
  4292. for i:=0 to ProcDefs.Count-1 do
  4293. begin
  4294. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4295. exit;
  4296. end;
  4297. weight:=ProcDefs.Count;
  4298. result:=true;
  4299. end;
  4300. function TImplementedInterface.getcopy:TImplementedInterface;
  4301. begin
  4302. Result:=TImplementedInterface.Create(nil);
  4303. {$warning: this is completely wrong on so many levels...}
  4304. { 1) the procdefs list will be freed once for each copy
  4305. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4306. 3) idem for the name mappings
  4307. }
  4308. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4309. end;
  4310. {****************************************************************************
  4311. TFORWARDDEF
  4312. ****************************************************************************}
  4313. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4314. begin
  4315. inherited create(forwarddef);
  4316. tosymname:=stringdup(s);
  4317. forwardpos:=pos;
  4318. end;
  4319. function tforwarddef.GetTypeName:string;
  4320. begin
  4321. GetTypeName:='unresolved forward to '+tosymname^;
  4322. end;
  4323. destructor tforwarddef.destroy;
  4324. begin
  4325. stringdispose(tosymname);
  4326. inherited destroy;
  4327. end;
  4328. {****************************************************************************
  4329. TUNDEFINEDDEF
  4330. ****************************************************************************}
  4331. constructor tundefineddef.create;
  4332. begin
  4333. inherited create(undefineddef);
  4334. end;
  4335. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4336. begin
  4337. inherited ppuload(undefineddef,ppufile);
  4338. end;
  4339. function tundefineddef.GetTypeName:string;
  4340. begin
  4341. GetTypeName:='<undefined type>';
  4342. end;
  4343. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4344. begin
  4345. inherited ppuwrite(ppufile);
  4346. ppufile.writeentry(ibundefineddef);
  4347. end;
  4348. {****************************************************************************
  4349. TERRORDEF
  4350. ****************************************************************************}
  4351. constructor terrordef.create;
  4352. begin
  4353. inherited create(errordef);
  4354. { prevent consecutive faults }
  4355. savesize:=1;
  4356. end;
  4357. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4358. begin
  4359. { Can't write errordefs to ppu }
  4360. internalerror(200411063);
  4361. end;
  4362. function terrordef.GetTypeName:string;
  4363. begin
  4364. GetTypeName:='<erroneous type>';
  4365. end;
  4366. function terrordef.getmangledparaname:string;
  4367. begin
  4368. getmangledparaname:='error';
  4369. end;
  4370. {****************************************************************************
  4371. Definition Helpers
  4372. ****************************************************************************}
  4373. function is_interfacecom(def: tdef): boolean;
  4374. begin
  4375. is_interfacecom:=
  4376. assigned(def) and
  4377. (def.typ=objectdef) and
  4378. (tobjectdef(def).objecttype=odt_interfacecom);
  4379. end;
  4380. function is_interfacecorba(def: tdef): boolean;
  4381. begin
  4382. is_interfacecorba:=
  4383. assigned(def) and
  4384. (def.typ=objectdef) and
  4385. (tobjectdef(def).objecttype=odt_interfacecorba);
  4386. end;
  4387. function is_interface(def: tdef): boolean;
  4388. begin
  4389. is_interface:=
  4390. assigned(def) and
  4391. (def.typ=objectdef) and
  4392. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4393. end;
  4394. function is_dispinterface(def: tdef): boolean;
  4395. begin
  4396. result:=
  4397. assigned(def) and
  4398. (def.typ=objectdef) and
  4399. (tobjectdef(def).objecttype=odt_dispinterface);
  4400. end;
  4401. function is_class(def: tdef): boolean;
  4402. begin
  4403. is_class:=
  4404. assigned(def) and
  4405. (def.typ=objectdef) and
  4406. (tobjectdef(def).objecttype=odt_class);
  4407. end;
  4408. function is_object(def: tdef): boolean;
  4409. begin
  4410. is_object:=
  4411. assigned(def) and
  4412. (def.typ=objectdef) and
  4413. (tobjectdef(def).objecttype=odt_object);
  4414. end;
  4415. function is_cppclass(def: tdef): boolean;
  4416. begin
  4417. is_cppclass:=
  4418. assigned(def) and
  4419. (def.typ=objectdef) and
  4420. (tobjectdef(def).objecttype=odt_cppclass);
  4421. end;
  4422. function is_objcclass(def: tdef): boolean;
  4423. begin
  4424. is_objcclass:=
  4425. assigned(def) and
  4426. (def.typ=objectdef) and
  4427. (tobjectdef(def).objecttype=odt_objcclass);
  4428. end;
  4429. function is_objcclassref(def: tdef): boolean;
  4430. begin
  4431. is_objcclassref:=
  4432. assigned(def) and
  4433. (def.typ=classrefdef) and
  4434. is_objcclass(tclassrefdef(def).pointeddef);
  4435. end;
  4436. function is_objcprotocol(def: tdef): boolean;
  4437. begin
  4438. result:=
  4439. assigned(def) and
  4440. (def.typ=objectdef) and
  4441. (tobjectdef(def).objecttype=odt_objcprotocol);
  4442. end;
  4443. function is_objccategory(def: tdef): boolean;
  4444. begin
  4445. result:=
  4446. assigned(def) and
  4447. (def.typ=objectdef) and
  4448. { if used as a forward type }
  4449. ((tobjectdef(def).objecttype=odt_objccategory) or
  4450. { if used as after it has been resolved }
  4451. ((tobjectdef(def).objecttype=odt_objcclass) and
  4452. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4453. end;
  4454. function is_objc_class_or_protocol(def: tdef): boolean;
  4455. begin
  4456. result:=
  4457. assigned(def) and
  4458. (def.typ=objectdef) and
  4459. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4460. end;
  4461. function is_objc_protocol_or_category(def: tdef): boolean;
  4462. begin
  4463. result:=
  4464. assigned(def) and
  4465. (def.typ=objectdef) and
  4466. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4467. ((tobjectdef(def).objecttype = odt_objcclass) and
  4468. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4469. end;
  4470. function is_class_or_interface(def: tdef): boolean;
  4471. begin
  4472. result:=
  4473. assigned(def) and
  4474. (def.typ=objectdef) and
  4475. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4476. end;
  4477. function is_class_or_interface_or_objc(def: tdef): boolean;
  4478. begin
  4479. result:=
  4480. assigned(def) and
  4481. (def.typ=objectdef) and
  4482. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4483. end;
  4484. function is_class_or_interface_or_object(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,odt_object]);
  4490. end;
  4491. function is_class_or_interface_or_dispinterface(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_dispinterface]);
  4497. end;
  4498. function is_class_or_interface_or_dispinterface_or_objc(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_dispinterface,odt_objcclass,odt_objcprotocol]);
  4504. end;
  4505. procedure loadobjctypes;
  4506. begin
  4507. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS').typedef);
  4508. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER').typedef);
  4509. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID').typedef);
  4510. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL').typedef);
  4511. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT').typedef);
  4512. end;
  4513. function use_vectorfpu(def : tdef) : boolean;
  4514. begin
  4515. {$ifdef x86}
  4516. {$define use_vectorfpuimplemented}
  4517. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4518. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4519. {$endif x86}
  4520. {$ifdef arm}
  4521. {$define use_vectorfpuimplemented}
  4522. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4523. {$endif arm}
  4524. {$ifndef use_vectorfpuimplemented}
  4525. use_vectorfpu:=false;
  4526. {$endif}
  4527. end;
  4528. end.