symdef.pas 131 KB

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