symdef.pas 131 KB

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