symtable.pas 171 KB

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