symtable.pas 170 KB

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