symtable.pas 95 KB

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