symdef.pas 138 KB

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