symtable.pas 114 KB

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