symdef.pas 131 KB

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