symtable.pas 154 KB

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