symtable.pas 180 KB

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