symdef.pas 131 KB

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