symtable.pas 98 KB

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