symtable.pas 131 KB

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