symdef.pas 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664
  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. if not is_void(pointeddef) then
  1814. result:=pointeddef.llvm_mangledname+'*'
  1815. else
  1816. result:='i8*'
  1817. end;
  1818. {$endif support_llvm}
  1819. {****************************************************************************
  1820. TCLASSREFDEF
  1821. ****************************************************************************}
  1822. constructor tclassrefdef.create(def:tdef);
  1823. begin
  1824. inherited create(classrefdef,def);
  1825. end;
  1826. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1827. begin
  1828. inherited ppuload(classrefdef,ppufile);
  1829. end;
  1830. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1831. begin
  1832. inherited ppuwrite(ppufile);
  1833. ppufile.writeentry(ibclassrefdef);
  1834. end;
  1835. function tclassrefdef.GetTypeName : string;
  1836. begin
  1837. GetTypeName:='Class Of '+pointeddef.typename;
  1838. end;
  1839. function tclassrefdef.is_publishable : boolean;
  1840. begin
  1841. result:=true;
  1842. end;
  1843. {$ifdef support_llvm}
  1844. procedure tclassrefdef.set_llvm_name_syms;
  1845. begin
  1846. tobjectdef(pointeddef).set_llvm_name_syms;
  1847. fllvm_name_sym:=tobjectdef(pointeddef).llvm_vmt_name_sym;
  1848. fllvm_pointer_name_sym:=current_asmdata.DefineAsmSymbol(llvm_mangledname+'*',AB_LOCAL,AT_DATA);
  1849. end;
  1850. {$endif support_llvm}
  1851. {***************************************************************************
  1852. TSETDEF
  1853. ***************************************************************************}
  1854. constructor tsetdef.create(def:tdef;low, high : aint);
  1855. var
  1856. setallocbits: aint;
  1857. packedsavesize: aint;
  1858. begin
  1859. inherited create(setdef);
  1860. elementdef:=def;
  1861. setmax:=high;
  1862. if (current_settings.setalloc=0) then
  1863. begin
  1864. setbase:=0;
  1865. if (high<32) then
  1866. savesize:=Sizeof(longint)
  1867. else if (high<256) then
  1868. savesize:=32
  1869. else
  1870. savesize:=(high+7) div 8
  1871. end
  1872. else
  1873. begin
  1874. setallocbits:=current_settings.setalloc*8;
  1875. setbase:=low and not(setallocbits-1);
  1876. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1877. savesize:=packedsavesize;
  1878. if savesize=3 then
  1879. savesize:=4;
  1880. end;
  1881. end;
  1882. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1883. begin
  1884. inherited ppuload(setdef,ppufile);
  1885. ppufile.getderef(elementdefderef);
  1886. savesize:=ppufile.getaint;
  1887. setbase:=ppufile.getaint;
  1888. setmax:=ppufile.getaint;
  1889. end;
  1890. function tsetdef.getcopy : tstoreddef;
  1891. begin
  1892. result:=tsetdef.create(elementdef,setbase,setmax);
  1893. { the copy might have been created with a different setalloc setting }
  1894. tsetdef(result).savesize:=savesize;
  1895. end;
  1896. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1897. begin
  1898. inherited ppuwrite(ppufile);
  1899. ppufile.putderef(elementdefderef);
  1900. ppufile.putaint(savesize);
  1901. ppufile.putaint(setbase);
  1902. ppufile.putaint(setmax);
  1903. ppufile.writeentry(ibsetdef);
  1904. end;
  1905. procedure tsetdef.buildderef;
  1906. begin
  1907. inherited buildderef;
  1908. elementdefderef.build(elementdef);
  1909. end;
  1910. procedure tsetdef.deref;
  1911. begin
  1912. inherited deref;
  1913. elementdef:=tdef(elementdefderef.resolve);
  1914. end;
  1915. function tsetdef.is_publishable : boolean;
  1916. begin
  1917. is_publishable:=savesize in [1,2,4];
  1918. end;
  1919. function tsetdef.GetTypeName : string;
  1920. begin
  1921. if assigned(elementdef) then
  1922. GetTypeName:='Set Of '+elementdef.typename
  1923. else
  1924. GetTypeName:='Empty Set';
  1925. end;
  1926. {***************************************************************************
  1927. TFORMALDEF
  1928. ***************************************************************************}
  1929. constructor tformaldef.create(Atyped:boolean);
  1930. begin
  1931. inherited create(formaldef);
  1932. typed:=Atyped;
  1933. savesize:=0;
  1934. end;
  1935. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1936. begin
  1937. inherited ppuload(formaldef,ppufile);
  1938. typed:=boolean(ppufile.getbyte);
  1939. savesize:=0;
  1940. end;
  1941. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1942. begin
  1943. inherited ppuwrite(ppufile);
  1944. ppufile.putbyte(byte(typed));
  1945. ppufile.writeentry(ibformaldef);
  1946. end;
  1947. function tformaldef.GetTypeName : string;
  1948. begin
  1949. if typed then
  1950. GetTypeName:='<Typed formal type>'
  1951. else
  1952. GetTypeName:='<Formal type>';
  1953. end;
  1954. {***************************************************************************
  1955. TARRAYDEF
  1956. ***************************************************************************}
  1957. constructor tarraydef.create(l,h : aint;def:tdef);
  1958. begin
  1959. inherited create(arraydef);
  1960. lowrange:=l;
  1961. highrange:=h;
  1962. rangedef:=def;
  1963. _elementdef:=nil;
  1964. arrayoptions:=[];
  1965. end;
  1966. constructor tarraydef.create_from_pointer(def:tdef);
  1967. begin
  1968. { use -1 so that the elecount will not overflow }
  1969. self.create(0,high(aint)-1,s32inttype);
  1970. arrayoptions:=[ado_IsConvertedPointer];
  1971. setelementdef(def);
  1972. end;
  1973. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  1974. begin
  1975. inherited ppuload(arraydef,ppufile);
  1976. { the addresses are calculated later }
  1977. ppufile.getderef(_elementdefderef);
  1978. ppufile.getderef(rangedefderef);
  1979. lowrange:=ppufile.getaint;
  1980. highrange:=ppufile.getaint;
  1981. ppufile.getsmallset(arrayoptions);
  1982. end;
  1983. function tarraydef.getcopy : tstoreddef;
  1984. begin
  1985. result:=tarraydef.create(lowrange,highrange,rangedef);
  1986. tarraydef(result).arrayoptions:=arrayoptions;
  1987. tarraydef(result)._elementdef:=_elementdef;
  1988. end;
  1989. procedure tarraydef.buildderef;
  1990. begin
  1991. inherited buildderef;
  1992. _elementdefderef.build(_elementdef);
  1993. rangedefderef.build(rangedef);
  1994. end;
  1995. procedure tarraydef.deref;
  1996. begin
  1997. inherited deref;
  1998. _elementdef:=tdef(_elementdefderef.resolve);
  1999. rangedef:=tdef(rangedefderef.resolve);
  2000. end;
  2001. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2002. begin
  2003. inherited ppuwrite(ppufile);
  2004. ppufile.putderef(_elementdefderef);
  2005. ppufile.putderef(rangedefderef);
  2006. ppufile.putaint(lowrange);
  2007. ppufile.putaint(highrange);
  2008. ppufile.putsmallset(arrayoptions);
  2009. ppufile.writeentry(ibarraydef);
  2010. end;
  2011. function tarraydef.elesize : aint;
  2012. begin
  2013. if (ado_IsBitPacked in arrayoptions) then
  2014. internalerror(2006080101);
  2015. if assigned(_elementdef) then
  2016. result:=_elementdef.size
  2017. else
  2018. result:=0;
  2019. end;
  2020. function tarraydef.elepackedbitsize : aint;
  2021. begin
  2022. if not(ado_IsBitPacked in arrayoptions) then
  2023. internalerror(2006080102);
  2024. if assigned(_elementdef) then
  2025. result:=_elementdef.packedbitsize
  2026. else
  2027. result:=0;
  2028. end;
  2029. function tarraydef.elecount : aword;
  2030. var
  2031. qhigh,qlow : qword;
  2032. begin
  2033. if ado_IsDynamicArray in arrayoptions then
  2034. begin
  2035. result:=0;
  2036. exit;
  2037. end;
  2038. if (highrange>0) and (lowrange<0) then
  2039. begin
  2040. qhigh:=highrange;
  2041. qlow:=qword(-lowrange);
  2042. { prevent overflow, return 0 to indicate overflow }
  2043. if qhigh+qlow>qword(high(aint)-1) then
  2044. result:=0
  2045. else
  2046. result:=qhigh+qlow+1;
  2047. end
  2048. else
  2049. result:=int64(highrange)-lowrange+1;
  2050. end;
  2051. function tarraydef.size : aint;
  2052. var
  2053. cachedelecount : aword;
  2054. cachedelesize : aint;
  2055. begin
  2056. if ado_IsDynamicArray in arrayoptions then
  2057. begin
  2058. size:=sizeof(pint);
  2059. exit;
  2060. end;
  2061. { Tarraydef.size may never be called for an open array! }
  2062. if highrange<lowrange then
  2063. internalerror(99080501);
  2064. if not (ado_IsBitPacked in arrayoptions) then
  2065. cachedelesize:=elesize
  2066. else
  2067. cachedelesize := elepackedbitsize;
  2068. cachedelecount:=elecount;
  2069. if (cachedelesize = 0) then
  2070. begin
  2071. size := 0;
  2072. exit;
  2073. end;
  2074. if (cachedelecount = 0) then
  2075. begin
  2076. size := -1;
  2077. exit;
  2078. end;
  2079. { prevent overflow, return -1 to indicate overflow }
  2080. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2081. if (cachedelecount > high(aint)) or
  2082. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2083. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2084. accessing the array, see ncgmem (PFV) }
  2085. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2086. begin
  2087. result:=-1;
  2088. exit;
  2089. end;
  2090. if (ado_IsBitPacked in arrayoptions) then
  2091. size:=(cachedelesize * aint(cachedelecount) + 7) div 8
  2092. else
  2093. result:=cachedelesize*aint(cachedelecount);
  2094. end;
  2095. procedure tarraydef.setelementdef(def:tdef);
  2096. begin
  2097. _elementdef:=def;
  2098. if not(
  2099. (ado_IsDynamicArray in arrayoptions) or
  2100. (ado_IsConvertedPointer in arrayoptions) or
  2101. (highrange<lowrange)
  2102. ) and
  2103. (size=-1) then
  2104. Message(sym_e_segment_too_large);
  2105. end;
  2106. function tarraydef.alignment : shortint;
  2107. begin
  2108. { alignment of dyn. arrays doesn't depend on the element size }
  2109. if (ado_IsDynamicArray in arrayoptions) then
  2110. alignment:=size_2_align(sizeof(pint))
  2111. { alignment is the size of the elements }
  2112. else if (elementdef.typ in [arraydef,recorddef]) or
  2113. ((elementdef.typ=objectdef) and
  2114. is_object(elementdef)) then
  2115. alignment:=elementdef.alignment
  2116. else if not (ado_IsBitPacked in arrayoptions) then
  2117. alignment:=size_2_align(elesize)
  2118. else
  2119. alignment:=packedbitsloadsize(elepackedbitsize);
  2120. end;
  2121. function tarraydef.needs_inittable : boolean;
  2122. begin
  2123. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2124. end;
  2125. function tarraydef.GetTypeName : string;
  2126. begin
  2127. if (ado_IsConstString in arrayoptions) then
  2128. result:='Constant String'
  2129. else if (ado_isarrayofconst in arrayoptions) or
  2130. (ado_isConstructor in arrayoptions) then
  2131. begin
  2132. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2133. GetTypeName:='Array Of Const'
  2134. else
  2135. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2136. end
  2137. else if (ado_IsDynamicArray in arrayoptions) then
  2138. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2139. else if ((highrange=-1) and (lowrange=0)) then
  2140. GetTypeName:='Open Array Of '+elementdef.typename
  2141. else
  2142. begin
  2143. result := '';
  2144. if (ado_IsBitPacked in arrayoptions) then
  2145. result:='Packed ';
  2146. if rangedef.typ=enumdef then
  2147. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2148. else
  2149. result:=result+'Array['+tostr(lowrange)+'..'+
  2150. tostr(highrange)+'] Of '+elementdef.typename
  2151. end;
  2152. end;
  2153. function tarraydef.getmangledparaname : string;
  2154. begin
  2155. if ado_isarrayofconst in arrayoptions then
  2156. getmangledparaname:='array_of_const'
  2157. else
  2158. if ((highrange=-1) and (lowrange=0)) then
  2159. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2160. else
  2161. internalerror(200204176);
  2162. end;
  2163. function tarraydef.is_publishable : boolean;
  2164. begin
  2165. Result:=ado_IsDynamicArray in arrayoptions;
  2166. end;
  2167. {***************************************************************************
  2168. tabstractrecorddef
  2169. ***************************************************************************}
  2170. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2171. begin
  2172. if t=gs_record then
  2173. GetSymtable:=symtable
  2174. else
  2175. GetSymtable:=nil;
  2176. end;
  2177. procedure tabstractrecorddef.reset;
  2178. begin
  2179. inherited reset;
  2180. tstoredsymtable(symtable).reset_all_defs;
  2181. end;
  2182. function tabstractrecorddef.is_packed:boolean;
  2183. begin
  2184. result:=tabstractrecordsymtable(symtable).is_packed;
  2185. end;
  2186. {***************************************************************************
  2187. trecorddef
  2188. ***************************************************************************}
  2189. constructor trecorddef.create(p : TSymtable);
  2190. begin
  2191. inherited create(recorddef);
  2192. symtable:=p;
  2193. { we can own the symtable only if nobody else owns a copy so far }
  2194. if symtable.refcount=1 then
  2195. symtable.defowner:=self;
  2196. isunion:=false;
  2197. end;
  2198. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2199. begin
  2200. inherited ppuload(recorddef,ppufile);
  2201. if df_copied_def in defoptions then
  2202. ppufile.getderef(cloneddefderef)
  2203. else
  2204. begin
  2205. symtable:=trecordsymtable.create(0);
  2206. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2207. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2208. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2209. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2210. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2211. trecordsymtable(symtable).ppuload(ppufile);
  2212. { requires usefieldalignment to be set }
  2213. symtable.defowner:=self;
  2214. end;
  2215. isunion:=false;
  2216. end;
  2217. destructor trecorddef.destroy;
  2218. begin
  2219. if assigned(symtable) then
  2220. begin
  2221. symtable.free;
  2222. symtable:=nil;
  2223. end;
  2224. inherited destroy;
  2225. end;
  2226. function trecorddef.getcopy : tstoreddef;
  2227. begin
  2228. result:=trecorddef.create(symtable.getcopy);
  2229. trecorddef(result).isunion:=isunion;
  2230. include(trecorddef(result).defoptions,df_copied_def);
  2231. end;
  2232. function trecorddef.needs_inittable : boolean;
  2233. begin
  2234. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2235. end;
  2236. procedure trecorddef.buildderef;
  2237. begin
  2238. inherited buildderef;
  2239. if df_copied_def in defoptions then
  2240. cloneddefderef.build(symtable.defowner)
  2241. else
  2242. tstoredsymtable(symtable).buildderef;
  2243. end;
  2244. procedure trecorddef.deref;
  2245. begin
  2246. inherited deref;
  2247. { now dereference the definitions }
  2248. if df_copied_def in defoptions then
  2249. begin
  2250. cloneddef:=trecorddef(cloneddefderef.resolve);
  2251. symtable:=cloneddef.symtable.getcopy;
  2252. end
  2253. else
  2254. tstoredsymtable(symtable).deref;
  2255. { assign TGUID? load only from system unit }
  2256. if not(assigned(rec_tguid)) and
  2257. (upper(typename)='TGUID') and
  2258. assigned(owner) and
  2259. assigned(owner.name) and
  2260. (owner.name^='SYSTEM') then
  2261. rec_tguid:=self;
  2262. end;
  2263. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2264. begin
  2265. inherited ppuwrite(ppufile);
  2266. if df_copied_def in defoptions then
  2267. ppufile.putderef(cloneddefderef)
  2268. else
  2269. begin
  2270. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2271. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2272. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2273. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2274. ppufile.putaint(trecordsymtable(symtable).datasize);
  2275. end;
  2276. ppufile.writeentry(ibrecorddef);
  2277. if not(df_copied_def in defoptions) then
  2278. trecordsymtable(symtable).ppuwrite(ppufile);
  2279. end;
  2280. function trecorddef.size:aint;
  2281. begin
  2282. result:=trecordsymtable(symtable).datasize;
  2283. end;
  2284. function trecorddef.alignment:shortint;
  2285. begin
  2286. alignment:=trecordsymtable(symtable).recordalignment;
  2287. end;
  2288. function trecorddef.padalignment:shortint;
  2289. begin
  2290. padalignment := trecordsymtable(symtable).padalignment;
  2291. end;
  2292. function trecorddef.GetTypeName : string;
  2293. begin
  2294. GetTypeName:='<record type>'
  2295. end;
  2296. {***************************************************************************
  2297. TABSTRACTPROCDEF
  2298. ***************************************************************************}
  2299. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2300. begin
  2301. inherited create(dt);
  2302. parast:=tparasymtable.create(self,level);
  2303. paras:=nil;
  2304. minparacount:=0;
  2305. maxparacount:=0;
  2306. proctypeoption:=potype_none;
  2307. proccalloption:=pocall_none;
  2308. procoptions:=[];
  2309. returndef:=voidtype;
  2310. savesize:=sizeof(pint);
  2311. requiredargarea:=0;
  2312. has_paraloc_info:=false;
  2313. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2314. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2315. end;
  2316. destructor tabstractprocdef.destroy;
  2317. begin
  2318. if assigned(paras) then
  2319. begin
  2320. {$ifdef MEMDEBUG}
  2321. memprocpara.start;
  2322. {$endif MEMDEBUG}
  2323. paras.free;
  2324. paras:=nil;
  2325. {$ifdef MEMDEBUG}
  2326. memprocpara.stop;
  2327. {$endif MEMDEBUG}
  2328. end;
  2329. if assigned(parast) then
  2330. begin
  2331. {$ifdef MEMDEBUG}
  2332. memprocparast.start;
  2333. {$endif MEMDEBUG}
  2334. parast.free;
  2335. parast:=nil;
  2336. {$ifdef MEMDEBUG}
  2337. memprocparast.stop;
  2338. {$endif MEMDEBUG}
  2339. end;
  2340. inherited destroy;
  2341. end;
  2342. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2343. begin
  2344. if (tsym(p).typ<>paravarsym) then
  2345. exit;
  2346. inc(plongint(arg)^);
  2347. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2348. begin
  2349. if not assigned(tparavarsym(p).defaultconstsym) then
  2350. inc(minparacount);
  2351. inc(maxparacount);
  2352. end;
  2353. end;
  2354. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2355. begin
  2356. if (tsym(p).typ<>paravarsym) then
  2357. exit;
  2358. paras.add(p);
  2359. end;
  2360. procedure tabstractprocdef.calcparas;
  2361. var
  2362. paracount : longint;
  2363. begin
  2364. { This can already be assigned when
  2365. we need to reresolve this unit (PFV) }
  2366. if assigned(paras) then
  2367. paras.free;
  2368. paras:=tparalist.create(false);
  2369. paracount:=0;
  2370. minparacount:=0;
  2371. maxparacount:=0;
  2372. parast.SymList.ForEachCall(@count_para,@paracount);
  2373. paras.capacity:=paracount;
  2374. { Insert parameters in table }
  2375. parast.SymList.ForEachCall(@insert_para,nil);
  2376. { Order parameters }
  2377. paras.sortparas;
  2378. end;
  2379. procedure tabstractprocdef.buildderef;
  2380. begin
  2381. { released procdef? }
  2382. if not assigned(parast) then
  2383. exit;
  2384. inherited buildderef;
  2385. returndefderef.build(returndef);
  2386. { parast }
  2387. tparasymtable(parast).buildderef;
  2388. end;
  2389. procedure tabstractprocdef.deref;
  2390. begin
  2391. inherited deref;
  2392. returndef:=tdef(returndefderef.resolve);
  2393. { parast }
  2394. tparasymtable(parast).deref;
  2395. { recalculated parameters }
  2396. calcparas;
  2397. end;
  2398. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2399. var
  2400. b : byte;
  2401. begin
  2402. inherited ppuload(dt,ppufile);
  2403. parast:=nil;
  2404. Paras:=nil;
  2405. minparacount:=0;
  2406. maxparacount:=0;
  2407. ppufile.getderef(returndefderef);
  2408. { TODO: remove fpu_used loading}
  2409. ppufile.getbyte;
  2410. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2411. proccalloption:=tproccalloption(ppufile.getbyte);
  2412. ppufile.getnormalset(procoptions);
  2413. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2414. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2415. if po_explicitparaloc in procoptions then
  2416. begin
  2417. b:=ppufile.getbyte;
  2418. if b<>sizeof(funcretloc[callerside]) then
  2419. internalerror(200411154);
  2420. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2421. end;
  2422. savesize:=sizeof(pint);
  2423. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2424. end;
  2425. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2426. var
  2427. oldintfcrc : boolean;
  2428. begin
  2429. { released procdef? }
  2430. if not assigned(parast) then
  2431. exit;
  2432. inherited ppuwrite(ppufile);
  2433. ppufile.putderef(returndefderef);
  2434. oldintfcrc:=ppufile.do_interface_crc;
  2435. ppufile.do_interface_crc:=false;
  2436. ppufile.putbyte(0);
  2437. ppufile.putbyte(ord(proctypeoption));
  2438. ppufile.putbyte(ord(proccalloption));
  2439. ppufile.putnormalset(procoptions);
  2440. ppufile.do_interface_crc:=oldintfcrc;
  2441. if (po_explicitparaloc in procoptions) then
  2442. begin
  2443. { Make a 'valid' funcretloc for procedures }
  2444. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2445. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2446. end;
  2447. end;
  2448. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2449. var
  2450. hs,s : string;
  2451. hp : TParavarsym;
  2452. hpc : tconstsym;
  2453. first : boolean;
  2454. i : integer;
  2455. begin
  2456. s:='';
  2457. first:=true;
  2458. for i:=0 to paras.count-1 do
  2459. begin
  2460. hp:=tparavarsym(paras[i]);
  2461. if not(vo_is_hidden_para in hp.varoptions) or
  2462. (showhidden) then
  2463. begin
  2464. if first then
  2465. begin
  2466. s:=s+'(';
  2467. first:=false;
  2468. end
  2469. else
  2470. s:=s+',';
  2471. if vo_is_hidden_para in hp.varoptions then
  2472. s:=s+'<';
  2473. case hp.varspez of
  2474. vs_var :
  2475. s:=s+'var ';
  2476. vs_const :
  2477. s:=s+'const ';
  2478. vs_out :
  2479. s:=s+'out ';
  2480. end;
  2481. if assigned(hp.vardef.typesym) then
  2482. begin
  2483. hs:=hp.vardef.typesym.realname;
  2484. if hs[1]<>'$' then
  2485. s:=s+hs
  2486. else
  2487. s:=s+hp.vardef.GetTypeName;
  2488. end
  2489. else
  2490. s:=s+hp.vardef.GetTypeName;
  2491. { default value }
  2492. if assigned(hp.defaultconstsym) then
  2493. begin
  2494. hpc:=tconstsym(hp.defaultconstsym);
  2495. hs:='';
  2496. case hpc.consttyp of
  2497. conststring,
  2498. constresourcestring :
  2499. begin
  2500. If hpc.value.len>0 then
  2501. begin
  2502. setLength(hs,hpc.value.len);
  2503. move(hpc.value.valueptr^,hs[1],hpc.value.len);
  2504. end;
  2505. end;
  2506. constreal :
  2507. str(pbestreal(hpc.value.valueptr)^,hs);
  2508. constpointer :
  2509. hs:=tostr(hpc.value.valueordptr);
  2510. constord :
  2511. begin
  2512. if is_boolean(hpc.constdef) then
  2513. begin
  2514. if hpc.value.valueord<>0 then
  2515. hs:='TRUE'
  2516. else
  2517. hs:='FALSE';
  2518. end
  2519. else
  2520. hs:=tostr(hpc.value.valueord);
  2521. end;
  2522. constnil :
  2523. hs:='nil';
  2524. constset :
  2525. hs:='<set>';
  2526. end;
  2527. if hs<>'' then
  2528. s:=s+'="'+hs+'"';
  2529. end;
  2530. if vo_is_hidden_para in hp.varoptions then
  2531. s:=s+'>';
  2532. end;
  2533. end;
  2534. if not first then
  2535. s:=s+')';
  2536. if (po_varargs in procoptions) then
  2537. s:=s+';VarArgs';
  2538. typename_paras:=s;
  2539. end;
  2540. function tabstractprocdef.is_methodpointer:boolean;
  2541. begin
  2542. result:=false;
  2543. end;
  2544. function tabstractprocdef.is_addressonly:boolean;
  2545. begin
  2546. result:=true;
  2547. end;
  2548. {***************************************************************************
  2549. TPROCDEF
  2550. ***************************************************************************}
  2551. constructor tprocdef.create(level:byte);
  2552. begin
  2553. inherited create(procdef,level);
  2554. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2555. _mangledname:=nil;
  2556. fileinfo:=current_filepos;
  2557. extnumber:=$ffff;
  2558. aliasnames:=TCmdStrList.create;
  2559. funcretsym:=nil;
  2560. forwarddef:=true;
  2561. interfacedef:=false;
  2562. hasforward:=false;
  2563. _class := nil;
  2564. import_dll:=nil;
  2565. import_name:=nil;
  2566. import_nr:=0;
  2567. inlininginfo:=nil;
  2568. {$ifdef i386}
  2569. fpu_used:=maxfpuregs;
  2570. {$endif i386}
  2571. end;
  2572. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2573. var
  2574. i,aliasnamescount : longint;
  2575. level : byte;
  2576. begin
  2577. inherited ppuload(procdef,ppufile);
  2578. if po_has_mangledname in procoptions then
  2579. _mangledname:=stringdup(ppufile.getstring)
  2580. else
  2581. _mangledname:=nil;
  2582. extnumber:=ppufile.getword;
  2583. level:=ppufile.getbyte;
  2584. ppufile.getderef(_classderef);
  2585. ppufile.getderef(procsymderef);
  2586. ppufile.getposinfo(fileinfo);
  2587. visibility:=tvisibility(ppufile.getbyte);
  2588. ppufile.getsmallset(symoptions);
  2589. {$ifdef powerpc}
  2590. { library symbol for AmigaOS/MorphOS }
  2591. ppufile.getderef(libsymderef);
  2592. {$endif powerpc}
  2593. { import stuff }
  2594. if po_has_importdll in procoptions then
  2595. import_dll:=stringdup(ppufile.getstring)
  2596. else
  2597. import_dll:=nil;
  2598. if po_has_importname in procoptions then
  2599. import_name:=stringdup(ppufile.getstring)
  2600. else
  2601. import_name:=nil;
  2602. import_nr:=ppufile.getword;
  2603. if (po_msgint in procoptions) then
  2604. messageinf.i:=ppufile.getlongint;
  2605. if (po_msgstr in procoptions) then
  2606. messageinf.str:=stringdup(ppufile.getstring);
  2607. if (po_dispid in procoptions) then
  2608. dispid:=ppufile.getlongint;
  2609. { inline stuff }
  2610. if (po_has_inlininginfo in procoptions) then
  2611. begin
  2612. ppufile.getderef(funcretsymderef);
  2613. new(inlininginfo);
  2614. ppufile.getsmallset(inlininginfo^.flags);
  2615. end
  2616. else
  2617. begin
  2618. inlininginfo:=nil;
  2619. funcretsym:=nil;
  2620. end;
  2621. aliasnames:=TCmdStrList.create;
  2622. { count alias names }
  2623. aliasnamescount:=ppufile.getbyte;
  2624. for i:=1 to aliasnamescount do
  2625. aliasnames.insert(ppufile.getstring);
  2626. { load para symtable }
  2627. parast:=tparasymtable.create(self,level);
  2628. tparasymtable(parast).ppuload(ppufile);
  2629. { load local symtable }
  2630. if (po_has_inlininginfo in procoptions) then
  2631. begin
  2632. localst:=tlocalsymtable.create(self,level);
  2633. tlocalsymtable(localst).ppuload(ppufile);
  2634. end
  2635. else
  2636. localst:=nil;
  2637. { inline stuff }
  2638. if (po_has_inlininginfo in procoptions) then
  2639. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2640. { default values for no persistent data }
  2641. if (cs_link_deffile in current_settings.globalswitches) and
  2642. (tf_need_export in target_info.flags) and
  2643. (po_exports in procoptions) then
  2644. deffile.AddExport(mangledname);
  2645. forwarddef:=false;
  2646. interfacedef:=false;
  2647. hasforward:=false;
  2648. { Disable po_has_inlining until the derefimpl is done }
  2649. exclude(procoptions,po_has_inlininginfo);
  2650. {$ifdef i386}
  2651. fpu_used:=maxfpuregs;
  2652. {$endif i386}
  2653. end;
  2654. destructor tprocdef.destroy;
  2655. begin
  2656. aliasnames.free;
  2657. aliasnames:=nil;
  2658. if assigned(localst) and
  2659. (localst.symtabletype<>staticsymtable) then
  2660. begin
  2661. {$ifdef MEMDEBUG}
  2662. memproclocalst.start;
  2663. {$endif MEMDEBUG}
  2664. localst.free;
  2665. localst:=nil;
  2666. {$ifdef MEMDEBUG}
  2667. memproclocalst.start;
  2668. {$endif MEMDEBUG}
  2669. end;
  2670. if assigned(inlininginfo) then
  2671. begin
  2672. {$ifdef MEMDEBUG}
  2673. memprocnodetree.start;
  2674. {$endif MEMDEBUG}
  2675. tnode(inlininginfo^.code).free;
  2676. {$ifdef MEMDEBUG}
  2677. memprocnodetree.start;
  2678. {$endif MEMDEBUG}
  2679. dispose(inlininginfo);
  2680. inlininginfo:=nil;
  2681. end;
  2682. stringdispose(resultname);
  2683. stringdispose(import_dll);
  2684. stringdispose(import_name);
  2685. if (po_msgstr in procoptions) then
  2686. stringdispose(messageinf.str);
  2687. if assigned(_mangledname) then
  2688. begin
  2689. {$ifdef MEMDEBUG}
  2690. memmanglednames.start;
  2691. {$endif MEMDEBUG}
  2692. stringdispose(_mangledname);
  2693. {$ifdef MEMDEBUG}
  2694. memmanglednames.stop;
  2695. {$endif MEMDEBUG}
  2696. end;
  2697. inherited destroy;
  2698. end;
  2699. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2700. var
  2701. oldintfcrc : boolean;
  2702. aliasnamescount : longint;
  2703. item : TCmdStrListItem;
  2704. begin
  2705. { released procdef? }
  2706. if not assigned(parast) then
  2707. exit;
  2708. inherited ppuwrite(ppufile);
  2709. if po_has_mangledname in procoptions then
  2710. ppufile.putstring(_mangledname^);
  2711. ppufile.putword(extnumber);
  2712. ppufile.putbyte(parast.symtablelevel);
  2713. ppufile.putderef(_classderef);
  2714. ppufile.putderef(procsymderef);
  2715. ppufile.putposinfo(fileinfo);
  2716. ppufile.putbyte(byte(visibility));
  2717. ppufile.putsmallset(symoptions);
  2718. {$ifdef powerpc}
  2719. { library symbol for AmigaOS/MorphOS }
  2720. ppufile.putderef(libsymderef);
  2721. {$endif powerpc}
  2722. { import }
  2723. if po_has_importdll in procoptions then
  2724. ppufile.putstring(import_dll^);
  2725. if po_has_importname in procoptions then
  2726. ppufile.putstring(import_name^);
  2727. ppufile.putword(import_nr);
  2728. if (po_msgint in procoptions) then
  2729. ppufile.putlongint(messageinf.i);
  2730. if (po_msgstr in procoptions) then
  2731. ppufile.putstring(messageinf.str^);
  2732. if (po_dispid in procoptions) then
  2733. ppufile.putlongint(dispid);
  2734. { inline stuff }
  2735. oldintfcrc:=ppufile.do_crc;
  2736. ppufile.do_crc:=false;
  2737. if (po_has_inlininginfo in procoptions) then
  2738. begin
  2739. ppufile.putderef(funcretsymderef);
  2740. ppufile.putsmallset(inlininginfo^.flags);
  2741. end;
  2742. { count alias names }
  2743. aliasnamescount:=0;
  2744. item:=TCmdStrListItem(aliasnames.first);
  2745. while assigned(item) do
  2746. begin
  2747. inc(aliasnamescount);
  2748. item:=TCmdStrListItem(item.next);
  2749. end;
  2750. if aliasnamescount>255 then
  2751. internalerror(200711021);
  2752. ppufile.putbyte(aliasnamescount);
  2753. item:=TCmdStrListItem(aliasnames.first);
  2754. while assigned(item) do
  2755. begin
  2756. ppufile.putstring(item.str);
  2757. item:=TCmdStrListItem(item.next);
  2758. end;
  2759. ppufile.do_crc:=oldintfcrc;
  2760. { write this entry }
  2761. ppufile.writeentry(ibprocdef);
  2762. { Save the para symtable, this is taken from the interface }
  2763. tparasymtable(parast).ppuwrite(ppufile);
  2764. { save localsymtable for inline procedures or when local
  2765. browser info is requested, this has no influence on the crc }
  2766. if (po_has_inlininginfo in procoptions) then
  2767. begin
  2768. oldintfcrc:=ppufile.do_crc;
  2769. ppufile.do_crc:=false;
  2770. tlocalsymtable(localst).ppuwrite(ppufile);
  2771. ppufile.do_crc:=oldintfcrc;
  2772. end;
  2773. { node tree for inlining }
  2774. oldintfcrc:=ppufile.do_crc;
  2775. ppufile.do_crc:=false;
  2776. if (po_has_inlininginfo in procoptions) then
  2777. ppuwritenodetree(ppufile,inlininginfo^.code);
  2778. ppufile.do_crc:=oldintfcrc;
  2779. end;
  2780. procedure tprocdef.reset;
  2781. begin
  2782. inherited reset;
  2783. procstarttai:=nil;
  2784. procendtai:=nil;
  2785. end;
  2786. function tprocdef.fullprocname(showhidden:boolean):string;
  2787. var
  2788. s : string;
  2789. t : ttoken;
  2790. begin
  2791. {$ifdef EXTDEBUG}
  2792. showhidden:=true;
  2793. {$endif EXTDEBUG}
  2794. s:='';
  2795. if owner.symtabletype=localsymtable then
  2796. s:=s+'local ';
  2797. if assigned(_class) then
  2798. begin
  2799. if po_classmethod in procoptions then
  2800. s:=s+'class ';
  2801. s:=s+_class.objrealname^+'.';
  2802. end;
  2803. if proctypeoption=potype_operator then
  2804. begin
  2805. for t:=NOTOKEN to last_overloaded do
  2806. if procsym.realname='$'+overloaded_names[t] then
  2807. begin
  2808. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2809. break;
  2810. end;
  2811. end
  2812. else
  2813. s:=s+procsym.realname+typename_paras(showhidden);
  2814. case proctypeoption of
  2815. potype_constructor:
  2816. s:='constructor '+s;
  2817. potype_destructor:
  2818. s:='destructor '+s;
  2819. else
  2820. if assigned(returndef) and
  2821. not(is_void(returndef)) then
  2822. s:=s+':'+returndef.GetTypeName;
  2823. end;
  2824. s:=s+';';
  2825. { forced calling convention? }
  2826. if (po_hascallingconvention in procoptions) then
  2827. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2828. if po_staticmethod in procoptions then
  2829. s:=s+' Static;';
  2830. fullprocname:=s;
  2831. end;
  2832. function tprocdef.is_methodpointer:boolean;
  2833. begin
  2834. result:=assigned(_class);
  2835. end;
  2836. function tprocdef.is_addressonly:boolean;
  2837. begin
  2838. result:=assigned(owner) and
  2839. (owner.symtabletype<>ObjectSymtable);
  2840. end;
  2841. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2842. begin
  2843. case t of
  2844. gs_local :
  2845. GetSymtable:=localst;
  2846. gs_para :
  2847. GetSymtable:=parast;
  2848. else
  2849. GetSymtable:=nil;
  2850. end;
  2851. end;
  2852. procedure tprocdef.buildderef;
  2853. begin
  2854. inherited buildderef;
  2855. _classderef.build(_class);
  2856. { procsym that originaly defined this definition, should be in the
  2857. same symtable }
  2858. procsymderef.build(procsym);
  2859. {$ifdef powerpc}
  2860. { library symbol for AmigaOS/MorphOS }
  2861. libsymderef.build(libsym);
  2862. {$endif powerpc}
  2863. end;
  2864. procedure tprocdef.buildderefimpl;
  2865. begin
  2866. inherited buildderefimpl;
  2867. { Localst is not available for main/unit init }
  2868. if assigned(localst) then
  2869. begin
  2870. tlocalsymtable(localst).buildderef;
  2871. tlocalsymtable(localst).buildderefimpl;
  2872. end;
  2873. { inline tree }
  2874. if (po_has_inlininginfo in procoptions) then
  2875. begin
  2876. funcretsymderef.build(funcretsym);
  2877. inlininginfo^.code.buildderefimpl;
  2878. end;
  2879. end;
  2880. procedure tprocdef.deref;
  2881. begin
  2882. inherited deref;
  2883. _class:=tobjectdef(_classderef.resolve);
  2884. { procsym that originaly defined this definition, should be in the
  2885. same symtable }
  2886. procsym:=tprocsym(procsymderef.resolve);
  2887. {$ifdef powerpc}
  2888. { library symbol for AmigaOS/MorphOS }
  2889. libsym:=tsym(libsymderef.resolve);
  2890. {$endif powerpc}
  2891. end;
  2892. procedure tprocdef.derefimpl;
  2893. begin
  2894. { Enable has_inlininginfo when the inlininginfo
  2895. structure is available. The has_inlininginfo was disabled
  2896. after the load, since the data was invalid }
  2897. if assigned(inlininginfo) then
  2898. include(procoptions,po_has_inlininginfo);
  2899. { Locals }
  2900. if assigned(localst) then
  2901. begin
  2902. tlocalsymtable(localst).deref;
  2903. tlocalsymtable(localst).derefimpl;
  2904. end;
  2905. { Inline }
  2906. if (po_has_inlininginfo in procoptions) then
  2907. begin
  2908. inlininginfo^.code.derefimpl;
  2909. { funcretsym, this is always located in the localst }
  2910. funcretsym:=tsym(funcretsymderef.resolve);
  2911. end
  2912. else
  2913. begin
  2914. { safety }
  2915. funcretsym:=nil;
  2916. end;
  2917. end;
  2918. function tprocdef.GetTypeName : string;
  2919. begin
  2920. GetTypeName := FullProcName(false);
  2921. end;
  2922. function tprocdef.mangledname : string;
  2923. var
  2924. hp : TParavarsym;
  2925. hs : string;
  2926. crc : dword;
  2927. newlen,
  2928. oldlen,
  2929. i : integer;
  2930. begin
  2931. if assigned(_mangledname) then
  2932. begin
  2933. {$ifdef compress}
  2934. mangledname:=minilzw_decode(_mangledname^);
  2935. {$else}
  2936. mangledname:=_mangledname^;
  2937. {$endif}
  2938. exit;
  2939. end;
  2940. { we need to use the symtable where the procsym is inserted,
  2941. because that is visible to the world }
  2942. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2943. oldlen:=length(mangledname);
  2944. { add parameter types }
  2945. for i:=0 to paras.count-1 do
  2946. begin
  2947. hp:=tparavarsym(paras[i]);
  2948. if not(vo_is_hidden_para in hp.varoptions) then
  2949. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2950. end;
  2951. { add resultdef, add $$ as separator to make it unique from a
  2952. parameter separator }
  2953. if not is_void(returndef) then
  2954. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2955. newlen:=length(mangledname);
  2956. { Replace with CRC if the parameter line is very long }
  2957. if (newlen-oldlen>12) and
  2958. ((newlen>128) or (newlen-oldlen>64)) then
  2959. begin
  2960. crc:=0;
  2961. for i:=0 to paras.count-1 do
  2962. begin
  2963. hp:=tparavarsym(paras[i]);
  2964. if not(vo_is_hidden_para in hp.varoptions) then
  2965. begin
  2966. hs:=hp.vardef.mangledparaname;
  2967. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2968. end;
  2969. end;
  2970. hs:=hp.vardef.mangledparaname;
  2971. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2972. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2973. end;
  2974. {$ifdef compress}
  2975. _mangledname:=stringdup(minilzw_encode(mangledname));
  2976. {$else}
  2977. _mangledname:=stringdup(mangledname);
  2978. {$endif}
  2979. end;
  2980. function tprocdef.cplusplusmangledname : string;
  2981. function getcppparaname(p : tdef) : string;
  2982. const
  2983. ordtype2str : array[tordtype] of string[2] = (
  2984. '',
  2985. 'Uc','Us','Ui','Us',
  2986. 'Sc','s','i','x',
  2987. 'b','b','b','b','b',
  2988. 'c','w','x');
  2989. var
  2990. s : string;
  2991. begin
  2992. case p.typ of
  2993. orddef:
  2994. s:=ordtype2str[torddef(p).ordtype];
  2995. pointerdef:
  2996. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  2997. else
  2998. internalerror(2103001);
  2999. end;
  3000. getcppparaname:=s;
  3001. end;
  3002. var
  3003. s,s2 : string;
  3004. hp : TParavarsym;
  3005. i : integer;
  3006. begin
  3007. { outdated gcc 2.x name mangling scheme }
  3008. {$ifdef NAMEMANGLING_GCC2}
  3009. s := procsym.realname;
  3010. if procsym.owner.symtabletype=ObjectSymtable then
  3011. begin
  3012. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3013. case proctypeoption of
  3014. potype_destructor:
  3015. s:='_$_'+tostr(length(s2))+s2;
  3016. potype_constructor:
  3017. s:='___'+tostr(length(s2))+s2;
  3018. else
  3019. s:='_'+s+'__'+tostr(length(s2))+s2;
  3020. end;
  3021. end
  3022. else s:=s+'__';
  3023. s:=s+'F';
  3024. { concat modifiers }
  3025. { !!!!! }
  3026. { now we handle the parameters }
  3027. if maxparacount>0 then
  3028. begin
  3029. for i:=0 to paras.count-1 do
  3030. begin
  3031. hp:=tparavarsym(paras[i]);
  3032. s2:=getcppparaname(hp.vardef);
  3033. if hp.varspez in [vs_var,vs_out] then
  3034. s2:='R'+s2;
  3035. s:=s+s2;
  3036. end;
  3037. end
  3038. else
  3039. s:=s+'v';
  3040. cplusplusmangledname:=s;
  3041. {$endif NAMEMANGLING_GCC2}
  3042. { gcc 3.x name mangling scheme }
  3043. if procsym.owner.symtabletype=ObjectSymtable then
  3044. begin
  3045. s:='_ZN';
  3046. s2:=tobjectdef(procsym.owner.defowner).objrealname^;
  3047. s:=s+tostr(length(s2))+s2;
  3048. case proctypeoption of
  3049. potype_constructor:
  3050. s:=s+'C1';
  3051. potype_destructor:
  3052. s:=s+'D1';
  3053. else
  3054. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3055. end;
  3056. s:=s+'E';
  3057. end
  3058. else
  3059. s:=procsym.realname;
  3060. { now we handle the parameters }
  3061. if maxparacount>0 then
  3062. begin
  3063. for i:=0 to paras.count-1 do
  3064. begin
  3065. hp:=tparavarsym(paras[i]);
  3066. s2:=getcppparaname(hp.vardef);
  3067. if hp.varspez in [vs_var,vs_out] then
  3068. s2:='R'+s2;
  3069. s:=s+s2;
  3070. end;
  3071. end
  3072. else
  3073. s:=s+'v';
  3074. cplusplusmangledname:=s;
  3075. end;
  3076. procedure tprocdef.setmangledname(const s : string);
  3077. begin
  3078. { This is not allowed anymore, the forward declaration
  3079. already needs to create the correct mangledname, no changes
  3080. afterwards are allowed (PFV) }
  3081. { Exception: interface definitions in mode macpas, since in that }
  3082. { case no reference to the old name can exist yet (JM) }
  3083. if assigned(_mangledname) then
  3084. if ((m_mac in current_settings.modeswitches) and
  3085. (interfacedef)) then
  3086. stringdispose(_mangledname)
  3087. else
  3088. internalerror(200411171);
  3089. {$ifdef compress}
  3090. _mangledname:=stringdup(minilzw_encode(s));
  3091. {$else}
  3092. _mangledname:=stringdup(s);
  3093. {$endif}
  3094. include(procoptions,po_has_mangledname);
  3095. end;
  3096. {***************************************************************************
  3097. TPROCVARDEF
  3098. ***************************************************************************}
  3099. constructor tprocvardef.create(level:byte);
  3100. begin
  3101. inherited create(procvardef,level);
  3102. end;
  3103. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3104. begin
  3105. inherited ppuload(procvardef,ppufile);
  3106. { load para symtable }
  3107. parast:=tparasymtable.create(self,unknown_level);
  3108. tparasymtable(parast).ppuload(ppufile);
  3109. end;
  3110. function tprocvardef.getcopy : tstoreddef;
  3111. var
  3112. i : tcallercallee;
  3113. j : longint;
  3114. begin
  3115. result:=tprocvardef.create(parast.symtablelevel);
  3116. tprocvardef(result).returndef:=returndef;
  3117. tprocvardef(result).returndefderef:=returndefderef;
  3118. tprocvardef(result).parast:=parast.getcopy;
  3119. tprocvardef(result).savesize:=savesize;
  3120. { create paralist copy }
  3121. tprocvardef(result).paras:=tparalist.create(false);
  3122. tprocvardef(result).paras.count:=paras.count;
  3123. for j:=0 to paras.count-1 do
  3124. tprocvardef(result).paras[j]:=paras[j];
  3125. tprocvardef(result).proctypeoption:=proctypeoption;
  3126. tprocvardef(result).proccalloption:=proccalloption;
  3127. tprocvardef(result).procoptions:=procoptions;
  3128. tprocvardef(result).requiredargarea:=requiredargarea;
  3129. tprocvardef(result).maxparacount:=maxparacount;
  3130. tprocvardef(result).minparacount:=minparacount;
  3131. for i:=low(tcallercallee) to high(tcallercallee) do
  3132. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3133. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3134. {$ifdef m68k}
  3135. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3136. {$endif}
  3137. end;
  3138. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3139. begin
  3140. inherited ppuwrite(ppufile);
  3141. { Write this entry }
  3142. ppufile.writeentry(ibprocvardef);
  3143. { Save the para symtable, this is taken from the interface }
  3144. tparasymtable(parast).ppuwrite(ppufile);
  3145. end;
  3146. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3147. begin
  3148. case t of
  3149. gs_para :
  3150. GetSymtable:=parast;
  3151. else
  3152. GetSymtable:=nil;
  3153. end;
  3154. end;
  3155. function tprocvardef.size : aint;
  3156. begin
  3157. if (po_methodpointer in procoptions) and
  3158. not(po_addressonly in procoptions) then
  3159. size:=2*sizeof(pint)
  3160. else
  3161. size:=sizeof(pint);
  3162. end;
  3163. function tprocvardef.is_methodpointer:boolean;
  3164. begin
  3165. result:=(po_methodpointer in procoptions);
  3166. end;
  3167. function tprocvardef.is_addressonly:boolean;
  3168. begin
  3169. result:=not(po_methodpointer in procoptions) or
  3170. (po_addressonly in procoptions);
  3171. end;
  3172. function tprocvardef.getmangledparaname:string;
  3173. begin
  3174. result:='procvar';
  3175. end;
  3176. function tprocvardef.is_publishable : boolean;
  3177. begin
  3178. is_publishable:=(po_methodpointer in procoptions);
  3179. end;
  3180. function tprocvardef.GetTypeName : string;
  3181. var
  3182. s: string;
  3183. showhidden : boolean;
  3184. begin
  3185. {$ifdef EXTDEBUG}
  3186. showhidden:=true;
  3187. {$else EXTDEBUG}
  3188. showhidden:=false;
  3189. {$endif EXTDEBUG}
  3190. s:='<';
  3191. if po_classmethod in procoptions then
  3192. s := s+'class method type of'
  3193. else
  3194. if po_addressonly in procoptions then
  3195. s := s+'address of'
  3196. else
  3197. s := s+'procedure variable type of';
  3198. if po_local in procoptions then
  3199. s := s+' local';
  3200. if assigned(returndef) and
  3201. (returndef<>voidtype) then
  3202. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3203. else
  3204. s:=s+' procedure'+typename_paras(showhidden);
  3205. if po_methodpointer in procoptions then
  3206. s := s+' of object';
  3207. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3208. end;
  3209. {***************************************************************************
  3210. TOBJECTDEF
  3211. ***************************************************************************}
  3212. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3213. begin
  3214. inherited create(objectdef);
  3215. objecttype:=ot;
  3216. objectoptions:=[];
  3217. childof:=nil;
  3218. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3219. { create space for vmt !! }
  3220. vmtentries:=TFPList.Create;
  3221. vmt_offset:=0;
  3222. set_parent(c);
  3223. objname:=stringdup(upper(n));
  3224. objrealname:=stringdup(n);
  3225. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3226. prepareguid;
  3227. { setup implemented interfaces }
  3228. if objecttype in [odt_class,odt_interfacecorba] then
  3229. ImplementedInterfaces:=TFPObjectList.Create(true)
  3230. else
  3231. ImplementedInterfaces:=nil;
  3232. writing_class_record_dbginfo:=false;
  3233. end;
  3234. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3235. var
  3236. i,
  3237. implintfcount : longint;
  3238. d : tderef;
  3239. ImplIntf : TImplementedInterface;
  3240. vmtentry : pvmtentry;
  3241. begin
  3242. inherited ppuload(objectdef,ppufile);
  3243. objecttype:=tobjecttyp(ppufile.getbyte);
  3244. objrealname:=stringdup(ppufile.getstring);
  3245. objname:=stringdup(upper(objrealname^));
  3246. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3247. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3248. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3249. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3250. vmt_offset:=ppufile.getlongint;
  3251. ppufile.getderef(childofderef);
  3252. ppufile.getsmallset(objectoptions);
  3253. { load guid }
  3254. iidstr:=nil;
  3255. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3256. begin
  3257. new(iidguid);
  3258. ppufile.getguid(iidguid^);
  3259. iidstr:=stringdup(ppufile.getstring);
  3260. end;
  3261. vmtentries:=TFPList.Create;
  3262. vmtentries.count:=ppufile.getlongint;
  3263. for i:=0 to vmtentries.count-1 do
  3264. begin
  3265. ppufile.getderef(d);
  3266. new(vmtentry);
  3267. vmtentry^.procdef:=nil;
  3268. vmtentry^.procdefderef:=d;
  3269. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3270. vmtentries[i]:=vmtentry;
  3271. end;
  3272. { load implemented interfaces }
  3273. if objecttype in [odt_class,odt_interfacecorba] then
  3274. begin
  3275. ImplementedInterfaces:=TFPObjectList.Create(true);
  3276. implintfcount:=ppufile.getlongint;
  3277. for i:=0 to implintfcount-1 do
  3278. begin
  3279. ppufile.getderef(d);
  3280. ImplIntf:=TImplementedInterface.Create_deref(d);
  3281. ImplIntf.IOffset:=ppufile.getlongint;
  3282. ImplementedInterfaces.Add(ImplIntf);
  3283. end;
  3284. end
  3285. else
  3286. ImplementedInterfaces:=nil;
  3287. if df_copied_def in defoptions then
  3288. ppufile.getderef(cloneddefderef)
  3289. else
  3290. tObjectSymtable(symtable).ppuload(ppufile);
  3291. { handles the predefined class tobject }
  3292. { the last TOBJECT which is loaded gets }
  3293. { it ! }
  3294. if (childof=nil) and
  3295. (objecttype=odt_class) and
  3296. (objname^='TOBJECT') then
  3297. class_tobject:=self;
  3298. if (childof=nil) and
  3299. (objecttype=odt_interfacecom) and
  3300. (objname^='IUNKNOWN') then
  3301. interface_iunknown:=self;
  3302. writing_class_record_dbginfo:=false;
  3303. end;
  3304. destructor tobjectdef.destroy;
  3305. begin
  3306. if assigned(symtable) then
  3307. begin
  3308. symtable.free;
  3309. symtable:=nil;
  3310. end;
  3311. stringdispose(objname);
  3312. stringdispose(objrealname);
  3313. stringdispose(iidstr);
  3314. if assigned(ImplementedInterfaces) then
  3315. begin
  3316. ImplementedInterfaces.free;
  3317. ImplementedInterfaces:=nil;
  3318. end;
  3319. if assigned(iidguid) then
  3320. begin
  3321. dispose(iidguid);
  3322. iidguid:=nil;
  3323. end;
  3324. if assigned(vmtentries) then
  3325. begin
  3326. resetvmtentries;
  3327. vmtentries.free;
  3328. vmtentries:=nil;
  3329. end;
  3330. inherited destroy;
  3331. end;
  3332. function tobjectdef.getcopy : tstoreddef;
  3333. var
  3334. i : longint;
  3335. begin
  3336. result:=tobjectdef.create(objecttype,objname^,childof);
  3337. { the constructor allocates a symtable which we release to avoid memory leaks }
  3338. tobjectdef(result).symtable.free;
  3339. tobjectdef(result).symtable:=symtable.getcopy;
  3340. if assigned(objname) then
  3341. tobjectdef(result).objname:=stringdup(objname^);
  3342. if assigned(objrealname) then
  3343. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3344. tobjectdef(result).objectoptions:=objectoptions;
  3345. include(tobjectdef(result).defoptions,df_copied_def);
  3346. tobjectdef(result).vmt_offset:=vmt_offset;
  3347. if assigned(iidguid) then
  3348. begin
  3349. new(tobjectdef(result).iidguid);
  3350. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3351. end;
  3352. if assigned(iidstr) then
  3353. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3354. if assigned(ImplementedInterfaces) then
  3355. begin
  3356. for i:=0 to ImplementedInterfaces.count-1 do
  3357. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3358. end;
  3359. if assigned(vmtentries) then
  3360. begin
  3361. tobjectdef(result).vmtentries:=TFPList.Create;
  3362. tobjectdef(result).copyvmtentries(self);
  3363. end;
  3364. end;
  3365. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3366. var
  3367. i : longint;
  3368. vmtentry : pvmtentry;
  3369. ImplIntf : TImplementedInterface;
  3370. begin
  3371. inherited ppuwrite(ppufile);
  3372. ppufile.putbyte(byte(objecttype));
  3373. ppufile.putstring(objrealname^);
  3374. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3375. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3376. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3377. ppufile.putlongint(vmt_offset);
  3378. ppufile.putderef(childofderef);
  3379. ppufile.putsmallset(objectoptions);
  3380. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3381. begin
  3382. ppufile.putguid(iidguid^);
  3383. ppufile.putstring(iidstr^);
  3384. end;
  3385. ppufile.putlongint(vmtentries.count);
  3386. for i:=0 to vmtentries.count-1 do
  3387. begin
  3388. vmtentry:=pvmtentry(vmtentries[i]);
  3389. ppufile.putderef(vmtentry^.procdefderef);
  3390. ppufile.putbyte(byte(vmtentry^.visibility));
  3391. end;
  3392. if assigned(ImplementedInterfaces) then
  3393. begin
  3394. ppufile.putlongint(ImplementedInterfaces.Count);
  3395. for i:=0 to ImplementedInterfaces.Count-1 do
  3396. begin
  3397. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3398. ppufile.putderef(ImplIntf.intfdefderef);
  3399. ppufile.putlongint(ImplIntf.Ioffset);
  3400. end;
  3401. end;
  3402. if df_copied_def in defoptions then
  3403. ppufile.putderef(cloneddefderef);
  3404. ppufile.writeentry(ibobjectdef);
  3405. if not(df_copied_def in defoptions) then
  3406. tObjectSymtable(symtable).ppuwrite(ppufile);
  3407. end;
  3408. function tobjectdef.GetTypeName:string;
  3409. begin
  3410. { in this case we will go in endless recursion, because then }
  3411. { there is no tsym associated yet with the def. It can occur }
  3412. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3413. { instead of the actual type name }
  3414. if not assigned(typesym) then
  3415. result:='<Currently Parsed Class>'
  3416. else
  3417. result:=typename;
  3418. end;
  3419. procedure tobjectdef.buildderef;
  3420. var
  3421. i : longint;
  3422. vmtentry : pvmtentry;
  3423. begin
  3424. inherited buildderef;
  3425. childofderef.build(childof);
  3426. if df_copied_def in defoptions then
  3427. cloneddefderef.build(symtable.defowner)
  3428. else
  3429. tstoredsymtable(symtable).buildderef;
  3430. for i:=0 to vmtentries.count-1 do
  3431. begin
  3432. vmtentry:=pvmtentry(vmtentries[i]);
  3433. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3434. end;
  3435. if assigned(ImplementedInterfaces) then
  3436. begin
  3437. for i:=0 to ImplementedInterfaces.count-1 do
  3438. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3439. end;
  3440. end;
  3441. procedure tobjectdef.deref;
  3442. var
  3443. i : longint;
  3444. vmtentry : pvmtentry;
  3445. begin
  3446. inherited deref;
  3447. childof:=tobjectdef(childofderef.resolve);
  3448. if df_copied_def in defoptions then
  3449. begin
  3450. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3451. symtable:=cloneddef.symtable.getcopy;
  3452. end
  3453. else
  3454. tstoredsymtable(symtable).deref;
  3455. for i:=0 to vmtentries.count-1 do
  3456. begin
  3457. vmtentry:=pvmtentry(vmtentries[i]);
  3458. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3459. end;
  3460. if assigned(ImplementedInterfaces) then
  3461. begin
  3462. for i:=0 to ImplementedInterfaces.count-1 do
  3463. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3464. end;
  3465. end;
  3466. procedure tobjectdef.buildderefimpl;
  3467. begin
  3468. inherited buildderefimpl;
  3469. if not (df_copied_def in defoptions) then
  3470. tstoredsymtable(symtable).buildderefimpl;
  3471. end;
  3472. procedure tobjectdef.derefimpl;
  3473. begin
  3474. inherited derefimpl;
  3475. if not (df_copied_def in defoptions) then
  3476. tstoredsymtable(symtable).derefimpl;
  3477. end;
  3478. procedure tobjectdef.resetvmtentries;
  3479. var
  3480. i : longint;
  3481. begin
  3482. for i:=0 to vmtentries.Count-1 do
  3483. Dispose(pvmtentry(vmtentries[i]));
  3484. vmtentries.clear;
  3485. end;
  3486. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3487. var
  3488. i : longint;
  3489. vmtentry : pvmtentry;
  3490. begin
  3491. resetvmtentries;
  3492. vmtentries.count:=objdef.vmtentries.count;
  3493. for i:=0 to objdef.vmtentries.count-1 do
  3494. begin
  3495. new(vmtentry);
  3496. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3497. vmtentries[i]:=vmtentry;
  3498. end;
  3499. end;
  3500. function tobjectdef.getparentdef:tdef;
  3501. begin
  3502. { TODO: Remove getparentdef hack}
  3503. { With 2 forward declared classes with the child class before the
  3504. parent class the child class is written earlier to the ppu. Leaving it
  3505. possible to have a reference to the parent class for property overriding,
  3506. but the parent class still has the childof not resolved yet (PFV) }
  3507. if childof=nil then
  3508. childof:=tobjectdef(childofderef.resolve);
  3509. result:=childof;
  3510. end;
  3511. procedure tobjectdef.prepareguid;
  3512. begin
  3513. { set up guid }
  3514. if not assigned(iidguid) then
  3515. begin
  3516. new(iidguid);
  3517. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3518. end;
  3519. { setup iidstring }
  3520. if not assigned(iidstr) then
  3521. iidstr:=stringdup(''); { default is empty string }
  3522. end;
  3523. procedure tobjectdef.set_parent( c : tobjectdef);
  3524. begin
  3525. if assigned(childof) then
  3526. exit;
  3527. childof:=c;
  3528. if not assigned(c) then
  3529. exit;
  3530. { inherit options and status }
  3531. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3532. { add the data of the anchestor class/object }
  3533. if (objecttype in [odt_class,odt_object]) then
  3534. begin
  3535. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3536. { inherit recordalignment }
  3537. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3538. if (oo_has_vmt in objectoptions) and
  3539. (oo_has_vmt in c.objectoptions) then
  3540. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3541. { if parent has a vmt field then the offset is the same for the child PM }
  3542. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3543. begin
  3544. vmt_offset:=c.vmt_offset;
  3545. include(objectoptions,oo_has_vmt);
  3546. end;
  3547. end;
  3548. end;
  3549. procedure tobjectdef.insertvmt;
  3550. var
  3551. vs: tfieldvarsym;
  3552. begin
  3553. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3554. exit;
  3555. if (oo_has_vmt in objectoptions) then
  3556. internalerror(12345)
  3557. else
  3558. begin
  3559. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3560. tObjectSymtable(symtable).fieldalignment);
  3561. if (tf_requires_proper_alignment in target_info.flags) then
  3562. begin
  3563. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3564. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3565. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3566. end;
  3567. vmt_offset:=tObjectSymtable(symtable).datasize;
  3568. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3569. hidesym(vs);
  3570. tObjectSymtable(symtable).insert(vs);
  3571. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3572. include(objectoptions,oo_has_vmt);
  3573. end;
  3574. end;
  3575. procedure tobjectdef.check_forwards;
  3576. begin
  3577. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) then
  3578. tstoredsymtable(symtable).check_forwards;
  3579. if (oo_is_forward in objectoptions) then
  3580. begin
  3581. { ok, in future, the forward can be resolved }
  3582. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3583. exclude(objectoptions,oo_is_forward);
  3584. end;
  3585. end;
  3586. { true, if self inherits from d (or if they are equal) }
  3587. function tobjectdef.is_related(d : tdef) : boolean;
  3588. var
  3589. hp : tobjectdef;
  3590. begin
  3591. hp:=self;
  3592. while assigned(hp) do
  3593. begin
  3594. if hp=d then
  3595. begin
  3596. is_related:=true;
  3597. exit;
  3598. end;
  3599. hp:=hp.childof;
  3600. end;
  3601. is_related:=false;
  3602. end;
  3603. function tobjectdef.FindDestructor : tprocdef;
  3604. var
  3605. objdef : tobjectdef;
  3606. i : longint;
  3607. sym : tsym;
  3608. pd : tprocdef;
  3609. begin
  3610. result:=nil;
  3611. objdef:=self;
  3612. while assigned(objdef) do
  3613. begin
  3614. for i:=0 to objdef.symtable.SymList.Count-1 do
  3615. begin
  3616. sym:=TSym(objdef.symtable.SymList[i]);
  3617. if sym.typ=procsym then
  3618. begin
  3619. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3620. if assigned(pd) then
  3621. begin
  3622. result:=pd;
  3623. exit;
  3624. end;
  3625. end;
  3626. end;
  3627. objdef:=objdef.childof;
  3628. end;
  3629. end;
  3630. function tobjectdef.implements_any_interfaces: boolean;
  3631. begin
  3632. result := (ImplementedInterfaces.Count > 0) or
  3633. (assigned(childof) and childof.implements_any_interfaces);
  3634. end;
  3635. {$ifdef support_llvm}
  3636. procedure tobjectdef.reset;
  3637. begin
  3638. fllvm_class_struct_name_sym:=nil;
  3639. fllvm_vmt_name_sym:=nil;
  3640. inherited reset;
  3641. end;
  3642. {$endif support_llvm}
  3643. function tobjectdef.size : aint;
  3644. begin
  3645. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3646. result:=sizeof(pint)
  3647. else
  3648. result:=tObjectSymtable(symtable).datasize;
  3649. end;
  3650. function tobjectdef.alignment:shortint;
  3651. begin
  3652. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3653. alignment:=sizeof(pint)
  3654. else
  3655. alignment:=tObjectSymtable(symtable).recordalignment;
  3656. end;
  3657. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3658. begin
  3659. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3660. case objecttype of
  3661. odt_class:
  3662. { the +2*sizeof(pint) is size and -size }
  3663. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3664. odt_interfacecom,odt_interfacecorba:
  3665. vmtmethodoffset:=index*sizeof(pint);
  3666. else
  3667. {$ifdef WITHDMT}
  3668. vmtmethodoffset:=(index+4)*sizeof(pint);
  3669. {$else WITHDMT}
  3670. vmtmethodoffset:=(index+3)*sizeof(pint);
  3671. {$endif WITHDMT}
  3672. end;
  3673. end;
  3674. function tobjectdef.vmt_mangledname : string;
  3675. begin
  3676. if not(oo_has_vmt in objectoptions) then
  3677. Message1(parser_n_object_has_no_vmt,objrealname^);
  3678. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3679. end;
  3680. function tobjectdef.needs_inittable : boolean;
  3681. begin
  3682. case objecttype of
  3683. odt_dispinterface,
  3684. odt_class :
  3685. needs_inittable:=false;
  3686. odt_interfacecom:
  3687. needs_inittable:=true;
  3688. odt_interfacecorba:
  3689. needs_inittable:=is_related(interface_iunknown);
  3690. odt_object:
  3691. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3692. odt_cppclass:
  3693. needs_inittable:=false;
  3694. else
  3695. internalerror(200108267);
  3696. end;
  3697. end;
  3698. function tobjectdef.members_need_inittable : boolean;
  3699. begin
  3700. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3701. end;
  3702. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3703. var
  3704. ImplIntf : TImplementedInterface;
  3705. i : longint;
  3706. begin
  3707. result:=nil;
  3708. if not assigned(ImplementedInterfaces) then
  3709. exit;
  3710. for i:=0 to ImplementedInterfaces.Count-1 do
  3711. begin
  3712. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3713. if ImplIntf.intfdef=aintfdef then
  3714. begin
  3715. result:=ImplIntf;
  3716. exit;
  3717. end;
  3718. end;
  3719. end;
  3720. function tobjectdef.is_publishable : boolean;
  3721. begin
  3722. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3723. end;
  3724. {$ifdef support_llvm}
  3725. procedure tobjectdef.set_llvm_name_syms;
  3726. begin
  3727. inherited set_llvm_name_syms;
  3728. if not assigned(fllvm_class_struct_name_sym) and
  3729. is_class_or_interface_or_dispinterface(self) then
  3730. begin
  3731. fllvm_class_struct_name_sym:=current_asmdata.DefineAsmSymbol(llvm_mangledname+'$$$struct',AB_LOCAL,AT_DATA);
  3732. fllvm_vmt_name_sym:=current_asmdata.DefineAsmSymbol(llvm_mangledname+'$$$vmt',AB_LOCAL,AT_DATA);
  3733. end;
  3734. end;
  3735. function tobjectdef.get_llvm_class_struct_name_sym: tasmsymbol;
  3736. begin
  3737. set_llvm_name_syms;
  3738. result:=fllvm_class_struct_name_sym;
  3739. end;
  3740. function tobjectdef.get_llvm_vmt_name_sym: tasmsymbol;
  3741. begin
  3742. set_llvm_name_syms;
  3743. result:=fllvm_vmt_name_sym;
  3744. end;
  3745. {$endif support_llvm}
  3746. {****************************************************************************
  3747. TImplementedInterface
  3748. ****************************************************************************}
  3749. constructor TImplementedInterface.create(aintf: tobjectdef);
  3750. begin
  3751. inherited create;
  3752. intfdef:=aintf;
  3753. IOffset:=-1;
  3754. IType:=etStandard;
  3755. NameMappings:=nil;
  3756. procdefs:=nil;
  3757. end;
  3758. constructor TImplementedInterface.create_deref(d:tderef);
  3759. begin
  3760. inherited create;
  3761. intfdef:=nil;
  3762. intfdefderef:=d;
  3763. IOffset:=-1;
  3764. IType:=etStandard;
  3765. NameMappings:=nil;
  3766. procdefs:=nil;
  3767. end;
  3768. destructor TImplementedInterface.destroy;
  3769. var
  3770. i : longint;
  3771. mappedname : pshortstring;
  3772. begin
  3773. if assigned(NameMappings) then
  3774. begin
  3775. for i:=0 to NameMappings.Count-1 do
  3776. begin
  3777. mappedname:=pshortstring(NameMappings[i]);
  3778. stringdispose(mappedname);
  3779. end;
  3780. NameMappings.free;
  3781. NameMappings:=nil;
  3782. end;
  3783. if assigned(procdefs) then
  3784. begin
  3785. procdefs.free;
  3786. procdefs:=nil;
  3787. end;
  3788. inherited destroy;
  3789. end;
  3790. procedure TImplementedInterface.buildderef;
  3791. begin
  3792. intfdefderef.build(intfdef);
  3793. end;
  3794. procedure TImplementedInterface.deref;
  3795. begin
  3796. intfdef:=tobjectdef(intfdefderef.resolve);
  3797. end;
  3798. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  3799. begin
  3800. if not assigned(NameMappings) then
  3801. NameMappings:=TFPHashList.Create;
  3802. NameMappings.Add(origname,stringdup(newname));
  3803. end;
  3804. function TImplementedInterface.GetMapping(const origname: string):string;
  3805. var
  3806. mappedname : pshortstring;
  3807. begin
  3808. result:='';
  3809. if not assigned(NameMappings) then
  3810. exit;
  3811. mappedname:=PShortstring(NameMappings.Find(origname));
  3812. if assigned(mappedname) then
  3813. result:=mappedname^;
  3814. end;
  3815. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  3816. begin
  3817. if not assigned(procdefs) then
  3818. procdefs:=TFPObjectList.Create(false);
  3819. { duplicate entries must be stored, because multiple }
  3820. { interfaces can declare methods with the same name }
  3821. { and all of these get their own VMT entry }
  3822. procdefs.Add(pd);
  3823. end;
  3824. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  3825. var
  3826. i : longint;
  3827. begin
  3828. result:=false;
  3829. { interfaces being implemented through delegation are not mergable (FK) }
  3830. if (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  3831. exit;
  3832. weight:=0;
  3833. { empty interface is mergeable }
  3834. if ProcDefs.Count=0 then
  3835. begin
  3836. result:=true;
  3837. exit;
  3838. end;
  3839. { The interface to merge must at least the number of
  3840. procedures of this interface }
  3841. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  3842. exit;
  3843. for i:=0 to ProcDefs.Count-1 do
  3844. begin
  3845. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  3846. exit;
  3847. end;
  3848. weight:=ProcDefs.Count;
  3849. result:=true;
  3850. end;
  3851. function TImplementedInterface.getcopy:TImplementedInterface;
  3852. begin
  3853. Result:=TImplementedInterface.Create(nil);
  3854. Move(pointer(self)^,pointer(result)^,InstanceSize);
  3855. end;
  3856. {****************************************************************************
  3857. TFORWARDDEF
  3858. ****************************************************************************}
  3859. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  3860. begin
  3861. inherited create(forwarddef);
  3862. tosymname:=stringdup(s);
  3863. forwardpos:=pos;
  3864. end;
  3865. function tforwarddef.GetTypeName:string;
  3866. begin
  3867. GetTypeName:='unresolved forward to '+tosymname^;
  3868. end;
  3869. destructor tforwarddef.destroy;
  3870. begin
  3871. stringdispose(tosymname);
  3872. inherited destroy;
  3873. end;
  3874. {****************************************************************************
  3875. TUNDEFINEDDEF
  3876. ****************************************************************************}
  3877. constructor tundefineddef.create;
  3878. begin
  3879. inherited create(undefineddef);
  3880. end;
  3881. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  3882. begin
  3883. inherited ppuload(undefineddef,ppufile);
  3884. end;
  3885. function tundefineddef.GetTypeName:string;
  3886. begin
  3887. GetTypeName:='<undefined type>';
  3888. end;
  3889. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  3890. begin
  3891. inherited ppuwrite(ppufile);
  3892. ppufile.writeentry(ibundefineddef);
  3893. end;
  3894. {****************************************************************************
  3895. TERRORDEF
  3896. ****************************************************************************}
  3897. constructor terrordef.create;
  3898. begin
  3899. inherited create(errordef);
  3900. { prevent consecutive faults }
  3901. savesize:=1;
  3902. end;
  3903. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  3904. begin
  3905. { Can't write errordefs to ppu }
  3906. internalerror(200411063);
  3907. end;
  3908. function terrordef.GetTypeName:string;
  3909. begin
  3910. GetTypeName:='<erroneous type>';
  3911. end;
  3912. function terrordef.getmangledparaname:string;
  3913. begin
  3914. getmangledparaname:='error';
  3915. end;
  3916. {****************************************************************************
  3917. Definition Helpers
  3918. ****************************************************************************}
  3919. function is_interfacecom(def: tdef): boolean;
  3920. begin
  3921. is_interfacecom:=
  3922. assigned(def) and
  3923. (def.typ=objectdef) and
  3924. (tobjectdef(def).objecttype=odt_interfacecom);
  3925. end;
  3926. function is_interfacecorba(def: tdef): boolean;
  3927. begin
  3928. is_interfacecorba:=
  3929. assigned(def) and
  3930. (def.typ=objectdef) and
  3931. (tobjectdef(def).objecttype=odt_interfacecorba);
  3932. end;
  3933. function is_interface(def: tdef): boolean;
  3934. begin
  3935. is_interface:=
  3936. assigned(def) and
  3937. (def.typ=objectdef) and
  3938. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  3939. end;
  3940. function is_dispinterface(def: tdef): boolean;
  3941. begin
  3942. result:=
  3943. assigned(def) and
  3944. (def.typ=objectdef) and
  3945. (tobjectdef(def).objecttype=odt_dispinterface);
  3946. end;
  3947. function is_class(def: tdef): boolean;
  3948. begin
  3949. is_class:=
  3950. assigned(def) and
  3951. (def.typ=objectdef) and
  3952. (tobjectdef(def).objecttype=odt_class);
  3953. end;
  3954. function is_object(def: tdef): boolean;
  3955. begin
  3956. is_object:=
  3957. assigned(def) and
  3958. (def.typ=objectdef) and
  3959. (tobjectdef(def).objecttype=odt_object);
  3960. end;
  3961. function is_cppclass(def: tdef): boolean;
  3962. begin
  3963. is_cppclass:=
  3964. assigned(def) and
  3965. (def.typ=objectdef) and
  3966. (tobjectdef(def).objecttype=odt_cppclass);
  3967. end;
  3968. function is_class_or_interface(def: tdef): boolean;
  3969. begin
  3970. result:=
  3971. assigned(def) and
  3972. (def.typ=objectdef) and
  3973. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  3974. end;
  3975. function is_class_or_interface_or_object(def: tdef): boolean;
  3976. begin
  3977. result:=
  3978. assigned(def) and
  3979. (def.typ=objectdef) and
  3980. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  3981. end;
  3982. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  3983. begin
  3984. result:=
  3985. assigned(def) and
  3986. (def.typ=objectdef) and
  3987. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  3988. end;
  3989. {$ifdef x86}
  3990. function use_sse(def : tdef) : boolean;
  3991. begin
  3992. use_sse:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  3993. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  3994. end;
  3995. {$endif x86}
  3996. end.