symtable.pas 118 KB

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