symtable.pas 138 KB

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