2
0

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. result:=inherited checkduplicate(hashedid,sym);
  1157. end;
  1158. {****************************************************************************
  1159. TAbstractLocalSymtable
  1160. ****************************************************************************}
  1161. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1162. var
  1163. oldtyp : byte;
  1164. begin
  1165. oldtyp:=ppufile.entrytyp;
  1166. ppufile.entrytyp:=subentryid;
  1167. inherited ppuwrite(ppufile);
  1168. ppufile.entrytyp:=oldtyp;
  1169. end;
  1170. function tabstractlocalsymtable.count_locals:longint;
  1171. var
  1172. i : longint;
  1173. sym : tsym;
  1174. begin
  1175. result:=0;
  1176. for i:=0 to SymList.Count-1 do
  1177. begin
  1178. sym:=tsym(SymList[i]);
  1179. { Count only varsyms, but ignore the funcretsym }
  1180. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1181. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1182. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1183. (tstoredsym(sym).refs>0)) then
  1184. inc(result);
  1185. end;
  1186. end;
  1187. {****************************************************************************
  1188. TLocalSymtable
  1189. ****************************************************************************}
  1190. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1191. begin
  1192. inherited create('');
  1193. defowner:=adefowner;
  1194. symtabletype:=localsymtable;
  1195. symtablelevel:=level;
  1196. end;
  1197. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1198. var
  1199. hsym : tsym;
  1200. begin
  1201. if not assigned(defowner) or
  1202. (defowner.typ<>procdef) then
  1203. internalerror(200602042);
  1204. result:=false;
  1205. hsym:=tsym(FindWithHash(hashedid));
  1206. if assigned(hsym) then
  1207. begin
  1208. { a local and the function can have the same
  1209. name in TP and Delphi, but RESULT not }
  1210. if (m_duplicate_names in current_settings.modeswitches) and
  1211. (hsym.typ in [absolutevarsym,localvarsym]) and
  1212. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1213. not((m_result in current_settings.modeswitches) and
  1214. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1215. HideSym(hsym)
  1216. else
  1217. DuplicateSym(hashedid,sym,hsym);
  1218. result:=true;
  1219. exit;
  1220. end;
  1221. { check also parasymtable, this needs to be done here because
  1222. of the special situation with the funcret sym that needs to be
  1223. hidden for tp and delphi modes }
  1224. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1225. if assigned(hsym) then
  1226. begin
  1227. { a local and the function can have the same
  1228. name in TP and Delphi, but RESULT not }
  1229. if (m_duplicate_names in current_settings.modeswitches) and
  1230. (sym.typ in [absolutevarsym,localvarsym]) and
  1231. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1232. not((m_result in current_settings.modeswitches) and
  1233. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1234. Hidesym(sym)
  1235. else
  1236. DuplicateSym(hashedid,sym,hsym);
  1237. result:=true;
  1238. exit;
  1239. end;
  1240. { check ObjectSymtable, skip this for funcret sym because
  1241. that will always be positive because it has the same name
  1242. as the procsym }
  1243. if not is_funcret_sym(sym) and
  1244. (defowner.typ=procdef) and
  1245. assigned(tprocdef(defowner).struct) and
  1246. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1247. (
  1248. not(m_delphi in current_settings.modeswitches) or
  1249. is_object(tprocdef(defowner).struct)
  1250. ) then
  1251. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1252. end;
  1253. {****************************************************************************
  1254. TParaSymtable
  1255. ****************************************************************************}
  1256. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1257. begin
  1258. inherited create('');
  1259. readonly:=false;
  1260. defowner:=adefowner;
  1261. symtabletype:=parasymtable;
  1262. symtablelevel:=level;
  1263. end;
  1264. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1265. begin
  1266. result:=inherited checkduplicate(hashedid,sym);
  1267. if result then
  1268. exit;
  1269. if not(m_duplicate_names in current_settings.modeswitches) and
  1270. assigned(defowner) and (defowner.typ=procdef) and
  1271. assigned(tprocdef(defowner).struct) and
  1272. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1273. (
  1274. not(m_delphi in current_settings.modeswitches) or
  1275. is_object(tprocdef(defowner).struct)
  1276. ) then
  1277. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1278. end;
  1279. procedure tparasymtable.insertdef(def: TDefEntry);
  1280. begin
  1281. if readonly then
  1282. defowner.owner.insertdef(def)
  1283. else
  1284. inherited insertdef(def);
  1285. end;
  1286. {****************************************************************************
  1287. TAbstractUniTSymtable
  1288. ****************************************************************************}
  1289. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1290. begin
  1291. inherited create(n);
  1292. moduleid:=id;
  1293. end;
  1294. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1295. var
  1296. hsym : tsym;
  1297. begin
  1298. result:=false;
  1299. hsym:=tsym(FindWithHash(hashedid));
  1300. if assigned(hsym) then
  1301. begin
  1302. if hsym.typ=symconst.namespacesym then
  1303. begin
  1304. case sym.typ of
  1305. symconst.namespacesym:;
  1306. symconst.unitsym:
  1307. begin
  1308. 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 }
  1309. tnamespacesym(hsym).unitsym:=tsym(sym);
  1310. end
  1311. else
  1312. HideSym(hsym);
  1313. end;
  1314. end
  1315. else
  1316. { In delphi (contrary to TP) you can have a symbol with the same name as the
  1317. unit, the unit can then not be accessed anymore using
  1318. <unit>.<id>, so we can hide the symbol.
  1319. Do the same if we add a namespace and there is a unit with the same name }
  1320. if (hsym.typ=symconst.unitsym) and
  1321. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  1322. begin
  1323. HideSym(hsym);
  1324. if sym.typ=symconst.namespacesym then
  1325. tnamespacesym(sym).unitsym:=tsym(hsym);
  1326. end
  1327. else
  1328. DuplicateSym(hashedid,sym,hsym);
  1329. result:=true;
  1330. exit;
  1331. end;
  1332. end;
  1333. function tabstractuniTSymtable.iscurrentunit:boolean;
  1334. begin
  1335. result:=assigned(current_module) and
  1336. (
  1337. (current_module.globalsymtable=self) or
  1338. (current_module.localsymtable=self)
  1339. );
  1340. end;
  1341. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  1342. var
  1343. p:integer;
  1344. n,ns:string;
  1345. oldsym:TSymEntry;
  1346. begin
  1347. insert(sym);
  1348. n:=sym.realname;
  1349. p:=pos('.',n);
  1350. ns:='';
  1351. while p>0 do
  1352. begin
  1353. if ns='' then
  1354. ns:=copy(n,1,p-1)
  1355. else
  1356. ns:=ns+'.'+copy(n,1,p-1);
  1357. system.delete(n,1,p);
  1358. oldsym:=Find(upper(ns));
  1359. if not Assigned(oldsym) or (oldsym.typ<>namespacesym) then
  1360. insert(tnamespacesym.create(ns));
  1361. p:=pos('.',n);
  1362. end;
  1363. end;
  1364. {****************************************************************************
  1365. TStaticSymtable
  1366. ****************************************************************************}
  1367. constructor tstaticsymtable.create(const n : string;id:word);
  1368. begin
  1369. inherited create(n,id);
  1370. symtabletype:=staticsymtable;
  1371. symtablelevel:=main_program_level;
  1372. end;
  1373. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1374. begin
  1375. inherited ppuload(ppufile);
  1376. { now we can deref the syms and defs }
  1377. deref;
  1378. end;
  1379. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1380. begin
  1381. inherited ppuwrite(ppufile);
  1382. end;
  1383. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1384. begin
  1385. result:=inherited checkduplicate(hashedid,sym);
  1386. if not result and
  1387. (current_module.localsymtable=self) and
  1388. assigned(current_module.globalsymtable) then
  1389. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1390. end;
  1391. {****************************************************************************
  1392. TGlobalSymtable
  1393. ****************************************************************************}
  1394. constructor tglobalsymtable.create(const n : string;id:word);
  1395. begin
  1396. inherited create(n,id);
  1397. symtabletype:=globalsymtable;
  1398. symtablelevel:=main_program_level;
  1399. end;
  1400. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1401. begin
  1402. inherited ppuload(ppufile);
  1403. { now we can deref the syms and defs }
  1404. deref;
  1405. end;
  1406. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1407. begin
  1408. { write the symtable entries }
  1409. inherited ppuwrite(ppufile);
  1410. end;
  1411. {****************************************************************************
  1412. TWITHSYMTABLE
  1413. ****************************************************************************}
  1414. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1415. begin
  1416. inherited create('');
  1417. symtabletype:=withsymtable;
  1418. withrefnode:=refnode;
  1419. { Replace SymList with the passed symlist }
  1420. SymList.free;
  1421. SymList:=ASymList;
  1422. defowner:=aowner;
  1423. end;
  1424. destructor twithsymtable.destroy;
  1425. begin
  1426. withrefnode.free;
  1427. { Disable SymList because we don't Own it }
  1428. SymList:=nil;
  1429. inherited destroy;
  1430. end;
  1431. procedure twithsymtable.clear;
  1432. begin
  1433. { remove no entry from a withsymtable as it is only a pointer to the
  1434. recorddef or objectdef symtable }
  1435. end;
  1436. procedure twithsymtable.insertdef(def:TDefEntry);
  1437. begin
  1438. { Definitions can't be registered in the withsymtable
  1439. because the withsymtable is removed after the with block.
  1440. We can't easily solve it here because the next symtable in the
  1441. stack is not known. }
  1442. internalerror(200602046);
  1443. end;
  1444. {****************************************************************************
  1445. TSTT_ExceptionSymtable
  1446. ****************************************************************************}
  1447. constructor tstt_excepTSymtable.create;
  1448. begin
  1449. inherited create('');
  1450. symtabletype:=stt_excepTSymtable;
  1451. end;
  1452. {****************************************************************************
  1453. TMacroSymtable
  1454. ****************************************************************************}
  1455. constructor tmacrosymtable.create(exported: boolean);
  1456. begin
  1457. inherited create('');
  1458. if exported then
  1459. symtabletype:=exportedmacrosymtable
  1460. else
  1461. symtabletype:=localmacrosymtable;
  1462. symtablelevel:=main_program_level;
  1463. end;
  1464. {****************************************************************************
  1465. TEnumSymtable
  1466. ****************************************************************************}
  1467. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  1468. var
  1469. value: longint;
  1470. def: tenumdef;
  1471. begin
  1472. // defowner = nil only when we are loading from ppu
  1473. if defowner<>nil then
  1474. begin
  1475. { First entry? Then we need to set the minval }
  1476. value:=tenumsym(sym).value;
  1477. def:=tenumdef(defowner);
  1478. if SymList.count=0 then
  1479. begin
  1480. if value>0 then
  1481. def.has_jumps:=true;
  1482. def.setmin(value);
  1483. def.setmax(value);
  1484. end
  1485. else
  1486. begin
  1487. { check for jumps }
  1488. if value>def.max+1 then
  1489. def.has_jumps:=true;
  1490. { update low and high }
  1491. if def.min>value then
  1492. def.setmin(value);
  1493. if def.max<value then
  1494. def.setmax(value);
  1495. end;
  1496. end;
  1497. inherited insert(sym, checkdup);
  1498. end;
  1499. constructor tenumsymtable.create(adefowner: tdef);
  1500. begin
  1501. inherited Create('');
  1502. symtabletype:=enumsymtable;
  1503. defowner:=adefowner;
  1504. end;
  1505. {****************************************************************************
  1506. TArraySymtable
  1507. ****************************************************************************}
  1508. procedure tarraysymtable.insertdef(def: TDefEntry);
  1509. begin
  1510. { Enums must also be available outside the record scope,
  1511. insert in the owner of this symtable }
  1512. if def.typ=enumdef then
  1513. defowner.owner.insertdef(def)
  1514. else
  1515. inherited insertdef(def);
  1516. end;
  1517. constructor tarraysymtable.create(adefowner: tdef);
  1518. begin
  1519. inherited Create('');
  1520. symtabletype:=arraysymtable;
  1521. defowner:=adefowner;
  1522. end;
  1523. {*****************************************************************************
  1524. Helper Routines
  1525. *****************************************************************************}
  1526. function FullTypeName(def,otherdef:tdef):string;
  1527. var
  1528. s1,s2 : string;
  1529. begin
  1530. if def.typ in [objectdef,recorddef] then
  1531. s1:=tabstractrecorddef(def).RttiName
  1532. else
  1533. s1:=def.typename;
  1534. { When the names are the same try to include the unit name }
  1535. if assigned(otherdef) and
  1536. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1537. begin
  1538. s2:=otherdef.typename;
  1539. if upper(s1)=upper(s2) then
  1540. s1:=def.owner.realname^+'.'+s1;
  1541. end;
  1542. FullTypeName:=s1;
  1543. end;
  1544. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  1545. begin
  1546. result:='';
  1547. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  1548. begin
  1549. if (result='') then
  1550. result:=symtable.name^
  1551. else
  1552. result:=symtable.name^+delimiter+result;
  1553. symtable:=symtable.defowner.owner;
  1554. end;
  1555. end;
  1556. procedure incompatibletypes(def1,def2:tdef);
  1557. begin
  1558. { When there is an errordef there is already an error message show }
  1559. if (def2.typ=errordef) or
  1560. (def1.typ=errordef) then
  1561. exit;
  1562. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1563. end;
  1564. procedure hidesym(sym:TSymEntry);
  1565. begin
  1566. sym.realname:='$hidden'+sym.realname;
  1567. tsym(sym).visibility:=vis_hidden;
  1568. end;
  1569. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1570. var
  1571. st : TSymtable;
  1572. begin
  1573. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1574. { Write hint where the original symbol was found }
  1575. st:=finduniTSymtable(origsym.owner);
  1576. with tsym(origsym).fileinfo do
  1577. begin
  1578. if assigned(st) and
  1579. (st.symtabletype=globalsymtable) and
  1580. st.iscurrentunit then
  1581. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1582. else if assigned(st.name) then
  1583. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1584. end;
  1585. { Rename duplicate sym to an unreachable name, but it can be
  1586. inserted in the symtable without errors }
  1587. inc(dupnr);
  1588. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1589. if assigned(dupsym) then
  1590. include(tsym(dupsym).symoptions,sp_implicitrename);
  1591. end;
  1592. {*****************************************************************************
  1593. Search
  1594. *****************************************************************************}
  1595. procedure addsymref(sym:tsym);
  1596. begin
  1597. { symbol uses count }
  1598. sym.IncRefCount;
  1599. { unit uses count }
  1600. if assigned(current_module) and
  1601. (sym.owner.symtabletype=globalsymtable) then
  1602. begin
  1603. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1604. internalerror(200501152);
  1605. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1606. end;
  1607. end;
  1608. function is_owned_by(childdef,ownerdef:tdef):boolean;
  1609. begin
  1610. result:=childdef=ownerdef;
  1611. if not result and assigned(childdef.owner.defowner) then
  1612. result:=is_owned_by(tdef(childdef.owner.defowner),ownerdef);
  1613. end;
  1614. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1615. var
  1616. symownerdef : tabstractrecorddef;
  1617. begin
  1618. result:=false;
  1619. { Get objdectdef owner of the symtable for the is_related checks }
  1620. if not assigned(symst) or
  1621. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  1622. internalerror(200810285);
  1623. symownerdef:=tabstractrecorddef(symst.defowner);
  1624. case symvisibility of
  1625. vis_private :
  1626. begin
  1627. { private symbols are allowed when we are in the same
  1628. module as they are defined }
  1629. result:=(
  1630. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1631. (symownerdef.owner.iscurrentunit)
  1632. ) or
  1633. ( // the case of specialize inside the generic declaration and nested types
  1634. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1635. (
  1636. assigned(current_structdef) and
  1637. (
  1638. (current_structdef=symownerdef) or
  1639. (current_structdef.owner.iscurrentunit)
  1640. )
  1641. ) or
  1642. (
  1643. not assigned(current_structdef) and
  1644. (symownerdef.owner.iscurrentunit)
  1645. )
  1646. );
  1647. end;
  1648. vis_strictprivate :
  1649. begin
  1650. result:=assigned(current_structdef) and
  1651. is_owned_by(current_structdef,symownerdef);
  1652. end;
  1653. vis_strictprotected :
  1654. begin
  1655. result:=(
  1656. assigned(current_structdef) and
  1657. (current_structdef.is_related(symownerdef) or
  1658. is_owned_by(current_structdef,symownerdef))
  1659. ) or
  1660. (
  1661. { helpers can access strict protected symbols }
  1662. is_objectpascal_helper(contextobjdef) and
  1663. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1664. );
  1665. end;
  1666. vis_protected :
  1667. begin
  1668. { protected symbols are visible in the module that defines them and
  1669. also visible to related objects. The related object must be defined
  1670. in the current module }
  1671. result:=(
  1672. (
  1673. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1674. (symownerdef.owner.iscurrentunit)
  1675. ) or
  1676. (
  1677. assigned(contextobjdef) and
  1678. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  1679. (contextobjdef.owner.iscurrentunit) and
  1680. contextobjdef.is_related(symownerdef)
  1681. ) or
  1682. ( // the case of specialize inside the generic declaration and nested types
  1683. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1684. (
  1685. assigned(current_structdef) and
  1686. (
  1687. (current_structdef=symownerdef) or
  1688. (current_structdef.owner.iscurrentunit)
  1689. )
  1690. ) or
  1691. (
  1692. not assigned(current_structdef) and
  1693. (symownerdef.owner.iscurrentunit)
  1694. ) or
  1695. (
  1696. { helpers can access protected symbols }
  1697. is_objectpascal_helper(contextobjdef) and
  1698. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1699. )
  1700. )
  1701. );
  1702. end;
  1703. vis_public,
  1704. vis_published :
  1705. result:=true;
  1706. end;
  1707. end;
  1708. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  1709. begin
  1710. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1711. end;
  1712. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  1713. var
  1714. i : longint;
  1715. pd : tprocdef;
  1716. begin
  1717. if sym.typ=procsym then
  1718. begin
  1719. { A procsym is visible, when there is at least one of the procdefs visible }
  1720. result:=false;
  1721. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1722. begin
  1723. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1724. if (pd.owner=sym.owner) and
  1725. is_visible_for_object(pd,contextobjdef) then
  1726. begin
  1727. result:=true;
  1728. exit;
  1729. end;
  1730. end;
  1731. end
  1732. else
  1733. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1734. end;
  1735. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1736. var
  1737. hashedid : THashedIDString;
  1738. contextstructdef : tabstractrecorddef;
  1739. stackitem : psymtablestackitem;
  1740. begin
  1741. result:=false;
  1742. hashedid.id:=s;
  1743. stackitem:=symtablestack.stack;
  1744. while assigned(stackitem) do
  1745. begin
  1746. srsymtable:=stackitem^.symtable;
  1747. if (srsymtable.symtabletype=objectsymtable) then
  1748. begin
  1749. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,true) then
  1750. begin
  1751. result:=true;
  1752. exit;
  1753. end;
  1754. end
  1755. else
  1756. begin
  1757. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1758. if assigned(srsym) then
  1759. begin
  1760. { use the class from withsymtable only when it is
  1761. defined in this unit }
  1762. if (srsymtable.symtabletype=withsymtable) and
  1763. assigned(srsymtable.defowner) and
  1764. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  1765. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1766. (srsymtable.defowner.owner.iscurrentunit) then
  1767. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  1768. else
  1769. contextstructdef:=current_structdef;
  1770. if not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  1771. is_visible_for_object(srsym,contextstructdef) then
  1772. begin
  1773. { we need to know if a procedure references symbols
  1774. in the static symtable, because then it can't be
  1775. inlined from outside this unit }
  1776. if assigned(current_procinfo) and
  1777. (srsym.owner.symtabletype=staticsymtable) then
  1778. include(current_procinfo.flags,pi_uses_static_symtable);
  1779. addsymref(srsym);
  1780. result:=true;
  1781. exit;
  1782. end;
  1783. end;
  1784. end;
  1785. stackitem:=stackitem^.next;
  1786. end;
  1787. srsym:=nil;
  1788. srsymtable:=nil;
  1789. end;
  1790. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1791. var
  1792. hashedid : THashedIDString;
  1793. stackitem : psymtablestackitem;
  1794. classh : tobjectdef;
  1795. begin
  1796. result:=false;
  1797. hashedid.id:=s;
  1798. stackitem:=symtablestack.stack;
  1799. while assigned(stackitem) do
  1800. begin
  1801. {
  1802. It is not possible to have type symbols in:
  1803. parameters
  1804. Exception are classes, objects, records, generic definitions and specializations
  1805. that have the parameterized types inserted in the symtable.
  1806. }
  1807. srsymtable:=stackitem^.symtable;
  1808. if (srsymtable.symtabletype=ObjectSymtable) then
  1809. begin
  1810. classh:=tobjectdef(srsymtable.defowner);
  1811. while assigned(classh) do
  1812. begin
  1813. srsymtable:=classh.symtable;
  1814. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1815. if assigned(srsym) and
  1816. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1817. is_visible_for_object(srsym,current_structdef) then
  1818. begin
  1819. addsymref(srsym);
  1820. result:=true;
  1821. exit;
  1822. end;
  1823. classh:=classh.childof;
  1824. end;
  1825. end
  1826. else
  1827. begin
  1828. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1829. if assigned(srsym) and
  1830. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1831. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  1832. begin
  1833. { we need to know if a procedure references symbols
  1834. in the static symtable, because then it can't be
  1835. inlined from outside this unit }
  1836. if assigned(current_procinfo) and
  1837. (srsym.owner.symtabletype=staticsymtable) then
  1838. include(current_procinfo.flags,pi_uses_static_symtable);
  1839. addsymref(srsym);
  1840. result:=true;
  1841. exit;
  1842. end;
  1843. end;
  1844. stackitem:=stackitem^.next;
  1845. end;
  1846. result:=false;
  1847. srsym:=nil;
  1848. srsymtable:=nil;
  1849. end;
  1850. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1851. var
  1852. pmod : tmodule;
  1853. begin
  1854. pmod:=tmodule(pm);
  1855. result:=false;
  1856. if assigned(pmod.globalsymtable) then
  1857. begin
  1858. srsym:=tsym(pmod.globalsymtable.Find(s));
  1859. if assigned(srsym) then
  1860. begin
  1861. srsymtable:=pmod.globalsymtable;
  1862. addsymref(srsym);
  1863. result:=true;
  1864. exit;
  1865. end;
  1866. end;
  1867. { If the module is the current unit we also need
  1868. to search the local symtable }
  1869. if (pmod=current_module) and
  1870. assigned(pmod.localsymtable) then
  1871. begin
  1872. srsym:=tsym(pmod.localsymtable.Find(s));
  1873. if assigned(srsym) then
  1874. begin
  1875. srsymtable:=pmod.localsymtable;
  1876. addsymref(srsym);
  1877. result:=true;
  1878. exit;
  1879. end;
  1880. end;
  1881. srsym:=nil;
  1882. srsymtable:=nil;
  1883. end;
  1884. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1885. var
  1886. stackitem : psymtablestackitem;
  1887. begin
  1888. result:=false;
  1889. stackitem:=symtablestack.stack;
  1890. while assigned(stackitem) do
  1891. begin
  1892. srsymtable:=stackitem^.symtable;
  1893. if (srsymtable.symtabletype=globalsymtable) and
  1894. (srsymtable.name^=unitname) then
  1895. begin
  1896. srsym:=tsym(srsymtable.find(symname));
  1897. if not assigned(srsym) then
  1898. break;
  1899. result:=true;
  1900. exit;
  1901. end;
  1902. stackitem:=stackitem^.next;
  1903. end;
  1904. { If the module is the current unit we also need
  1905. to search the local symtable }
  1906. if (current_module.globalsymtable=srsymtable) and
  1907. assigned(current_module.localsymtable) then
  1908. begin
  1909. srsymtable:=current_module.localsymtable;
  1910. srsym:=tsym(srsymtable.find(symname));
  1911. if assigned(srsym) then
  1912. begin
  1913. result:=true;
  1914. exit;
  1915. end;
  1916. end;
  1917. end;
  1918. function find_real_objcclass_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  1919. var
  1920. hashedid : THashedIDString;
  1921. stackitem : psymtablestackitem;
  1922. srsymtable : tsymtable;
  1923. srsym : tsym;
  1924. begin
  1925. { not a formal definition -> return it }
  1926. if not(oo_is_formal in pd.objectoptions) then
  1927. begin
  1928. result:=pd;
  1929. exit;
  1930. end;
  1931. hashedid.id:=pd.typesym.name;
  1932. stackitem:=symtablestack.stack;
  1933. while assigned(stackitem) do
  1934. begin
  1935. srsymtable:=stackitem^.symtable;
  1936. { ObjC classes can't appear in generics or as nested class
  1937. definitions }
  1938. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) then
  1939. begin
  1940. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1941. if assigned(srsym) and
  1942. (srsym.typ=typesym) and
  1943. is_objcclass(ttypesym(srsym).typedef) and
  1944. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  1945. begin
  1946. { the external name for the formal and the real definition must match }
  1947. if tobjectdef(ttypesym(srsym).typedef).objextname^<>pd.objextname^ then
  1948. begin
  1949. Message2(sym_e_external_class_name_mismatch1,pd.objextname^,pd.typename);
  1950. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,tobjectdef(ttypesym(srsym).typedef).objextname^);
  1951. end;
  1952. result:=tobjectdef(ttypesym(srsym).typedef);
  1953. if assigned(current_procinfo) and
  1954. (srsym.owner.symtabletype=staticsymtable) then
  1955. include(current_procinfo.flags,pi_uses_static_symtable);
  1956. addsymref(srsym);
  1957. exit;
  1958. end;
  1959. end;
  1960. stackitem:=stackitem^.next;
  1961. end;
  1962. { nothing found: optionally give an error and return the original
  1963. (empty) one }
  1964. if erroronfailure then
  1965. Message1(sym_e_objc_formal_class_not_resolved,pd.objrealname^);
  1966. result:=pd;
  1967. end;
  1968. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:boolean):boolean;
  1969. var
  1970. hashedid : THashedIDString;
  1971. orgclass : tobjectdef;
  1972. i : longint;
  1973. begin
  1974. orgclass:=classh;
  1975. { in case this is a formal objcclass, first find the real definition }
  1976. if assigned(classh) then
  1977. begin
  1978. if (oo_is_formal in classh.objectoptions) then
  1979. classh:=find_real_objcclass_definition(classh,true);
  1980. { The contextclassh is used for visibility. The classh must be equal to
  1981. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1982. parent or a class helper. }
  1983. if not (contextclassh.is_related(classh) or
  1984. (assigned(contextclassh.extendeddef) and
  1985. (contextclassh.extendeddef.typ=objectdef) and
  1986. contextclassh.extendeddef.is_related(classh))) then
  1987. internalerror(200811161);
  1988. end;
  1989. result:=false;
  1990. hashedid.id:=s;
  1991. { an Objective-C protocol can inherit from multiple other protocols
  1992. -> uses ImplementedInterfaces instead }
  1993. if is_objcprotocol(classh) then
  1994. begin
  1995. srsymtable:=classh.symtable;
  1996. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1997. if assigned(srsym) and
  1998. is_visible_for_object(srsym,contextclassh) then
  1999. begin
  2000. addsymref(srsym);
  2001. result:=true;
  2002. exit;
  2003. end;
  2004. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2005. begin
  2006. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,false) then
  2007. begin
  2008. result:=true;
  2009. exit;
  2010. end;
  2011. end;
  2012. end
  2013. else
  2014. if is_objectpascal_helper(classh) then
  2015. begin
  2016. { helpers have their own obscure search logic... }
  2017. result:=searchsym_in_helper(classh,contextclassh,s,srsym,srsymtable,false);
  2018. if result then
  2019. exit;
  2020. end
  2021. else
  2022. begin
  2023. while assigned(classh) do
  2024. begin
  2025. { search for a class helper method first if this is an Object
  2026. Pascal class }
  2027. if is_class(classh) and searchhelper then
  2028. begin
  2029. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2030. if result then
  2031. { if the procsym is overloaded we need to use the
  2032. "original" symbol; the helper symbol will be found when
  2033. searching for overloads }
  2034. if (srsym.typ<>procsym) or
  2035. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2036. exit;
  2037. end;
  2038. srsymtable:=classh.symtable;
  2039. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2040. if assigned(srsym) and
  2041. is_visible_for_object(srsym,contextclassh) then
  2042. begin
  2043. addsymref(srsym);
  2044. result:=true;
  2045. exit;
  2046. end;
  2047. classh:=classh.childof;
  2048. end;
  2049. end;
  2050. if is_objcclass(orgclass) then
  2051. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2052. else
  2053. begin
  2054. srsym:=nil;
  2055. srsymtable:=nil;
  2056. end;
  2057. end;
  2058. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2059. var
  2060. hashedid : THashedIDString;
  2061. begin
  2062. result:=false;
  2063. hashedid.id:=s;
  2064. { search for a record helper method first }
  2065. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2066. if result then
  2067. { if the procsym is overloaded we need to use the
  2068. "original" symbol; the helper symbol will be found when
  2069. searching for overloads }
  2070. if (srsym.typ<>procsym) or
  2071. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2072. exit;
  2073. srsymtable:=recordh.symtable;
  2074. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2075. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2076. begin
  2077. addsymref(srsym);
  2078. result:=true;
  2079. exit;
  2080. end;
  2081. srsym:=nil;
  2082. srsymtable:=nil;
  2083. end;
  2084. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2085. var
  2086. def : tdef;
  2087. i : longint;
  2088. begin
  2089. { in case this is a formal objcclass, first find the real definition }
  2090. if assigned(classh) and
  2091. (oo_is_formal in classh.objectoptions) then
  2092. classh:=find_real_objcclass_definition(classh,true);
  2093. result:=false;
  2094. def:=nil;
  2095. while assigned(classh) do
  2096. begin
  2097. for i:=0 to classh.symtable.DefList.Count-1 do
  2098. begin
  2099. def:=tstoreddef(classh.symtable.DefList[i]);
  2100. { Find also all hidden private methods to
  2101. be compatible with delphi, see tw6203 (PFV) }
  2102. if (def.typ=procdef) and
  2103. (po_msgint in tprocdef(def).procoptions) and
  2104. (tprocdef(def).messageinf.i=msgid) then
  2105. begin
  2106. srdef:=def;
  2107. srsym:=tprocdef(def).procsym;
  2108. srsymtable:=classh.symtable;
  2109. addsymref(srsym);
  2110. result:=true;
  2111. exit;
  2112. end;
  2113. end;
  2114. classh:=classh.childof;
  2115. end;
  2116. srdef:=nil;
  2117. srsym:=nil;
  2118. srsymtable:=nil;
  2119. end;
  2120. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2121. var
  2122. def : tdef;
  2123. i : longint;
  2124. begin
  2125. { in case this is a formal objcclass, first find the real definition }
  2126. if assigned(classh) and
  2127. (oo_is_formal in classh.objectoptions) then
  2128. classh:=find_real_objcclass_definition(classh,true);
  2129. result:=false;
  2130. def:=nil;
  2131. while assigned(classh) do
  2132. begin
  2133. for i:=0 to classh.symtable.DefList.Count-1 do
  2134. begin
  2135. def:=tstoreddef(classh.symtable.DefList[i]);
  2136. { Find also all hidden private methods to
  2137. be compatible with delphi, see tw6203 (PFV) }
  2138. if (def.typ=procdef) and
  2139. (po_msgstr in tprocdef(def).procoptions) and
  2140. (tprocdef(def).messageinf.str^=s) then
  2141. begin
  2142. srsym:=tprocdef(def).procsym;
  2143. srsymtable:=classh.symtable;
  2144. addsymref(srsym);
  2145. result:=true;
  2146. exit;
  2147. end;
  2148. end;
  2149. classh:=classh.childof;
  2150. end;
  2151. srsym:=nil;
  2152. srsymtable:=nil;
  2153. end;
  2154. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  2155. var
  2156. hashedid : THashedIDString;
  2157. parentclassh : tobjectdef;
  2158. begin
  2159. result:=false;
  2160. if not is_objectpascal_helper(classh) then
  2161. Internalerror(2011030101);
  2162. hashedid.id:=s;
  2163. { in a helper things are a bit more complex:
  2164. 1. search the symbol in the helper (if not "inherited")
  2165. 2. search the symbol in the extended type
  2166. 3. search the symbol in the parent helpers
  2167. 4. only classes: search the symbol in the parents of the extended type
  2168. }
  2169. if not aHasInherited then
  2170. begin
  2171. { search in the helper itself }
  2172. srsymtable:=classh.symtable;
  2173. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2174. if assigned(srsym) and
  2175. is_visible_for_object(srsym,contextclassh) then
  2176. begin
  2177. addsymref(srsym);
  2178. result:=true;
  2179. exit;
  2180. end;
  2181. end;
  2182. { now search in the extended type itself }
  2183. if classh.extendeddef.typ in [recorddef,objectdef] then
  2184. begin
  2185. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  2186. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2187. if assigned(srsym) and
  2188. is_visible_for_object(srsym,contextclassh) then
  2189. begin
  2190. addsymref(srsym);
  2191. result:=true;
  2192. exit;
  2193. end;
  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.