symtable.pas 158 KB

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