symtable.pas 100 KB

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