symtable.pas 180 KB

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