symtable.pas 95 KB

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