symdef.pas 133 KB

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