symtable.pas 114 KB

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