symtable.pas 112 KB

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