symdef.pas 136 KB

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