symtable.pas 74 KB

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