symdef.pas 131 KB

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