symtable.pas 172 KB

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