symtable.pas 75 KB

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