symdef.pas 137 KB

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