symtable.pas 98 KB

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