symtable.pas 96 KB

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