symtable.pas 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745
  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. def:=generrordef;
  1449. { If a record contains a union, it does not contain a "single
  1450. non-composite field" in the context of certain ABIs requiring
  1451. special treatment for such records }
  1452. if (defowner.typ=recorddef) and
  1453. trecorddef(defowner).isunion then
  1454. exit;
  1455. { a record/object can contain other things than fields }
  1456. currentsymlist:=symlist;
  1457. { recurse in arrays and records }
  1458. repeat
  1459. sym:=nil;
  1460. { record has one field? }
  1461. for i:=0 to currentsymlist.Count-1 do
  1462. begin
  1463. if (tsym(currentsymlist[i]).typ=fieldvarsym) and
  1464. not(sp_static in tsym(currentsymlist[i]).symoptions) then
  1465. begin
  1466. if result then
  1467. begin
  1468. result:=false;
  1469. exit;
  1470. end;
  1471. result:=true;
  1472. sym:=tfieldvarsym(currentsymlist[i])
  1473. end;
  1474. end;
  1475. if assigned(sym) then
  1476. begin
  1477. { if the field is an array, does it contain one element? }
  1478. currentdef:=sym.vardef;
  1479. while (currentdef.typ=arraydef) and
  1480. not is_special_array(currentdef) do
  1481. begin
  1482. if tarraydef(currentdef).elecount<>1 then
  1483. begin
  1484. result:=false;
  1485. exit;
  1486. end;
  1487. currentdef:=tarraydef(currentdef).elementdef;
  1488. end;
  1489. { if the array element is again a record, continue descending }
  1490. if currentdef.typ=recorddef then
  1491. begin
  1492. { the record might be empty, so reset the result until we've
  1493. really found something }
  1494. result:=false;
  1495. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1496. end
  1497. else
  1498. begin
  1499. { otherwise we found the type of the single element }
  1500. def:=currentdef;
  1501. exit;
  1502. end;
  1503. end
  1504. else
  1505. exit
  1506. until false;
  1507. end;
  1508. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1509. begin
  1510. result:=defowner.owner;
  1511. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1512. result:=result.defowner.owner;
  1513. end;
  1514. procedure tabstractrecordsymtable.do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  1515. var
  1516. sym : tsym absolute data;
  1517. fsym : tfieldvarsym absolute data;
  1518. mop : tmanagementoperator;
  1519. entry : pmanagementoperator_offset_entry;
  1520. sublist : tfplist;
  1521. i : longint;
  1522. begin
  1523. if sym.typ<>fieldvarsym then
  1524. exit;
  1525. if not is_record(fsym.vardef) and not is_object(fsym.vardef) and not is_cppclass(fsym.vardef) then
  1526. exit;
  1527. mop:=tmanagementoperator(ptruint(arg));
  1528. if not assigned(mop_list[mop]) then
  1529. internalerror(2018082303);
  1530. if is_record(fsym.vardef) then
  1531. begin
  1532. if mop in trecordsymtable(trecorddef(fsym.vardef).symtable).managementoperators then
  1533. begin
  1534. new(entry);
  1535. entry^.pd:=search_management_operator(mop,fsym.vardef);
  1536. if not assigned(entry^.pd) then
  1537. internalerror(2018082302);
  1538. entry^.offset:=fsym.fieldoffset;
  1539. mop_list[mop].add(entry);
  1540. end;
  1541. end;
  1542. sublist:=tfplist.create;
  1543. tabstractrecordsymtable(tabstractrecorddef(fsym.vardef).symtable).get_managementoperator_offset_list(mop,sublist);
  1544. for i:=0 to sublist.count-1 do
  1545. begin
  1546. entry:=pmanagementoperator_offset_entry(sublist[i]);
  1547. entry^.offset:=entry^.offset+fsym.fieldoffset;
  1548. mop_list[mop].add(entry);
  1549. end;
  1550. { we don't need to remove the entries as they become part of list }
  1551. sublist.free;
  1552. end;
  1553. procedure tabstractrecordsymtable.get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  1554. var
  1555. i : longint;
  1556. entry,entrycopy : pmanagementoperator_offset_entry;
  1557. begin
  1558. if not assigned(list) then
  1559. internalerror(2018082301);
  1560. if mop=mop_none then
  1561. exit;
  1562. if not (mop in has_fields_with_mop) then
  1563. { none of the fields or one of the field's fields has the requested operator }
  1564. exit;
  1565. if not assigned(mop_list[mop]) then
  1566. begin
  1567. mop_list[mop]:=tfplist.create;
  1568. SymList.ForEachCall(@do_get_managementoperator_offset_list,pointer(ptruint(mop)));
  1569. end;
  1570. for i:=0 to mop_list[mop].count-1 do
  1571. begin
  1572. entry:=pmanagementoperator_offset_entry(mop_list[mop][i]);
  1573. New(entrycopy);
  1574. entrycopy^:=entry^;
  1575. list.add(entrycopy);
  1576. end;
  1577. end;
  1578. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1579. begin
  1580. _datasize:=val;
  1581. if (usefieldalignment=bit_alignment) then
  1582. { can overflow in non bitpacked records }
  1583. databitsize:=val*8;
  1584. end;
  1585. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1586. var
  1587. l : asizeint;
  1588. varalignfield,
  1589. varalign : shortint;
  1590. vardef : tdef;
  1591. begin
  1592. { Calculate field offset }
  1593. l:=sym.getsize;
  1594. vardef:=sym.vardef;
  1595. varalign:=vardef.structalignment;
  1596. case usefieldalignment of
  1597. bit_alignment:
  1598. { has to be handled separately }
  1599. internalerror(2012071301);
  1600. C_alignment:
  1601. begin
  1602. { Calc the alignment size for C style records }
  1603. if (varalign>4) and
  1604. ((varalign mod 4)<>0) and
  1605. (vardef.typ=arraydef) then
  1606. Message1(sym_w_wrong_C_pack,vardef.typename);
  1607. if varalign=0 then
  1608. varalign:=l;
  1609. if (globalfieldalignment<maxCrecordalign) then
  1610. begin
  1611. if (varalign>16) and (globalfieldalignment<32) then
  1612. globalfieldalignment:=32
  1613. else if (varalign>12) and (globalfieldalignment<16) then
  1614. globalfieldalignment:=16
  1615. { 12 is needed for long double }
  1616. else if (varalign>8) and (globalfieldalignment<12) then
  1617. globalfieldalignment:=12
  1618. else if (varalign>4) and (globalfieldalignment<8) then
  1619. globalfieldalignment:=8
  1620. else if (varalign>2) and (globalfieldalignment<4) then
  1621. globalfieldalignment:=4
  1622. else if (varalign>1) and (globalfieldalignment<2) then
  1623. globalfieldalignment:=2;
  1624. end;
  1625. globalfieldalignment:=min(globalfieldalignment,maxCrecordalign);
  1626. end;
  1627. mac68k_alignment:
  1628. begin
  1629. { mac68k alignment (C description):
  1630. * char is aligned to 1 byte
  1631. * everything else (except vector) is aligned to 2 bytes
  1632. * vector is aligned to 16 bytes
  1633. }
  1634. if l>1 then
  1635. globalfieldalignment:=2
  1636. else
  1637. globalfieldalignment:=1;
  1638. varalign:=2;
  1639. end;
  1640. end;
  1641. if varalign=0 then
  1642. varalign:=size_2_align(l);
  1643. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1644. result:=align(base,varalignfield);
  1645. end;
  1646. function tabstractrecordsymtable.iscurrentunit: boolean;
  1647. begin
  1648. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1649. end;
  1650. {****************************************************************************
  1651. TRecordSymtable
  1652. ****************************************************************************}
  1653. constructor trecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1654. begin
  1655. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1656. symtabletype:=recordsymtable;
  1657. end;
  1658. { this procedure is reserved for inserting case variant into
  1659. a record symtable }
  1660. { the offset is the location of the start of the variant
  1661. and datasize and dataalignment corresponds to
  1662. the complete size (see code in pdecl unit) PM }
  1663. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : asizeint);
  1664. var
  1665. sym : tsym;
  1666. def : tdef;
  1667. i : integer;
  1668. varalignrecord,varalign,
  1669. storesize,storealign : asizeint;
  1670. bitsize: tcgint;
  1671. begin
  1672. storesize:=_datasize;
  1673. storealign:=fieldalignment;
  1674. _datasize:=offset;
  1675. if (usefieldalignment=bit_alignment) then
  1676. databitsize:=offset*8;
  1677. { We move the ownership of the defs and symbols to the new recordsymtable.
  1678. The old unionsymtable keeps the references, but doesn't own the
  1679. objects anymore }
  1680. unionst.DefList.OwnsObjects:=false;
  1681. unionst.SymList.OwnsObjects:=false;
  1682. { copy symbols }
  1683. for i:=0 to unionst.SymList.Count-1 do
  1684. begin
  1685. sym:=TSym(unionst.SymList[i]);
  1686. if sym.typ<>fieldvarsym then
  1687. internalerror(200601272);
  1688. if tfieldvarsym(sym).fieldoffset=0 then
  1689. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1690. { add to this record symtable, checking for duplicate names }
  1691. // unionst.SymList.List.List^[i].Data:=nil;
  1692. insert(sym);
  1693. varalign:=tfieldvarsym(sym).vardef.alignment;
  1694. if varalign=0 then
  1695. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1696. { retrieve size }
  1697. if (usefieldalignment=bit_alignment) then
  1698. begin
  1699. { bit packed records are limited to high(aint) bits }
  1700. { instead of bytes to avoid double precision }
  1701. { arithmetic in offset calculations }
  1702. if is_ordinal(tfieldvarsym(sym).vardef) then
  1703. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1704. else
  1705. begin
  1706. bitsize:=tfieldvarsym(sym).getsize;
  1707. if (bitsize>high(asizeint) div 8) then
  1708. Message(sym_e_segment_too_large);
  1709. bitsize:=bitsize*8;
  1710. end;
  1711. if bitsize>high(asizeint)-databitsize then
  1712. begin
  1713. Message(sym_e_segment_too_large);
  1714. _datasize:=high(asizeint);
  1715. databitsize:=high(asizeint);
  1716. end
  1717. else
  1718. begin
  1719. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1720. _datasize:=(databitsize+7) div 8;
  1721. end;
  1722. tfieldvarsym(sym).fieldoffset:=databitsize;
  1723. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1724. end
  1725. else
  1726. begin
  1727. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1728. begin
  1729. Message(sym_e_segment_too_large);
  1730. _datasize:=high(asizeint);
  1731. end
  1732. else
  1733. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1734. { update address }
  1735. tfieldvarsym(sym).fieldoffset:=_datasize;
  1736. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1737. end;
  1738. { update alignment of this record }
  1739. if (usefieldalignment<>C_alignment) and
  1740. (usefieldalignment<>mac68k_alignment) then
  1741. recordalignment:=max(recordalignment,varalignrecord);
  1742. end;
  1743. { update alignment for C records }
  1744. if (usefieldalignment=C_alignment) and
  1745. (usefieldalignment<>mac68k_alignment) then
  1746. recordalignment:=max(recordalignment,unionst.recordalignment);
  1747. { Register defs in the new record symtable }
  1748. for i:=0 to unionst.DefList.Count-1 do
  1749. begin
  1750. def:=TDef(unionst.DefList[i]);
  1751. def.ChangeOwner(self);
  1752. end;
  1753. _datasize:=storesize;
  1754. fieldalignment:=storealign;
  1755. { If a record contains a union, it does not contain a "single
  1756. non-composite field" in the context of certain ABIs requiring
  1757. special treatment for such records }
  1758. if defowner.typ=recorddef then
  1759. trecorddef(defowner).isunion:=true;
  1760. end;
  1761. procedure trecordsymtable.includemanagementoperator(mop:tmanagementoperator);
  1762. begin
  1763. if mop in managementoperators then
  1764. exit;
  1765. include(managementoperators,mop);
  1766. end;
  1767. {****************************************************************************
  1768. TObjectSymtable
  1769. ****************************************************************************}
  1770. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1771. begin
  1772. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1773. symtabletype:=ObjectSymtable;
  1774. defowner:=adefowner;
  1775. end;
  1776. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1777. var
  1778. hsym: tsym;
  1779. warn: boolean;
  1780. begin
  1781. result:=false;
  1782. if not assigned(defowner) then
  1783. internalerror(200602061);
  1784. { procsym and propertysym have special code
  1785. to override values in inherited classes. For other
  1786. symbols check for duplicates }
  1787. if not(sym.typ in [procsym,propertysym]) then
  1788. begin
  1789. { but private ids can be reused }
  1790. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1791. if assigned(hsym) and
  1792. (
  1793. (
  1794. not(m_delphi in current_settings.modeswitches) and
  1795. is_visible_for_object(hsym,tobjectdef(defowner))
  1796. ) or
  1797. (
  1798. { In Delphi, you can repeat members of a parent class. You can't }
  1799. { do this for objects however, and you (obviouly) can't }
  1800. { declare two fields with the same name in a single class }
  1801. (m_delphi in current_settings.modeswitches) and
  1802. (
  1803. is_object(tdef(defowner)) or
  1804. (hsym.owner = self)
  1805. )
  1806. )
  1807. ) then
  1808. begin
  1809. { only watn when a parameter/local variable in a method
  1810. conflicts with a category method, because this can easily
  1811. happen due to all possible categories being imported via
  1812. CocoaAll }
  1813. warn:=
  1814. (is_objccategory(tdef(hsym.owner.defowner)) or
  1815. is_classhelper(tdef(hsym.owner.defowner))) and
  1816. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1817. DuplicateSym(hashedid,sym,hsym,warn);
  1818. result:=true;
  1819. end;
  1820. end
  1821. else
  1822. result:=inherited checkduplicate(hashedid,sym);
  1823. end;
  1824. {$ifdef llvm}
  1825. {****************************************************************************
  1826. tLlvmShadowSymtableEntry
  1827. ****************************************************************************}
  1828. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1829. begin
  1830. fdef:=def;
  1831. ffieldoffset:=fieldoffset;
  1832. end;
  1833. {****************************************************************************
  1834. TLlvmShadowSymtable
  1835. ****************************************************************************}
  1836. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1837. begin
  1838. result:=get_by_llvm_index(f.llvmfieldnr)
  1839. end;
  1840. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1841. begin
  1842. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1843. end;
  1844. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1845. begin
  1846. equivst:=st;
  1847. curroffset:=0;
  1848. symdeflist:=tfpobjectlist.create(true);
  1849. generate;
  1850. end;
  1851. destructor tllvmshadowsymtable.destroy;
  1852. begin
  1853. symdeflist.free;
  1854. end;
  1855. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1856. var
  1857. sizectr,
  1858. tmpsize: aint;
  1859. begin
  1860. case equivst.usefieldalignment of
  1861. bit_alignment:
  1862. begin
  1863. { curoffset: bit address after the previous field. }
  1864. { llvm has no special support for bitfields in records, }
  1865. { so we replace them with plain bytes. }
  1866. { as soon as a single bit of a byte is allocated, we }
  1867. { allocate the byte in the llvm shadow record }
  1868. if (fieldoffset>curroffset) then
  1869. curroffset:=align(curroffset,8);
  1870. { fields in bitpacked records always start either right }
  1871. { after the previous one, or at the next byte boundary. }
  1872. if (curroffset<>fieldoffset) then
  1873. internalerror(2008051002);
  1874. if is_ordinal(vardef) then
  1875. begin
  1876. tmpsize:=vardef.packedbitsize;
  1877. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1878. inc(curroffset,tmpsize);
  1879. tmpsize:=0;
  1880. while sizectr<>0 do
  1881. begin
  1882. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1883. dec(sizectr);
  1884. inc(tmpsize);
  1885. end;
  1886. end
  1887. else
  1888. begin
  1889. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1890. if not(derefclass) then
  1891. inc(curroffset,vardef.size*8)
  1892. else
  1893. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1894. end;
  1895. end
  1896. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1897. begin
  1898. { curoffset: address right after the previous field }
  1899. while (fieldoffset>curroffset) do
  1900. begin
  1901. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1902. inc(curroffset);
  1903. end;
  1904. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1905. if not(derefclass) then
  1906. inc(curroffset,vardef.size)
  1907. else
  1908. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1909. end
  1910. else
  1911. { default for llvm, don't add explicit padding }
  1912. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1913. end
  1914. end;
  1915. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1916. begin
  1917. case equivst.usefieldalignment of
  1918. { already correct in this case }
  1919. bit_alignment:
  1920. ;
  1921. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1922. begin
  1923. { add padding fields }
  1924. while (finalsize>curroffset) do
  1925. begin
  1926. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1927. inc(curroffset);
  1928. end;
  1929. end;
  1930. end;
  1931. end;
  1932. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1933. var
  1934. sym: tfieldvarsym;
  1935. lastoffset: aint;
  1936. newalignment: aint;
  1937. i, j: longint;
  1938. begin
  1939. i:=0;
  1940. while (i<equivst.symlist.count) do
  1941. begin
  1942. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1943. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  1944. begin
  1945. inc(i);
  1946. continue;
  1947. end;
  1948. sym:=tfieldvarsym(equivst.symlist[i]);
  1949. { a "better" algorithm might be to use the largest }
  1950. { variant in case of (bit)packing, since then }
  1951. { alignment doesn't matter }
  1952. if (vo_is_first_field in sym.varoptions) then
  1953. begin
  1954. { we assume that all fields are processed in order. }
  1955. if (variantstarts.count<>0) then
  1956. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1957. else
  1958. lastoffset:=-1;
  1959. { new variant at same level as last one: use if higher alignment }
  1960. if (lastoffset=sym.fieldoffset) then
  1961. begin
  1962. if (equivst.fieldalignment<>bit_alignment) then
  1963. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1964. else
  1965. newalignment:=1;
  1966. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1967. variantstarts[variantstarts.count-1]:=sym;
  1968. end
  1969. { variant at deeper level than last one -> add }
  1970. else if (lastoffset<sym.fieldoffset) then
  1971. variantstarts.add(sym)
  1972. else
  1973. begin
  1974. { a variant at a less deep level, so backtrack }
  1975. j:=variantstarts.count-2;
  1976. while (j>=0) do
  1977. begin
  1978. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1979. break;
  1980. dec(j);
  1981. end;
  1982. if (j<0) then
  1983. internalerror(2008051003);
  1984. { new variant has higher alignment? }
  1985. if (equivst.fieldalignment<>bit_alignment) then
  1986. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1987. else
  1988. newalignment:=1;
  1989. { yes, replace and remove previous nested variants }
  1990. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1991. begin
  1992. variantstarts[j]:=sym;
  1993. variantstarts.count:=j+1;
  1994. end
  1995. { no, skip this variant }
  1996. else
  1997. begin
  1998. inc(i);
  1999. while (i<equivst.symlist.count) and
  2000. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  2001. (sp_static in tsym(equivst.symlist[i]).symoptions) or
  2002. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  2003. inc(i);
  2004. continue;
  2005. end;
  2006. end;
  2007. end;
  2008. inc(i);
  2009. end;
  2010. end;
  2011. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  2012. var
  2013. lastvaroffsetprocessed: aint;
  2014. i, equivcount, varcount: longint;
  2015. begin
  2016. { if it's an object/class, the first entry is the parent (if there is one) }
  2017. if (equivst.symtabletype=objectsymtable) and
  2018. assigned(tobjectdef(equivst.defowner).childof) then
  2019. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  2020. equivcount:=equivst.symlist.count;
  2021. varcount:=0;
  2022. i:=0;
  2023. lastvaroffsetprocessed:=-1;
  2024. while (i<equivcount) do
  2025. begin
  2026. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  2027. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  2028. begin
  2029. inc(i);
  2030. continue;
  2031. end;
  2032. { start of a new variant? }
  2033. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  2034. begin
  2035. { if we want to process the same variant offset twice, it means that we }
  2036. { got to the end and are trying to process the next variant part -> stop }
  2037. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  2038. break;
  2039. if (varcount>=variantstarts.count) then
  2040. internalerror(2008051005);
  2041. { new variant part -> use the one with the biggest alignment }
  2042. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  2043. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  2044. inc(varcount);
  2045. if (i<0) then
  2046. internalerror(2008051004);
  2047. end;
  2048. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  2049. inc(i);
  2050. end;
  2051. addalignmentpadding(equivst.datasize);
  2052. end;
  2053. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  2054. var
  2055. i, varcount: longint;
  2056. shadowindex: longint;
  2057. equivcount : longint;
  2058. begin
  2059. varcount:=0;
  2060. shadowindex:=0;
  2061. equivcount:=equivst.symlist.count;
  2062. i:=0;
  2063. while (i < equivcount) do
  2064. begin
  2065. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  2066. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  2067. begin
  2068. inc(i);
  2069. continue;
  2070. end;
  2071. { start of a new variant? }
  2072. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  2073. begin
  2074. { back up to a less deeply nested variant level? }
  2075. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  2076. dec(varcount);
  2077. { it's possible that some variants are more deeply nested than the
  2078. one we recorded in the shadowsymtable (since we recorded the one
  2079. with the biggest alignment, not necessarily the biggest one in size
  2080. }
  2081. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  2082. varcount:=variantstarts.count-1
  2083. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  2084. internalerror(2008051006);
  2085. { reset the shadowindex to the start of this variant. }
  2086. { in case the llvmfieldnr is not (yet) set for this }
  2087. { field, shadowindex will simply be reset to zero and }
  2088. { we'll start searching from the start of the record }
  2089. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  2090. if (varcount<pred(variantstarts.count)) then
  2091. inc(varcount);
  2092. end;
  2093. { find the last shadowfield whose offset <= the current field's offset }
  2094. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  2095. (shadowindex<symdeflist.count-1) and
  2096. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  2097. inc(shadowindex);
  2098. { set the field number and potential offset from that field (in case }
  2099. { of overlapping variants) }
  2100. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  2101. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  2102. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  2103. inc(i);
  2104. end;
  2105. end;
  2106. procedure tllvmshadowsymtable.generate;
  2107. var
  2108. variantstarts: tfplist;
  2109. begin
  2110. variantstarts:=tfplist.create;
  2111. { first go through the entire record and }
  2112. { store the fieldvarsyms of the variants }
  2113. { with the highest alignment }
  2114. findvariantstarts(variantstarts);
  2115. { now go through the regular fields and the selected variants, }
  2116. { and add them to the llvm shadow record symtable }
  2117. buildtable(variantstarts);
  2118. { finally map all original fields to the llvm definition }
  2119. buildmapping(variantstarts);
  2120. variantstarts.free;
  2121. end;
  2122. {$endif llvm}
  2123. {****************************************************************************
  2124. TAbstractSubSymtable
  2125. ****************************************************************************}
  2126. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  2127. var
  2128. oldtyp : byte;
  2129. begin
  2130. oldtyp:=ppufile.entrytyp;
  2131. ppufile.entrytyp:=subentryid;
  2132. inherited ppuwrite(ppufile);
  2133. ppufile.entrytyp:=oldtyp;
  2134. end;
  2135. {****************************************************************************
  2136. TAbstractLocalSymtable
  2137. ****************************************************************************}
  2138. function tabstractlocalsymtable.count_locals:longint;
  2139. var
  2140. i : longint;
  2141. sym : tsym;
  2142. begin
  2143. result:=0;
  2144. for i:=0 to SymList.Count-1 do
  2145. begin
  2146. sym:=tsym(SymList[i]);
  2147. { Count only varsyms, but ignore the funcretsym }
  2148. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  2149. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  2150. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  2151. (tstoredsym(sym).refs>0)) then
  2152. inc(result);
  2153. end;
  2154. end;
  2155. function tabstractlocalsymtable.iscurrentunit: boolean;
  2156. begin
  2157. Result:=
  2158. assigned(defowner) and
  2159. defowner.owner.iscurrentunit;
  2160. end;
  2161. {****************************************************************************
  2162. TLocalSymtable
  2163. ****************************************************************************}
  2164. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  2165. begin
  2166. inherited create('');
  2167. defowner:=adefowner;
  2168. symtabletype:=localsymtable;
  2169. symtablelevel:=level;
  2170. end;
  2171. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2172. var
  2173. hsym : tsym;
  2174. begin
  2175. if not assigned(defowner) or
  2176. (defowner.typ<>procdef) then
  2177. internalerror(200602042);
  2178. result:=false;
  2179. hsym:=tsym(FindWithHash(hashedid));
  2180. if assigned(hsym) then
  2181. begin
  2182. { a local and the function can have the same
  2183. name in TP and Delphi, but RESULT not }
  2184. if (m_duplicate_names in current_settings.modeswitches) and
  2185. (hsym.typ in [absolutevarsym,localvarsym]) and
  2186. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  2187. not((m_result in current_settings.modeswitches) and
  2188. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  2189. HideSym(hsym)
  2190. else
  2191. DuplicateSym(hashedid,sym,hsym,false);
  2192. result:=true;
  2193. exit;
  2194. end;
  2195. { check also parasymtable, this needs to be done here because
  2196. of the special situation with the funcret sym that needs to be
  2197. hidden for tp and delphi modes }
  2198. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  2199. if assigned(hsym) then
  2200. begin
  2201. { a local and the function can have the same
  2202. name in TP and Delphi, but RESULT not }
  2203. if (m_duplicate_names in current_settings.modeswitches) and
  2204. (sym.typ in [absolutevarsym,localvarsym]) and
  2205. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  2206. not((m_result in current_settings.modeswitches) and
  2207. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  2208. Hidesym(sym)
  2209. else
  2210. DuplicateSym(hashedid,sym,hsym,false);
  2211. result:=true;
  2212. exit;
  2213. end;
  2214. { check ObjectSymtable, skip this for funcret sym because
  2215. that will always be positive because it has the same name
  2216. as the procsym }
  2217. if not is_funcret_sym(sym) and
  2218. (defowner.typ=procdef) and
  2219. assigned(tprocdef(defowner).struct) and
  2220. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2221. (
  2222. not(m_delphi in current_settings.modeswitches) or
  2223. is_object(tprocdef(defowner).struct)
  2224. ) then
  2225. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2226. end;
  2227. {****************************************************************************
  2228. TParaSymtable
  2229. ****************************************************************************}
  2230. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2231. begin
  2232. inherited create('');
  2233. readonly:=false;
  2234. defowner:=adefowner;
  2235. symtabletype:=parasymtable;
  2236. symtablelevel:=level;
  2237. end;
  2238. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2239. begin
  2240. result:=inherited checkduplicate(hashedid,sym);
  2241. if result then
  2242. exit;
  2243. if not(m_duplicate_names in current_settings.modeswitches) and
  2244. assigned(defowner) and (defowner.typ=procdef) and
  2245. assigned(tprocdef(defowner).struct) and
  2246. assigned(tprocdef(defowner).owner) and
  2247. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2248. (
  2249. not(m_delphi in current_settings.modeswitches) or
  2250. is_object(tprocdef(defowner).struct)
  2251. ) then
  2252. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2253. end;
  2254. procedure tparasymtable.insertdef(def: TDefEntry);
  2255. begin
  2256. if readonly then
  2257. defowner.owner.insertdef(def)
  2258. else
  2259. inherited insertdef(def);
  2260. end;
  2261. {****************************************************************************
  2262. TAbstractUniTSymtable
  2263. ****************************************************************************}
  2264. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2265. begin
  2266. inherited create(n);
  2267. moduleid:=id;
  2268. end;
  2269. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2270. var
  2271. hsym : tsym;
  2272. begin
  2273. result:=false;
  2274. hsym:=tsym(FindWithHash(hashedid));
  2275. if assigned(hsym) then
  2276. begin
  2277. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2278. exit;
  2279. if hsym.typ=symconst.namespacesym then
  2280. begin
  2281. case sym.typ of
  2282. symconst.namespacesym:;
  2283. symconst.unitsym:
  2284. begin
  2285. 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 }
  2286. tnamespacesym(hsym).unitsym:=tsym(sym);
  2287. end
  2288. else
  2289. HideSym(hsym);
  2290. end;
  2291. end
  2292. else
  2293. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2294. unit, the unit can then not be accessed anymore using
  2295. <unit>.<id>, so we can hide the symbol.
  2296. Do the same if we add a namespace and there is a unit with the same name }
  2297. if (hsym.typ=symconst.unitsym) and
  2298. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2299. begin
  2300. HideSym(hsym);
  2301. if sym.typ=symconst.namespacesym then
  2302. tnamespacesym(sym).unitsym:=tsym(hsym);
  2303. end
  2304. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2305. in this case, copy the isoindex and make the original symbol invisible }
  2306. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2307. and (tprogramparasym(hsym).isoindex<>0) then
  2308. begin
  2309. HideSym(hsym);
  2310. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2311. end
  2312. else if (m_iso in current_settings.modeswitches) and (hsym.typ=unitsym) then
  2313. HideSym(hsym)
  2314. else
  2315. DuplicateSym(hashedid,sym,hsym,false);
  2316. result:=true;
  2317. exit;
  2318. end;
  2319. end;
  2320. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2321. begin
  2322. result:=find(n);
  2323. if assigned(result)and(result.typ<>namespacesym)then
  2324. result:=nil;
  2325. end;
  2326. function tabstractuniTSymtable.iscurrentunit:boolean;
  2327. begin
  2328. result:=assigned(current_module) and
  2329. (
  2330. (current_module.globalsymtable=self) or
  2331. (current_module.localsymtable=self)
  2332. );
  2333. end;
  2334. function tabstractuniTSymtable.needs_init_final: boolean;
  2335. begin
  2336. if not init_final_check_done then
  2337. begin
  2338. result:=inherited needs_init_final;
  2339. if not result then
  2340. begin
  2341. result:=has_class_condestructors;
  2342. if result then
  2343. include(tableoptions,sto_needs_init_final);
  2344. end;
  2345. end;
  2346. result:=sto_needs_init_final in tableoptions;
  2347. end;
  2348. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2349. var
  2350. p:integer;
  2351. n,ns:string;
  2352. oldsym:TSymEntry;
  2353. begin
  2354. insert(sym);
  2355. n:=sym.realname;
  2356. p:=pos('.',n);
  2357. ns:='';
  2358. while p>0 do
  2359. begin
  2360. if ns='' then
  2361. ns:=copy(n,1,p-1)
  2362. else
  2363. ns:=ns+'.'+copy(n,1,p-1);
  2364. system.delete(n,1,p);
  2365. oldsym:=findnamespace(upper(ns));
  2366. if not assigned(oldsym) then
  2367. insert(cnamespacesym.create(ns));
  2368. p:=pos('.',n);
  2369. end;
  2370. end;
  2371. procedure CheckForClassConDestructors(p:TObject;arg:pointer);
  2372. var
  2373. result: pboolean absolute arg;
  2374. begin
  2375. if result^ then
  2376. exit;
  2377. if (tdef(p).typ in [objectdef,recorddef]) and
  2378. not (df_generic in tdef(p).defoptions) then
  2379. begin
  2380. { first check the class... }
  2381. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  2382. result^:=true;;
  2383. { ... and then also check all subclasses }
  2384. if not result^ then
  2385. tabstractrecorddef(p).symtable.deflist.foreachcall(@CheckForClassConDestructors,arg);
  2386. end;
  2387. end;
  2388. function tabstractuniTSymtable.has_class_condestructors: boolean;
  2389. begin
  2390. result:=false;
  2391. deflist.foreachcall(@CheckForClassConDestructors,@result);
  2392. end;
  2393. {****************************************************************************
  2394. TStaticSymtable
  2395. ****************************************************************************}
  2396. constructor tstaticsymtable.create(const n : string;id:word);
  2397. begin
  2398. inherited create(n,id);
  2399. symtabletype:=staticsymtable;
  2400. symtablelevel:=main_program_level;
  2401. currentvisibility:=vis_private;
  2402. end;
  2403. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2404. begin
  2405. inherited ppuload(ppufile);
  2406. { now we can deref the syms and defs }
  2407. deref(false);
  2408. end;
  2409. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2410. begin
  2411. inherited ppuwrite(ppufile);
  2412. end;
  2413. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2414. begin
  2415. result:=inherited checkduplicate(hashedid,sym);
  2416. if not result and
  2417. (current_module.localsymtable=self) and
  2418. assigned(current_module.globalsymtable) then
  2419. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2420. end;
  2421. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2422. begin
  2423. result:=inherited findnamespace(n);
  2424. if not assigned(result) and
  2425. (current_module.localsymtable=self) and
  2426. assigned(current_module.globalsymtable) then
  2427. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2428. end;
  2429. {****************************************************************************
  2430. TGlobalSymtable
  2431. ****************************************************************************}
  2432. constructor tglobalsymtable.create(const n : string;id:word);
  2433. begin
  2434. inherited create(n,id);
  2435. symtabletype:=globalsymtable;
  2436. symtablelevel:=main_program_level;
  2437. end;
  2438. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2439. begin
  2440. inherited ppuload(ppufile);
  2441. { now we can deref the syms and defs }
  2442. deref(false);
  2443. end;
  2444. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2445. begin
  2446. { write the symtable entries }
  2447. inherited ppuwrite(ppufile);
  2448. end;
  2449. {*****************************************************************************
  2450. tspecializesymtable
  2451. *****************************************************************************}
  2452. constructor tspecializesymtable.create(const n : string;id:word);
  2453. begin
  2454. inherited create(n,id);
  2455. { the specialize symtable does not own the syms and defs as they are all
  2456. moved to a different symtable before the symtable is destroyed; this
  2457. avoids calls to "extract" }
  2458. symlist.ownsobjects:=false;
  2459. deflist.ownsobjects:=false;
  2460. end;
  2461. function tspecializesymtable.iscurrentunit: boolean;
  2462. begin
  2463. Result:=true;
  2464. end;
  2465. {****************************************************************************
  2466. TWITHSYMTABLE
  2467. ****************************************************************************}
  2468. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2469. begin
  2470. inherited create('');
  2471. symtabletype:=withsymtable;
  2472. withrefnode:=refnode;
  2473. { Replace SymList with the passed symlist }
  2474. SymList.free;
  2475. SymList:=ASymList;
  2476. defowner:=aowner;
  2477. end;
  2478. destructor twithsymtable.destroy;
  2479. begin
  2480. if refcount>1 then
  2481. exit;
  2482. withrefnode.free;
  2483. { Disable SymList because we don't Own it }
  2484. SymList:=nil;
  2485. inherited destroy;
  2486. end;
  2487. procedure twithsymtable.clear;
  2488. begin
  2489. { remove no entry from a withsymtable as it is only a pointer to the
  2490. recorddef or objectdef symtable }
  2491. end;
  2492. procedure twithsymtable.insertdef(def:TDefEntry);
  2493. begin
  2494. { Definitions can't be registered in the withsymtable
  2495. because the withsymtable is removed after the with block.
  2496. We can't easily solve it here because the next symtable in the
  2497. stack is not known. }
  2498. internalerror(200602046);
  2499. end;
  2500. {****************************************************************************
  2501. TSTT_ExceptionSymtable
  2502. ****************************************************************************}
  2503. constructor tstt_excepTSymtable.create;
  2504. begin
  2505. inherited create('');
  2506. symtabletype:=stt_excepTSymtable;
  2507. end;
  2508. {****************************************************************************
  2509. TMacroSymtable
  2510. ****************************************************************************}
  2511. constructor tmacrosymtable.create(exported: boolean);
  2512. begin
  2513. inherited create('');
  2514. if exported then
  2515. symtabletype:=exportedmacrosymtable
  2516. else
  2517. symtabletype:=localmacrosymtable;
  2518. symtablelevel:=main_program_level;
  2519. end;
  2520. {****************************************************************************
  2521. TEnumSymtable
  2522. ****************************************************************************}
  2523. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  2524. var
  2525. value: longint;
  2526. def: tenumdef;
  2527. begin
  2528. // defowner = nil only when we are loading from ppu
  2529. if defowner<>nil then
  2530. begin
  2531. { First entry? Then we need to set the minval }
  2532. value:=tenumsym(sym).value;
  2533. def:=tenumdef(defowner);
  2534. if SymList.count=0 then
  2535. begin
  2536. if value>0 then
  2537. def.has_jumps:=true;
  2538. def.setmin(value);
  2539. def.setmax(value);
  2540. end
  2541. else
  2542. begin
  2543. { check for jumps }
  2544. if value>def.max+1 then
  2545. def.has_jumps:=true;
  2546. { update low and high }
  2547. if def.min>value then
  2548. def.setmin(value);
  2549. if def.max<value then
  2550. def.setmax(value);
  2551. end;
  2552. end;
  2553. inherited insert(sym, checkdup);
  2554. end;
  2555. constructor tenumsymtable.create(adefowner: tdef);
  2556. begin
  2557. inherited Create('');
  2558. symtabletype:=enumsymtable;
  2559. defowner:=adefowner;
  2560. end;
  2561. {****************************************************************************
  2562. TArraySymtable
  2563. ****************************************************************************}
  2564. procedure tarraysymtable.insertdef(def: TDefEntry);
  2565. begin
  2566. { Enums must also be available outside the record scope,
  2567. insert in the owner of this symtable }
  2568. if def.typ=enumdef then
  2569. defowner.owner.insertdef(def)
  2570. else
  2571. inherited insertdef(def);
  2572. end;
  2573. constructor tarraysymtable.create(adefowner: tdef);
  2574. begin
  2575. inherited Create('');
  2576. symtabletype:=arraysymtable;
  2577. defowner:=adefowner;
  2578. end;
  2579. {*****************************************************************************
  2580. Helper Routines
  2581. *****************************************************************************}
  2582. function FullTypeName(def,otherdef:tdef):string;
  2583. var
  2584. s1,s2 : string;
  2585. begin
  2586. if def.typ in [objectdef,recorddef] then
  2587. s1:=tabstractrecorddef(def).RttiName
  2588. else
  2589. s1:=def.typename;
  2590. { When the names are the same try to include the unit name }
  2591. if assigned(otherdef) and
  2592. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2593. begin
  2594. s2:=otherdef.typename;
  2595. if upper(s1)=upper(s2) then
  2596. s1:=def.owner.realname^+'.'+s1;
  2597. end;
  2598. FullTypeName:=s1;
  2599. end;
  2600. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  2601. begin
  2602. result:='';
  2603. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2604. begin
  2605. if (result='') then
  2606. if symtable.name<>nil then
  2607. result:=symtable.name^
  2608. else
  2609. else
  2610. if symtable.name<>nil then
  2611. result:=symtable.name^+delimiter+result
  2612. else
  2613. result:=delimiter+result;
  2614. symtable:=symtable.defowner.owner;
  2615. end;
  2616. end;
  2617. function generate_objectpascal_helper_key(def:tdef):string;
  2618. begin
  2619. if not assigned(def) then
  2620. internalerror(2013020501);
  2621. if def.typ in [recorddef,objectdef] then
  2622. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2623. else
  2624. result:=make_mangledname('',def.owner,def.typesym.name);
  2625. end;
  2626. procedure incompatibletypes(def1,def2:tdef);
  2627. begin
  2628. { When there is an errordef there is already an error message show }
  2629. if (def2.typ=errordef) or
  2630. (def1.typ=errordef) then
  2631. exit;
  2632. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2633. end;
  2634. procedure hidesym(sym:TSymEntry);
  2635. begin
  2636. sym.realname:='$hidden'+sym.realname;
  2637. tsym(sym).visibility:=vis_hidden;
  2638. end;
  2639. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2640. var
  2641. st : TSymtable;
  2642. filename : TIDString;
  2643. begin
  2644. if not warn then
  2645. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2646. else
  2647. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2648. { Write hint where the original symbol was found }
  2649. st:=finduniTSymtable(origsym.owner);
  2650. with tsym(origsym).fileinfo do
  2651. begin
  2652. if assigned(st) and
  2653. (st.symtabletype=globalsymtable) and
  2654. st.iscurrentunit then
  2655. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2656. else if assigned(st.name) then
  2657. begin
  2658. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2659. if filename<>'' then
  2660. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2661. else
  2662. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2663. end;
  2664. end;
  2665. { Rename duplicate sym to an unreachable name, but it can be
  2666. inserted in the symtable without errors }
  2667. inc(dupnr);
  2668. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2669. if assigned(dupsym) then
  2670. include(tsym(dupsym).symoptions,sp_implicitrename);
  2671. end;
  2672. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2673. begin
  2674. result:=false;
  2675. if not assigned(sym) or not (sym is tstoredsym) then
  2676. Internalerror(2011081101);
  2677. { For generics a dummy symbol without the parameter count is created
  2678. if such a symbol not yet exists so that different parts of the
  2679. parser can find that symbol. If that symbol is still a
  2680. undefineddef we replace the generic dummy symbol's
  2681. name with a "dup" name and use the new symbol as the generic dummy
  2682. symbol }
  2683. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2684. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2685. (m_delphi in current_settings.modeswitches) then
  2686. begin
  2687. inc(dupnr);
  2688. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2689. include(tsym(sym).symoptions,sp_implicitrename);
  2690. { we need to find the new symbol now if checking for a dummy }
  2691. include(symoptions,sp_generic_dummy);
  2692. result:=true;
  2693. end;
  2694. end;
  2695. procedure write_system_parameter_lists(const name:string);
  2696. var
  2697. srsym:tprocsym;
  2698. begin
  2699. srsym:=tprocsym(systemunit.find(name));
  2700. if not assigned(srsym) or not (srsym.typ=procsym) then
  2701. internalerror(2016060302);
  2702. srsym.write_parameter_lists(nil);
  2703. end;
  2704. {*****************************************************************************
  2705. Search
  2706. *****************************************************************************}
  2707. procedure addsymref(sym:tsym);
  2708. var
  2709. owner: tsymtable;
  2710. begin
  2711. { for symbols used in preprocessor expressions, we don't want to
  2712. increase references count (for smaller final binaries) }
  2713. if not assigned(current_scanner) then
  2714. internalerror(2017050601);
  2715. if current_scanner.in_preproc_comp_expr then
  2716. exit;
  2717. { symbol uses count }
  2718. sym.IncRefCount;
  2719. owner:=sym.owner;
  2720. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2721. owner:=tdef(owner.defowner).owner;
  2722. if assigned(current_module) and
  2723. (owner.symtabletype=globalsymtable) then
  2724. begin
  2725. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2726. internalerror(200501152);
  2727. { unit uses count }
  2728. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2729. { Note: don't check the symtable directly as owner might be
  2730. a specialize symtable which is a globalsymtable as well }
  2731. if (
  2732. assigned(current_module.globalsymtable) and
  2733. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2734. ) or (
  2735. assigned(current_module.localsymtable) and
  2736. (current_module.localsymtable.moduleid<>owner.moduleid)
  2737. ) then
  2738. { symbol is imported from another unit }
  2739. current_module.addimportedsym(sym);
  2740. end;
  2741. end;
  2742. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2743. begin
  2744. result:=nesteddef=ownerdef;
  2745. if not result and
  2746. { types declared locally in a record method are not defined in the
  2747. record itself }
  2748. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2749. assigned(nesteddef.owner.defowner) then
  2750. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2751. end;
  2752. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2753. begin
  2754. result:=assigned(childsym) and (childsym.owner=symtable);
  2755. if not result and assigned(childsym) and
  2756. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2757. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2758. end;
  2759. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2760. begin
  2761. result:=false;
  2762. if not assigned(def1) or not assigned(def2) then
  2763. exit;
  2764. { for both defs walk to the topmost generic }
  2765. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2766. def1:=tdef(def1.owner.defowner);
  2767. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2768. def2:=tdef(def2.owner.defowner);
  2769. result:=def1=def2;
  2770. end;
  2771. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2772. var
  2773. uname : string;
  2774. begin
  2775. { TODO : check regarding arrays and records declared as their type }
  2776. if not (def.typ in [recorddef,objectdef]) then
  2777. internalerror(2012051501);
  2778. uname:=upper(srsym.realname);
  2779. repeat
  2780. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2781. begin
  2782. result:=def;
  2783. exit;
  2784. end;
  2785. def:=tdef(def.owner.defowner);
  2786. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2787. result:=nil;
  2788. end;
  2789. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2790. begin
  2791. { TODO : check regarding arrays and records declared as their type }
  2792. if not (nesteddef.typ in [recorddef,objectdef]) then
  2793. internalerror(2012051601);
  2794. repeat
  2795. if tstoreddef(nesteddef).genericdef=genericdef then
  2796. begin
  2797. resultdef:=nesteddef;
  2798. result:=true;
  2799. exit;
  2800. end;
  2801. nesteddef:=tdef(nesteddef.owner.defowner);
  2802. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2803. resultdef:=nil;
  2804. result:=false;
  2805. end;
  2806. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2807. symvisibility: visibility of the symbol
  2808. contextobjdef: via which def the symbol is accessed, e.g.:
  2809. fieldname:=1 -> contextobjdef = current_structdef
  2810. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2811. }
  2812. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2813. var
  2814. symownerdef : tabstractrecorddef;
  2815. nonlocalst : tsymtable;
  2816. isspezproc : boolean;
  2817. begin
  2818. result:=false;
  2819. { Get objdectdef owner of the symtable for the is_related checks }
  2820. if not assigned(symst) or
  2821. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2822. internalerror(200810285);
  2823. symownerdef:=tabstractrecorddef(symst.defowner);
  2824. { specializations might belong to a localsymtable or parasymtable }
  2825. nonlocalst:=symownerdef.owner;
  2826. if tstoreddef(symst.defowner).is_specialization then
  2827. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2828. nonlocalst:=nonlocalst.defowner.owner;
  2829. isspezproc:=false;
  2830. if assigned(current_procinfo) then
  2831. begin
  2832. if current_procinfo.procdef.is_specialization and
  2833. assigned(current_procinfo.procdef.struct) then
  2834. isspezproc:=true;
  2835. end;
  2836. case symvisibility of
  2837. vis_private :
  2838. begin
  2839. { private symbols are allowed when we are in the same
  2840. module as they are defined }
  2841. result:=(
  2842. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2843. (nonlocalst.iscurrentunit)
  2844. ) or
  2845. ( // the case of specialize inside the generic declaration and nested types
  2846. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2847. (
  2848. assigned(current_structdef) and
  2849. (
  2850. (current_structdef=symownerdef) or
  2851. (current_structdef.owner.iscurrentunit)
  2852. )
  2853. ) or
  2854. (
  2855. not assigned(current_structdef) and
  2856. (symownerdef.owner.iscurrentunit)
  2857. ) or
  2858. { access from a generic method that belongs to the class
  2859. but that is specialized elsewere }
  2860. (
  2861. isspezproc and
  2862. (current_procinfo.procdef.struct=current_structdef)
  2863. ) or
  2864. { specializations may access private symbols that their
  2865. generics are allowed to access }
  2866. (
  2867. assigned(current_structdef) and
  2868. (df_specialization in current_structdef.defoptions) and
  2869. (symst.moduleid=current_structdef.genericdef.owner.moduleid)
  2870. )
  2871. );
  2872. end;
  2873. vis_strictprivate :
  2874. begin
  2875. result:=assigned(current_structdef) and
  2876. is_owned_by(current_structdef,symownerdef);
  2877. end;
  2878. vis_strictprotected :
  2879. begin
  2880. result:=(
  2881. { access from nested class }
  2882. assigned(current_structdef) and
  2883. is_owned_by(current_structdef,symownerdef)
  2884. ) or
  2885. (
  2886. { access from child class }
  2887. assigned(contextobjdef) and
  2888. assigned(current_structdef) and
  2889. def_is_related(contextobjdef,symownerdef) and
  2890. def_is_related(current_structdef,contextobjdef)
  2891. ) or
  2892. (
  2893. { helpers can access strict protected symbols }
  2894. is_objectpascal_helper(contextobjdef) and
  2895. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2896. ) or
  2897. (
  2898. { same as above, but from context of call node inside
  2899. helper method }
  2900. is_objectpascal_helper(current_structdef) and
  2901. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2902. );
  2903. end;
  2904. vis_protected :
  2905. begin
  2906. { protected symbols are visible in the module that defines them and
  2907. also visible to related objects. The related object must be defined
  2908. in the current module }
  2909. result:=(
  2910. (
  2911. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2912. (nonlocalst.iscurrentunit)
  2913. ) or
  2914. (
  2915. assigned(contextobjdef) and
  2916. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable]) and
  2917. (contextobjdef.owner.iscurrentunit) and
  2918. def_is_related(contextobjdef,symownerdef)
  2919. ) or
  2920. ( // the case of specialize inside the generic declaration and nested types
  2921. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2922. (
  2923. assigned(current_structdef) and
  2924. (
  2925. (current_structdef=symownerdef) or
  2926. (current_structdef.owner.iscurrentunit)
  2927. )
  2928. ) or
  2929. (
  2930. not assigned(current_structdef) and
  2931. (symownerdef.owner.iscurrentunit)
  2932. ) or
  2933. (
  2934. { helpers can access protected symbols }
  2935. is_objectpascal_helper(contextobjdef) and
  2936. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2937. )
  2938. ) or
  2939. { access from a generic method that belongs to the class
  2940. but that is specialized elsewere }
  2941. (
  2942. isspezproc and
  2943. (current_procinfo.procdef.struct=current_structdef)
  2944. ) or
  2945. { specializations may access private symbols that their
  2946. generics are allowed to access }
  2947. (
  2948. assigned(current_structdef) and
  2949. (df_specialization in current_structdef.defoptions) and
  2950. (symst.moduleid=current_structdef.genericdef.owner.moduleid)
  2951. )
  2952. );
  2953. end;
  2954. vis_public,
  2955. vis_published :
  2956. result:=true;
  2957. end;
  2958. end;
  2959. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2960. begin
  2961. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2962. end;
  2963. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2964. var
  2965. i : longint;
  2966. pd : tprocdef;
  2967. begin
  2968. if sym.typ=procsym then
  2969. begin
  2970. { A procsym is visible, when there is at least one of the procdefs visible }
  2971. result:=false;
  2972. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2973. begin
  2974. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2975. if (pd.owner=sym.owner) and
  2976. is_visible_for_object(pd,contextobjdef) then
  2977. begin
  2978. result:=true;
  2979. exit;
  2980. end;
  2981. end;
  2982. end
  2983. else
  2984. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2985. end;
  2986. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2987. begin
  2988. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2989. end;
  2990. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2991. begin
  2992. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2993. end;
  2994. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2995. var
  2996. hashedid: THashedIDString;
  2997. contextstructdef: tabstractrecorddef;
  2998. stackitem: psymtablestackitem;
  2999. begin
  3000. result:=false;
  3001. hashedid.id:=s;
  3002. stackitem:=symtablestack.stack;
  3003. while assigned(stackitem) do
  3004. begin
  3005. srsymtable:=stackitem^.symtable;
  3006. if (srsymtable.symtabletype=objectsymtable) then
  3007. begin
  3008. { TODO : implement the search for an option in classes as well }
  3009. if ssf_search_option in flags then
  3010. begin
  3011. result:=false;
  3012. exit;
  3013. end;
  3014. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  3015. begin
  3016. result:=true;
  3017. exit;
  3018. end;
  3019. end
  3020. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  3021. (srsymtable.defowner.typ=undefineddef)) then
  3022. begin
  3023. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3024. { First check if it is a unit/namespace symbol.
  3025. They are visible only if they are from the current unit or
  3026. unit of generic of currently processed specialization. }
  3027. if assigned(srsym) and
  3028. (
  3029. not(srsym.typ in [unitsym,namespacesym]) or
  3030. srsymtable.iscurrentunit or
  3031. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid)) or
  3032. (
  3033. assigned(current_procinfo) and
  3034. (df_specialization in current_procinfo.procdef.defoptions) and
  3035. (current_procinfo.procdef.genericdef.owner.moduleid=srsymtable.moduleid)
  3036. )
  3037. ) and
  3038. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  3039. begin
  3040. { use the class from withsymtable only when it is
  3041. defined in this unit }
  3042. if (srsymtable.symtabletype=withsymtable) and
  3043. assigned(srsymtable.defowner) and
  3044. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  3045. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  3046. (srsymtable.defowner.owner.iscurrentunit) then
  3047. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  3048. else
  3049. contextstructdef:=current_structdef;
  3050. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  3051. is_visible_for_object(srsym,contextstructdef) then
  3052. begin
  3053. { we need to know if a procedure references symbols
  3054. in the static symtable, because then it can't be
  3055. inlined from outside this unit }
  3056. if assigned(current_procinfo) and
  3057. (srsym.owner.symtabletype=staticsymtable) then
  3058. include(current_procinfo.flags,pi_uses_static_symtable);
  3059. if not (ssf_no_addsymref in flags) then
  3060. addsymref(srsym);
  3061. result:=true;
  3062. exit;
  3063. end;
  3064. end;
  3065. end;
  3066. stackitem:=stackitem^.next;
  3067. end;
  3068. srsym:=nil;
  3069. srsymtable:=nil;
  3070. end;
  3071. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  3072. srsymtable:TSymtable;option:tsymoption):boolean;
  3073. begin
  3074. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  3075. end;
  3076. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3077. var
  3078. hashedid : THashedIDString;
  3079. stackitem : psymtablestackitem;
  3080. classh : tobjectdef;
  3081. begin
  3082. result:=false;
  3083. hashedid.id:=s;
  3084. stackitem:=symtablestack.stack;
  3085. while assigned(stackitem) do
  3086. begin
  3087. {
  3088. It is not possible to have type symbols in:
  3089. parameters
  3090. Exception are classes, objects, records, generic definitions and specializations
  3091. that have the parameterized types inserted in the symtable.
  3092. }
  3093. srsymtable:=stackitem^.symtable;
  3094. if (srsymtable.symtabletype=ObjectSymtable) then
  3095. begin
  3096. classh:=tobjectdef(srsymtable.defowner);
  3097. while assigned(classh) do
  3098. begin
  3099. srsymtable:=classh.symtable;
  3100. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3101. if assigned(srsym) and
  3102. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3103. is_visible_for_object(srsym,current_structdef) then
  3104. begin
  3105. addsymref(srsym);
  3106. result:=true;
  3107. exit;
  3108. end;
  3109. classh:=classh.childof;
  3110. end;
  3111. end
  3112. else
  3113. begin
  3114. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3115. if assigned(srsym) and
  3116. (
  3117. not(srsym.typ in [unitsym,namespacesym]) or
  3118. srsymtable.iscurrentunit or
  3119. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  3120. ) and
  3121. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3122. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  3123. begin
  3124. { we need to know if a procedure references symbols
  3125. in the static symtable, because then it can't be
  3126. inlined from outside this unit }
  3127. if assigned(current_procinfo) and
  3128. (srsym.owner.symtabletype=staticsymtable) then
  3129. include(current_procinfo.flags,pi_uses_static_symtable);
  3130. addsymref(srsym);
  3131. result:=true;
  3132. exit;
  3133. end;
  3134. end;
  3135. stackitem:=stackitem^.next;
  3136. end;
  3137. result:=false;
  3138. srsym:=nil;
  3139. srsymtable:=nil;
  3140. end;
  3141. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3142. var
  3143. pmod : tmodule;
  3144. begin
  3145. pmod:=tmodule(pm);
  3146. result:=false;
  3147. if assigned(pmod.globalsymtable) then
  3148. begin
  3149. srsym:=tsym(pmod.globalsymtable.Find(s));
  3150. if assigned(srsym) then
  3151. begin
  3152. srsymtable:=pmod.globalsymtable;
  3153. addsymref(srsym);
  3154. result:=true;
  3155. exit;
  3156. end;
  3157. end;
  3158. { If the module is the current unit we also need
  3159. to search the local symtable }
  3160. if (pmod=current_module) and
  3161. assigned(pmod.localsymtable) then
  3162. begin
  3163. srsym:=tsym(pmod.localsymtable.Find(s));
  3164. if assigned(srsym) then
  3165. begin
  3166. srsymtable:=pmod.localsymtable;
  3167. addsymref(srsym);
  3168. result:=true;
  3169. exit;
  3170. end;
  3171. end;
  3172. srsym:=nil;
  3173. srsymtable:=nil;
  3174. end;
  3175. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  3176. var
  3177. stackitem : psymtablestackitem;
  3178. begin
  3179. result:=false;
  3180. stackitem:=symtablestack.stack;
  3181. while assigned(stackitem) do
  3182. begin
  3183. srsymtable:=stackitem^.symtable;
  3184. if (srsymtable.symtabletype=globalsymtable) and
  3185. (srsymtable.name^=unitname) then
  3186. begin
  3187. srsym:=tsym(srsymtable.find(symname));
  3188. if not assigned(srsym) then
  3189. break;
  3190. result:=true;
  3191. exit;
  3192. end;
  3193. stackitem:=stackitem^.next;
  3194. end;
  3195. { If the module is the current unit we also need
  3196. to search the local symtable }
  3197. if assigned(current_module.localsymtable) and
  3198. (current_module.localsymtable.name^=unitname) then
  3199. begin
  3200. srsymtable:=current_module.localsymtable;
  3201. srsym:=tsym(srsymtable.find(symname));
  3202. if assigned(srsym) then
  3203. begin
  3204. result:=true;
  3205. exit;
  3206. end;
  3207. end;
  3208. end;
  3209. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  3210. begin
  3211. result:=pd;
  3212. if pd.typ<>objectdef then
  3213. exit;
  3214. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  3215. end;
  3216. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  3217. var
  3218. hashedid : THashedIDString;
  3219. stackitem : psymtablestackitem;
  3220. srsymtable : tsymtable;
  3221. srsym : tsym;
  3222. formalname,
  3223. foundname : shortstring;
  3224. formalnameptr,
  3225. foundnameptr: pshortstring;
  3226. begin
  3227. { not a formal definition -> return it }
  3228. if not(oo_is_formal in pd.objectoptions) then
  3229. begin
  3230. result:=pd;
  3231. exit;
  3232. end;
  3233. hashedid.id:=pd.typesym.name;
  3234. stackitem:=symtablestack.stack;
  3235. while assigned(stackitem) do
  3236. begin
  3237. srsymtable:=stackitem^.symtable;
  3238. { ObjC classes can't appear in generics or as nested class
  3239. definitions. Java classes can. }
  3240. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  3241. (is_java_class_or_interface(pd) and
  3242. (srsymtable.symtabletype=ObjectSymtable)) then
  3243. begin
  3244. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3245. if assigned(srsym) and
  3246. (srsym.typ=typesym) and
  3247. (ttypesym(srsym).typedef.typ=objectdef) and
  3248. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  3249. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3250. begin
  3251. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3252. begin
  3253. { the external name for the formal and the real
  3254. definition must match }
  3255. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  3256. assigned(pd.import_lib) then
  3257. begin
  3258. if assigned(pd.import_lib) then
  3259. formalname:=pd.import_lib^+'.'
  3260. else
  3261. formalname:='';
  3262. formalname:=formalname+pd.objextname^;
  3263. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  3264. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  3265. else
  3266. foundname:='';
  3267. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3268. formalnameptr:=@formalname;
  3269. foundnameptr:=@foundname;
  3270. end
  3271. else
  3272. begin
  3273. formalnameptr:=pd.objextname;
  3274. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3275. end;
  3276. if foundnameptr^<>formalnameptr^ then
  3277. begin
  3278. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3279. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3280. end;
  3281. end;
  3282. result:=tobjectdef(ttypesym(srsym).typedef);
  3283. if assigned(current_procinfo) and
  3284. (srsym.owner.symtabletype=staticsymtable) then
  3285. include(current_procinfo.flags,pi_uses_static_symtable);
  3286. addsymref(srsym);
  3287. exit;
  3288. end;
  3289. end;
  3290. stackitem:=stackitem^.next;
  3291. end;
  3292. { nothing found: optionally give an error and return the original
  3293. (empty) one }
  3294. if erroronfailure then
  3295. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3296. result:=pd;
  3297. end;
  3298. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3299. var
  3300. hashedid : THashedIDString;
  3301. orgclass : tobjectdef;
  3302. i : longint;
  3303. begin
  3304. orgclass:=classh;
  3305. { in case this is a formal class, first find the real definition }
  3306. if assigned(classh) then
  3307. begin
  3308. if (oo_is_formal in classh.objectoptions) then
  3309. classh:=find_real_class_definition(classh,true);
  3310. { The contextclassh is used for visibility. The classh must be equal to
  3311. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3312. parent or a class helper. }
  3313. if not (def_is_related(contextclassh,classh) or
  3314. (is_classhelper(contextclassh) and
  3315. assigned(tobjectdef(contextclassh).extendeddef) and
  3316. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3317. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3318. internalerror(200811161);
  3319. end;
  3320. result:=false;
  3321. hashedid.id:=s;
  3322. { an Objective-C protocol or Java interface can inherit from multiple
  3323. other protocols/interfaces -> use ImplementedInterfaces instead }
  3324. if is_objcprotocol(classh) or
  3325. is_javainterface(classh) then
  3326. begin
  3327. srsymtable:=classh.symtable;
  3328. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3329. if assigned(srsym) and
  3330. is_visible_for_object(srsym,contextclassh) then
  3331. begin
  3332. if not (ssf_no_addsymref in flags) then
  3333. addsymref(srsym);
  3334. result:=true;
  3335. exit;
  3336. end;
  3337. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3338. begin
  3339. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3340. begin
  3341. result:=true;
  3342. exit;
  3343. end;
  3344. end;
  3345. end
  3346. else
  3347. if is_objectpascal_helper(classh) then
  3348. begin
  3349. { helpers have their own obscure search logic... }
  3350. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3351. if result then
  3352. exit;
  3353. end
  3354. else
  3355. begin
  3356. while assigned(classh) do
  3357. begin
  3358. { search for a class helper method first if this is an Object
  3359. Pascal class and we haven't yet found a helper symbol }
  3360. if (classh.objecttype in objecttypes_with_helpers) and
  3361. (ssf_search_helper in flags) then
  3362. begin
  3363. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3364. { an eventual overload inside the extended type's hierarchy
  3365. will be found by tcallcandidates }
  3366. if result then
  3367. exit;
  3368. end;
  3369. srsymtable:=classh.symtable;
  3370. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3371. if assigned(srsym) and
  3372. is_visible_for_object(srsym,contextclassh) then
  3373. begin
  3374. if not (ssf_no_addsymref in flags) then
  3375. addsymref(srsym);
  3376. result:=true;
  3377. exit;
  3378. end;
  3379. classh:=classh.childof;
  3380. end;
  3381. end;
  3382. if is_objcclass(orgclass) then
  3383. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3384. else
  3385. begin
  3386. srsym:=nil;
  3387. srsymtable:=nil;
  3388. end;
  3389. end;
  3390. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3391. var
  3392. hashedid : THashedIDString;
  3393. begin
  3394. result:=false;
  3395. hashedid.id:=s;
  3396. { search for a record helper method first }
  3397. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3398. if result then
  3399. { an eventual overload inside the extended type's hierarchy
  3400. will be found by tcallcandidates }
  3401. exit;
  3402. srsymtable:=recordh.symtable;
  3403. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3404. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3405. begin
  3406. addsymref(srsym);
  3407. result:=true;
  3408. exit;
  3409. end;
  3410. srsym:=nil;
  3411. srsymtable:=nil;
  3412. end;
  3413. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3414. var
  3415. def : tdef;
  3416. i : longint;
  3417. begin
  3418. { in case this is a formal class, first find the real definition }
  3419. if assigned(classh) and
  3420. (oo_is_formal in classh.objectoptions) then
  3421. classh:=find_real_class_definition(classh,true);
  3422. result:=false;
  3423. def:=nil;
  3424. while assigned(classh) do
  3425. begin
  3426. for i:=0 to classh.symtable.DefList.Count-1 do
  3427. begin
  3428. def:=tstoreddef(classh.symtable.DefList[i]);
  3429. { Find also all hidden private methods to
  3430. be compatible with delphi, see tw6203 (PFV) }
  3431. if (def.typ=procdef) and
  3432. (po_msgint in tprocdef(def).procoptions) and
  3433. (tprocdef(def).messageinf.i=msgid) then
  3434. begin
  3435. srdef:=def;
  3436. srsym:=tprocdef(def).procsym;
  3437. srsymtable:=classh.symtable;
  3438. addsymref(srsym);
  3439. result:=true;
  3440. exit;
  3441. end;
  3442. end;
  3443. classh:=classh.childof;
  3444. end;
  3445. srdef:=nil;
  3446. srsym:=nil;
  3447. srsymtable:=nil;
  3448. end;
  3449. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3450. var
  3451. def : tdef;
  3452. i : longint;
  3453. begin
  3454. { in case this is a formal class, first find the real definition }
  3455. if assigned(classh) and
  3456. (oo_is_formal in classh.objectoptions) then
  3457. classh:=find_real_class_definition(classh,true);
  3458. result:=false;
  3459. def:=nil;
  3460. while assigned(classh) do
  3461. begin
  3462. for i:=0 to classh.symtable.DefList.Count-1 do
  3463. begin
  3464. def:=tstoreddef(classh.symtable.DefList[i]);
  3465. { Find also all hidden private methods to
  3466. be compatible with delphi, see tw6203 (PFV) }
  3467. if (def.typ=procdef) and
  3468. (po_msgstr in tprocdef(def).procoptions) and
  3469. (tprocdef(def).messageinf.str^=s) then
  3470. begin
  3471. srsym:=tprocdef(def).procsym;
  3472. srsymtable:=classh.symtable;
  3473. addsymref(srsym);
  3474. result:=true;
  3475. exit;
  3476. end;
  3477. end;
  3478. classh:=classh.childof;
  3479. end;
  3480. srsym:=nil;
  3481. srsymtable:=nil;
  3482. end;
  3483. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3484. var
  3485. hashedid : THashedIDString;
  3486. parentclassh : tobjectdef;
  3487. begin
  3488. result:=false;
  3489. if not is_objectpascal_helper(classh) then
  3490. Internalerror(2011030101);
  3491. hashedid.id:=s;
  3492. { in a helper things are a bit more complex:
  3493. 1. search the symbol in the helper (if not "inherited")
  3494. 2. search the symbol in the extended type
  3495. 3. search the symbol in the parent helpers
  3496. 4. only classes: search the symbol in the parents of the extended type
  3497. }
  3498. if not (ssf_has_inherited in flags) then
  3499. begin
  3500. { search in the helper itself }
  3501. srsymtable:=classh.symtable;
  3502. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3503. if assigned(srsym) and
  3504. is_visible_for_object(srsym,contextclassh) then
  3505. begin
  3506. if not (ssf_no_addsymref in flags) then
  3507. addsymref(srsym);
  3508. result:=true;
  3509. exit;
  3510. end;
  3511. end;
  3512. { now search in the extended type itself }
  3513. { Note: the extendeddef might be Nil if we are currently parsing the
  3514. extended type itself and the identifier was not found }
  3515. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3516. begin
  3517. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3518. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3519. if assigned(srsym) and
  3520. is_visible_for_object(srsym,contextclassh) then
  3521. begin
  3522. if not (ssf_no_addsymref in flags) then
  3523. addsymref(srsym);
  3524. result:=true;
  3525. exit;
  3526. end;
  3527. end;
  3528. { now search in the parent helpers }
  3529. parentclassh:=classh.childof;
  3530. while assigned(parentclassh) do
  3531. begin
  3532. srsymtable:=parentclassh.symtable;
  3533. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3534. if assigned(srsym) and
  3535. is_visible_for_object(srsym,contextclassh) then
  3536. begin
  3537. if not (ssf_no_addsymref in flags) then
  3538. addsymref(srsym);
  3539. result:=true;
  3540. exit;
  3541. end;
  3542. parentclassh:=parentclassh.childof;
  3543. end;
  3544. if is_class(classh.extendeddef) then
  3545. { now search in the parents of the extended class (with helpers!) }
  3546. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3547. { addsymref is already called by searchsym_in_class }
  3548. end;
  3549. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3550. var
  3551. sym : Tprocsym;
  3552. hashedid : THashedIDString;
  3553. curreq,
  3554. besteq : tequaltype;
  3555. currpd,
  3556. bestpd : tprocdef;
  3557. stackitem : psymtablestackitem;
  3558. shortstringcount : longint;
  3559. isexplicit,
  3560. checkshortstring : boolean;
  3561. begin
  3562. hashedid.id:=overloaded_names[assignment_type];
  3563. besteq:=te_incompatible;
  3564. bestpd:=nil;
  3565. stackitem:=symtablestack.stack;
  3566. { special handling for assignments to shortstrings with a specific length:
  3567. - if we get an operator to ShortString we use that
  3568. - if we get only a single String[x] operator we use that
  3569. - otherwise it's a nogo }
  3570. isexplicit:=assignment_type=_OP_EXPLICIT;
  3571. shortstringcount:=0;
  3572. checkshortstring:=not isexplicit and is_shortstring(to_def) and (tstringdef(to_def).len<>255);
  3573. while assigned(stackitem) do
  3574. begin
  3575. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3576. if sym<>nil then
  3577. begin
  3578. if sym.typ<>procsym then
  3579. internalerror(200402031);
  3580. { if the source type is an alias then this is only the second choice,
  3581. if you mess with this code, check tw4093 }
  3582. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq,isexplicit);
  3583. { we found a ShortString overload, use that and be done }
  3584. if checkshortstring and
  3585. assigned(currpd) and
  3586. is_shortstring(currpd.returndef) and
  3587. (tstringdef(currpd.returndef).len=255) then
  3588. begin
  3589. besteq:=curreq;
  3590. bestpd:=currpd;
  3591. break;
  3592. end;
  3593. { independently of the operator being better count if we encountered
  3594. multpile String[x] operators }
  3595. if checkshortstring and assigned(currpd) and is_shortstring(currpd.returndef) then
  3596. inc(shortstringcount);
  3597. if curreq>besteq then
  3598. begin
  3599. besteq:=curreq;
  3600. bestpd:=currpd;
  3601. { don't stop searching if we have a String[x] operator cause
  3602. we might find a ShortString one or multiple ones (which
  3603. leads to no operator use) }
  3604. if (besteq=te_exact) and not checkshortstring then
  3605. break;
  3606. end;
  3607. end;
  3608. stackitem:=stackitem^.next;
  3609. end;
  3610. if checkshortstring and (shortstringcount>1) then
  3611. bestpd:=nil;
  3612. result:=bestpd;
  3613. end;
  3614. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3615. begin
  3616. { search record/object symtable first for a suitable operator }
  3617. if from_def.typ in [recorddef,objectdef] then
  3618. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3619. if to_def.typ in [recorddef,objectdef] then
  3620. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3621. { if type conversion is explicit then search first for explicit
  3622. operator overload and if not found then use implicit operator }
  3623. if explicit then
  3624. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3625. else
  3626. result:=nil;
  3627. if result=nil then
  3628. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3629. { restore symtable stack }
  3630. if to_def.typ in [recorddef,objectdef] then
  3631. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3632. if from_def.typ in [recorddef,objectdef] then
  3633. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3634. end;
  3635. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3636. var
  3637. sym : Tprocsym;
  3638. hashedid : THashedIDString;
  3639. curreq,
  3640. besteq : tequaltype;
  3641. currpd,
  3642. bestpd : tprocdef;
  3643. stackitem : psymtablestackitem;
  3644. begin
  3645. hashedid.id:='enumerator';
  3646. besteq:=te_incompatible;
  3647. bestpd:=nil;
  3648. stackitem:=symtablestack.stack;
  3649. while assigned(stackitem) do
  3650. begin
  3651. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3652. if sym<>nil then
  3653. begin
  3654. if sym.typ<>procsym then
  3655. internalerror(200910241);
  3656. { if the source type is an alias then this is only the second choice,
  3657. if you mess with this code, check tw4093 }
  3658. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3659. if curreq>besteq then
  3660. begin
  3661. besteq:=curreq;
  3662. bestpd:=currpd;
  3663. if (besteq=te_exact) then
  3664. break;
  3665. end;
  3666. end;
  3667. stackitem:=stackitem^.next;
  3668. end;
  3669. result:=bestpd;
  3670. end;
  3671. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  3672. var
  3673. sym : Tprocsym;
  3674. hashedid : THashedIDString;
  3675. optoken: ttoken;
  3676. begin
  3677. optoken := managementoperator2tok[mop];
  3678. if (optoken<first_managment_operator) or
  3679. (optoken>last_managment_operator) then
  3680. internalerror(201602280);
  3681. hashedid.id:=overloaded_names[optoken];
  3682. if not (pd.typ in [recorddef]) then
  3683. internalerror(201602281);
  3684. sym:=Tprocsym(tabstractrecorddef(pd).symtable.FindWithHash(hashedid));
  3685. if sym<>nil then
  3686. begin
  3687. if sym.typ<>procsym then
  3688. internalerror(201602282);
  3689. result:=sym.find_procdef_bytype(potype_operator);
  3690. end
  3691. else
  3692. result:=nil;
  3693. end;
  3694. function search_system_type(const s: TIDString): ttypesym;
  3695. var
  3696. sym : tsym;
  3697. begin
  3698. sym:=tsym(systemunit.Find(s));
  3699. if not assigned(sym) or
  3700. (sym.typ<>typesym) then
  3701. message1(cg_f_unknown_system_type,s);
  3702. result:=ttypesym(sym);
  3703. end;
  3704. function try_search_system_type(const s: TIDString): ttypesym;
  3705. var
  3706. sym : tsym;
  3707. begin
  3708. sym:=tsym(systemunit.Find(s));
  3709. if not assigned(sym) then
  3710. result:=nil
  3711. else
  3712. begin
  3713. if sym.typ<>typesym then
  3714. message1(cg_f_unknown_system_type,s);
  3715. result:=ttypesym(sym);
  3716. end;
  3717. end;
  3718. function try_search_current_module_type(const s: TIDString): ttypesym;
  3719. var
  3720. found: boolean;
  3721. srsymtable: tsymtable;
  3722. srsym: tsym;
  3723. begin
  3724. if s[1]='$' then
  3725. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  3726. else
  3727. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  3728. if found then
  3729. begin
  3730. if (srsym.typ<>typesym) then
  3731. internalerror(2014091207);
  3732. result:=ttypesym(srsym);
  3733. end
  3734. else
  3735. result:=nil;
  3736. end;
  3737. function search_system_proc(const s: TIDString): tprocdef;
  3738. var
  3739. srsym: tsym;
  3740. begin
  3741. srsym:=tsym(systemunit.find(s));
  3742. if not assigned(srsym) and
  3743. (cs_compilesystem in current_settings.moduleswitches) then
  3744. srsym:=tsym(systemunit.Find(upper(s)));
  3745. if not assigned(srsym) or
  3746. (srsym.typ<>procsym) then
  3747. message1(cg_f_unknown_compilerproc,s);
  3748. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3749. end;
  3750. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  3751. var
  3752. srsymtable: tsymtable;
  3753. sym: tsym;
  3754. begin
  3755. sym:=nil;
  3756. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  3757. (sym.typ=typesym) then
  3758. begin
  3759. result:=ttypesym(sym);
  3760. exit;
  3761. end
  3762. else
  3763. begin
  3764. if throwerror then
  3765. message2(cg_f_unknown_type_in_unit,typename,unitname);
  3766. result:=nil;
  3767. end;
  3768. end;
  3769. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  3770. var
  3771. s: string;
  3772. list: TFPObjectList;
  3773. i: integer;
  3774. st: tsymtable;
  3775. begin
  3776. result:=false;
  3777. odef:=nil;
  3778. { when there are no helpers active currently then we don't need to do
  3779. anything }
  3780. if current_module.extendeddefs.count=0 then
  3781. exit;
  3782. if (df_genconstraint in pd.defoptions) then
  3783. begin
  3784. { if we have a constraint for a class type or a single interface we
  3785. use that to resolve helpers at declaration time of the generic,
  3786. otherwise there can't be any helpers as the type isn't known yet }
  3787. if pd.typ=objectdef then
  3788. pd:=tobjectdef(pd).getparentdef
  3789. else
  3790. exit;
  3791. end;
  3792. { no helpers for anonymous types }
  3793. if ((pd.typ in [recorddef,objectdef]) and
  3794. (
  3795. not assigned(tabstractrecorddef(pd).objrealname) or
  3796. (tabstractrecorddef(pd).objrealname^='')
  3797. )
  3798. ) or
  3799. not assigned(pd.typesym) then
  3800. exit;
  3801. { if pd is defined inside a procedure we must not use make_mangledname
  3802. (as a helper may not be defined in a procedure this is no problem...)}
  3803. st:=pd.owner;
  3804. while st.symtabletype in [objectsymtable,recordsymtable] do
  3805. st:=st.defowner.owner;
  3806. if st.symtabletype=localsymtable then
  3807. exit;
  3808. { the mangled name is used as the key for tmodule.extendeddefs }
  3809. s:=generate_objectpascal_helper_key(pd);
  3810. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  3811. if assigned(list) and (list.count>0) then
  3812. begin
  3813. i:=list.count-1;
  3814. repeat
  3815. odef:=tobjectdef(list[list.count-1]);
  3816. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  3817. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  3818. dec(i);
  3819. until result or (i<0);
  3820. if not result then
  3821. { just to be sure that noone uses odef }
  3822. odef:=nil;
  3823. end;
  3824. end;
  3825. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3826. var
  3827. hashedid : THashedIDString;
  3828. classh : tobjectdef;
  3829. i : integer;
  3830. pdef : tprocdef;
  3831. begin
  3832. result:=false;
  3833. { if there is no class helper for the class then there is no need to
  3834. search further }
  3835. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  3836. exit;
  3837. hashedid.id:=s;
  3838. repeat
  3839. srsymtable:=classh.symtable;
  3840. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3841. if srsym<>nil then
  3842. begin
  3843. case srsym.typ of
  3844. procsym:
  3845. begin
  3846. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3847. begin
  3848. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  3849. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  3850. continue;
  3851. { we need to know if a procedure references symbols
  3852. in the static symtable, because then it can't be
  3853. inlined from outside this unit }
  3854. if assigned(current_procinfo) and
  3855. (srsym.owner.symtabletype=staticsymtable) then
  3856. include(current_procinfo.flags,pi_uses_static_symtable);
  3857. { the first found method wins }
  3858. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3859. srsymtable:=srsym.owner;
  3860. addsymref(srsym);
  3861. result:=true;
  3862. exit;
  3863. end;
  3864. end;
  3865. typesym,
  3866. fieldvarsym,
  3867. constsym,
  3868. enumsym,
  3869. undefinedsym,
  3870. propertysym:
  3871. begin
  3872. addsymref(srsym);
  3873. result:=true;
  3874. exit;
  3875. end;
  3876. else
  3877. internalerror(2014041101);
  3878. end;
  3879. end;
  3880. { try the helper parent if available }
  3881. classh:=classh.childof;
  3882. until classh=nil;
  3883. srsym:=nil;
  3884. srsymtable:=nil;
  3885. end;
  3886. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3887. var
  3888. searchst : tsymtable;
  3889. searchsym : tsym;
  3890. hashedid : THashedIDString;
  3891. stackitem : psymtablestackitem;
  3892. i : longint;
  3893. founddefowner,
  3894. defowner : tobjectdef;
  3895. begin
  3896. hashedid.id:=class_helper_prefix+s;
  3897. stackitem:=symtablestack.stack;
  3898. result:=false;
  3899. srsym:=nil;
  3900. srsymtable:=nil;
  3901. founddefowner:=nil;
  3902. while assigned(stackitem) do
  3903. begin
  3904. searchst:=stackitem^.symtable;
  3905. searchsym:=tsym(searchst.FindWithHash(hashedid));
  3906. if assigned(searchsym) then
  3907. begin
  3908. if not(searchst.symtabletype in [globalsymtable,staticsymtable]) or
  3909. not(searchsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3910. (searchsym.typ<>procsym) then
  3911. internalerror(2009111505);
  3912. { check whether this procsym includes a helper for this particular class }
  3913. for i:=0 to tprocsym(searchsym).procdeflist.count-1 do
  3914. begin
  3915. { does pd inherit from (or is the same as) the class
  3916. that this method's category extended?
  3917. Warning: this list contains both category and objcclass methods
  3918. (for id.randommethod), so only check category methods here
  3919. }
  3920. defowner:=tobjectdef(tprocdef(tprocsym(searchsym).procdeflist[i]).owner.defowner);
  3921. if is_objccategory(defowner) and
  3922. def_is_related(pd,defowner.childof) then
  3923. begin
  3924. { we need to know if a procedure references symbols
  3925. in the static symtable, because then it can't be
  3926. inlined from outside this unit }
  3927. if assigned(current_procinfo) and
  3928. (searchsym.owner.symtabletype=staticsymtable) then
  3929. include(current_procinfo.flags,pi_uses_static_symtable);
  3930. { Stop looking if this is a category that extends the specified
  3931. class itself. There might be other categories that extend this,
  3932. but that doesn't matter. If it extens a parent, keep looking
  3933. in case we find the symbol in a category that extends this class
  3934. (or a closer parent).
  3935. }
  3936. if not result or
  3937. def_is_related(defowner.childof,founddefowner) then
  3938. begin
  3939. founddefowner:=defowner.childof;
  3940. srsym:=tprocdef(tprocsym(searchsym).procdeflist[i]).procsym;
  3941. srsymtable:=srsym.owner;
  3942. result:=true;
  3943. if pd=founddefowner then
  3944. begin
  3945. addsymref(srsym);
  3946. exit;
  3947. end;
  3948. end;
  3949. end;
  3950. end;
  3951. end;
  3952. stackitem:=stackitem^.next;
  3953. end;
  3954. if result then
  3955. begin
  3956. addsymref(srsym);
  3957. exit;
  3958. end;
  3959. end;
  3960. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3961. var
  3962. hashedid : THashedIDString;
  3963. stackitem : psymtablestackitem;
  3964. i : longint;
  3965. begin
  3966. hashedid.id:=class_helper_prefix+s;
  3967. stackitem:=symtablestack.stack;
  3968. while assigned(stackitem) do
  3969. begin
  3970. srsymtable:=stackitem^.symtable;
  3971. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3972. if assigned(srsym) then
  3973. begin
  3974. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3975. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3976. (srsym.typ<>procsym) then
  3977. internalerror(2009112005);
  3978. { check whether this procsym includes a helper for this particular class }
  3979. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3980. begin
  3981. { we need to know if a procedure references symbols
  3982. in the static symtable, because then it can't be
  3983. inlined from outside this unit }
  3984. if assigned(current_procinfo) and
  3985. (srsym.owner.symtabletype=staticsymtable) then
  3986. include(current_procinfo.flags,pi_uses_static_symtable);
  3987. { no need to keep looking. There might be other
  3988. methods with the same name, but that doesn't matter
  3989. as far as the basic procsym is concerned.
  3990. }
  3991. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3992. { We need the symtable in which the classhelper-like sym
  3993. is located, not the objectdef. The reason is that the
  3994. callnode will climb the symtablestack until it encounters
  3995. this symtable to start looking for overloads (and it won't
  3996. find the objectsymtable in which this method sym is
  3997. located
  3998. srsymtable:=srsym.owner;
  3999. }
  4000. addsymref(srsym);
  4001. result:=true;
  4002. exit;
  4003. end;
  4004. end;
  4005. stackitem:=stackitem^.next;
  4006. end;
  4007. srsym:=nil;
  4008. srsymtable:=nil;
  4009. result:=false;
  4010. end;
  4011. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  4012. { searches n in symtable of pd and all anchestors }
  4013. var
  4014. srsymtable : tsymtable;
  4015. begin
  4016. { in case this is a formal class, first find the real definition }
  4017. if (oo_is_formal in pd.objectoptions) then
  4018. pd:=find_real_class_definition(tobjectdef(pd),true);
  4019. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  4020. exit;
  4021. result:=search_struct_member_no_helper(pd,s);
  4022. if assigned(result) then
  4023. exit;
  4024. { not found, now look for class helpers }
  4025. if is_objcclass(pd) then
  4026. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  4027. end;
  4028. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  4029. var
  4030. hashedid : THashedIDString;
  4031. srsym : tsym;
  4032. begin
  4033. hashedid.id:=s;
  4034. while assigned(pd) do
  4035. begin
  4036. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  4037. if assigned(srsym) then
  4038. begin
  4039. result:=srsym;
  4040. exit;
  4041. end;
  4042. if pd.typ=objectdef then
  4043. pd:=tobjectdef(pd).childof
  4044. else
  4045. pd:=nil;
  4046. end;
  4047. result:=nil;
  4048. end;
  4049. function search_macro(const s : string):tsym;
  4050. var
  4051. stackitem : psymtablestackitem;
  4052. hashedid : THashedIDString;
  4053. srsym : tsym;
  4054. begin
  4055. hashedid.id:=s;
  4056. { First search the localmacrosymtable before searching the
  4057. global macrosymtables from the units }
  4058. if assigned(current_module) then
  4059. begin
  4060. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  4061. if assigned(srsym) then
  4062. begin
  4063. result:= srsym;
  4064. exit;
  4065. end;
  4066. end;
  4067. stackitem:=macrosymtablestack.stack;
  4068. while assigned(stackitem) do
  4069. begin
  4070. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  4071. if assigned(srsym) then
  4072. begin
  4073. result:= srsym;
  4074. exit;
  4075. end;
  4076. stackitem:=stackitem^.next;
  4077. end;
  4078. result:= nil;
  4079. end;
  4080. function defined_macro(const s : string):boolean;
  4081. var
  4082. mac: tmacro;
  4083. begin
  4084. mac:=tmacro(search_macro(s));
  4085. if assigned(mac) then
  4086. begin
  4087. mac.is_used:=true;
  4088. defined_macro:=mac.defined;
  4089. end
  4090. else
  4091. defined_macro:=false;
  4092. end;
  4093. {****************************************************************************
  4094. Object Helpers
  4095. ****************************************************************************}
  4096. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  4097. { returns the default property of a class, searches also anchestors }
  4098. var
  4099. _defaultprop : tpropertysym;
  4100. helperpd : tobjectdef;
  4101. begin
  4102. _defaultprop:=nil;
  4103. { first search in helper's hierarchy }
  4104. if search_last_objectpascal_helper(pd,nil,helperpd) then
  4105. while assigned(helperpd) do
  4106. begin
  4107. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  4108. if assigned(_defaultprop) then
  4109. break;
  4110. helperpd:=helperpd.childof;
  4111. end;
  4112. if assigned(_defaultprop) then
  4113. begin
  4114. search_default_property:=_defaultprop;
  4115. exit;
  4116. end;
  4117. { now search in the type's hierarchy itself }
  4118. while assigned(pd) do
  4119. begin
  4120. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  4121. if assigned(_defaultprop) then
  4122. break;
  4123. if (pd.typ=objectdef) then
  4124. pd:=tobjectdef(pd).childof
  4125. else
  4126. break;
  4127. end;
  4128. search_default_property:=_defaultprop;
  4129. end;
  4130. {****************************************************************************
  4131. Macro Helpers
  4132. ****************************************************************************}
  4133. procedure def_system_macro(const name : string);
  4134. var
  4135. mac : tmacro;
  4136. s: string;
  4137. begin
  4138. if name = '' then
  4139. internalerror(2004121202);
  4140. s:= upper(name);
  4141. mac:=tmacro(search_macro(s));
  4142. if not assigned(mac) then
  4143. begin
  4144. mac:=tmacro.create(s);
  4145. if assigned(current_module) then
  4146. current_module.localmacrosymtable.insert(mac)
  4147. else
  4148. initialmacrosymtable.insert(mac);
  4149. end;
  4150. Message1(parser_c_macro_defined,mac.name);
  4151. mac.defined:=true;
  4152. end;
  4153. procedure set_system_macro(const name, value : string);
  4154. var
  4155. mac : tmacro;
  4156. s: string;
  4157. begin
  4158. if name = '' then
  4159. internalerror(2004121203);
  4160. s:= upper(name);
  4161. mac:=tmacro(search_macro(s));
  4162. if not assigned(mac) then
  4163. begin
  4164. mac:=tmacro.create(s);
  4165. if assigned(current_module) then
  4166. current_module.localmacrosymtable.insert(mac)
  4167. else
  4168. initialmacrosymtable.insert(mac);
  4169. end
  4170. else
  4171. begin
  4172. mac.is_compiler_var:=false;
  4173. if assigned(mac.buftext) then
  4174. freemem(mac.buftext,mac.buflen);
  4175. end;
  4176. Message2(parser_c_macro_set_to,mac.name,value);
  4177. mac.buflen:=length(value);
  4178. getmem(mac.buftext,mac.buflen);
  4179. move(value[1],mac.buftext^,mac.buflen);
  4180. mac.defined:=true;
  4181. end;
  4182. procedure set_system_compvar(const name, value : string);
  4183. var
  4184. mac : tmacro;
  4185. s: string;
  4186. begin
  4187. if name = '' then
  4188. internalerror(2004121204);
  4189. s:= upper(name);
  4190. mac:=tmacro(search_macro(s));
  4191. if not assigned(mac) then
  4192. begin
  4193. mac:=tmacro.create(s);
  4194. mac.is_compiler_var:=true;
  4195. if assigned(current_module) then
  4196. current_module.localmacrosymtable.insert(mac)
  4197. else
  4198. initialmacrosymtable.insert(mac);
  4199. end
  4200. else
  4201. begin
  4202. mac.is_compiler_var:=true;
  4203. if assigned(mac.buftext) then
  4204. freemem(mac.buftext,mac.buflen);
  4205. end;
  4206. Message2(parser_c_macro_set_to,mac.name,value);
  4207. mac.buflen:=length(value);
  4208. getmem(mac.buftext,mac.buflen);
  4209. move(value[1],mac.buftext^,mac.buflen);
  4210. mac.defined:=true;
  4211. end;
  4212. procedure undef_system_macro(const name : string);
  4213. var
  4214. mac : tmacro;
  4215. s: string;
  4216. begin
  4217. if name = '' then
  4218. internalerror(2004121205);
  4219. s:= upper(name);
  4220. mac:=tmacro(search_macro(s));
  4221. if not assigned(mac) then
  4222. {If not found, then it's already undefined.}
  4223. else
  4224. begin
  4225. Message1(parser_c_macro_undefined,mac.name);
  4226. mac.defined:=false;
  4227. mac.is_compiler_var:=false;
  4228. { delete old definition }
  4229. if assigned(mac.buftext) then
  4230. begin
  4231. freemem(mac.buftext,mac.buflen);
  4232. mac.buftext:=nil;
  4233. end;
  4234. end;
  4235. end;
  4236. {$ifdef UNITALIASES}
  4237. {****************************************************************************
  4238. TUNIT_ALIAS
  4239. ****************************************************************************}
  4240. constructor tunit_alias.create(const n:string);
  4241. var
  4242. i : longint;
  4243. begin
  4244. i:=pos('=',n);
  4245. if i=0 then
  4246. fail;
  4247. inherited createname(Copy(n,1,i-1));
  4248. newname:=stringdup(Copy(n,i+1,255));
  4249. end;
  4250. destructor tunit_alias.destroy;
  4251. begin
  4252. stringdispose(newname);
  4253. inherited destroy;
  4254. end;
  4255. procedure addunitalias(const n:string);
  4256. begin
  4257. unitaliases^.insert(tunit_alias,init(Upper(n))));
  4258. end;
  4259. function getunitalias(const n:string):string;
  4260. var
  4261. p : punit_alias;
  4262. begin
  4263. p:=punit_alias(unitaliases^.Find(Upper(n)));
  4264. if assigned(p) then
  4265. getunitalias:=punit_alias(p).newname^
  4266. else
  4267. getunitalias:=n;
  4268. end;
  4269. {$endif UNITALIASES}
  4270. {****************************************************************************
  4271. Init/Done Symtable
  4272. ****************************************************************************}
  4273. procedure InitSymtable;
  4274. begin
  4275. { Reset symbolstack }
  4276. symtablestack:=nil;
  4277. systemunit:=nil;
  4278. { create error syms and def }
  4279. generrorsym:=terrorsym.create;
  4280. generrordef:=cerrordef.create;
  4281. { macros }
  4282. initialmacrosymtable:=tmacrosymtable.create(false);
  4283. macrosymtablestack:=TSymtablestack.create;
  4284. macrosymtablestack.push(initialmacrosymtable);
  4285. {$ifdef UNITALIASES}
  4286. { unit aliases }
  4287. unitaliases:=TFPHashObjectList.create;
  4288. {$endif}
  4289. { set some global vars to nil, might be important for the ide }
  4290. class_tobject:=nil;
  4291. interface_iunknown:=nil;
  4292. interface_idispatch:=nil;
  4293. rec_tguid:=nil;
  4294. rec_jmp_buf:=nil;
  4295. rec_exceptaddr:=nil;
  4296. objc_metaclasstype:=nil;
  4297. objc_superclasstype:=nil;
  4298. objc_idtype:=nil;
  4299. objc_seltype:=nil;
  4300. objc_objecttype:=nil;
  4301. dupnr:=0;
  4302. end;
  4303. procedure DoneSymtable;
  4304. begin
  4305. generrorsym.owner:=nil;
  4306. generrorsym.free;
  4307. generrordef.owner:=nil;
  4308. generrordef.free;
  4309. initialmacrosymtable.free;
  4310. macrosymtablestack.free;
  4311. {$ifdef UNITALIASES}
  4312. unitaliases.free;
  4313. {$endif}
  4314. end;
  4315. end.