symtable.pas 183 KB

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