symtable.pas 94 KB

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