symtable.pas 187 KB

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