symtable.pas 153 KB

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