2
0

symtable.pas 159 KB

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