symtable.pas 98 KB

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