symtable.pas 104 KB

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