symtable.pas 171 KB

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