symtable.pas 167 KB

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