symdef.pas 137 KB

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