symtable.pas 95 KB

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