symtable.pas 117 KB

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