symtable.pas 115 KB

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