symtable.pas 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  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 symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,globtype,tokens,
  23. { symtable }
  24. symconst,symbase,symtype,symdef,symsym;
  25. {****************************************************************************
  26. Symtable types
  27. ****************************************************************************}
  28. type
  29. tstoredsymtable = class(TSymtable)
  30. private
  31. init_final_check_done : boolean;
  32. procedure _needs_init_final(sym:TObject;arg:pointer);
  33. procedure do_init_final_check;
  34. procedure check_forward(sym:TObject;arg:pointer);
  35. procedure check_block_valid(def: TObject;arg:pointer);
  36. procedure register_defs(def:tobject;arg:pointer);
  37. procedure register_syms(sym:tobject;arg:pointer);
  38. procedure labeldefined(sym:TObject;arg:pointer);
  39. procedure varsymbolused(sym:TObject;arg:pointer);
  40. procedure TestPrivate(sym:TObject;arg:pointer);
  41. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  42. procedure loaddefs(ppufile:tcompilerppufile);
  43. procedure loadsyms(ppufile:tcompilerppufile);
  44. procedure writedefs(ppufile:tcompilerppufile);
  45. procedure writesyms(ppufile:tcompilerppufile);
  46. public
  47. constructor create(const s:string);
  48. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  49. procedure delete(sym:TSymEntry);override;
  50. { load/write }
  51. procedure ppuload(ppufile:tcompilerppufile);virtual;
  52. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  53. procedure buildderef;
  54. procedure buildderefimpl;
  55. { buildderef but only for (recursively) used symbols/defs }
  56. procedure buildderef_registered;
  57. procedure deref(only_registered: boolean);virtual;
  58. procedure derefimpl(only_registered: boolean);virtual;
  59. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  60. procedure allsymbolsused;
  61. procedure allprivatesused;
  62. procedure check_forwards;
  63. procedure checklabels;
  64. function needs_init_final : boolean; virtual;
  65. function has_non_trivial_init:boolean;virtual;
  66. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  67. procedure register_children;
  68. end;
  69. {$ifdef llvm}
  70. tllvmshadowsymtableentry = class
  71. constructor create(def: tdef; fieldoffset: aint);
  72. private
  73. ffieldoffset: aint;
  74. fdef: tdef;
  75. public
  76. property fieldoffset: aint read ffieldoffset;
  77. property def: tdef read fdef;
  78. end;
  79. tllvmshadowsymtable = class;
  80. {$endif llvm}
  81. tmanagementoperator_offset_entry = record
  82. pd : tprocdef;
  83. offset : asizeint;
  84. end;
  85. pmanagementoperator_offset_entry = ^tmanagementoperator_offset_entry;
  86. tabstractrecordsymtable = class(tstoredsymtable)
  87. {$ifdef llvm}
  88. private
  89. fllvmst: tllvmshadowsymtable;
  90. function getllvmshadowsymtabll: tllvmshadowsymtable;
  91. {$endif llvm}
  92. public
  93. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  94. recordalignment, { alignment desired when inserting this record }
  95. fieldalignment, { alignment current alignment used when fields are inserted }
  96. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  97. recordalignmin, { local equivalents of global settings, so that records can }
  98. maxCrecordalign: shortint; { be created with custom settings internally }
  99. has_fields_with_mop : tmanagementoperators; { whether any of the fields has the need for a management operator (or one of the field's fields) }
  100. constructor create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  101. destructor destroy;override;
  102. procedure ppuload(ppufile:tcompilerppufile);override;
  103. procedure ppuwrite(ppufile:tcompilerppufile);override;
  104. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  105. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  106. procedure addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  107. { returns the field closest to this offset (may not be exact because
  108. of padding; internalerrors for variant records, assumes fields are
  109. ordered by increasing offset) }
  110. function findfieldbyoffset(offset:asizeint): tfieldvarsym;
  111. procedure addalignmentpadding;
  112. procedure insertdef(def:TDefEntry);override;
  113. function is_packed: boolean;
  114. function has_single_field(out def:tdef): boolean;
  115. function get_unit_symtable: tsymtable;
  116. { collects all management operators of the specified type in list (which
  117. is not cleared); the entries are copies and thus must be freed by the
  118. caller }
  119. procedure get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  120. protected
  121. { size in bytes including padding }
  122. _datasize : asizeint;
  123. { size in bits of the data in case of bitpacked record. Only important during construction, }
  124. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  125. databitsize : asizeint;
  126. { size in bytes of padding }
  127. _paddingsize : word;
  128. { array of tmanagementoperator_offset_entry lists; only assigned if
  129. they had been queried once by get_management_operator_list }
  130. mop_list : array[tmanagementoperator] of tfplist;
  131. procedure setdatasize(val: asizeint);
  132. function getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  133. procedure do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  134. public
  135. function iscurrentunit: boolean; override;
  136. property datasize : asizeint read _datasize write setdatasize;
  137. property paddingsize: word read _paddingsize write _paddingsize;
  138. {$ifdef llvm}
  139. property llvmst: tllvmshadowsymtable read getllvmshadowsymtabll;
  140. {$endif llvm}
  141. end;
  142. trecordsymtable = class(tabstractrecordsymtable)
  143. public
  144. { maybe someday is worth to move managementoperators to }
  145. { tabstractrecordsymtable to perform management class operators for }
  146. { object/classes. In XE5 and newer is possible to use class operator }
  147. { for classes (like for Delphi .NET before) only for Delphi NEXTGEN }
  148. managementoperators : tmanagementoperators;
  149. constructor create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  150. procedure insertunionst(unionst : trecordsymtable;offset : asizeint);
  151. procedure includemanagementoperator(mop:tmanagementoperator);
  152. end;
  153. tObjectSymtable = class(tabstractrecordsymtable)
  154. public
  155. constructor create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  156. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  157. end;
  158. {$ifdef llvm}
  159. { llvm record definitions cannot contain variant/union parts, }
  160. { you have to flatten them first. the tllvmshadowsymtable }
  161. { contains a flattened version of a record/object symtable }
  162. tllvmshadowsymtable = class
  163. private
  164. equivst: tabstractrecordsymtable;
  165. curroffset: aint;
  166. function get(f: tfieldvarsym): tllvmshadowsymtableentry;
  167. function get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  168. public
  169. symdeflist: TFPObjectList;
  170. constructor create(st: tabstractrecordsymtable);
  171. destructor destroy; override;
  172. property entries[index: tfieldvarsym]: tllvmshadowsymtableentry read get; default;
  173. { warning: do not call this with field.llvmfieldnr, as
  174. field.llvmfieldnr will only be initialised when the llvm shadow
  175. symtable is accessed for the first time. Use the default/entries
  176. property instead in this case }
  177. property entries_by_llvm_index[index: longint]: tllvmshadowsymtableentry read get_by_llvm_index;
  178. private
  179. // generate the table
  180. procedure generate;
  181. // helpers
  182. procedure appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  183. procedure findvariantstarts(variantstarts: tfplist);
  184. procedure addalignmentpadding(finalsize: aint);
  185. procedure buildmapping(variantstarts: tfplist);
  186. procedure buildtable(variantstarts: tfplist);
  187. end;
  188. {$endif llvm}
  189. { tabstractsubsymtable }
  190. tabstractsubsymtable = class(tstoredsymtable)
  191. public
  192. procedure ppuwrite(ppufile:tcompilerppufile);override;
  193. end;
  194. { tabstractlocalsymtable }
  195. tabstractlocalsymtable = class(tabstractsubsymtable)
  196. public
  197. function count_locals:longint;
  198. function iscurrentunit: boolean; override;
  199. end;
  200. tlocalsymtable = class(tabstractlocalsymtable)
  201. public
  202. constructor create(adefowner:tdef;level:byte);
  203. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  204. end;
  205. { tparasymtable }
  206. tparasymtable = class(tabstractlocalsymtable)
  207. public
  208. readonly: boolean;
  209. constructor create(adefowner:tdef;level:byte);
  210. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  211. procedure insertdef(def:TDefEntry);override;
  212. end;
  213. tabstractuniTSymtable = class(tstoredsymtable)
  214. public
  215. constructor create(const n : string;id:word);
  216. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  217. function findnamespace(const n:string):TSymEntry;virtual;
  218. function iscurrentunit:boolean;override;
  219. function needs_init_final: boolean; override;
  220. procedure insertunit(sym:TSymEntry);
  221. function has_class_condestructors: boolean;
  222. end;
  223. tglobalsymtable = class(tabstractuniTSymtable)
  224. public
  225. unittypecount : word;
  226. constructor create(const n : string;id:word);
  227. procedure ppuload(ppufile:tcompilerppufile);override;
  228. procedure ppuwrite(ppufile:tcompilerppufile);override;
  229. end;
  230. tstaticsymtable = class(tabstractuniTSymtable)
  231. public
  232. constructor create(const n : string;id:word);
  233. procedure ppuload(ppufile:tcompilerppufile);override;
  234. procedure ppuwrite(ppufile:tcompilerppufile);override;
  235. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  236. function findnamespace(const n:string):TSymEntry;override;
  237. end;
  238. tspecializesymtable = class(tglobalsymtable)
  239. public
  240. constructor create(const n : string;id:word);
  241. function iscurrentunit:boolean;override;
  242. end;
  243. twithsymtable = class(TSymtable)
  244. withrefnode : tobject; { tnode }
  245. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  246. destructor destroy;override;
  247. procedure clear;override;
  248. procedure insertdef(def:TDefEntry);override;
  249. end;
  250. tstt_excepTSymtable = class(TSymtable)
  251. public
  252. constructor create;
  253. end;
  254. tmacrosymtable = class(tstoredsymtable)
  255. public
  256. constructor create(exported: boolean);
  257. end;
  258. { tenumsymtable }
  259. tenumsymtable = class(tabstractsubsymtable)
  260. public
  261. procedure insert(sym: TSymEntry; checkdup: boolean = true); override;
  262. constructor create(adefowner:tdef);
  263. end;
  264. { tarraysymtable }
  265. tarraysymtable = class(tabstractsubsymtable)
  266. public
  267. procedure insertdef(def:TDefEntry);override;
  268. constructor create(adefowner:tdef);
  269. end;
  270. var
  271. systemunit : tglobalsymtable; { pointer to the system unit }
  272. type
  273. tsymbol_search_flag = (
  274. ssf_search_option,
  275. ssf_search_helper,
  276. ssf_has_inherited,
  277. ssf_no_addsymref
  278. );
  279. tsymbol_search_flags = set of tsymbol_search_flag;
  280. {****************************************************************************
  281. Functions
  282. ****************************************************************************}
  283. {*** Misc ***}
  284. function FullTypeName(def,otherdef:tdef):string;
  285. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  286. { def is the extended type of a helper }
  287. function generate_objectpascal_helper_key(def:tdef):string;
  288. procedure incompatibletypes(def1,def2:tdef);
  289. procedure hidesym(sym:TSymEntry);
  290. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym:TSymEntry; warn: boolean);
  291. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  292. { writes all declarations for the specified system unit symbol }
  293. procedure write_system_parameter_lists(const name:string);
  294. {*** Search ***}
  295. procedure addsymref(sym:tsym);
  296. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  297. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  298. function defs_belong_to_same_generic(def1,def2:tdef):boolean;
  299. function get_generic_in_hierarchy_by_name(srsym:tsym;def:tdef):tdef;
  300. function return_specialization_of_generic(nesteddef,genericdef:tdef;out resultdef:tdef):boolean;
  301. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  302. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  303. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  304. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  305. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  306. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  307. { searches for a symbol with the given name that has the given option in
  308. symoptions set }
  309. function searchsym_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;option:tsymoption):boolean;
  310. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  311. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  312. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  313. function searchsym_in_class(classh: tobjectdef; contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  314. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  315. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  316. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  317. { searches symbols inside of a helper's implementation }
  318. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  319. function search_system_type(const s: TIDString): ttypesym;
  320. function try_search_system_type(const s: TIDString): ttypesym;
  321. function try_search_current_module_type(const s: TIDString): ttypesym;
  322. function search_system_proc(const s: TIDString): tprocdef;
  323. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  324. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  325. function search_struct_member_no_helper(pd : tabstractrecorddef;const s : string):tsym;
  326. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  327. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  328. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  329. { searches for the helper definition that's currently active for pd }
  330. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  331. { searches whether the symbol s is available in the currently active }
  332. { helper for pd }
  333. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  334. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  335. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  336. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  337. {and returns it if found. Returns nil otherwise.}
  338. function search_macro(const s : string):tsym;
  339. { Additionally to searching for a macro, also checks whether it's still }
  340. { actually defined (could be disable using "undef") }
  341. function defined_macro(const s : string):boolean;
  342. { Look for a system procedure (no overloads supported) }
  343. { returns a list of helpers in the current module for the def }
  344. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  345. {*** Object Helpers ***}
  346. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  347. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  348. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  349. {*** Macro Helpers ***}
  350. {If called initially, the following procedures manipulate macros in }
  351. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  352. {Name can be given in any case (it will be converted to upper case).}
  353. procedure def_system_macro(const name : string);
  354. procedure set_system_macro(const name, value : string);
  355. procedure set_system_compvar(const name, value : string);
  356. procedure undef_system_macro(const name : string);
  357. {*** symtable stack ***}
  358. { $ifdef DEBUG
  359. procedure test_symtablestack;
  360. procedure list_symtablestack;
  361. $endif DEBUG}
  362. {$ifdef UNITALIASES}
  363. type
  364. punit_alias = ^tunit_alias;
  365. tunit_alias = object(TNamedIndexItem)
  366. newname : pshortstring;
  367. constructor init(const n:string);
  368. destructor done;virtual;
  369. end;
  370. var
  371. unitaliases : pdictionary;
  372. procedure addunitalias(const n:string);
  373. function getunitalias(const n:string):string;
  374. {$endif UNITALIASES}
  375. {*** Init / Done ***}
  376. procedure IniTSymtable;
  377. procedure DoneSymtable;
  378. const
  379. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  380. { NOTOKEN } 'error',
  381. { _PLUS } 'plus',
  382. { _MINUS } 'minus',
  383. { _STAR } 'star',
  384. { _SLASH } 'slash',
  385. { _EQ } 'equal',
  386. { _GT } 'greater',
  387. { _LT } 'lower',
  388. { _GTE } 'greater_or_equal',
  389. { _LTE } 'lower_or_equal',
  390. { _NE } 'not_equal',
  391. { _SYMDIF } 'sym_diff',
  392. { _STARSTAR } 'starstar',
  393. { _OP_AS } 'as',
  394. { _OP_IN } 'in',
  395. { _OP_IS } 'is',
  396. { _OP_OR } 'or',
  397. { _OP_AND } 'and',
  398. { _OP_DIV } 'div',
  399. { _OP_MOD } 'mod',
  400. { _OP_NOT } 'not',
  401. { _OP_SHL } 'shl',
  402. { _OP_SHR } 'shr',
  403. { _OP_XOR } 'xor',
  404. { _ASSIGNMENT } 'assign',
  405. { _OP_EXPLICIT } 'explicit',
  406. { _OP_ENUMERATOR } 'enumerator',
  407. { _OP_INITIALIZE } 'initialize',
  408. { _OP_FINALIZE } 'finalize',
  409. { _OP_ADDREF } 'addref',
  410. { _OP_COPY } 'copy',
  411. { _OP_INC } 'inc',
  412. { _OP_DEC } 'dec');
  413. managementoperator2tok:array[tmanagementoperator] of ttoken = (
  414. { mop_none } NOTOKEN,
  415. { mop_initialize } _OP_INITIALIZE,
  416. { mop_finalize } _OP_FINALIZE,
  417. { mop_addref } _OP_ADDREF,
  418. { mop_copy } _OP_COPY
  419. );
  420. implementation
  421. uses
  422. { global }
  423. verbose,globals,
  424. { symtable }
  425. symutil,defutil,defcmp,objcdef,
  426. { module }
  427. fmodule,
  428. { codegen }
  429. procinfo,
  430. { ppu }
  431. entfile,
  432. { parser }
  433. scanner
  434. ;
  435. var
  436. dupnr : longint; { unique number for duplicate symbols }
  437. {*****************************************************************************
  438. TStoredSymtable
  439. *****************************************************************************}
  440. constructor tstoredsymtable.create(const s:string);
  441. begin
  442. inherited create(s);
  443. { Note: this happens for the initial macro symtable, so no error here }
  444. if not assigned(current_module) then
  445. comment(v_debug,'Current module not available for module id')
  446. else
  447. moduleid:=current_module.moduleid;
  448. end;
  449. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  450. begin
  451. inherited insert(sym,checkdup);
  452. init_final_check_done:=false;
  453. end;
  454. procedure tstoredsymtable.delete(sym:TSymEntry);
  455. begin
  456. inherited delete(sym);
  457. init_final_check_done:=false;
  458. end;
  459. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  460. begin
  461. { load the table's flags }
  462. if ppufile.readentry<>ibsymtableoptions then
  463. Message(unit_f_ppu_read_error);
  464. ppufile.getsmallset(tableoptions);
  465. { load definitions }
  466. loaddefs(ppufile);
  467. { load symbols }
  468. loadsyms(ppufile);
  469. init_final_check_done:=true;
  470. end;
  471. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  472. begin
  473. { ensure that we have the sto_needs_init_final flag set if needed }
  474. if not init_final_check_done then
  475. needs_init_final;
  476. { write the table's flags }
  477. ppufile.putsmallset(tableoptions);
  478. ppufile.writeentry(ibsymtableoptions);
  479. { write definitions }
  480. writedefs(ppufile);
  481. { write symbols }
  482. writesyms(ppufile);
  483. end;
  484. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  485. var
  486. def : tdef;
  487. b : byte;
  488. begin
  489. def:=nil;
  490. { load start of definition section, which holds the amount of defs }
  491. if ppufile.readentry<>ibstartdefs then
  492. Message(unit_f_ppu_read_error);
  493. { read definitions }
  494. repeat
  495. b:=ppufile.readentry;
  496. case b of
  497. ibpointerdef : def:=cpointerdef.ppuload(ppufile);
  498. ibarraydef : def:=carraydef.ppuload(ppufile);
  499. iborddef : def:=corddef.ppuload(ppufile);
  500. ibfloatdef : def:=cfloatdef.ppuload(ppufile);
  501. ibprocdef : def:=cprocdef.ppuload(ppufile);
  502. ibshortstringdef : def:=cstringdef.loadshort(ppufile);
  503. iblongstringdef : def:=cstringdef.loadlong(ppufile);
  504. ibansistringdef : def:=cstringdef.loadansi(ppufile);
  505. ibwidestringdef : def:=cstringdef.loadwide(ppufile);
  506. ibunicodestringdef : def:=cstringdef.loadunicode(ppufile);
  507. ibrecorddef : def:=crecorddef.ppuload(ppufile);
  508. ibobjectdef : def:=cobjectdef.ppuload(ppufile);
  509. ibenumdef : def:=cenumdef.ppuload(ppufile);
  510. ibsetdef : def:=csetdef.ppuload(ppufile);
  511. ibprocvardef : def:=cprocvardef.ppuload(ppufile);
  512. ibfiledef : def:=cfiledef.ppuload(ppufile);
  513. ibclassrefdef : def:=cclassrefdef.ppuload(ppufile);
  514. ibformaldef : def:=cformaldef.ppuload(ppufile);
  515. ibvariantdef : def:=cvariantdef.ppuload(ppufile);
  516. ibundefineddef : def:=cundefineddef.ppuload(ppufile);
  517. ibenddefs : break;
  518. ibend : Message(unit_f_ppu_read_error);
  519. else
  520. Message1(unit_f_ppu_invalid_entry,tostr(b));
  521. end;
  522. InsertDef(def);
  523. until false;
  524. end;
  525. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  526. var
  527. b : byte;
  528. sym : tsym;
  529. begin
  530. sym:=nil;
  531. { load start of definition section, which holds the amount of defs }
  532. if ppufile.readentry<>ibstartsyms then
  533. Message(unit_f_ppu_read_error);
  534. { now read the symbols }
  535. repeat
  536. b:=ppufile.readentry;
  537. case b of
  538. ibtypesym : sym:=ctypesym.ppuload(ppufile);
  539. ibprocsym : sym:=cprocsym.ppuload(ppufile);
  540. ibconstsym : sym:=cconstsym.ppuload(ppufile);
  541. ibstaticvarsym : sym:=cstaticvarsym.ppuload(ppufile);
  542. iblocalvarsym : sym:=clocalvarsym.ppuload(ppufile);
  543. ibparavarsym : sym:=cparavarsym.ppuload(ppufile);
  544. ibfieldvarsym : sym:=cfieldvarsym.ppuload(ppufile);
  545. ibabsolutevarsym : sym:=cabsolutevarsym.ppuload(ppufile);
  546. ibenumsym : sym:=cenumsym.ppuload(ppufile);
  547. ibpropertysym : sym:=cpropertysym.ppuload(ppufile);
  548. ibunitsym : sym:=cunitsym.ppuload(ppufile);
  549. iblabelsym : sym:=clabelsym.ppuload(ppufile);
  550. ibsyssym : sym:=csyssym.ppuload(ppufile);
  551. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  552. ibnamespacesym : sym:=cnamespacesym.ppuload(ppufile);
  553. ibendsyms : break;
  554. ibend : Message(unit_f_ppu_read_error);
  555. else
  556. Message1(unit_f_ppu_invalid_entry,tostr(b));
  557. end;
  558. Insert(sym,false);
  559. until false;
  560. end;
  561. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  562. var
  563. defcount,
  564. i : longint;
  565. def : tstoreddef;
  566. begin
  567. defcount:=0;
  568. for i:=0 to DefList.Count-1 do
  569. if tstoreddef(DefList[i]).is_registered then
  570. inc(defcount);
  571. { each definition get a number, write then the amount of defs to the
  572. ibstartdef entry }
  573. ppufile.putlongint(defcount);
  574. ppufile.writeentry(ibstartdefs);
  575. { now write the definition }
  576. for i:=0 to DefList.Count-1 do
  577. begin
  578. def:=tstoreddef(DefList[i]);
  579. if def.is_registered then
  580. def.ppuwrite(ppufile);
  581. end;
  582. { write end of definitions }
  583. ppufile.writeentry(ibenddefs);
  584. end;
  585. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  586. var
  587. symcount,
  588. i : longint;
  589. sym : Tstoredsym;
  590. begin
  591. symcount:=0;
  592. for i:=0 to SymList.Count-1 do
  593. if tstoredsym(SymList[i]).is_registered then
  594. inc(symcount);
  595. { each definition get a number, write then the amount of syms and the
  596. datasize to the ibsymdef entry }
  597. ppufile.putlongint(symcount);
  598. ppufile.writeentry(ibstartsyms);
  599. { foreach is used to write all symbols }
  600. for i:=0 to SymList.Count-1 do
  601. begin
  602. sym:=tstoredsym(SymList[i]);
  603. if sym.is_registered then
  604. sym.ppuwrite(ppufile);
  605. end;
  606. { end of symbols }
  607. ppufile.writeentry(ibendsyms);
  608. end;
  609. procedure tstoredsymtable.buildderef;
  610. var
  611. i : longint;
  612. def : tstoreddef;
  613. sym : tstoredsym;
  614. begin
  615. { interface definitions }
  616. for i:=0 to DefList.Count-1 do
  617. begin
  618. def:=tstoreddef(DefList[i]);
  619. def.buildderef;
  620. end;
  621. { interface symbols }
  622. for i:=0 to SymList.Count-1 do
  623. begin
  624. sym:=tstoredsym(SymList[i]);
  625. sym.buildderef;
  626. end;
  627. end;
  628. procedure tstoredsymtable.buildderefimpl;
  629. var
  630. i : longint;
  631. def : tstoreddef;
  632. begin
  633. { implementation definitions }
  634. for i:=0 to DefList.Count-1 do
  635. begin
  636. def:=tstoreddef(DefList[i]);
  637. def.buildderefimpl;
  638. end;
  639. end;
  640. procedure tstoredsymtable.buildderef_registered;
  641. var
  642. def : tstoreddef;
  643. sym : tstoredsym;
  644. i : longint;
  645. defidmax,
  646. symidmax: longint;
  647. newbuiltdefderefs,
  648. builtdefderefs,
  649. builtsymderefs: array of boolean;
  650. changed: boolean;
  651. begin
  652. newbuiltdefderefs:=nil;
  653. builtdefderefs:=nil;
  654. builtsymderefs:=nil;
  655. { tdefs for which we already built the deref }
  656. setlength(builtdefderefs,deflist.count);
  657. { tdefs for which we built the deref in this iteration }
  658. setlength(newbuiltdefderefs,deflist.count);
  659. { syms for which we already built the deref }
  660. setlength(builtsymderefs,symlist.count);
  661. repeat
  662. { we only have to store the defs (recursively) referred by wpo info
  663. or inlined routines in the static symbtable }
  664. { current number of registered defs/syms }
  665. defidmax:=current_module.deflist.count;
  666. symidmax:=current_module.symlist.count;
  667. changed:=false;
  668. { build the derefs for the registered defs we haven't processed yet }
  669. for i:=0 to DefList.Count-1 do
  670. begin
  671. if not builtdefderefs[i] then
  672. begin
  673. def:=tstoreddef(DefList[i]);
  674. if def.is_registered then
  675. begin
  676. def.buildderef;
  677. newbuiltdefderefs[i]:=true;
  678. builtdefderefs[i]:=true;
  679. changed:=true;
  680. end;
  681. end;
  682. end;
  683. { same for the syms }
  684. for i:=0 to SymList.Count-1 do
  685. begin
  686. if not builtsymderefs[i] then
  687. begin
  688. sym:=tstoredsym(SymList[i]);
  689. if sym.is_registered then
  690. begin
  691. sym.buildderef;
  692. builtsymderefs[i]:=true;
  693. changed:=true;
  694. end;
  695. end;
  696. end;
  697. { now buildderefimpl for the defs we processed in this iteration }
  698. for i:=0 to DefList.Count-1 do
  699. begin
  700. if newbuiltdefderefs[i] then
  701. begin
  702. newbuiltdefderefs[i]:=false;
  703. tstoreddef(DefList[i]).buildderefimpl;
  704. changed:=true;
  705. end;
  706. end;
  707. { stop when no new defs or syms have been registered while processing
  708. the currently registered ones (defs/syms get added to the module's
  709. deflist/symlist when they are registered) }
  710. until not changed and
  711. (defidmax=current_module.deflist.count) and
  712. (symidmax=current_module.symlist.count);
  713. end;
  714. procedure tstoredsymtable.deref(only_registered: boolean);
  715. var
  716. i : longint;
  717. def : tstoreddef;
  718. sym : tstoredsym;
  719. begin
  720. { first deref the interface ttype symbols. This is needs
  721. to be done before the interface defs are derefed, because
  722. the interface defs can contain references to the type symbols
  723. which then already need to contain a resolved typedef field (PFV) }
  724. for i:=0 to SymList.Count-1 do
  725. begin
  726. sym:=tstoredsym(SymList[i]);
  727. if (sym.typ=typesym) and
  728. (not only_registered or
  729. sym.is_registered) then
  730. sym.deref;
  731. end;
  732. { interface definitions }
  733. for i:=0 to DefList.Count-1 do
  734. begin
  735. def:=tstoreddef(DefList[i]);
  736. if not only_registered or
  737. def.is_registered then
  738. def.deref;
  739. end;
  740. { interface symbols }
  741. for i:=0 to SymList.Count-1 do
  742. begin
  743. sym:=tstoredsym(SymList[i]);
  744. if (not only_registered or
  745. sym.is_registered) and
  746. (sym.typ<>typesym) then
  747. sym.deref;
  748. end;
  749. end;
  750. procedure tstoredsymtable.derefimpl(only_registered: boolean);
  751. var
  752. i : longint;
  753. def : tstoreddef;
  754. begin
  755. { implementation definitions }
  756. for i:=0 to DefList.Count-1 do
  757. begin
  758. def:=tstoreddef(DefList[i]);
  759. if not only_registered or
  760. def.is_registered then
  761. def.derefimpl;
  762. end;
  763. end;
  764. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  765. var
  766. hsym : tsym;
  767. begin
  768. hsym:=tsym(FindWithHash(hashedid));
  769. if assigned(hsym) then
  770. DuplicateSym(hashedid,sym,hsym,false);
  771. result:=assigned(hsym);
  772. end;
  773. {**************************************
  774. Callbacks
  775. **************************************}
  776. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  777. begin
  778. if tsym(sym).typ=procsym then
  779. tprocsym(sym).check_forward
  780. { check also object method table }
  781. { we needn't to test the def list }
  782. { because each object has to have a type sym,
  783. only test objects declarations, not type renamings }
  784. else
  785. if (tsym(sym).typ=typesym) and
  786. assigned(ttypesym(sym).typedef) and
  787. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  788. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  789. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  790. end;
  791. procedure tstoredsymtable.check_block_valid(def: TObject; arg: pointer);
  792. var
  793. founderrordef: tdef;
  794. begin
  795. { all parameters passed to a block must be handled by the Objective-C
  796. runtime }
  797. if is_block(tdef(def)) and
  798. not objcchecktype(tdef(def),founderrordef) then
  799. if assigned(tdef(def).typesym) then
  800. MessagePos1(tdef(def).typesym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename)
  801. else
  802. Message1(type_e_objc_type_unsupported,tprocvardef(def).typename)
  803. end;
  804. procedure tstoredsymtable.register_syms(sym:tobject;arg:pointer);
  805. begin
  806. tsym(sym).register_sym;
  807. end;
  808. procedure tstoredsymtable.register_defs(def:tobject;arg:pointer);
  809. begin
  810. tdef(def).register_def;
  811. end;
  812. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  813. begin
  814. if (tsym(sym).typ=labelsym) and
  815. not(tlabelsym(sym).defined) then
  816. begin
  817. if tlabelsym(sym).used then
  818. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  819. else
  820. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  821. end;
  822. end;
  823. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  824. begin
  825. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  826. ((tsym(sym).owner.symtabletype in
  827. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  828. begin
  829. { unused symbol should be reported only if no }
  830. { error is reported }
  831. { if the symbol is in a register it is used }
  832. { also don't count the value parameters which have local copies }
  833. { also don't claim for high param of open parameters (PM) }
  834. { also don't complain about unused symbols in generic procedures }
  835. { and methods }
  836. { and neither in abstract methods }
  837. if (Errorcount<>0) or
  838. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  839. (sp_internal in tsym(sym).symoptions) or
  840. ((assigned(tsym(sym).owner.defowner) and
  841. (tsym(sym).owner.defowner.typ=procdef) and
  842. ((df_generic in tprocdef(tsym(sym).owner.defowner).defoptions) or
  843. (po_abstractmethod in tprocdef(tsym(sym).owner.defowner).procoptions)))) then
  844. exit;
  845. if (tstoredsym(sym).refs=0) then
  846. begin
  847. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  848. begin
  849. { don't warn about the result of constructors }
  850. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  851. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  852. not (po_noreturn in tprocdef(tsym(sym).owner.defowner).procoptions) and
  853. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  854. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  855. end
  856. else if (tsym(sym).owner.symtabletype=parasymtable) then
  857. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  858. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  859. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  860. else
  861. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  862. end
  863. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  864. begin
  865. if (tsym(sym).owner.symtabletype=parasymtable) then
  866. begin
  867. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  868. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  869. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  870. end
  871. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  872. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  873. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  874. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  875. end
  876. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  877. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  878. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  879. end
  880. else if ((tsym(sym).owner.symtabletype in
  881. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  882. begin
  883. if (Errorcount<>0) or
  884. (sp_internal in tsym(sym).symoptions) then
  885. exit;
  886. { do not claim for inherited private fields !! }
  887. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  888. case tsym(sym).typ of
  889. typesym:
  890. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  891. constsym:
  892. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  893. propertysym:
  894. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  895. else
  896. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  897. end
  898. { units references are problematic }
  899. else
  900. begin
  901. if (tsym(sym).refs=0) and
  902. not(tsym(sym).typ in [enumsym,unitsym,namespacesym]) and
  903. not(is_funcret_sym(tsym(sym))) and
  904. { don't complain about compiler generated syms for specializations, see also #13405 }
  905. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  906. (pos('$',ttypesym(sym).Realname)<>0)) and
  907. (
  908. (tsym(sym).typ<>procsym) or
  909. ((tsym(sym).owner.symtabletype=staticsymtable) and
  910. not current_module.is_unit)
  911. ) and
  912. { don't complain about alias for hidden _cmd parameter to
  913. obj-c methods }
  914. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  915. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  916. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  917. end;
  918. end;
  919. end;
  920. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  921. begin
  922. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  923. varsymbolused(sym,arg);
  924. end;
  925. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  926. begin
  927. {
  928. Don't test simple object aliases PM
  929. }
  930. if (tsym(sym).typ=typesym) and
  931. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  932. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  933. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  934. end;
  935. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  936. begin
  937. if (tsym(sym).typ=propertysym) and
  938. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  939. ppointer(arg)^:=sym;
  940. end;
  941. procedure tstoredsymtable.register_children;
  942. begin
  943. SymList.ForEachCall(@register_syms,nil);
  944. DefList.ForEachCall(@register_defs,nil);
  945. end;
  946. {***********************************************
  947. Process all entries
  948. ***********************************************}
  949. { checks, if all procsyms and methods are defined }
  950. procedure tstoredsymtable.check_forwards;
  951. begin
  952. SymList.ForEachCall(@check_forward,nil);
  953. { check whether all block definitions contain valid Objective-C types
  954. (now that all forward definitions have been resolved) }
  955. DefList.ForEachCall(@check_block_valid,nil);
  956. end;
  957. procedure tstoredsymtable.checklabels;
  958. begin
  959. SymList.ForEachCall(@labeldefined,nil);
  960. end;
  961. procedure tstoredsymtable.allsymbolsused;
  962. begin
  963. SymList.ForEachCall(@varsymbolused,nil);
  964. end;
  965. procedure tstoredsymtable.allprivatesused;
  966. begin
  967. SymList.ForEachCall(@objectprivatesymbolused,nil);
  968. end;
  969. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  970. begin
  971. if [sto_needs_init_final,sto_has_non_trivial_init] <= tableoptions then
  972. exit;
  973. { don't check static symbols - they can be present in structures only and
  974. always have a reference to a symbol defined on unit level }
  975. if sp_static in tsym(sym).symoptions then
  976. exit;
  977. case tsym(sym).typ of
  978. fieldvarsym,
  979. staticvarsym,
  980. localvarsym,
  981. paravarsym :
  982. begin
  983. if assigned(tabstractvarsym(sym).vardef) and
  984. is_managed_type(tabstractvarsym(sym).vardef) then
  985. include(tableoptions,sto_needs_init_final);
  986. if is_record((tabstractvarsym(sym).vardef)) and
  987. (mop_initialize in trecordsymtable(trecorddef(tabstractvarsym(sym).vardef).symtable).managementoperators) then
  988. include(tableoptions,sto_has_non_trivial_init);
  989. end;
  990. else
  991. ;
  992. end;
  993. end;
  994. procedure tstoredsymtable.do_init_final_check;
  995. begin
  996. if not init_final_check_done then
  997. begin
  998. exclude(tableoptions,sto_needs_init_final);
  999. exclude(tableoptions,sto_has_non_trivial_init);
  1000. SymList.ForEachCall(@_needs_init_final,nil);
  1001. init_final_check_done:=true;
  1002. end;
  1003. end;
  1004. { returns true, if p contains data which needs init/final code }
  1005. function tstoredsymtable.needs_init_final : boolean;
  1006. begin
  1007. do_init_final_check;
  1008. result:=sto_needs_init_final in tableoptions;
  1009. end;
  1010. function tstoredsymtable.has_non_trivial_init:boolean;
  1011. begin
  1012. do_init_final_check;
  1013. result:=sto_has_non_trivial_init in tableoptions;
  1014. end;
  1015. {****************************************************************************
  1016. TAbstractRecordSymtable
  1017. ****************************************************************************}
  1018. {$ifdef llvm}
  1019. function tabstractrecordsymtable.getllvmshadowsymtabll: tllvmshadowsymtable;
  1020. begin
  1021. if not assigned(fllvmst) then
  1022. fllvmst:=tllvmshadowsymtable.create(self);
  1023. result:=fllvmst;
  1024. end;
  1025. {$endif llvm}
  1026. constructor tabstractrecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1027. begin
  1028. inherited create(n);
  1029. _datasize:=0;
  1030. databitsize:=0;
  1031. recordalignment:=1;
  1032. usefieldalignment:=usealign;
  1033. recordalignmin:=recordminalign;
  1034. maxCrecordalign:=recordmaxCalign;
  1035. padalignment:=1;
  1036. { recordalign C_alignment means C record packing, that starts
  1037. with an alignment of 1 }
  1038. case usealign of
  1039. C_alignment,
  1040. bit_alignment:
  1041. fieldalignment:=1;
  1042. mac68k_alignment:
  1043. fieldalignment:=2;
  1044. else
  1045. fieldalignment:=usealign;
  1046. end;
  1047. end;
  1048. destructor tabstractrecordsymtable.destroy;
  1049. { for some reason a compiler built with 3.3.1 fails building the libxml2
  1050. package if the below define is not defined and thus the code snippet is
  1051. part of the destructor itself and not a nested procedure; until that bug
  1052. is fixed this is used as a workaround :/ }
  1053. {$define codegen_workaround}
  1054. {$ifdef codegen_workaround}
  1055. procedure free_mop_list(mop:tmanagementoperator);
  1056. var
  1057. i : longint;
  1058. begin
  1059. if assigned(mop_list[mop]) then
  1060. for i:=0 to mop_list[mop].count-1 do
  1061. dispose(pmanagementoperator_offset_entry(mop_list[mop][i]));
  1062. mop_list[mop].free;
  1063. end;
  1064. {$endif codegen_workaround}
  1065. var
  1066. mop : tmanagementoperator;
  1067. {$ifndef codegen_workaround}
  1068. i : longint;
  1069. {$endif codegen_workaround}
  1070. begin
  1071. if refcount>1 then
  1072. exit;
  1073. {$ifdef llvm}
  1074. fllvmst.free;
  1075. {$endif llvm}
  1076. for mop:=low(tmanagementoperator) to high(tmanagementoperator) do
  1077. begin
  1078. {$ifdef codegen_workaround}
  1079. free_mop_list(mop);
  1080. {$else codegen_workaround}
  1081. if assigned(mop_list[mop]) then
  1082. for i:=0 to mop_list[mop].count-1 do
  1083. dispose(pmanagementoperator_offset_entry(mop_list[mop][i]));
  1084. mop_list[mop].free;
  1085. {$endif codegen_workaround}
  1086. end;
  1087. inherited destroy;
  1088. end;
  1089. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  1090. begin
  1091. if ppufile.readentry<>ibrecsymtableoptions then
  1092. Message(unit_f_ppu_read_error);
  1093. recordalignment:=shortint(ppufile.getbyte);
  1094. usefieldalignment:=shortint(ppufile.getbyte);
  1095. recordalignmin:=shortint(ppufile.getbyte);
  1096. if (usefieldalignment=C_alignment) then
  1097. fieldalignment:=shortint(ppufile.getbyte);
  1098. ppufile.getsmallset(has_fields_with_mop);
  1099. inherited ppuload(ppufile);
  1100. end;
  1101. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  1102. var
  1103. oldtyp : byte;
  1104. begin
  1105. oldtyp:=ppufile.entrytyp;
  1106. ppufile.entrytyp:=subentryid;
  1107. { in case of classes using C alignment, the alignment of the parent
  1108. affects the alignment of fields of the childs }
  1109. ppufile.putbyte(byte(recordalignment));
  1110. ppufile.putbyte(byte(usefieldalignment));
  1111. ppufile.putbyte(byte(recordalignmin));
  1112. if (usefieldalignment=C_alignment) then
  1113. ppufile.putbyte(byte(fieldalignment));
  1114. { it's not really a "symtableoption", but loading this from the record
  1115. def requires storing the set in the recorddef at least between
  1116. ppuload and deref/derefimpl }
  1117. ppufile.putsmallset(has_fields_with_mop);
  1118. ppufile.writeentry(ibrecsymtableoptions);
  1119. inherited ppuwrite(ppufile);
  1120. ppufile.entrytyp:=oldtyp;
  1121. end;
  1122. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  1123. begin
  1124. { optimal alignment of the record when declaring a variable of this }
  1125. { type is independent of the packrecords setting }
  1126. if (fieldoffs mod fieldalign) = 0 then
  1127. result:=fieldalign
  1128. else if (fieldalign >= 16) and
  1129. ((fieldoffs mod 16) = 0) and
  1130. ((fieldalign mod 16) = 0) then
  1131. result:=16
  1132. else if (fieldalign >= 8) and
  1133. ((fieldoffs mod 8) = 0) and
  1134. ((fieldalign mod 8) = 0) then
  1135. result:=8
  1136. else if (fieldalign >= 4) and
  1137. ((fieldoffs mod 4) = 0) and
  1138. ((fieldalign mod 4) = 0) then
  1139. result:=4
  1140. else if (fieldalign >= 2) and
  1141. ((fieldoffs mod 2) = 0) and
  1142. ((fieldalign mod 2) = 0) then
  1143. result:=2
  1144. else
  1145. result:=1;
  1146. end;
  1147. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1148. var
  1149. varalignrecord: shortint;
  1150. begin
  1151. case usefieldalignment of
  1152. C_alignment:
  1153. varalignrecord:=used_align(varalign,recordalignmin,maxCrecordalign);
  1154. mac68k_alignment:
  1155. varalignrecord:=2;
  1156. else
  1157. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1158. end;
  1159. recordalignment:=max(recordalignment,varalignrecord);
  1160. end;
  1161. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1162. var
  1163. l : asizeint;
  1164. varalign : shortint;
  1165. vardef : tdef;
  1166. begin
  1167. if (sym.owner<>self) then
  1168. internalerror(200602031);
  1169. if sym.fieldoffset<>-1 then
  1170. internalerror(200602032);
  1171. { set visibility for the symbol }
  1172. sym.visibility:=vis;
  1173. { this symbol can't be loaded to a register }
  1174. sym.varregable:=vr_none;
  1175. { management operators }
  1176. if sym.vardef.typ in [recorddef,objectdef] then
  1177. has_fields_with_mop:=has_fields_with_mop + tabstractrecordsymtable(tabstractrecorddef(sym.vardef).symtable).has_fields_with_mop;
  1178. if sym.vardef.typ=recorddef then
  1179. has_fields_with_mop:=has_fields_with_mop + trecordsymtable(trecorddef(sym.vardef).symtable).managementoperators;
  1180. { Calculate field offset }
  1181. l:=sym.getsize;
  1182. vardef:=sym.vardef;
  1183. varalign:=vardef.structalignment;
  1184. case usefieldalignment of
  1185. bit_alignment:
  1186. begin
  1187. { bitpacking only happens for ordinals, the rest is aligned at }
  1188. { 1 byte (compatible with GPC/GCC) }
  1189. if is_ordinal(vardef) then
  1190. begin
  1191. sym.fieldoffset:=databitsize;
  1192. l:=sym.getpackedbitsize;
  1193. end
  1194. else
  1195. begin
  1196. databitsize:=_datasize*8;
  1197. sym.fieldoffset:=databitsize;
  1198. if (l>high(asizeint) div 8) then
  1199. Message(sym_e_segment_too_large);
  1200. l:=l*8;
  1201. end;
  1202. if varalign=0 then
  1203. varalign:=size_2_align(l);
  1204. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1205. { bit packed records are limited to high(aint) bits }
  1206. { instead of bytes to avoid double precision }
  1207. { arithmetic in offset calculations }
  1208. if int64(l)>high(asizeint)-sym.fieldoffset then
  1209. begin
  1210. Message(sym_e_segment_too_large);
  1211. _datasize:=high(asizeint);
  1212. databitsize:=high(asizeint);
  1213. end
  1214. else
  1215. begin
  1216. databitsize:=sym.fieldoffset+l;
  1217. _datasize:=(databitsize+7) div 8;
  1218. end;
  1219. { rest is not applicable }
  1220. exit;
  1221. end;
  1222. else
  1223. begin
  1224. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1225. if l>high(asizeint)-sym.fieldoffset then
  1226. begin
  1227. Message(sym_e_segment_too_large);
  1228. _datasize:=high(asizeint);
  1229. end
  1230. else
  1231. _datasize:=sym.fieldoffset+l;
  1232. { Calc alignment needed for this record }
  1233. alignrecord(sym.fieldoffset,varalign);
  1234. end;
  1235. end;
  1236. end;
  1237. function field_alignment_compare(item1, item2: pointer): integer;
  1238. var
  1239. field1: tfieldvarsym absolute item1;
  1240. field2: tfieldvarsym absolute item2;
  1241. begin
  1242. { we don't care about static fields, those become global variables }
  1243. if (sp_static in field1.symoptions) or
  1244. (sp_static in field2.symoptions) then
  1245. exit(0);
  1246. { sort from large to small alignment, and in case of the same alignment
  1247. in declaration order (items declared close together are possibly
  1248. also related and hence possibly used together -> putting them next
  1249. to each other can improve cache behaviour) }
  1250. result:=field2.vardef.alignment-field1.vardef.alignment;
  1251. if result=0 then
  1252. result:=field1.fieldoffset-field2.fieldoffset;
  1253. end;
  1254. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1255. var
  1256. fieldvs, insertfieldvs: tfieldvarsym;
  1257. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1258. i, j, bestfieldindex: longint;
  1259. globalfieldalignment,
  1260. prevglobalfieldalignment,
  1261. newfieldalignment: shortint;
  1262. changed: boolean;
  1263. begin
  1264. if maybereorder and
  1265. (cs_opt_reorder_fields in current_settings.optimizerswitches) then
  1266. begin
  1267. { assign dummy field offsets so we can know their order in the
  1268. sorting routine }
  1269. for i:=0 to list.count-1 do
  1270. tfieldvarsym(list[i]).fieldoffset:=i;
  1271. { sort the non-class fields to minimise losses due to alignment }
  1272. list.sort(@field_alignment_compare);
  1273. { now fill up gaps caused by alignment skips with smaller fields
  1274. where possible }
  1275. repeat
  1276. i:=0;
  1277. base:=_datasize;
  1278. globalfieldalignment:=fieldalignment;
  1279. changed:=false;
  1280. while i<list.count do
  1281. begin
  1282. fieldvs:=tfieldvarsym(list[i]);
  1283. if sp_static in fieldvs.symoptions then
  1284. begin
  1285. inc(i);
  1286. continue;
  1287. end;
  1288. prevglobalfieldalignment:=globalfieldalignment;
  1289. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1290. newfieldalignment:=globalfieldalignment;
  1291. { size of the gap between the end of the previous field and
  1292. the start of the current one }
  1293. space:=fieldoffset-base;
  1294. bestspaceleft:=space;
  1295. while space>0 do
  1296. begin
  1297. bestfieldindex:=-1;
  1298. bestinsertfieldoffset:=-1;
  1299. for j:=i+1 to list.count-1 do
  1300. begin
  1301. insertfieldvs:=tfieldvarsym(list[j]);
  1302. if sp_static in insertfieldvs.symoptions then
  1303. continue;
  1304. insertfieldsize:=insertfieldvs.getsize;
  1305. { can the new field fit possibly in the gap? }
  1306. if insertfieldsize<=space then
  1307. begin
  1308. { restore globalfieldalignment to situation before
  1309. the original field was inserted }
  1310. globalfieldalignment:=prevglobalfieldalignment;
  1311. { at what offset would it be inserted? (this new
  1312. field has its own alignment requirements, which
  1313. may make it impossible to fit after all) }
  1314. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1315. globalfieldalignment:=prevglobalfieldalignment;
  1316. { taking into account the alignment, does it still
  1317. fit and if so, does it fit better than the
  1318. previously found best fit? }
  1319. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1320. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1321. begin
  1322. { new best fit }
  1323. bestfieldindex:=j;
  1324. bestinsertfieldoffset:=insertfieldoffset;
  1325. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1326. if bestspaceleft=0 then
  1327. break;
  1328. end;
  1329. end;
  1330. end;
  1331. { if we didn't find any field to fit, stop trying for this
  1332. gap }
  1333. if bestfieldindex=-1 then
  1334. break;
  1335. changed:=true;
  1336. { we found a field to insert -> adjust the new base
  1337. address }
  1338. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1339. { update globalfieldalignment for this newly inserted
  1340. field }
  1341. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1342. { move the new field before the current one }
  1343. list.move(bestfieldindex,i);
  1344. { and skip the new field (which is now at position i) }
  1345. inc(i);
  1346. { there may be more space left -> continue }
  1347. space:=bestspaceleft;
  1348. end;
  1349. if base>fieldoffset then
  1350. internalerror(2012071302);
  1351. { check the next field }
  1352. base:=fieldoffset+fieldvs.getsize;
  1353. { since the original field had the same or greater alignment
  1354. than anything we inserted before it, the global field
  1355. alignment is still the same now as it was originally after
  1356. inserting that field }
  1357. globalfieldalignment:=newfieldalignment;
  1358. inc(i);
  1359. end;
  1360. { there may be small gaps left *before* inserted fields }
  1361. until not changed;
  1362. end;
  1363. { reset the dummy field offsets }
  1364. for i:=0 to list.count-1 do
  1365. tfieldvarsym(list[i]).fieldoffset:=-1;
  1366. { finally, set the actual field offsets }
  1367. for i:=0 to list.count-1 do
  1368. begin
  1369. fieldvs:=tfieldvarsym(list[i]);
  1370. { static data fields are already inserted in the globalsymtable }
  1371. if not(sp_static in fieldvs.symoptions) then
  1372. begin
  1373. { read_record_fields already set the visibility of the fields,
  1374. because a single list can contain symbols with different
  1375. visibility }
  1376. addfield(fieldvs,fieldvs.visibility);
  1377. end;
  1378. end;
  1379. end;
  1380. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1381. var
  1382. i: longint;
  1383. sym: tsym;
  1384. begin
  1385. { there could be multiple fields in case of a variant record }
  1386. if (defowner.typ=recorddef) and
  1387. trecorddef(defowner).isunion then
  1388. internalerror(2014090403);
  1389. for i:=0 to SymList.count-1 do
  1390. begin
  1391. sym:=tsym(symlist[i]);
  1392. if (sym.typ=fieldvarsym) and
  1393. not(sp_static in sym.symoptions) and
  1394. (tfieldvarsym(sym).fieldoffset>=offset) then
  1395. begin
  1396. result:=tfieldvarsym(sym);
  1397. exit;
  1398. end;
  1399. end;
  1400. result:=nil;
  1401. end;
  1402. procedure tabstractrecordsymtable.addalignmentpadding;
  1403. var
  1404. padded_datasize: asizeint;
  1405. begin
  1406. { make the record size aligned correctly so it can be
  1407. used as elements in an array. For C records we
  1408. use the fieldalignment, because that is updated with the
  1409. used alignment. }
  1410. if (padalignment = 1) then
  1411. case usefieldalignment of
  1412. C_alignment:
  1413. padalignment:=fieldalignment;
  1414. { bitpacked }
  1415. bit_alignment:
  1416. padalignment:=1;
  1417. { mac68k: always round to multiple of 2 }
  1418. mac68k_alignment:
  1419. padalignment:=2;
  1420. { default/no packrecords specified }
  1421. 0:
  1422. padalignment:=recordalignment
  1423. { specific packrecords setting -> use as upper limit }
  1424. else
  1425. padalignment:=min(recordalignment,usefieldalignment);
  1426. end;
  1427. padded_datasize:=align(_datasize,padalignment);
  1428. _paddingsize:=padded_datasize-_datasize;
  1429. _datasize:=padded_datasize;
  1430. end;
  1431. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1432. begin
  1433. { Enums must also be available outside the record scope,
  1434. insert in the owner of this symtable }
  1435. if def.typ=enumdef then
  1436. defowner.owner.insertdef(def)
  1437. else
  1438. inherited insertdef(def);
  1439. end;
  1440. function tabstractrecordsymtable.is_packed: boolean;
  1441. begin
  1442. result:=usefieldalignment=bit_alignment;
  1443. end;
  1444. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1445. var
  1446. i: longint;
  1447. currentsymlist: TFPHashObjectList;
  1448. currentdef: tdef;
  1449. sym: tfieldvarsym;
  1450. begin
  1451. result:=false;
  1452. { If a record contains a union, it does not contain a "single
  1453. non-composite field" in the context of certain ABIs requiring
  1454. special treatment for such records }
  1455. if (defowner.typ=recorddef) and
  1456. trecorddef(defowner).isunion then
  1457. exit;
  1458. { a record/object can contain other things than fields }
  1459. currentsymlist:=symlist;
  1460. { recurse in arrays and records }
  1461. sym:=nil;
  1462. repeat
  1463. { record has one field? }
  1464. for i:=0 to currentsymlist.Count-1 do
  1465. begin
  1466. if (tsym(currentsymlist[i]).typ=fieldvarsym) and
  1467. not(sp_static in tsym(currentsymlist[i]).symoptions) then
  1468. begin
  1469. if result then
  1470. begin
  1471. result:=false;
  1472. exit;
  1473. end;
  1474. result:=true;
  1475. sym:=tfieldvarsym(currentsymlist[i])
  1476. end;
  1477. end;
  1478. if assigned(sym) then
  1479. begin
  1480. { if the field is an array, does it contain one element? }
  1481. currentdef:=sym.vardef;
  1482. while (currentdef.typ=arraydef) and
  1483. not is_special_array(currentdef) do
  1484. begin
  1485. if tarraydef(currentdef).elecount<>1 then
  1486. begin
  1487. result:=false;
  1488. exit;
  1489. end;
  1490. currentdef:=tarraydef(currentdef).elementdef;
  1491. end;
  1492. { if the array element is again a record, continue descending }
  1493. if currentdef.typ=recorddef then
  1494. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1495. else
  1496. begin
  1497. { otherwise we found the type of the single element }
  1498. def:=currentdef;
  1499. exit;
  1500. end;
  1501. end
  1502. else
  1503. exit
  1504. until false;
  1505. end;
  1506. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1507. begin
  1508. result:=defowner.owner;
  1509. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1510. result:=result.defowner.owner;
  1511. end;
  1512. procedure tabstractrecordsymtable.do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  1513. var
  1514. sym : tsym absolute data;
  1515. fsym : tfieldvarsym absolute data;
  1516. mop : tmanagementoperator;
  1517. entry : pmanagementoperator_offset_entry;
  1518. sublist : tfplist;
  1519. i : longint;
  1520. begin
  1521. if sym.typ<>fieldvarsym then
  1522. exit;
  1523. if not is_record(fsym.vardef) and not is_object(fsym.vardef) and not is_cppclass(fsym.vardef) then
  1524. exit;
  1525. mop:=tmanagementoperator(ptruint(arg));
  1526. if not assigned(mop_list[mop]) then
  1527. internalerror(2018082303);
  1528. if is_record(fsym.vardef) then
  1529. begin
  1530. if mop in trecordsymtable(trecorddef(fsym.vardef).symtable).managementoperators then
  1531. begin
  1532. new(entry);
  1533. entry^.pd:=search_management_operator(mop,fsym.vardef);
  1534. if not assigned(entry^.pd) then
  1535. internalerror(2018082302);
  1536. entry^.offset:=fsym.fieldoffset;
  1537. mop_list[mop].add(entry);
  1538. end;
  1539. end;
  1540. sublist:=tfplist.create;
  1541. tabstractrecordsymtable(tabstractrecorddef(fsym.vardef).symtable).get_managementoperator_offset_list(mop,sublist);
  1542. for i:=0 to sublist.count-1 do
  1543. begin
  1544. entry:=pmanagementoperator_offset_entry(sublist[i]);
  1545. entry^.offset:=entry^.offset+fsym.fieldoffset;
  1546. mop_list[mop].add(entry);
  1547. end;
  1548. { we don't need to remove the entries as they become part of list }
  1549. sublist.free;
  1550. end;
  1551. procedure tabstractrecordsymtable.get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  1552. var
  1553. i : longint;
  1554. entry,entrycopy : pmanagementoperator_offset_entry;
  1555. begin
  1556. if not assigned(list) then
  1557. internalerror(2018082301);
  1558. if mop=mop_none then
  1559. exit;
  1560. if not (mop in has_fields_with_mop) then
  1561. { none of the fields or one of the field's fields has the requested operator }
  1562. exit;
  1563. if not assigned(mop_list[mop]) then
  1564. begin
  1565. mop_list[mop]:=tfplist.create;
  1566. SymList.ForEachCall(@do_get_managementoperator_offset_list,pointer(ptruint(mop)));
  1567. end;
  1568. for i:=0 to mop_list[mop].count-1 do
  1569. begin
  1570. entry:=pmanagementoperator_offset_entry(mop_list[mop][i]);
  1571. New(entrycopy);
  1572. entrycopy^:=entry^;
  1573. list.add(entrycopy);
  1574. end;
  1575. end;
  1576. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1577. begin
  1578. _datasize:=val;
  1579. if (usefieldalignment=bit_alignment) then
  1580. { can overflow in non bitpacked records }
  1581. databitsize:=val*8;
  1582. end;
  1583. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1584. var
  1585. l : asizeint;
  1586. varalignfield,
  1587. varalign : shortint;
  1588. vardef : tdef;
  1589. begin
  1590. { Calculate field offset }
  1591. l:=sym.getsize;
  1592. vardef:=sym.vardef;
  1593. varalign:=vardef.structalignment;
  1594. case usefieldalignment of
  1595. bit_alignment:
  1596. { has to be handled separately }
  1597. internalerror(2012071301);
  1598. C_alignment:
  1599. begin
  1600. { Calc the alignment size for C style records }
  1601. if (varalign>4) and
  1602. ((varalign mod 4)<>0) and
  1603. (vardef.typ=arraydef) then
  1604. Message1(sym_w_wrong_C_pack,vardef.typename);
  1605. if varalign=0 then
  1606. varalign:=l;
  1607. if (globalfieldalignment<maxCrecordalign) then
  1608. begin
  1609. if (varalign>16) and (globalfieldalignment<32) then
  1610. globalfieldalignment:=32
  1611. else if (varalign>12) and (globalfieldalignment<16) then
  1612. globalfieldalignment:=16
  1613. { 12 is needed for long double }
  1614. else if (varalign>8) and (globalfieldalignment<12) then
  1615. globalfieldalignment:=12
  1616. else if (varalign>4) and (globalfieldalignment<8) then
  1617. globalfieldalignment:=8
  1618. else if (varalign>2) and (globalfieldalignment<4) then
  1619. globalfieldalignment:=4
  1620. else if (varalign>1) and (globalfieldalignment<2) then
  1621. globalfieldalignment:=2;
  1622. end;
  1623. globalfieldalignment:=min(globalfieldalignment,maxCrecordalign);
  1624. end;
  1625. mac68k_alignment:
  1626. begin
  1627. { mac68k alignment (C description):
  1628. * char is aligned to 1 byte
  1629. * everything else (except vector) is aligned to 2 bytes
  1630. * vector is aligned to 16 bytes
  1631. }
  1632. if l>1 then
  1633. globalfieldalignment:=2
  1634. else
  1635. globalfieldalignment:=1;
  1636. varalign:=2;
  1637. end;
  1638. end;
  1639. if varalign=0 then
  1640. varalign:=size_2_align(l);
  1641. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1642. result:=align(base,varalignfield);
  1643. end;
  1644. function tabstractrecordsymtable.iscurrentunit: boolean;
  1645. begin
  1646. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1647. end;
  1648. {****************************************************************************
  1649. TRecordSymtable
  1650. ****************************************************************************}
  1651. constructor trecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1652. begin
  1653. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1654. symtabletype:=recordsymtable;
  1655. end;
  1656. { this procedure is reserved for inserting case variant into
  1657. a record symtable }
  1658. { the offset is the location of the start of the variant
  1659. and datasize and dataalignment corresponds to
  1660. the complete size (see code in pdecl unit) PM }
  1661. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : asizeint);
  1662. var
  1663. sym : tsym;
  1664. def : tdef;
  1665. i : integer;
  1666. varalignrecord,varalign,
  1667. storesize,storealign : asizeint;
  1668. bitsize: tcgint;
  1669. begin
  1670. storesize:=_datasize;
  1671. storealign:=fieldalignment;
  1672. _datasize:=offset;
  1673. if (usefieldalignment=bit_alignment) then
  1674. databitsize:=offset*8;
  1675. { We move the ownership of the defs and symbols to the new recordsymtable.
  1676. The old unionsymtable keeps the references, but doesn't own the
  1677. objects anymore }
  1678. unionst.DefList.OwnsObjects:=false;
  1679. unionst.SymList.OwnsObjects:=false;
  1680. { copy symbols }
  1681. for i:=0 to unionst.SymList.Count-1 do
  1682. begin
  1683. sym:=TSym(unionst.SymList[i]);
  1684. if sym.typ<>fieldvarsym then
  1685. internalerror(200601272);
  1686. if tfieldvarsym(sym).fieldoffset=0 then
  1687. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1688. { add to this record symtable, checking for duplicate names }
  1689. // unionst.SymList.List.List^[i].Data:=nil;
  1690. insert(sym);
  1691. varalign:=tfieldvarsym(sym).vardef.alignment;
  1692. if varalign=0 then
  1693. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1694. { retrieve size }
  1695. if (usefieldalignment=bit_alignment) then
  1696. begin
  1697. { bit packed records are limited to high(aint) bits }
  1698. { instead of bytes to avoid double precision }
  1699. { arithmetic in offset calculations }
  1700. if is_ordinal(tfieldvarsym(sym).vardef) then
  1701. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1702. else
  1703. begin
  1704. bitsize:=tfieldvarsym(sym).getsize;
  1705. if (bitsize>high(asizeint) div 8) then
  1706. Message(sym_e_segment_too_large);
  1707. bitsize:=bitsize*8;
  1708. end;
  1709. if bitsize>high(asizeint)-databitsize then
  1710. begin
  1711. Message(sym_e_segment_too_large);
  1712. _datasize:=high(asizeint);
  1713. databitsize:=high(asizeint);
  1714. end
  1715. else
  1716. begin
  1717. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1718. _datasize:=(databitsize+7) div 8;
  1719. end;
  1720. tfieldvarsym(sym).fieldoffset:=databitsize;
  1721. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1722. end
  1723. else
  1724. begin
  1725. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1726. begin
  1727. Message(sym_e_segment_too_large);
  1728. _datasize:=high(asizeint);
  1729. end
  1730. else
  1731. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1732. { update address }
  1733. tfieldvarsym(sym).fieldoffset:=_datasize;
  1734. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1735. end;
  1736. { update alignment of this record }
  1737. if (usefieldalignment<>C_alignment) and
  1738. (usefieldalignment<>mac68k_alignment) then
  1739. recordalignment:=max(recordalignment,varalignrecord);
  1740. end;
  1741. { update alignment for C records }
  1742. if (usefieldalignment=C_alignment) and
  1743. (usefieldalignment<>mac68k_alignment) then
  1744. recordalignment:=max(recordalignment,unionst.recordalignment);
  1745. { Register defs in the new record symtable }
  1746. for i:=0 to unionst.DefList.Count-1 do
  1747. begin
  1748. def:=TDef(unionst.DefList[i]);
  1749. def.ChangeOwner(self);
  1750. end;
  1751. _datasize:=storesize;
  1752. fieldalignment:=storealign;
  1753. { If a record contains a union, it does not contain a "single
  1754. non-composite field" in the context of certain ABIs requiring
  1755. special treatment for such records }
  1756. if defowner.typ=recorddef then
  1757. trecorddef(defowner).isunion:=true;
  1758. end;
  1759. procedure trecordsymtable.includemanagementoperator(mop:tmanagementoperator);
  1760. begin
  1761. if mop in managementoperators then
  1762. exit;
  1763. include(managementoperators,mop);
  1764. end;
  1765. {****************************************************************************
  1766. TObjectSymtable
  1767. ****************************************************************************}
  1768. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1769. begin
  1770. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1771. symtabletype:=ObjectSymtable;
  1772. defowner:=adefowner;
  1773. end;
  1774. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1775. var
  1776. hsym: tsym;
  1777. warn: boolean;
  1778. begin
  1779. result:=false;
  1780. if not assigned(defowner) then
  1781. internalerror(200602061);
  1782. { procsym and propertysym have special code
  1783. to override values in inherited classes. For other
  1784. symbols check for duplicates }
  1785. if not(sym.typ in [procsym,propertysym]) then
  1786. begin
  1787. { but private ids can be reused }
  1788. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1789. if assigned(hsym) and
  1790. (
  1791. (
  1792. not(m_delphi in current_settings.modeswitches) and
  1793. is_visible_for_object(hsym,tobjectdef(defowner))
  1794. ) or
  1795. (
  1796. { In Delphi, you can repeat members of a parent class. You can't }
  1797. { do this for objects however, and you (obviouly) can't }
  1798. { declare two fields with the same name in a single class }
  1799. (m_delphi in current_settings.modeswitches) and
  1800. (
  1801. is_object(tdef(defowner)) or
  1802. (hsym.owner = self)
  1803. )
  1804. )
  1805. ) then
  1806. begin
  1807. { only watn when a parameter/local variable in a method
  1808. conflicts with a category method, because this can easily
  1809. happen due to all possible categories being imported via
  1810. CocoaAll }
  1811. warn:=
  1812. (is_objccategory(tdef(hsym.owner.defowner)) or
  1813. is_classhelper(tdef(hsym.owner.defowner))) and
  1814. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1815. DuplicateSym(hashedid,sym,hsym,warn);
  1816. result:=true;
  1817. end;
  1818. end
  1819. else
  1820. result:=inherited checkduplicate(hashedid,sym);
  1821. end;
  1822. {$ifdef llvm}
  1823. {****************************************************************************
  1824. tLlvmShadowSymtableEntry
  1825. ****************************************************************************}
  1826. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1827. begin
  1828. fdef:=def;
  1829. ffieldoffset:=fieldoffset;
  1830. end;
  1831. {****************************************************************************
  1832. TLlvmShadowSymtable
  1833. ****************************************************************************}
  1834. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1835. begin
  1836. result:=get_by_llvm_index(f.llvmfieldnr)
  1837. end;
  1838. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1839. begin
  1840. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1841. end;
  1842. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1843. begin
  1844. equivst:=st;
  1845. curroffset:=0;
  1846. symdeflist:=tfpobjectlist.create(true);
  1847. generate;
  1848. end;
  1849. destructor tllvmshadowsymtable.destroy;
  1850. begin
  1851. symdeflist.free;
  1852. end;
  1853. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1854. var
  1855. sizectr,
  1856. tmpsize: aint;
  1857. begin
  1858. case equivst.usefieldalignment of
  1859. bit_alignment:
  1860. begin
  1861. { curoffset: bit address after the previous field. }
  1862. { llvm has no special support for bitfields in records, }
  1863. { so we replace them with plain bytes. }
  1864. { as soon as a single bit of a byte is allocated, we }
  1865. { allocate the byte in the llvm shadow record }
  1866. if (fieldoffset>curroffset) then
  1867. curroffset:=align(curroffset,8);
  1868. { fields in bitpacked records always start either right }
  1869. { after the previous one, or at the next byte boundary. }
  1870. if (curroffset<>fieldoffset) then
  1871. internalerror(2008051002);
  1872. if is_ordinal(vardef) then
  1873. begin
  1874. tmpsize:=vardef.packedbitsize;
  1875. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1876. inc(curroffset,tmpsize);
  1877. tmpsize:=0;
  1878. while sizectr<>0 do
  1879. begin
  1880. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1881. dec(sizectr);
  1882. inc(tmpsize);
  1883. end;
  1884. end
  1885. else
  1886. begin
  1887. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1888. if not(derefclass) then
  1889. inc(curroffset,vardef.size*8)
  1890. else
  1891. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1892. end;
  1893. end
  1894. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1895. begin
  1896. { curoffset: address right after the previous field }
  1897. while (fieldoffset>curroffset) do
  1898. begin
  1899. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1900. inc(curroffset);
  1901. end;
  1902. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1903. if not(derefclass) then
  1904. inc(curroffset,vardef.size)
  1905. else
  1906. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1907. end
  1908. else
  1909. { default for llvm, don't add explicit padding }
  1910. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1911. end
  1912. end;
  1913. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1914. begin
  1915. case equivst.usefieldalignment of
  1916. { already correct in this case }
  1917. bit_alignment:
  1918. ;
  1919. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1920. begin
  1921. { add padding fields }
  1922. while (finalsize>curroffset) do
  1923. begin
  1924. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1925. inc(curroffset);
  1926. end;
  1927. end;
  1928. end;
  1929. end;
  1930. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1931. var
  1932. sym: tfieldvarsym;
  1933. lastoffset: aint;
  1934. newalignment: aint;
  1935. i, j: longint;
  1936. begin
  1937. i:=0;
  1938. while (i<equivst.symlist.count) do
  1939. begin
  1940. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1941. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  1942. begin
  1943. inc(i);
  1944. continue;
  1945. end;
  1946. sym:=tfieldvarsym(equivst.symlist[i]);
  1947. { a "better" algorithm might be to use the largest }
  1948. { variant in case of (bit)packing, since then }
  1949. { alignment doesn't matter }
  1950. if (vo_is_first_field in sym.varoptions) then
  1951. begin
  1952. { we assume that all fields are processed in order. }
  1953. if (variantstarts.count<>0) then
  1954. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1955. else
  1956. lastoffset:=-1;
  1957. { new variant at same level as last one: use if higher alignment }
  1958. if (lastoffset=sym.fieldoffset) then
  1959. begin
  1960. if (equivst.fieldalignment<>bit_alignment) then
  1961. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1962. else
  1963. newalignment:=1;
  1964. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1965. variantstarts[variantstarts.count-1]:=sym;
  1966. end
  1967. { variant at deeper level than last one -> add }
  1968. else if (lastoffset<sym.fieldoffset) then
  1969. variantstarts.add(sym)
  1970. else
  1971. begin
  1972. { a variant at a less deep level, so backtrack }
  1973. j:=variantstarts.count-2;
  1974. while (j>=0) do
  1975. begin
  1976. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1977. break;
  1978. dec(j);
  1979. end;
  1980. if (j<0) then
  1981. internalerror(2008051003);
  1982. { new variant has higher alignment? }
  1983. if (equivst.fieldalignment<>bit_alignment) then
  1984. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1985. else
  1986. newalignment:=1;
  1987. { yes, replace and remove previous nested variants }
  1988. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1989. begin
  1990. variantstarts[j]:=sym;
  1991. variantstarts.count:=j+1;
  1992. end
  1993. { no, skip this variant }
  1994. else
  1995. begin
  1996. inc(i);
  1997. while (i<equivst.symlist.count) and
  1998. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1999. (sp_static in tsym(equivst.symlist[i]).symoptions) or
  2000. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  2001. inc(i);
  2002. continue;
  2003. end;
  2004. end;
  2005. end;
  2006. inc(i);
  2007. end;
  2008. end;
  2009. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  2010. var
  2011. lastvaroffsetprocessed: aint;
  2012. i, equivcount, varcount: longint;
  2013. begin
  2014. { if it's an object/class, the first entry is the parent (if there is one) }
  2015. if (equivst.symtabletype=objectsymtable) and
  2016. assigned(tobjectdef(equivst.defowner).childof) then
  2017. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  2018. equivcount:=equivst.symlist.count;
  2019. varcount:=0;
  2020. i:=0;
  2021. lastvaroffsetprocessed:=-1;
  2022. while (i<equivcount) do
  2023. begin
  2024. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  2025. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  2026. begin
  2027. inc(i);
  2028. continue;
  2029. end;
  2030. { start of a new variant? }
  2031. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  2032. begin
  2033. { if we want to process the same variant offset twice, it means that we }
  2034. { got to the end and are trying to process the next variant part -> stop }
  2035. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  2036. break;
  2037. if (varcount>=variantstarts.count) then
  2038. internalerror(2008051005);
  2039. { new variant part -> use the one with the biggest alignment }
  2040. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  2041. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  2042. inc(varcount);
  2043. if (i<0) then
  2044. internalerror(2008051004);
  2045. end;
  2046. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  2047. inc(i);
  2048. end;
  2049. addalignmentpadding(equivst.datasize);
  2050. end;
  2051. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  2052. var
  2053. i, varcount: longint;
  2054. shadowindex: longint;
  2055. equivcount : longint;
  2056. begin
  2057. varcount:=0;
  2058. shadowindex:=0;
  2059. equivcount:=equivst.symlist.count;
  2060. i:=0;
  2061. while (i < equivcount) do
  2062. begin
  2063. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  2064. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  2065. begin
  2066. inc(i);
  2067. continue;
  2068. end;
  2069. { start of a new variant? }
  2070. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  2071. begin
  2072. { back up to a less deeply nested variant level? }
  2073. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  2074. dec(varcount);
  2075. { it's possible that some variants are more deeply nested than the
  2076. one we recorded in the shadowsymtable (since we recorded the one
  2077. with the biggest alignment, not necessarily the biggest one in size
  2078. }
  2079. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  2080. varcount:=variantstarts.count-1
  2081. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  2082. internalerror(2008051006);
  2083. { reset the shadowindex to the start of this variant. }
  2084. { in case the llvmfieldnr is not (yet) set for this }
  2085. { field, shadowindex will simply be reset to zero and }
  2086. { we'll start searching from the start of the record }
  2087. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  2088. if (varcount<pred(variantstarts.count)) then
  2089. inc(varcount);
  2090. end;
  2091. { find the last shadowfield whose offset <= the current field's offset }
  2092. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  2093. (shadowindex<symdeflist.count-1) and
  2094. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  2095. inc(shadowindex);
  2096. { set the field number and potential offset from that field (in case }
  2097. { of overlapping variants) }
  2098. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  2099. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  2100. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  2101. inc(i);
  2102. end;
  2103. end;
  2104. procedure tllvmshadowsymtable.generate;
  2105. var
  2106. variantstarts: tfplist;
  2107. begin
  2108. variantstarts:=tfplist.create;
  2109. { first go through the entire record and }
  2110. { store the fieldvarsyms of the variants }
  2111. { with the highest alignment }
  2112. findvariantstarts(variantstarts);
  2113. { now go through the regular fields and the selected variants, }
  2114. { and add them to the llvm shadow record symtable }
  2115. buildtable(variantstarts);
  2116. { finally map all original fields to the llvm definition }
  2117. buildmapping(variantstarts);
  2118. variantstarts.free;
  2119. end;
  2120. {$endif llvm}
  2121. {****************************************************************************
  2122. TAbstractSubSymtable
  2123. ****************************************************************************}
  2124. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  2125. var
  2126. oldtyp : byte;
  2127. begin
  2128. oldtyp:=ppufile.entrytyp;
  2129. ppufile.entrytyp:=subentryid;
  2130. inherited ppuwrite(ppufile);
  2131. ppufile.entrytyp:=oldtyp;
  2132. end;
  2133. {****************************************************************************
  2134. TAbstractLocalSymtable
  2135. ****************************************************************************}
  2136. function tabstractlocalsymtable.count_locals:longint;
  2137. var
  2138. i : longint;
  2139. sym : tsym;
  2140. begin
  2141. result:=0;
  2142. for i:=0 to SymList.Count-1 do
  2143. begin
  2144. sym:=tsym(SymList[i]);
  2145. { Count only varsyms, but ignore the funcretsym }
  2146. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  2147. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  2148. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  2149. (tstoredsym(sym).refs>0)) then
  2150. inc(result);
  2151. end;
  2152. end;
  2153. function tabstractlocalsymtable.iscurrentunit: boolean;
  2154. begin
  2155. Result:=
  2156. assigned(defowner) and
  2157. defowner.owner.iscurrentunit;
  2158. end;
  2159. {****************************************************************************
  2160. TLocalSymtable
  2161. ****************************************************************************}
  2162. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  2163. begin
  2164. inherited create('');
  2165. defowner:=adefowner;
  2166. symtabletype:=localsymtable;
  2167. symtablelevel:=level;
  2168. end;
  2169. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2170. var
  2171. hsym : tsym;
  2172. begin
  2173. if not assigned(defowner) or
  2174. (defowner.typ<>procdef) then
  2175. internalerror(200602042);
  2176. result:=false;
  2177. hsym:=tsym(FindWithHash(hashedid));
  2178. if assigned(hsym) then
  2179. begin
  2180. { a local and the function can have the same
  2181. name in TP and Delphi, but RESULT not }
  2182. if (m_duplicate_names in current_settings.modeswitches) and
  2183. (hsym.typ in [absolutevarsym,localvarsym]) and
  2184. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  2185. not((m_result in current_settings.modeswitches) and
  2186. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  2187. HideSym(hsym)
  2188. else
  2189. DuplicateSym(hashedid,sym,hsym,false);
  2190. result:=true;
  2191. exit;
  2192. end;
  2193. { check also parasymtable, this needs to be done here because
  2194. of the special situation with the funcret sym that needs to be
  2195. hidden for tp and delphi modes }
  2196. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  2197. if assigned(hsym) then
  2198. begin
  2199. { a local and the function can have the same
  2200. name in TP and Delphi, but RESULT not }
  2201. if (m_duplicate_names in current_settings.modeswitches) and
  2202. (sym.typ in [absolutevarsym,localvarsym]) and
  2203. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  2204. not((m_result in current_settings.modeswitches) and
  2205. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  2206. Hidesym(sym)
  2207. else
  2208. DuplicateSym(hashedid,sym,hsym,false);
  2209. result:=true;
  2210. exit;
  2211. end;
  2212. { check ObjectSymtable, skip this for funcret sym because
  2213. that will always be positive because it has the same name
  2214. as the procsym }
  2215. if not is_funcret_sym(sym) and
  2216. (defowner.typ=procdef) and
  2217. assigned(tprocdef(defowner).struct) and
  2218. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2219. (
  2220. not(m_delphi in current_settings.modeswitches) or
  2221. is_object(tprocdef(defowner).struct)
  2222. ) then
  2223. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2224. end;
  2225. {****************************************************************************
  2226. TParaSymtable
  2227. ****************************************************************************}
  2228. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2229. begin
  2230. inherited create('');
  2231. readonly:=false;
  2232. defowner:=adefowner;
  2233. symtabletype:=parasymtable;
  2234. symtablelevel:=level;
  2235. end;
  2236. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2237. begin
  2238. result:=inherited checkduplicate(hashedid,sym);
  2239. if result then
  2240. exit;
  2241. if not(m_duplicate_names in current_settings.modeswitches) and
  2242. assigned(defowner) and (defowner.typ=procdef) and
  2243. assigned(tprocdef(defowner).struct) and
  2244. assigned(tprocdef(defowner).owner) and
  2245. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2246. (
  2247. not(m_delphi in current_settings.modeswitches) or
  2248. is_object(tprocdef(defowner).struct)
  2249. ) then
  2250. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2251. end;
  2252. procedure tparasymtable.insertdef(def: TDefEntry);
  2253. begin
  2254. if readonly then
  2255. defowner.owner.insertdef(def)
  2256. else
  2257. inherited insertdef(def);
  2258. end;
  2259. {****************************************************************************
  2260. TAbstractUniTSymtable
  2261. ****************************************************************************}
  2262. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2263. begin
  2264. inherited create(n);
  2265. moduleid:=id;
  2266. end;
  2267. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2268. var
  2269. hsym : tsym;
  2270. begin
  2271. result:=false;
  2272. hsym:=tsym(FindWithHash(hashedid));
  2273. if assigned(hsym) then
  2274. begin
  2275. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2276. exit;
  2277. if hsym.typ=symconst.namespacesym then
  2278. begin
  2279. case sym.typ of
  2280. symconst.namespacesym:;
  2281. symconst.unitsym:
  2282. begin
  2283. HideSym(sym); { if we add a unit and there is a namespace with the same name then hide the unit name and not the namespace }
  2284. tnamespacesym(hsym).unitsym:=tsym(sym);
  2285. end
  2286. else
  2287. HideSym(hsym);
  2288. end;
  2289. end
  2290. else
  2291. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2292. unit, the unit can then not be accessed anymore using
  2293. <unit>.<id>, so we can hide the symbol.
  2294. Do the same if we add a namespace and there is a unit with the same name }
  2295. if (hsym.typ=symconst.unitsym) and
  2296. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2297. begin
  2298. HideSym(hsym);
  2299. if sym.typ=symconst.namespacesym then
  2300. tnamespacesym(sym).unitsym:=tsym(hsym);
  2301. end
  2302. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2303. in this case, copy the isoindex and make the original symbol invisible }
  2304. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2305. and (tprogramparasym(hsym).isoindex<>0) then
  2306. begin
  2307. HideSym(hsym);
  2308. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2309. end
  2310. else
  2311. DuplicateSym(hashedid,sym,hsym,false);
  2312. result:=true;
  2313. exit;
  2314. end;
  2315. end;
  2316. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2317. begin
  2318. result:=find(n);
  2319. if assigned(result)and(result.typ<>namespacesym)then
  2320. result:=nil;
  2321. end;
  2322. function tabstractuniTSymtable.iscurrentunit:boolean;
  2323. begin
  2324. result:=assigned(current_module) and
  2325. (
  2326. (current_module.globalsymtable=self) or
  2327. (current_module.localsymtable=self)
  2328. );
  2329. end;
  2330. function tabstractuniTSymtable.needs_init_final: boolean;
  2331. begin
  2332. if not init_final_check_done then
  2333. begin
  2334. result:=inherited needs_init_final;
  2335. if not result then
  2336. begin
  2337. result:=has_class_condestructors;
  2338. if result then
  2339. include(tableoptions,sto_needs_init_final);
  2340. end;
  2341. end;
  2342. result:=sto_needs_init_final in tableoptions;
  2343. end;
  2344. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2345. var
  2346. p:integer;
  2347. n,ns:string;
  2348. oldsym:TSymEntry;
  2349. begin
  2350. insert(sym);
  2351. n:=sym.realname;
  2352. p:=pos('.',n);
  2353. ns:='';
  2354. while p>0 do
  2355. begin
  2356. if ns='' then
  2357. ns:=copy(n,1,p-1)
  2358. else
  2359. ns:=ns+'.'+copy(n,1,p-1);
  2360. system.delete(n,1,p);
  2361. oldsym:=findnamespace(upper(ns));
  2362. if not assigned(oldsym) then
  2363. insert(cnamespacesym.create(ns));
  2364. p:=pos('.',n);
  2365. end;
  2366. end;
  2367. procedure CheckForClassConDestructors(p:TObject;arg:pointer);
  2368. var
  2369. result: pboolean absolute arg;
  2370. begin
  2371. if result^ then
  2372. exit;
  2373. if (tdef(p).typ in [objectdef,recorddef]) and
  2374. not (df_generic in tdef(p).defoptions) then
  2375. begin
  2376. { first check the class... }
  2377. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  2378. result^:=true;;
  2379. { ... and then also check all subclasses }
  2380. if not result^ then
  2381. tabstractrecorddef(p).symtable.deflist.foreachcall(@CheckForClassConDestructors,arg);
  2382. end;
  2383. end;
  2384. function tabstractuniTSymtable.has_class_condestructors: boolean;
  2385. begin
  2386. result:=false;
  2387. deflist.foreachcall(@CheckForClassConDestructors,@result);
  2388. end;
  2389. {****************************************************************************
  2390. TStaticSymtable
  2391. ****************************************************************************}
  2392. constructor tstaticsymtable.create(const n : string;id:word);
  2393. begin
  2394. inherited create(n,id);
  2395. symtabletype:=staticsymtable;
  2396. symtablelevel:=main_program_level;
  2397. currentvisibility:=vis_private;
  2398. end;
  2399. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2400. begin
  2401. inherited ppuload(ppufile);
  2402. { now we can deref the syms and defs }
  2403. deref(false);
  2404. end;
  2405. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2406. begin
  2407. inherited ppuwrite(ppufile);
  2408. end;
  2409. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2410. begin
  2411. result:=inherited checkduplicate(hashedid,sym);
  2412. if not result and
  2413. (current_module.localsymtable=self) and
  2414. assigned(current_module.globalsymtable) then
  2415. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2416. end;
  2417. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2418. begin
  2419. result:=inherited findnamespace(n);
  2420. if not assigned(result) and
  2421. (current_module.localsymtable=self) and
  2422. assigned(current_module.globalsymtable) then
  2423. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2424. end;
  2425. {****************************************************************************
  2426. TGlobalSymtable
  2427. ****************************************************************************}
  2428. constructor tglobalsymtable.create(const n : string;id:word);
  2429. begin
  2430. inherited create(n,id);
  2431. symtabletype:=globalsymtable;
  2432. symtablelevel:=main_program_level;
  2433. end;
  2434. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2435. begin
  2436. inherited ppuload(ppufile);
  2437. { now we can deref the syms and defs }
  2438. deref(false);
  2439. end;
  2440. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2441. begin
  2442. { write the symtable entries }
  2443. inherited ppuwrite(ppufile);
  2444. end;
  2445. {*****************************************************************************
  2446. tspecializesymtable
  2447. *****************************************************************************}
  2448. constructor tspecializesymtable.create(const n : string;id:word);
  2449. begin
  2450. inherited create(n,id);
  2451. { the specialize symtable does not own the syms and defs as they are all
  2452. moved to a different symtable before the symtable is destroyed; this
  2453. avoids calls to "extract" }
  2454. symlist.ownsobjects:=false;
  2455. deflist.ownsobjects:=false;
  2456. end;
  2457. function tspecializesymtable.iscurrentunit: boolean;
  2458. begin
  2459. Result:=true;
  2460. end;
  2461. {****************************************************************************
  2462. TWITHSYMTABLE
  2463. ****************************************************************************}
  2464. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2465. begin
  2466. inherited create('');
  2467. symtabletype:=withsymtable;
  2468. withrefnode:=refnode;
  2469. { Replace SymList with the passed symlist }
  2470. SymList.free;
  2471. SymList:=ASymList;
  2472. defowner:=aowner;
  2473. end;
  2474. destructor twithsymtable.destroy;
  2475. begin
  2476. if refcount>1 then
  2477. exit;
  2478. withrefnode.free;
  2479. { Disable SymList because we don't Own it }
  2480. SymList:=nil;
  2481. inherited destroy;
  2482. end;
  2483. procedure twithsymtable.clear;
  2484. begin
  2485. { remove no entry from a withsymtable as it is only a pointer to the
  2486. recorddef or objectdef symtable }
  2487. end;
  2488. procedure twithsymtable.insertdef(def:TDefEntry);
  2489. begin
  2490. { Definitions can't be registered in the withsymtable
  2491. because the withsymtable is removed after the with block.
  2492. We can't easily solve it here because the next symtable in the
  2493. stack is not known. }
  2494. internalerror(200602046);
  2495. end;
  2496. {****************************************************************************
  2497. TSTT_ExceptionSymtable
  2498. ****************************************************************************}
  2499. constructor tstt_excepTSymtable.create;
  2500. begin
  2501. inherited create('');
  2502. symtabletype:=stt_excepTSymtable;
  2503. end;
  2504. {****************************************************************************
  2505. TMacroSymtable
  2506. ****************************************************************************}
  2507. constructor tmacrosymtable.create(exported: boolean);
  2508. begin
  2509. inherited create('');
  2510. if exported then
  2511. symtabletype:=exportedmacrosymtable
  2512. else
  2513. symtabletype:=localmacrosymtable;
  2514. symtablelevel:=main_program_level;
  2515. end;
  2516. {****************************************************************************
  2517. TEnumSymtable
  2518. ****************************************************************************}
  2519. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  2520. var
  2521. value: longint;
  2522. def: tenumdef;
  2523. begin
  2524. // defowner = nil only when we are loading from ppu
  2525. if defowner<>nil then
  2526. begin
  2527. { First entry? Then we need to set the minval }
  2528. value:=tenumsym(sym).value;
  2529. def:=tenumdef(defowner);
  2530. if SymList.count=0 then
  2531. begin
  2532. if value>0 then
  2533. def.has_jumps:=true;
  2534. def.setmin(value);
  2535. def.setmax(value);
  2536. end
  2537. else
  2538. begin
  2539. { check for jumps }
  2540. if value>def.max+1 then
  2541. def.has_jumps:=true;
  2542. { update low and high }
  2543. if def.min>value then
  2544. def.setmin(value);
  2545. if def.max<value then
  2546. def.setmax(value);
  2547. end;
  2548. end;
  2549. inherited insert(sym, checkdup);
  2550. end;
  2551. constructor tenumsymtable.create(adefowner: tdef);
  2552. begin
  2553. inherited Create('');
  2554. symtabletype:=enumsymtable;
  2555. defowner:=adefowner;
  2556. end;
  2557. {****************************************************************************
  2558. TArraySymtable
  2559. ****************************************************************************}
  2560. procedure tarraysymtable.insertdef(def: TDefEntry);
  2561. begin
  2562. { Enums must also be available outside the record scope,
  2563. insert in the owner of this symtable }
  2564. if def.typ=enumdef then
  2565. defowner.owner.insertdef(def)
  2566. else
  2567. inherited insertdef(def);
  2568. end;
  2569. constructor tarraysymtable.create(adefowner: tdef);
  2570. begin
  2571. inherited Create('');
  2572. symtabletype:=arraysymtable;
  2573. defowner:=adefowner;
  2574. end;
  2575. {*****************************************************************************
  2576. Helper Routines
  2577. *****************************************************************************}
  2578. function FullTypeName(def,otherdef:tdef):string;
  2579. var
  2580. s1,s2 : string;
  2581. begin
  2582. if def.typ in [objectdef,recorddef] then
  2583. s1:=tabstractrecorddef(def).RttiName
  2584. else
  2585. s1:=def.typename;
  2586. { When the names are the same try to include the unit name }
  2587. if assigned(otherdef) and
  2588. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2589. begin
  2590. s2:=otherdef.typename;
  2591. if upper(s1)=upper(s2) then
  2592. s1:=def.owner.realname^+'.'+s1;
  2593. end;
  2594. FullTypeName:=s1;
  2595. end;
  2596. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  2597. begin
  2598. result:='';
  2599. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2600. begin
  2601. if (result='') then
  2602. if symtable.name<>nil then
  2603. result:=symtable.name^
  2604. else
  2605. else
  2606. if symtable.name<>nil then
  2607. result:=symtable.name^+delimiter+result
  2608. else
  2609. result:=delimiter+result;
  2610. symtable:=symtable.defowner.owner;
  2611. end;
  2612. end;
  2613. function generate_objectpascal_helper_key(def:tdef):string;
  2614. begin
  2615. if not assigned(def) then
  2616. internalerror(2013020501);
  2617. if def.typ in [recorddef,objectdef] then
  2618. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2619. else
  2620. result:=make_mangledname('',def.owner,def.typesym.name);
  2621. end;
  2622. procedure incompatibletypes(def1,def2:tdef);
  2623. begin
  2624. { When there is an errordef there is already an error message show }
  2625. if (def2.typ=errordef) or
  2626. (def1.typ=errordef) then
  2627. exit;
  2628. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2629. end;
  2630. procedure hidesym(sym:TSymEntry);
  2631. begin
  2632. sym.realname:='$hidden'+sym.realname;
  2633. tsym(sym).visibility:=vis_hidden;
  2634. end;
  2635. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2636. var
  2637. st : TSymtable;
  2638. filename : TIDString;
  2639. begin
  2640. if not warn then
  2641. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2642. else
  2643. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2644. { Write hint where the original symbol was found }
  2645. st:=finduniTSymtable(origsym.owner);
  2646. with tsym(origsym).fileinfo do
  2647. begin
  2648. if assigned(st) and
  2649. (st.symtabletype=globalsymtable) and
  2650. st.iscurrentunit then
  2651. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2652. else if assigned(st.name) then
  2653. begin
  2654. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2655. if filename<>'' then
  2656. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2657. else
  2658. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2659. end;
  2660. end;
  2661. { Rename duplicate sym to an unreachable name, but it can be
  2662. inserted in the symtable without errors }
  2663. inc(dupnr);
  2664. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2665. if assigned(dupsym) then
  2666. include(tsym(dupsym).symoptions,sp_implicitrename);
  2667. end;
  2668. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2669. begin
  2670. result:=false;
  2671. if not assigned(sym) or not (sym is tstoredsym) then
  2672. Internalerror(2011081101);
  2673. { For generics a dummy symbol without the parameter count is created
  2674. if such a symbol not yet exists so that different parts of the
  2675. parser can find that symbol. If that symbol is still a
  2676. undefineddef we replace the generic dummy symbol's
  2677. name with a "dup" name and use the new symbol as the generic dummy
  2678. symbol }
  2679. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2680. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2681. (m_delphi in current_settings.modeswitches) then
  2682. begin
  2683. inc(dupnr);
  2684. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2685. include(tsym(sym).symoptions,sp_implicitrename);
  2686. { we need to find the new symbol now if checking for a dummy }
  2687. include(symoptions,sp_generic_dummy);
  2688. result:=true;
  2689. end;
  2690. end;
  2691. procedure write_system_parameter_lists(const name:string);
  2692. var
  2693. srsym:tprocsym;
  2694. begin
  2695. srsym:=tprocsym(systemunit.find(name));
  2696. if not assigned(srsym) or not (srsym.typ=procsym) then
  2697. internalerror(2016060302);
  2698. srsym.write_parameter_lists(nil);
  2699. end;
  2700. {*****************************************************************************
  2701. Search
  2702. *****************************************************************************}
  2703. procedure addsymref(sym:tsym);
  2704. var
  2705. owner: tsymtable;
  2706. begin
  2707. { for symbols used in preprocessor expressions, we don't want to
  2708. increase references count (for smaller final binaries) }
  2709. if not assigned(current_scanner) then
  2710. internalerror(2017050601);
  2711. if current_scanner.in_preproc_comp_expr then
  2712. exit;
  2713. { symbol uses count }
  2714. sym.IncRefCount;
  2715. owner:=sym.owner;
  2716. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2717. owner:=tdef(owner.defowner).owner;
  2718. if assigned(current_module) and
  2719. (owner.symtabletype=globalsymtable) then
  2720. begin
  2721. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2722. internalerror(200501152);
  2723. { unit uses count }
  2724. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2725. { Note: don't check the symtable directly as owner might be
  2726. a specialize symtable which is a globalsymtable as well }
  2727. if (
  2728. assigned(current_module.globalsymtable) and
  2729. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2730. ) or (
  2731. assigned(current_module.localsymtable) and
  2732. (current_module.localsymtable.moduleid<>owner.moduleid)
  2733. ) then
  2734. { symbol is imported from another unit }
  2735. current_module.addimportedsym(sym);
  2736. end;
  2737. end;
  2738. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2739. begin
  2740. result:=nesteddef=ownerdef;
  2741. if not result and
  2742. { types declared locally in a record method are not defined in the
  2743. record itself }
  2744. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2745. assigned(nesteddef.owner.defowner) then
  2746. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2747. end;
  2748. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2749. begin
  2750. result:=assigned(childsym) and (childsym.owner=symtable);
  2751. if not result and assigned(childsym) and
  2752. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2753. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2754. end;
  2755. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2756. begin
  2757. result:=false;
  2758. if not assigned(def1) or not assigned(def2) then
  2759. exit;
  2760. { for both defs walk to the topmost generic }
  2761. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2762. def1:=tdef(def1.owner.defowner);
  2763. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2764. def2:=tdef(def2.owner.defowner);
  2765. result:=def1=def2;
  2766. end;
  2767. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2768. var
  2769. uname : string;
  2770. begin
  2771. { TODO : check regarding arrays and records declared as their type }
  2772. if not (def.typ in [recorddef,objectdef]) then
  2773. internalerror(2012051501);
  2774. uname:=upper(srsym.realname);
  2775. repeat
  2776. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2777. begin
  2778. result:=def;
  2779. exit;
  2780. end;
  2781. def:=tdef(def.owner.defowner);
  2782. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2783. result:=nil;
  2784. end;
  2785. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2786. begin
  2787. { TODO : check regarding arrays and records declared as their type }
  2788. if not (nesteddef.typ in [recorddef,objectdef]) then
  2789. internalerror(2012051601);
  2790. repeat
  2791. if tstoreddef(nesteddef).genericdef=genericdef then
  2792. begin
  2793. resultdef:=nesteddef;
  2794. result:=true;
  2795. exit;
  2796. end;
  2797. nesteddef:=tdef(nesteddef.owner.defowner);
  2798. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2799. resultdef:=nil;
  2800. result:=false;
  2801. end;
  2802. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2803. symvisibility: visibility of the symbol
  2804. contextobjdef: via which def the symbol is accessed, e.g.:
  2805. fieldname:=1 -> contextobjdef = current_structdef
  2806. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2807. }
  2808. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2809. var
  2810. symownerdef : tabstractrecorddef;
  2811. nonlocalst : tsymtable;
  2812. isspezproc : boolean;
  2813. begin
  2814. result:=false;
  2815. { Get objdectdef owner of the symtable for the is_related checks }
  2816. if not assigned(symst) or
  2817. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2818. internalerror(200810285);
  2819. symownerdef:=tabstractrecorddef(symst.defowner);
  2820. { specializations might belong to a localsymtable or parasymtable }
  2821. nonlocalst:=symownerdef.owner;
  2822. if tstoreddef(symst.defowner).is_specialization then
  2823. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2824. nonlocalst:=nonlocalst.defowner.owner;
  2825. isspezproc:=false;
  2826. if assigned(current_procinfo) then
  2827. begin
  2828. if current_procinfo.procdef.is_specialization and
  2829. assigned(current_procinfo.procdef.struct) then
  2830. isspezproc:=true;
  2831. end;
  2832. case symvisibility of
  2833. vis_private :
  2834. begin
  2835. { private symbols are allowed when we are in the same
  2836. module as they are defined }
  2837. result:=(
  2838. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2839. (nonlocalst.iscurrentunit)
  2840. ) or
  2841. ( // the case of specialize inside the generic declaration and nested types
  2842. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2843. (
  2844. assigned(current_structdef) and
  2845. (
  2846. (current_structdef=symownerdef) or
  2847. (current_structdef.owner.iscurrentunit)
  2848. )
  2849. ) or
  2850. (
  2851. not assigned(current_structdef) and
  2852. (symownerdef.owner.iscurrentunit)
  2853. ) or
  2854. { access from a generic method that belongs to the class
  2855. but that is specialized elsewere }
  2856. (
  2857. isspezproc and
  2858. (current_procinfo.procdef.struct=current_structdef)
  2859. ) or
  2860. { specializations may access private symbols that their
  2861. generics are allowed to access }
  2862. (
  2863. assigned(current_structdef) and
  2864. (df_specialization in current_structdef.defoptions) and
  2865. (symst.moduleid=current_structdef.genericdef.owner.moduleid)
  2866. )
  2867. );
  2868. end;
  2869. vis_strictprivate :
  2870. begin
  2871. result:=assigned(current_structdef) and
  2872. is_owned_by(current_structdef,symownerdef);
  2873. end;
  2874. vis_strictprotected :
  2875. begin
  2876. result:=(
  2877. { access from nested class }
  2878. assigned(current_structdef) and
  2879. is_owned_by(current_structdef,symownerdef)
  2880. ) or
  2881. (
  2882. { access from child class }
  2883. assigned(contextobjdef) and
  2884. assigned(current_structdef) and
  2885. def_is_related(contextobjdef,symownerdef) and
  2886. def_is_related(current_structdef,contextobjdef)
  2887. ) or
  2888. (
  2889. { helpers can access strict protected symbols }
  2890. is_objectpascal_helper(contextobjdef) and
  2891. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2892. ) or
  2893. (
  2894. { same as above, but from context of call node inside
  2895. helper method }
  2896. is_objectpascal_helper(current_structdef) and
  2897. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2898. );
  2899. end;
  2900. vis_protected :
  2901. begin
  2902. { protected symbols are visible in the module that defines them and
  2903. also visible to related objects. The related object must be defined
  2904. in the current module }
  2905. result:=(
  2906. (
  2907. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2908. (nonlocalst.iscurrentunit)
  2909. ) or
  2910. (
  2911. assigned(contextobjdef) and
  2912. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable]) and
  2913. (contextobjdef.owner.iscurrentunit) and
  2914. def_is_related(contextobjdef,symownerdef)
  2915. ) or
  2916. ( // the case of specialize inside the generic declaration and nested types
  2917. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2918. (
  2919. assigned(current_structdef) and
  2920. (
  2921. (current_structdef=symownerdef) or
  2922. (current_structdef.owner.iscurrentunit)
  2923. )
  2924. ) or
  2925. (
  2926. not assigned(current_structdef) and
  2927. (symownerdef.owner.iscurrentunit)
  2928. ) or
  2929. (
  2930. { helpers can access protected symbols }
  2931. is_objectpascal_helper(contextobjdef) and
  2932. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2933. )
  2934. ) or
  2935. { access from a generic method that belongs to the class
  2936. but that is specialized elsewere }
  2937. (
  2938. isspezproc and
  2939. (current_procinfo.procdef.struct=current_structdef)
  2940. ) or
  2941. { specializations may access private symbols that their
  2942. generics are allowed to access }
  2943. (
  2944. assigned(current_structdef) and
  2945. (df_specialization in current_structdef.defoptions) and
  2946. (symst.moduleid=current_structdef.genericdef.owner.moduleid)
  2947. )
  2948. );
  2949. end;
  2950. vis_public,
  2951. vis_published :
  2952. result:=true;
  2953. else
  2954. internalerror(2019050702);
  2955. end;
  2956. end;
  2957. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2958. begin
  2959. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2960. end;
  2961. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2962. var
  2963. i : longint;
  2964. pd : tprocdef;
  2965. begin
  2966. if sym.typ=procsym then
  2967. begin
  2968. { A procsym is visible, when there is at least one of the procdefs visible }
  2969. result:=false;
  2970. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2971. begin
  2972. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2973. if (pd.owner=sym.owner) and
  2974. is_visible_for_object(pd,contextobjdef) then
  2975. begin
  2976. result:=true;
  2977. exit;
  2978. end;
  2979. end;
  2980. end
  2981. else
  2982. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2983. end;
  2984. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2985. begin
  2986. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2987. end;
  2988. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2989. begin
  2990. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2991. end;
  2992. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2993. var
  2994. hashedid: THashedIDString;
  2995. contextstructdef: tabstractrecorddef;
  2996. stackitem: psymtablestackitem;
  2997. begin
  2998. result:=false;
  2999. hashedid.id:=s;
  3000. stackitem:=symtablestack.stack;
  3001. while assigned(stackitem) do
  3002. begin
  3003. srsymtable:=stackitem^.symtable;
  3004. if (srsymtable.symtabletype=objectsymtable) then
  3005. begin
  3006. { TODO : implement the search for an option in classes as well }
  3007. if ssf_search_option in flags then
  3008. begin
  3009. result:=false;
  3010. exit;
  3011. end;
  3012. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  3013. begin
  3014. result:=true;
  3015. exit;
  3016. end;
  3017. end
  3018. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  3019. (srsymtable.defowner.typ=undefineddef)) then
  3020. begin
  3021. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3022. { First check if it is a unit/namespace symbol.
  3023. They are visible only if they are from the current unit or
  3024. unit of generic of currently processed specialization. }
  3025. if assigned(srsym) and
  3026. (
  3027. not(srsym.typ in [unitsym,namespacesym]) or
  3028. srsymtable.iscurrentunit or
  3029. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid)) or
  3030. (
  3031. assigned(current_procinfo) and
  3032. (df_specialization in current_procinfo.procdef.defoptions) and
  3033. (current_procinfo.procdef.genericdef.owner.moduleid=srsymtable.moduleid)
  3034. )
  3035. ) and
  3036. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  3037. begin
  3038. { use the class from withsymtable only when it is
  3039. defined in this unit }
  3040. if (srsymtable.symtabletype=withsymtable) and
  3041. assigned(srsymtable.defowner) and
  3042. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  3043. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  3044. (srsymtable.defowner.owner.iscurrentunit) then
  3045. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  3046. else
  3047. contextstructdef:=current_structdef;
  3048. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  3049. is_visible_for_object(srsym,contextstructdef) then
  3050. begin
  3051. { we need to know if a procedure references symbols
  3052. in the static symtable, because then it can't be
  3053. inlined from outside this unit }
  3054. if assigned(current_procinfo) and
  3055. (srsym.owner.symtabletype=staticsymtable) then
  3056. include(current_procinfo.flags,pi_uses_static_symtable);
  3057. if not (ssf_no_addsymref in flags) then
  3058. addsymref(srsym);
  3059. result:=true;
  3060. exit;
  3061. end;
  3062. end;
  3063. end;
  3064. stackitem:=stackitem^.next;
  3065. end;
  3066. srsym:=nil;
  3067. srsymtable:=nil;
  3068. end;
  3069. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  3070. srsymtable:TSymtable;option:tsymoption):boolean;
  3071. begin
  3072. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  3073. end;
  3074. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3075. var
  3076. hashedid : THashedIDString;
  3077. stackitem : psymtablestackitem;
  3078. classh : tobjectdef;
  3079. begin
  3080. result:=false;
  3081. hashedid.id:=s;
  3082. stackitem:=symtablestack.stack;
  3083. while assigned(stackitem) do
  3084. begin
  3085. {
  3086. It is not possible to have type symbols in:
  3087. parameters
  3088. Exception are classes, objects, records, generic definitions and specializations
  3089. that have the parameterized types inserted in the symtable.
  3090. }
  3091. srsymtable:=stackitem^.symtable;
  3092. if (srsymtable.symtabletype=ObjectSymtable) then
  3093. begin
  3094. classh:=tobjectdef(srsymtable.defowner);
  3095. while assigned(classh) do
  3096. begin
  3097. srsymtable:=classh.symtable;
  3098. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3099. if assigned(srsym) and
  3100. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3101. is_visible_for_object(srsym,current_structdef) then
  3102. begin
  3103. addsymref(srsym);
  3104. result:=true;
  3105. exit;
  3106. end;
  3107. classh:=classh.childof;
  3108. end;
  3109. end
  3110. else
  3111. begin
  3112. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3113. if assigned(srsym) and
  3114. (
  3115. not(srsym.typ in [unitsym,namespacesym]) or
  3116. srsymtable.iscurrentunit or
  3117. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  3118. ) and
  3119. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3120. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  3121. begin
  3122. { we need to know if a procedure references symbols
  3123. in the static symtable, because then it can't be
  3124. inlined from outside this unit }
  3125. if assigned(current_procinfo) and
  3126. (srsym.owner.symtabletype=staticsymtable) then
  3127. include(current_procinfo.flags,pi_uses_static_symtable);
  3128. addsymref(srsym);
  3129. result:=true;
  3130. exit;
  3131. end;
  3132. end;
  3133. stackitem:=stackitem^.next;
  3134. end;
  3135. result:=false;
  3136. srsym:=nil;
  3137. srsymtable:=nil;
  3138. end;
  3139. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3140. var
  3141. pmod : tmodule;
  3142. begin
  3143. pmod:=tmodule(pm);
  3144. result:=false;
  3145. if assigned(pmod.globalsymtable) then
  3146. begin
  3147. srsym:=tsym(pmod.globalsymtable.Find(s));
  3148. if assigned(srsym) then
  3149. begin
  3150. srsymtable:=pmod.globalsymtable;
  3151. addsymref(srsym);
  3152. result:=true;
  3153. exit;
  3154. end;
  3155. end;
  3156. { If the module is the current unit we also need
  3157. to search the local symtable }
  3158. if (pmod=current_module) and
  3159. assigned(pmod.localsymtable) then
  3160. begin
  3161. srsym:=tsym(pmod.localsymtable.Find(s));
  3162. if assigned(srsym) then
  3163. begin
  3164. srsymtable:=pmod.localsymtable;
  3165. addsymref(srsym);
  3166. result:=true;
  3167. exit;
  3168. end;
  3169. end;
  3170. srsym:=nil;
  3171. srsymtable:=nil;
  3172. end;
  3173. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  3174. var
  3175. stackitem : psymtablestackitem;
  3176. begin
  3177. result:=false;
  3178. stackitem:=symtablestack.stack;
  3179. while assigned(stackitem) do
  3180. begin
  3181. srsymtable:=stackitem^.symtable;
  3182. if (srsymtable.symtabletype=globalsymtable) and
  3183. (srsymtable.name^=unitname) then
  3184. begin
  3185. srsym:=tsym(srsymtable.find(symname));
  3186. if not assigned(srsym) then
  3187. break;
  3188. result:=true;
  3189. exit;
  3190. end;
  3191. stackitem:=stackitem^.next;
  3192. end;
  3193. { If the module is the current unit we also need
  3194. to search the local symtable }
  3195. if assigned(current_module.localsymtable) and
  3196. (current_module.localsymtable.name^=unitname) then
  3197. begin
  3198. srsymtable:=current_module.localsymtable;
  3199. srsym:=tsym(srsymtable.find(symname));
  3200. if assigned(srsym) then
  3201. begin
  3202. result:=true;
  3203. exit;
  3204. end;
  3205. end;
  3206. end;
  3207. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  3208. begin
  3209. result:=pd;
  3210. if pd.typ<>objectdef then
  3211. exit;
  3212. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  3213. end;
  3214. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  3215. var
  3216. hashedid : THashedIDString;
  3217. stackitem : psymtablestackitem;
  3218. srsymtable : tsymtable;
  3219. srsym : tsym;
  3220. formalname,
  3221. foundname : shortstring;
  3222. formalnameptr,
  3223. foundnameptr: pshortstring;
  3224. begin
  3225. { not a formal definition -> return it }
  3226. if not(oo_is_formal in pd.objectoptions) then
  3227. begin
  3228. result:=pd;
  3229. exit;
  3230. end;
  3231. hashedid.id:=pd.typesym.name;
  3232. stackitem:=symtablestack.stack;
  3233. while assigned(stackitem) do
  3234. begin
  3235. srsymtable:=stackitem^.symtable;
  3236. { ObjC classes can't appear in generics or as nested class
  3237. definitions. Java classes can. }
  3238. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  3239. (is_java_class_or_interface(pd) and
  3240. (srsymtable.symtabletype=ObjectSymtable)) then
  3241. begin
  3242. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3243. if assigned(srsym) and
  3244. (srsym.typ=typesym) and
  3245. (ttypesym(srsym).typedef.typ=objectdef) and
  3246. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  3247. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3248. begin
  3249. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3250. begin
  3251. { the external name for the formal and the real
  3252. definition must match }
  3253. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  3254. assigned(pd.import_lib) then
  3255. begin
  3256. if assigned(pd.import_lib) then
  3257. formalname:=pd.import_lib^+'.'
  3258. else
  3259. formalname:='';
  3260. formalname:=formalname+pd.objextname^;
  3261. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  3262. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  3263. else
  3264. foundname:='';
  3265. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3266. formalnameptr:=@formalname;
  3267. foundnameptr:=@foundname;
  3268. end
  3269. else
  3270. begin
  3271. formalnameptr:=pd.objextname;
  3272. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3273. end;
  3274. if foundnameptr^<>formalnameptr^ then
  3275. begin
  3276. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3277. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3278. end;
  3279. end;
  3280. result:=tobjectdef(ttypesym(srsym).typedef);
  3281. if assigned(current_procinfo) and
  3282. (srsym.owner.symtabletype=staticsymtable) then
  3283. include(current_procinfo.flags,pi_uses_static_symtable);
  3284. addsymref(srsym);
  3285. exit;
  3286. end;
  3287. end;
  3288. stackitem:=stackitem^.next;
  3289. end;
  3290. { nothing found: optionally give an error and return the original
  3291. (empty) one }
  3292. if erroronfailure then
  3293. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3294. result:=pd;
  3295. end;
  3296. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3297. var
  3298. hashedid : THashedIDString;
  3299. orgclass : tobjectdef;
  3300. i : longint;
  3301. begin
  3302. orgclass:=classh;
  3303. { in case this is a formal class, first find the real definition }
  3304. if assigned(classh) then
  3305. begin
  3306. if (oo_is_formal in classh.objectoptions) then
  3307. classh:=find_real_class_definition(classh,true);
  3308. { The contextclassh is used for visibility. The classh must be equal to
  3309. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3310. parent or a class helper. }
  3311. if not (def_is_related(contextclassh,classh) or
  3312. (is_classhelper(contextclassh) and
  3313. assigned(tobjectdef(contextclassh).extendeddef) and
  3314. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3315. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3316. internalerror(200811161);
  3317. end;
  3318. result:=false;
  3319. hashedid.id:=s;
  3320. { an Objective-C protocol or Java interface can inherit from multiple
  3321. other protocols/interfaces -> use ImplementedInterfaces instead }
  3322. if is_objcprotocol(classh) or
  3323. is_javainterface(classh) then
  3324. begin
  3325. srsymtable:=classh.symtable;
  3326. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3327. if assigned(srsym) and
  3328. is_visible_for_object(srsym,contextclassh) then
  3329. begin
  3330. if not (ssf_no_addsymref in flags) then
  3331. addsymref(srsym);
  3332. result:=true;
  3333. exit;
  3334. end;
  3335. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3336. begin
  3337. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3338. begin
  3339. result:=true;
  3340. exit;
  3341. end;
  3342. end;
  3343. end
  3344. else
  3345. if is_objectpascal_helper(classh) then
  3346. begin
  3347. { helpers have their own obscure search logic... }
  3348. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3349. if result then
  3350. exit;
  3351. end
  3352. else
  3353. begin
  3354. while assigned(classh) do
  3355. begin
  3356. { search for a class helper method first if this is an Object
  3357. Pascal class and we haven't yet found a helper symbol }
  3358. if (classh.objecttype in objecttypes_with_helpers) and
  3359. (ssf_search_helper in flags) then
  3360. begin
  3361. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3362. { an eventual overload inside the extended type's hierarchy
  3363. will be found by tcallcandidates }
  3364. if result then
  3365. exit;
  3366. end;
  3367. srsymtable:=classh.symtable;
  3368. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3369. if assigned(srsym) and
  3370. is_visible_for_object(srsym,contextclassh) then
  3371. begin
  3372. if not (ssf_no_addsymref in flags) then
  3373. addsymref(srsym);
  3374. result:=true;
  3375. exit;
  3376. end;
  3377. classh:=classh.childof;
  3378. end;
  3379. end;
  3380. if is_objcclass(orgclass) then
  3381. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3382. else
  3383. begin
  3384. srsym:=nil;
  3385. srsymtable:=nil;
  3386. end;
  3387. end;
  3388. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3389. var
  3390. hashedid : THashedIDString;
  3391. begin
  3392. result:=false;
  3393. hashedid.id:=s;
  3394. { search for a record helper method first }
  3395. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3396. if result then
  3397. { an eventual overload inside the extended type's hierarchy
  3398. will be found by tcallcandidates }
  3399. exit;
  3400. srsymtable:=recordh.symtable;
  3401. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3402. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3403. begin
  3404. addsymref(srsym);
  3405. result:=true;
  3406. exit;
  3407. end;
  3408. srsym:=nil;
  3409. srsymtable:=nil;
  3410. end;
  3411. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3412. var
  3413. def : tdef;
  3414. i : longint;
  3415. begin
  3416. { in case this is a formal class, first find the real definition }
  3417. if assigned(classh) and
  3418. (oo_is_formal in classh.objectoptions) then
  3419. classh:=find_real_class_definition(classh,true);
  3420. result:=false;
  3421. def:=nil;
  3422. while assigned(classh) do
  3423. begin
  3424. for i:=0 to classh.symtable.DefList.Count-1 do
  3425. begin
  3426. def:=tstoreddef(classh.symtable.DefList[i]);
  3427. { Find also all hidden private methods to
  3428. be compatible with delphi, see tw6203 (PFV) }
  3429. if (def.typ=procdef) and
  3430. (po_msgint in tprocdef(def).procoptions) and
  3431. (tprocdef(def).messageinf.i=msgid) then
  3432. begin
  3433. srdef:=def;
  3434. srsym:=tprocdef(def).procsym;
  3435. srsymtable:=classh.symtable;
  3436. addsymref(srsym);
  3437. result:=true;
  3438. exit;
  3439. end;
  3440. end;
  3441. classh:=classh.childof;
  3442. end;
  3443. srdef:=nil;
  3444. srsym:=nil;
  3445. srsymtable:=nil;
  3446. end;
  3447. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3448. var
  3449. def : tdef;
  3450. i : longint;
  3451. begin
  3452. { in case this is a formal class, first find the real definition }
  3453. if assigned(classh) and
  3454. (oo_is_formal in classh.objectoptions) then
  3455. classh:=find_real_class_definition(classh,true);
  3456. result:=false;
  3457. def:=nil;
  3458. while assigned(classh) do
  3459. begin
  3460. for i:=0 to classh.symtable.DefList.Count-1 do
  3461. begin
  3462. def:=tstoreddef(classh.symtable.DefList[i]);
  3463. { Find also all hidden private methods to
  3464. be compatible with delphi, see tw6203 (PFV) }
  3465. if (def.typ=procdef) and
  3466. (po_msgstr in tprocdef(def).procoptions) and
  3467. (tprocdef(def).messageinf.str^=s) then
  3468. begin
  3469. srsym:=tprocdef(def).procsym;
  3470. srsymtable:=classh.symtable;
  3471. addsymref(srsym);
  3472. result:=true;
  3473. exit;
  3474. end;
  3475. end;
  3476. classh:=classh.childof;
  3477. end;
  3478. srsym:=nil;
  3479. srsymtable:=nil;
  3480. end;
  3481. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;forward;
  3482. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3483. var
  3484. hashedid : THashedIDString;
  3485. parentclassh : tobjectdef;
  3486. begin
  3487. result:=false;
  3488. if not is_objectpascal_helper(classh) then
  3489. Internalerror(2011030101);
  3490. hashedid.id:=s;
  3491. { in a helper things are a bit more complex:
  3492. 1. search the symbol in the helper (if not "inherited")
  3493. 2. search the symbol in the extended type
  3494. 3. search the symbol in the parent helpers
  3495. 4. only classes: search the symbol in the parents of the extended type
  3496. }
  3497. if not (ssf_has_inherited in flags) then
  3498. begin
  3499. { search in the helper itself }
  3500. srsymtable:=classh.symtable;
  3501. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3502. if assigned(srsym) and
  3503. is_visible_for_object(srsym,contextclassh) then
  3504. begin
  3505. if not (ssf_no_addsymref in flags) then
  3506. addsymref(srsym);
  3507. result:=true;
  3508. exit;
  3509. end;
  3510. end;
  3511. { now search in the extended type itself }
  3512. { Note: the extendeddef might be Nil if we are currently parsing the
  3513. extended type itself and the identifier was not found }
  3514. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3515. begin
  3516. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3517. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3518. if assigned(srsym) and
  3519. is_visible_for_object(srsym,contextclassh) then
  3520. begin
  3521. if not (ssf_no_addsymref in flags) then
  3522. addsymref(srsym);
  3523. result:=true;
  3524. exit;
  3525. end;
  3526. end;
  3527. { now search in the parent helpers }
  3528. parentclassh:=classh.childof;
  3529. while assigned(parentclassh) do
  3530. begin
  3531. srsymtable:=parentclassh.symtable;
  3532. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3533. if assigned(srsym) and
  3534. is_visible_for_object(srsym,contextclassh) then
  3535. begin
  3536. if not (ssf_no_addsymref in flags) then
  3537. addsymref(srsym);
  3538. result:=true;
  3539. exit;
  3540. end;
  3541. parentclassh:=parentclassh.childof;
  3542. end;
  3543. { now search in the parents of the extended class (with helpers!) }
  3544. if is_class(classh.extendeddef) then
  3545. begin
  3546. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3547. { addsymref is already called by searchsym_in_class }
  3548. if result then
  3549. exit;
  3550. end;
  3551. { now search all helpers using the extendeddef as the starting point }
  3552. if m_multi_helpers in current_settings.modeswitches then
  3553. result:=search_best_objectpascal_helper(s,classh.extendeddef,contextclassh,srsym,srsymtable);
  3554. end;
  3555. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3556. var
  3557. sym : Tprocsym;
  3558. hashedid : THashedIDString;
  3559. curreq,
  3560. besteq : tequaltype;
  3561. currpd,
  3562. bestpd : tprocdef;
  3563. stackitem : psymtablestackitem;
  3564. begin
  3565. hashedid.id:=overloaded_names[assignment_type];
  3566. besteq:=te_incompatible;
  3567. bestpd:=nil;
  3568. stackitem:=symtablestack.stack;
  3569. while assigned(stackitem) do
  3570. begin
  3571. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3572. if sym<>nil then
  3573. begin
  3574. if sym.typ<>procsym then
  3575. internalerror(200402031);
  3576. { if the source type is an alias then this is only the second choice,
  3577. if you mess with this code, check tw4093 }
  3578. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  3579. if curreq>besteq then
  3580. begin
  3581. besteq:=curreq;
  3582. bestpd:=currpd;
  3583. if (besteq=te_exact) then
  3584. break;
  3585. end;
  3586. end;
  3587. stackitem:=stackitem^.next;
  3588. end;
  3589. result:=bestpd;
  3590. end;
  3591. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3592. begin
  3593. { search record/object symtable first for a suitable operator }
  3594. if from_def.typ in [recorddef,objectdef] then
  3595. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3596. if to_def.typ in [recorddef,objectdef] then
  3597. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3598. { if type conversion is explicit then search first for explicit
  3599. operator overload and if not found then use implicit operator }
  3600. if explicit then
  3601. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3602. else
  3603. result:=nil;
  3604. if result=nil then
  3605. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3606. { restore symtable stack }
  3607. if to_def.typ in [recorddef,objectdef] then
  3608. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3609. if from_def.typ in [recorddef,objectdef] then
  3610. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3611. end;
  3612. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3613. var
  3614. sym : Tprocsym;
  3615. hashedid : THashedIDString;
  3616. curreq,
  3617. besteq : tequaltype;
  3618. currpd,
  3619. bestpd : tprocdef;
  3620. stackitem : psymtablestackitem;
  3621. begin
  3622. hashedid.id:='enumerator';
  3623. besteq:=te_incompatible;
  3624. bestpd:=nil;
  3625. stackitem:=symtablestack.stack;
  3626. while assigned(stackitem) do
  3627. begin
  3628. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3629. if sym<>nil then
  3630. begin
  3631. if sym.typ<>procsym then
  3632. internalerror(200910241);
  3633. { if the source type is an alias then this is only the second choice,
  3634. if you mess with this code, check tw4093 }
  3635. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3636. if curreq>besteq then
  3637. begin
  3638. besteq:=curreq;
  3639. bestpd:=currpd;
  3640. if (besteq=te_exact) then
  3641. break;
  3642. end;
  3643. end;
  3644. stackitem:=stackitem^.next;
  3645. end;
  3646. result:=bestpd;
  3647. end;
  3648. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  3649. var
  3650. sym : Tprocsym;
  3651. hashedid : THashedIDString;
  3652. optoken: ttoken;
  3653. begin
  3654. optoken := managementoperator2tok[mop];
  3655. if (optoken<first_managment_operator) or
  3656. (optoken>last_managment_operator) then
  3657. internalerror(201602280);
  3658. hashedid.id:=overloaded_names[optoken];
  3659. if not (pd.typ in [recorddef]) then
  3660. internalerror(201602281);
  3661. sym:=Tprocsym(tabstractrecorddef(pd).symtable.FindWithHash(hashedid));
  3662. if sym<>nil then
  3663. begin
  3664. if sym.typ<>procsym then
  3665. internalerror(201602282);
  3666. result:=sym.find_procdef_bytype(potype_operator);
  3667. end
  3668. else
  3669. result:=nil;
  3670. end;
  3671. function search_system_type(const s: TIDString): ttypesym;
  3672. var
  3673. sym : tsym;
  3674. begin
  3675. sym:=tsym(systemunit.Find(s));
  3676. if not assigned(sym) or
  3677. (sym.typ<>typesym) then
  3678. message1(cg_f_unknown_system_type,s);
  3679. result:=ttypesym(sym);
  3680. end;
  3681. function try_search_system_type(const s: TIDString): ttypesym;
  3682. var
  3683. sym : tsym;
  3684. begin
  3685. sym:=tsym(systemunit.Find(s));
  3686. if not assigned(sym) then
  3687. result:=nil
  3688. else
  3689. begin
  3690. if sym.typ<>typesym then
  3691. message1(cg_f_unknown_system_type,s);
  3692. result:=ttypesym(sym);
  3693. end;
  3694. end;
  3695. function try_search_current_module_type(const s: TIDString): ttypesym;
  3696. var
  3697. found: boolean;
  3698. srsymtable: tsymtable;
  3699. srsym: tsym;
  3700. begin
  3701. if s[1]='$' then
  3702. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  3703. else
  3704. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  3705. if found then
  3706. begin
  3707. if (srsym.typ<>typesym) then
  3708. internalerror(2014091207);
  3709. result:=ttypesym(srsym);
  3710. end
  3711. else
  3712. result:=nil;
  3713. end;
  3714. function search_system_proc(const s: TIDString): tprocdef;
  3715. var
  3716. srsym: tsym;
  3717. begin
  3718. srsym:=tsym(systemunit.find(s));
  3719. if not assigned(srsym) and
  3720. (cs_compilesystem in current_settings.moduleswitches) then
  3721. srsym:=tsym(systemunit.Find(upper(s)));
  3722. if not assigned(srsym) or
  3723. (srsym.typ<>procsym) then
  3724. message1(cg_f_unknown_compilerproc,s);
  3725. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3726. end;
  3727. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  3728. var
  3729. srsymtable: tsymtable;
  3730. sym: tsym;
  3731. begin
  3732. sym:=nil;
  3733. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  3734. (sym.typ=typesym) then
  3735. begin
  3736. result:=ttypesym(sym);
  3737. exit;
  3738. end
  3739. else
  3740. begin
  3741. if throwerror then
  3742. message2(cg_f_unknown_type_in_unit,typename,unitname);
  3743. result:=nil;
  3744. end;
  3745. end;
  3746. function search_sym_in_helperdef(const s: string;classh : tobjectdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable): boolean;
  3747. var
  3748. hashedid : THashedIDString;
  3749. pdef : tprocdef;
  3750. i : integer;
  3751. begin
  3752. hashedid.id:=s;
  3753. result:=false;
  3754. repeat
  3755. srsymtable:=classh.symtable;
  3756. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3757. if srsym<>nil then
  3758. begin
  3759. case srsym.typ of
  3760. procsym:
  3761. begin
  3762. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3763. begin
  3764. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  3765. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  3766. continue;
  3767. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3768. srsymtable:=srsym.owner;
  3769. result:=true;
  3770. exit;
  3771. end;
  3772. end;
  3773. typesym,
  3774. fieldvarsym,
  3775. constsym,
  3776. enumsym,
  3777. undefinedsym,
  3778. propertysym:
  3779. begin
  3780. result:=true;
  3781. exit;
  3782. end;
  3783. else
  3784. internalerror(2014041101);
  3785. end;
  3786. end;
  3787. { try the helper parent if available }
  3788. classh:=classh.childof;
  3789. until classh=nil;
  3790. end;
  3791. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  3792. var
  3793. s : string;
  3794. st : tsymtable;
  3795. begin
  3796. result:=nil;
  3797. { when there are no helpers active currently then we don't need to do
  3798. anything }
  3799. if current_module.extendeddefs.count=0 then
  3800. exit;
  3801. { no helpers for anonymous types }
  3802. if ((pd.typ in [recorddef,objectdef]) and
  3803. (
  3804. not assigned(tabstractrecorddef(pd).objrealname) or
  3805. (tabstractrecorddef(pd).objrealname^='')
  3806. )
  3807. ) or
  3808. not assigned(pd.typesym) then
  3809. exit;
  3810. { if pd is defined inside a procedure we must not use make_mangledname
  3811. (as a helper may not be defined in a procedure this is no problem...)}
  3812. st:=pd.owner;
  3813. while st.symtabletype in [objectsymtable,recordsymtable] do
  3814. st:=st.defowner.owner;
  3815. if st.symtabletype=localsymtable then
  3816. exit;
  3817. { the mangled name is used as the key for tmodule.extendeddefs }
  3818. s:=generate_objectpascal_helper_key(pd);
  3819. result:=TFPObjectList(current_module.extendeddefs.Find(s));
  3820. end;
  3821. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;
  3822. var
  3823. s : string;
  3824. list : TFPObjectList;
  3825. i : integer;
  3826. st : tsymtable;
  3827. odef : tobjectdef;
  3828. begin
  3829. result:=false;
  3830. list:=get_objectpascal_helpers(pd);
  3831. if assigned(list) and (list.count>0) then
  3832. begin
  3833. i:=list.count-1;
  3834. repeat
  3835. odef:=tobjectdef(list[i]);
  3836. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  3837. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  3838. if result then
  3839. result:=search_sym_in_helperdef(name,odef,contextclassh,srsym,srsymtable);
  3840. dec(i);
  3841. until result or (i<0);
  3842. end;
  3843. end;
  3844. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  3845. var
  3846. s : string;
  3847. list : TFPObjectList;
  3848. i : integer;
  3849. begin
  3850. result:=false;
  3851. odef:=nil;
  3852. list:=get_objectpascal_helpers(pd);
  3853. if assigned(list) and (list.count>0) then
  3854. begin
  3855. i:=list.count-1;
  3856. repeat
  3857. odef:=tobjectdef(list[list.count-1]);
  3858. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  3859. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  3860. dec(i);
  3861. until result or (i<0);
  3862. if not result then
  3863. { just to be sure that noone uses odef }
  3864. odef:=nil;
  3865. end;
  3866. end;
  3867. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3868. var
  3869. classh : tobjectdef;
  3870. begin
  3871. result:=false;
  3872. { if there is no class helper for the class then there is no need to
  3873. search further }
  3874. if m_multi_helpers in current_settings.modeswitches then
  3875. result:=search_best_objectpascal_helper(s,pd,contextclassh,srsym,srsymtable)
  3876. else
  3877. begin
  3878. if search_last_objectpascal_helper(pd,contextclassh,classh) and
  3879. search_sym_in_helperdef(s,classh,contextclassh,srsym,srsymtable) then
  3880. result:=true;
  3881. end;
  3882. if result then
  3883. begin
  3884. { we need to know if a procedure references symbols
  3885. in the static symtable, because then it can't be
  3886. inlined from outside this unit }
  3887. if (srsym.typ=procsym) and
  3888. assigned(current_procinfo) and
  3889. (srsym.owner.symtabletype=staticsymtable) then
  3890. include(current_procinfo.flags,pi_uses_static_symtable);
  3891. addsymref(srsym);
  3892. end
  3893. else
  3894. begin
  3895. srsym:=nil;
  3896. srsymtable:=nil;
  3897. end;
  3898. end;
  3899. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3900. var
  3901. hashedid : THashedIDString;
  3902. stackitem : psymtablestackitem;
  3903. i : longint;
  3904. defowner : tobjectdef;
  3905. begin
  3906. hashedid.id:=class_helper_prefix+s;
  3907. stackitem:=symtablestack.stack;
  3908. while assigned(stackitem) do
  3909. begin
  3910. srsymtable:=stackitem^.symtable;
  3911. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3912. if assigned(srsym) then
  3913. begin
  3914. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3915. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3916. (srsym.typ<>procsym) then
  3917. internalerror(2009111505);
  3918. { check whether this procsym includes a helper for this particular class }
  3919. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3920. begin
  3921. { does pd inherit from (or is the same as) the class
  3922. that this method's category extended?
  3923. Warning: this list contains both category and objcclass methods
  3924. (for id.randommethod), so only check category methods here
  3925. }
  3926. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  3927. if is_objccategory(defowner) and
  3928. def_is_related(pd,defowner.childof) then
  3929. begin
  3930. { we need to know if a procedure references symbols
  3931. in the static symtable, because then it can't be
  3932. inlined from outside this unit }
  3933. if assigned(current_procinfo) and
  3934. (srsym.owner.symtabletype=staticsymtable) then
  3935. include(current_procinfo.flags,pi_uses_static_symtable);
  3936. { no need to keep looking. There might be other
  3937. categories that extend this, a parent or child
  3938. class with a method with the same name (either
  3939. overriding this one, or overridden by this one),
  3940. but that doesn't matter as far as the basic
  3941. procsym is concerned.
  3942. }
  3943. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3944. srsymtable:=srsym.owner;
  3945. addsymref(srsym);
  3946. result:=true;
  3947. exit;
  3948. end;
  3949. end;
  3950. end;
  3951. stackitem:=stackitem^.next;
  3952. end;
  3953. srsym:=nil;
  3954. srsymtable:=nil;
  3955. result:=false;
  3956. end;
  3957. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3958. var
  3959. hashedid : THashedIDString;
  3960. stackitem : psymtablestackitem;
  3961. i : longint;
  3962. begin
  3963. hashedid.id:=class_helper_prefix+s;
  3964. stackitem:=symtablestack.stack;
  3965. while assigned(stackitem) do
  3966. begin
  3967. srsymtable:=stackitem^.symtable;
  3968. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3969. if assigned(srsym) then
  3970. begin
  3971. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3972. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3973. (srsym.typ<>procsym) then
  3974. internalerror(2009112005);
  3975. { check whether this procsym includes a helper for this particular class }
  3976. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3977. begin
  3978. { we need to know if a procedure references symbols
  3979. in the static symtable, because then it can't be
  3980. inlined from outside this unit }
  3981. if assigned(current_procinfo) and
  3982. (srsym.owner.symtabletype=staticsymtable) then
  3983. include(current_procinfo.flags,pi_uses_static_symtable);
  3984. { no need to keep looking. There might be other
  3985. methods with the same name, but that doesn't matter
  3986. as far as the basic procsym is concerned.
  3987. }
  3988. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3989. { We need the symtable in which the classhelper-like sym
  3990. is located, not the objectdef. The reason is that the
  3991. callnode will climb the symtablestack until it encounters
  3992. this symtable to start looking for overloads (and it won't
  3993. find the objectsymtable in which this method sym is
  3994. located
  3995. srsymtable:=srsym.owner;
  3996. }
  3997. addsymref(srsym);
  3998. result:=true;
  3999. exit;
  4000. end;
  4001. end;
  4002. stackitem:=stackitem^.next;
  4003. end;
  4004. srsym:=nil;
  4005. srsymtable:=nil;
  4006. result:=false;
  4007. end;
  4008. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  4009. { searches n in symtable of pd and all anchestors }
  4010. var
  4011. srsymtable : tsymtable;
  4012. begin
  4013. { in case this is a formal class, first find the real definition }
  4014. if (oo_is_formal in pd.objectoptions) then
  4015. pd:=find_real_class_definition(tobjectdef(pd),true);
  4016. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  4017. exit;
  4018. result:=search_struct_member_no_helper(pd,s);
  4019. if assigned(result) then
  4020. exit;
  4021. { not found, now look for class helpers }
  4022. if is_objcclass(pd) then
  4023. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  4024. end;
  4025. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  4026. var
  4027. hashedid : THashedIDString;
  4028. srsym : tsym;
  4029. begin
  4030. hashedid.id:=s;
  4031. while assigned(pd) do
  4032. begin
  4033. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  4034. if assigned(srsym) then
  4035. begin
  4036. result:=srsym;
  4037. exit;
  4038. end;
  4039. if pd.typ=objectdef then
  4040. pd:=tobjectdef(pd).childof
  4041. else
  4042. pd:=nil;
  4043. end;
  4044. result:=nil;
  4045. end;
  4046. function search_macro(const s : string):tsym;
  4047. var
  4048. stackitem : psymtablestackitem;
  4049. hashedid : THashedIDString;
  4050. srsym : tsym;
  4051. begin
  4052. hashedid.id:=s;
  4053. { First search the localmacrosymtable before searching the
  4054. global macrosymtables from the units }
  4055. if assigned(current_module) then
  4056. begin
  4057. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  4058. if assigned(srsym) then
  4059. begin
  4060. result:= srsym;
  4061. exit;
  4062. end;
  4063. end;
  4064. stackitem:=macrosymtablestack.stack;
  4065. while assigned(stackitem) do
  4066. begin
  4067. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  4068. if assigned(srsym) then
  4069. begin
  4070. result:= srsym;
  4071. exit;
  4072. end;
  4073. stackitem:=stackitem^.next;
  4074. end;
  4075. result:= nil;
  4076. end;
  4077. function defined_macro(const s : string):boolean;
  4078. var
  4079. mac: tmacro;
  4080. begin
  4081. mac:=tmacro(search_macro(s));
  4082. if assigned(mac) then
  4083. begin
  4084. mac.is_used:=true;
  4085. defined_macro:=mac.defined;
  4086. end
  4087. else
  4088. defined_macro:=false;
  4089. end;
  4090. {****************************************************************************
  4091. Object Helpers
  4092. ****************************************************************************}
  4093. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  4094. { returns the default property of a class, searches also anchestors }
  4095. var
  4096. _defaultprop : tpropertysym;
  4097. helperpd : tobjectdef;
  4098. begin
  4099. _defaultprop:=nil;
  4100. { first search in helper's hierarchy }
  4101. if search_last_objectpascal_helper(pd,nil,helperpd) then
  4102. while assigned(helperpd) do
  4103. begin
  4104. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  4105. if assigned(_defaultprop) then
  4106. break;
  4107. helperpd:=helperpd.childof;
  4108. end;
  4109. if assigned(_defaultprop) then
  4110. begin
  4111. search_default_property:=_defaultprop;
  4112. exit;
  4113. end;
  4114. { now search in the type's hierarchy itself }
  4115. while assigned(pd) do
  4116. begin
  4117. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  4118. if assigned(_defaultprop) then
  4119. break;
  4120. if (pd.typ=objectdef) then
  4121. pd:=tobjectdef(pd).childof
  4122. else
  4123. break;
  4124. end;
  4125. search_default_property:=_defaultprop;
  4126. end;
  4127. {****************************************************************************
  4128. Macro Helpers
  4129. ****************************************************************************}
  4130. procedure def_system_macro(const name : string);
  4131. var
  4132. mac : tmacro;
  4133. s: string;
  4134. begin
  4135. if name = '' then
  4136. internalerror(2004121202);
  4137. s:= upper(name);
  4138. mac:=tmacro(search_macro(s));
  4139. if not assigned(mac) then
  4140. begin
  4141. mac:=tmacro.create(s);
  4142. if assigned(current_module) then
  4143. current_module.localmacrosymtable.insert(mac)
  4144. else
  4145. initialmacrosymtable.insert(mac);
  4146. end;
  4147. Message1(parser_c_macro_defined,mac.name);
  4148. mac.defined:=true;
  4149. end;
  4150. procedure set_system_macro(const name, value : string);
  4151. var
  4152. mac : tmacro;
  4153. s: string;
  4154. begin
  4155. if name = '' then
  4156. internalerror(2004121203);
  4157. s:= upper(name);
  4158. mac:=tmacro(search_macro(s));
  4159. if not assigned(mac) then
  4160. begin
  4161. mac:=tmacro.create(s);
  4162. if assigned(current_module) then
  4163. current_module.localmacrosymtable.insert(mac)
  4164. else
  4165. initialmacrosymtable.insert(mac);
  4166. end
  4167. else
  4168. begin
  4169. mac.is_compiler_var:=false;
  4170. if assigned(mac.buftext) then
  4171. freemem(mac.buftext,mac.buflen);
  4172. end;
  4173. Message2(parser_c_macro_set_to,mac.name,value);
  4174. mac.buflen:=length(value);
  4175. getmem(mac.buftext,mac.buflen);
  4176. move(value[1],mac.buftext^,mac.buflen);
  4177. mac.defined:=true;
  4178. end;
  4179. procedure set_system_compvar(const name, value : string);
  4180. var
  4181. mac : tmacro;
  4182. s: string;
  4183. begin
  4184. if name = '' then
  4185. internalerror(2004121204);
  4186. s:= upper(name);
  4187. mac:=tmacro(search_macro(s));
  4188. if not assigned(mac) then
  4189. begin
  4190. mac:=tmacro.create(s);
  4191. mac.is_compiler_var:=true;
  4192. if assigned(current_module) then
  4193. current_module.localmacrosymtable.insert(mac)
  4194. else
  4195. initialmacrosymtable.insert(mac);
  4196. end
  4197. else
  4198. begin
  4199. mac.is_compiler_var:=true;
  4200. if assigned(mac.buftext) then
  4201. freemem(mac.buftext,mac.buflen);
  4202. end;
  4203. Message2(parser_c_macro_set_to,mac.name,value);
  4204. mac.buflen:=length(value);
  4205. getmem(mac.buftext,mac.buflen);
  4206. move(value[1],mac.buftext^,mac.buflen);
  4207. mac.defined:=true;
  4208. end;
  4209. procedure undef_system_macro(const name : string);
  4210. var
  4211. mac : tmacro;
  4212. s: string;
  4213. begin
  4214. if name = '' then
  4215. internalerror(2004121205);
  4216. s:= upper(name);
  4217. mac:=tmacro(search_macro(s));
  4218. if not assigned(mac) then
  4219. {If not found, then it's already undefined.}
  4220. else
  4221. begin
  4222. Message1(parser_c_macro_undefined,mac.name);
  4223. mac.defined:=false;
  4224. mac.is_compiler_var:=false;
  4225. { delete old definition }
  4226. if assigned(mac.buftext) then
  4227. begin
  4228. freemem(mac.buftext,mac.buflen);
  4229. mac.buftext:=nil;
  4230. end;
  4231. end;
  4232. end;
  4233. {$ifdef UNITALIASES}
  4234. {****************************************************************************
  4235. TUNIT_ALIAS
  4236. ****************************************************************************}
  4237. constructor tunit_alias.create(const n:string);
  4238. var
  4239. i : longint;
  4240. begin
  4241. i:=pos('=',n);
  4242. if i=0 then
  4243. fail;
  4244. inherited createname(Copy(n,1,i-1));
  4245. newname:=stringdup(Copy(n,i+1,255));
  4246. end;
  4247. destructor tunit_alias.destroy;
  4248. begin
  4249. stringdispose(newname);
  4250. inherited destroy;
  4251. end;
  4252. procedure addunitalias(const n:string);
  4253. begin
  4254. unitaliases^.insert(tunit_alias,init(Upper(n))));
  4255. end;
  4256. function getunitalias(const n:string):string;
  4257. var
  4258. p : punit_alias;
  4259. begin
  4260. p:=punit_alias(unitaliases^.Find(Upper(n)));
  4261. if assigned(p) then
  4262. getunitalias:=punit_alias(p).newname^
  4263. else
  4264. getunitalias:=n;
  4265. end;
  4266. {$endif UNITALIASES}
  4267. {****************************************************************************
  4268. Init/Done Symtable
  4269. ****************************************************************************}
  4270. procedure InitSymtable;
  4271. begin
  4272. { Reset symbolstack }
  4273. symtablestack:=nil;
  4274. systemunit:=nil;
  4275. { create error syms and def }
  4276. generrorsym:=terrorsym.create;
  4277. generrordef:=cerrordef.create;
  4278. { macros }
  4279. initialmacrosymtable:=tmacrosymtable.create(false);
  4280. macrosymtablestack:=TSymtablestack.create;
  4281. macrosymtablestack.push(initialmacrosymtable);
  4282. {$ifdef UNITALIASES}
  4283. { unit aliases }
  4284. unitaliases:=TFPHashObjectList.create;
  4285. {$endif}
  4286. { set some global vars to nil, might be important for the ide }
  4287. class_tobject:=nil;
  4288. interface_iunknown:=nil;
  4289. interface_idispatch:=nil;
  4290. rec_tguid:=nil;
  4291. rec_jmp_buf:=nil;
  4292. rec_exceptaddr:=nil;
  4293. objc_metaclasstype:=nil;
  4294. objc_superclasstype:=nil;
  4295. objc_idtype:=nil;
  4296. objc_seltype:=nil;
  4297. objc_objecttype:=nil;
  4298. dupnr:=0;
  4299. end;
  4300. procedure DoneSymtable;
  4301. begin
  4302. generrorsym.owner:=nil;
  4303. generrorsym.free;
  4304. generrordef.owner:=nil;
  4305. generrordef.free;
  4306. initialmacrosymtable.free;
  4307. macrosymtablestack.free;
  4308. {$ifdef UNITALIASES}
  4309. unitaliases.free;
  4310. {$endif}
  4311. end;
  4312. end.