symdef.pas 137 KB

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