symdef.pas 136 KB

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