symtable.pas 140 KB

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