symtable.pas 153 KB

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