symtable.pas 92 KB

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