symtable.pas 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241
  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 assigned(tabstractvarsym(sym).vardef) and
  713. is_managed_type(tabstractvarsym(sym).vardef) then
  714. b_needs_init_final:=true;
  715. end;
  716. end;
  717. end;
  718. { returns true, if p contains data which needs init/final code }
  719. function tstoredsymtable.needs_init_final : boolean;
  720. begin
  721. b_needs_init_final:=false;
  722. SymList.ForEachCall(@_needs_init_final,nil);
  723. needs_init_final:=b_needs_init_final;
  724. end;
  725. {****************************************************************************
  726. TAbstractRecordSymtable
  727. ****************************************************************************}
  728. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  729. begin
  730. inherited create(n);
  731. moduleid:=current_module.moduleid;
  732. _datasize:=0;
  733. databitsize:=0;
  734. recordalignment:=1;
  735. usefieldalignment:=usealign;
  736. padalignment:=1;
  737. { recordalign C_alignment means C record packing, that starts
  738. with an alignment of 1 }
  739. case usealign of
  740. C_alignment,
  741. bit_alignment:
  742. fieldalignment:=1;
  743. mac68k_alignment:
  744. fieldalignment:=2;
  745. else
  746. fieldalignment:=usealign;
  747. end;
  748. end;
  749. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  750. begin
  751. if ppufile.readentry<>ibrecsymtableoptions then
  752. Message(unit_f_ppu_read_error);
  753. recordalignment:=shortint(ppufile.getbyte);
  754. usefieldalignment:=shortint(ppufile.getbyte);
  755. if (usefieldalignment=C_alignment) then
  756. fieldalignment:=shortint(ppufile.getbyte);
  757. inherited ppuload(ppufile);
  758. end;
  759. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  760. var
  761. oldtyp : byte;
  762. begin
  763. oldtyp:=ppufile.entrytyp;
  764. ppufile.entrytyp:=subentryid;
  765. { in case of classes using C alignment, the alignment of the parent
  766. affects the alignment of fields of the childs }
  767. ppufile.putbyte(byte(recordalignment));
  768. ppufile.putbyte(byte(usefieldalignment));
  769. if (usefieldalignment=C_alignment) then
  770. ppufile.putbyte(byte(fieldalignment));
  771. ppufile.writeentry(ibrecsymtableoptions);
  772. inherited ppuwrite(ppufile);
  773. ppufile.entrytyp:=oldtyp;
  774. end;
  775. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  776. begin
  777. { optimal alignment of the record when declaring a variable of this }
  778. { type is independent of the packrecords setting }
  779. if (fieldoffs mod fieldalign) = 0 then
  780. result:=fieldalign
  781. else if (fieldalign >= 16) and
  782. ((fieldoffs mod 16) = 0) and
  783. ((fieldalign mod 16) = 0) then
  784. result:=16
  785. else if (fieldalign >= 8) and
  786. ((fieldoffs mod 8) = 0) and
  787. ((fieldalign mod 8) = 0) then
  788. result:=8
  789. else if (fieldalign >= 4) and
  790. ((fieldoffs mod 4) = 0) and
  791. ((fieldalign mod 4) = 0) then
  792. result:=4
  793. else if (fieldalign >= 2) and
  794. ((fieldoffs mod 2) = 0) and
  795. ((fieldalign mod 2) = 0) then
  796. result:=2
  797. else
  798. result:=1;
  799. end;
  800. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  801. var
  802. varalignrecord: shortint;
  803. begin
  804. case usefieldalignment of
  805. C_alignment:
  806. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  807. mac68k_alignment:
  808. varalignrecord:=2;
  809. else
  810. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  811. end;
  812. recordalignment:=max(recordalignment,varalignrecord);
  813. end;
  814. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  815. var
  816. l : asizeint;
  817. varalignfield,
  818. varalign : shortint;
  819. vardef : tdef;
  820. begin
  821. if (sym.owner<>self) then
  822. internalerror(200602031);
  823. if sym.fieldoffset<>-1 then
  824. internalerror(200602032);
  825. { set visibility for the symbol }
  826. sym.visibility:=vis;
  827. { this symbol can't be loaded to a register }
  828. sym.varregable:=vr_none;
  829. { Calculate field offset }
  830. l:=sym.getsize;
  831. vardef:=sym.vardef;
  832. varalign:=vardef.alignment;
  833. {$if defined(powerpc) or defined(powerpc64)}
  834. { aix is really annoying: the recommended scalar alignment for both
  835. int64 and double is 64 bits, but in structs int64 has to be aligned
  836. to 8 bytes and double to 4 bytes }
  837. if (target_info.system in systems_aix) and
  838. is_double(vardef) then
  839. varalign:=4;
  840. {$endif powerpc or powerpc64}
  841. case usefieldalignment of
  842. bit_alignment:
  843. begin
  844. { bitpacking only happens for ordinals, the rest is aligned at }
  845. { 1 byte (compatible with GPC/GCC) }
  846. if is_ordinal(vardef) then
  847. begin
  848. sym.fieldoffset:=databitsize;
  849. l:=sym.getpackedbitsize;
  850. end
  851. else
  852. begin
  853. databitsize:=_datasize*8;
  854. sym.fieldoffset:=databitsize;
  855. if (l>high(asizeint) div 8) then
  856. Message(sym_e_segment_too_large);
  857. l:=l*8;
  858. end;
  859. if varalign=0 then
  860. varalign:=size_2_align(l);
  861. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  862. { bit packed records are limited to high(aint) bits }
  863. { instead of bytes to avoid double precision }
  864. { arithmetic in offset calculations }
  865. if int64(l)>high(asizeint)-sym.fieldoffset then
  866. begin
  867. Message(sym_e_segment_too_large);
  868. _datasize:=high(asizeint);
  869. databitsize:=high(asizeint);
  870. end
  871. else
  872. begin
  873. databitsize:=sym.fieldoffset+l;
  874. _datasize:=(databitsize+7) div 8;
  875. end;
  876. { rest is not applicable }
  877. exit;
  878. end;
  879. { Calc the alignment size for C style records }
  880. C_alignment:
  881. begin
  882. if (varalign>4) and
  883. ((varalign mod 4)<>0) and
  884. (vardef.typ=arraydef) then
  885. Message1(sym_w_wrong_C_pack,vardef.typename);
  886. if varalign=0 then
  887. varalign:=l;
  888. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  889. begin
  890. if (varalign>16) and (fieldalignment<32) then
  891. fieldalignment:=32
  892. else if (varalign>12) and (fieldalignment<16) then
  893. fieldalignment:=16
  894. { 12 is needed for long double }
  895. else if (varalign>8) and (fieldalignment<12) then
  896. fieldalignment:=12
  897. else if (varalign>4) and (fieldalignment<8) then
  898. fieldalignment:=8
  899. else if (varalign>2) and (fieldalignment<4) then
  900. fieldalignment:=4
  901. else if (varalign>1) and (fieldalignment<2) then
  902. fieldalignment:=2;
  903. end;
  904. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  905. end;
  906. mac68k_alignment:
  907. begin
  908. { mac68k alignment (C description):
  909. * char is aligned to 1 byte
  910. * everything else (except vector) is aligned to 2 bytes
  911. * vector is aligned to 16 bytes
  912. }
  913. if l>1 then
  914. fieldalignment:=2
  915. else
  916. fieldalignment:=1;
  917. varalign:=2;
  918. end;
  919. end;
  920. if varalign=0 then
  921. varalign:=size_2_align(l);
  922. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  923. sym.fieldoffset:=align(_datasize,varalignfield);
  924. if l>high(asizeint)-sym.fieldoffset then
  925. begin
  926. Message(sym_e_segment_too_large);
  927. _datasize:=high(aint);
  928. end
  929. else
  930. _datasize:=sym.fieldoffset+l;
  931. { Calc alignment needed for this record }
  932. alignrecord(sym.fieldoffset,varalign);
  933. end;
  934. procedure tabstractrecordsymtable.addalignmentpadding;
  935. var
  936. padded_datasize: asizeint;
  937. begin
  938. { make the record size aligned correctly so it can be
  939. used as elements in an array. For C records we
  940. use the fieldalignment, because that is updated with the
  941. used alignment. }
  942. if (padalignment = 1) then
  943. case usefieldalignment of
  944. C_alignment:
  945. padalignment:=fieldalignment;
  946. { bitpacked }
  947. bit_alignment:
  948. padalignment:=1;
  949. { mac68k: always round to multiple of 2 }
  950. mac68k_alignment:
  951. padalignment:=2;
  952. { default/no packrecords specified }
  953. 0:
  954. padalignment:=recordalignment
  955. { specific packrecords setting -> use as upper limit }
  956. else
  957. padalignment:=min(recordalignment,usefieldalignment);
  958. end;
  959. padded_datasize:=align(_datasize,padalignment);
  960. _paddingsize:=padded_datasize-_datasize;
  961. _datasize:=padded_datasize;
  962. end;
  963. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  964. begin
  965. { Enums must also be available outside the record scope,
  966. insert in the owner of this symtable }
  967. if def.typ=enumdef then
  968. defowner.owner.insertdef(def)
  969. else
  970. inherited insertdef(def);
  971. end;
  972. function tabstractrecordsymtable.is_packed: boolean;
  973. begin
  974. result:=usefieldalignment=bit_alignment;
  975. end;
  976. function tabstractrecordsymtable.has_single_field(out sym: tfieldvarsym): boolean;
  977. var
  978. i: longint;
  979. begin
  980. result:=false;
  981. { If a record contains a union, it does not contain a "single
  982. non-composite field" in the context of certain ABIs requiring
  983. special treatment for such records }
  984. if (defowner.typ=recorddef) and
  985. trecorddef(defowner).isunion then
  986. exit;
  987. { a record/object can contain other things than fields }
  988. for i:=0 to SymList.Count-1 do
  989. begin
  990. if tsym(symlist[i]).typ=fieldvarsym then
  991. begin
  992. if result then
  993. begin
  994. result:=false;
  995. exit;
  996. end;
  997. result:=true;
  998. sym:=tfieldvarsym(symlist[i])
  999. end;
  1000. end;
  1001. end;
  1002. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1003. begin
  1004. result:=defowner.owner;
  1005. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1006. result:=result.defowner.owner;
  1007. end;
  1008. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1009. begin
  1010. _datasize:=val;
  1011. if (usefieldalignment=bit_alignment) then
  1012. { can overflow in non bitpacked records }
  1013. databitsize:=val*8;
  1014. end;
  1015. function tabstractrecordsymtable.iscurrentunit: boolean;
  1016. begin
  1017. Result := Assigned(current_module) and (current_module.moduleid=moduleid);
  1018. end;
  1019. {****************************************************************************
  1020. TRecordSymtable
  1021. ****************************************************************************}
  1022. constructor trecordsymtable.create(const n:string;usealign:shortint);
  1023. begin
  1024. inherited create(n,usealign);
  1025. symtabletype:=recordsymtable;
  1026. end;
  1027. { this procedure is reserved for inserting case variant into
  1028. a record symtable }
  1029. { the offset is the location of the start of the variant
  1030. and datasize and dataalignment corresponds to
  1031. the complete size (see code in pdecl unit) PM }
  1032. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  1033. var
  1034. sym : tsym;
  1035. def : tdef;
  1036. i : integer;
  1037. varalignrecord,varalign,
  1038. storesize,storealign : aint;
  1039. bitsize: tcgint;
  1040. begin
  1041. storesize:=_datasize;
  1042. storealign:=fieldalignment;
  1043. _datasize:=offset;
  1044. if (usefieldalignment=bit_alignment) then
  1045. databitsize:=offset*8;
  1046. { We move the ownership of the defs and symbols to the new recordsymtable.
  1047. The old unionsymtable keeps the references, but doesn't own the
  1048. objects anymore }
  1049. unionst.DefList.OwnsObjects:=false;
  1050. unionst.SymList.OwnsObjects:=false;
  1051. { copy symbols }
  1052. for i:=0 to unionst.SymList.Count-1 do
  1053. begin
  1054. sym:=TSym(unionst.SymList[i]);
  1055. if sym.typ<>fieldvarsym then
  1056. internalerror(200601272);
  1057. if tfieldvarsym(sym).fieldoffset=0 then
  1058. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1059. { add to this record symtable }
  1060. // unionst.SymList.List.List^[i].Data:=nil;
  1061. sym.ChangeOwner(self);
  1062. varalign:=tfieldvarsym(sym).vardef.alignment;
  1063. if varalign=0 then
  1064. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1065. { retrieve size }
  1066. if (usefieldalignment=bit_alignment) then
  1067. begin
  1068. { bit packed records are limited to high(aint) bits }
  1069. { instead of bytes to avoid double precision }
  1070. { arithmetic in offset calculations }
  1071. if is_ordinal(tfieldvarsym(sym).vardef) then
  1072. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1073. else
  1074. begin
  1075. bitsize:=tfieldvarsym(sym).getsize;
  1076. if (bitsize>high(asizeint) div 8) then
  1077. Message(sym_e_segment_too_large);
  1078. bitsize:=bitsize*8;
  1079. end;
  1080. if bitsize>high(asizeint)-databitsize then
  1081. begin
  1082. Message(sym_e_segment_too_large);
  1083. _datasize:=high(aint);
  1084. databitsize:=high(aint);
  1085. end
  1086. else
  1087. begin
  1088. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1089. _datasize:=(databitsize+7) div 8;
  1090. end;
  1091. tfieldvarsym(sym).fieldoffset:=databitsize;
  1092. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1093. end
  1094. else
  1095. begin
  1096. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1097. begin
  1098. Message(sym_e_segment_too_large);
  1099. _datasize:=high(aint);
  1100. end
  1101. else
  1102. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1103. { update address }
  1104. tfieldvarsym(sym).fieldoffset:=_datasize;
  1105. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1106. end;
  1107. { update alignment of this record }
  1108. if (usefieldalignment<>C_alignment) and
  1109. (usefieldalignment<>mac68k_alignment) then
  1110. recordalignment:=max(recordalignment,varalignrecord);
  1111. end;
  1112. { update alignment for C records }
  1113. if (usefieldalignment=C_alignment) and
  1114. (usefieldalignment<>mac68k_alignment) then
  1115. recordalignment:=max(recordalignment,unionst.recordalignment);
  1116. { Register defs in the new record symtable }
  1117. for i:=0 to unionst.DefList.Count-1 do
  1118. begin
  1119. def:=TDef(unionst.DefList[i]);
  1120. def.ChangeOwner(self);
  1121. end;
  1122. _datasize:=storesize;
  1123. fieldalignment:=storealign;
  1124. { If a record contains a union, it does not contain a "single
  1125. non-composite field" in the context of certain ABIs requiring
  1126. special treatment for such records }
  1127. if defowner.typ=recorddef then
  1128. trecorddef(defowner).isunion:=true;
  1129. end;
  1130. {****************************************************************************
  1131. TObjectSymtable
  1132. ****************************************************************************}
  1133. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  1134. begin
  1135. inherited create(n,usealign);
  1136. symtabletype:=ObjectSymtable;
  1137. defowner:=adefowner;
  1138. end;
  1139. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1140. var
  1141. hsym : tsym;
  1142. begin
  1143. result:=false;
  1144. if not assigned(defowner) then
  1145. internalerror(200602061);
  1146. { procsym and propertysym have special code
  1147. to override values in inherited classes. For other
  1148. symbols check for duplicates }
  1149. if not(sym.typ in [procsym,propertysym]) then
  1150. begin
  1151. { but private ids can be reused }
  1152. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1153. if assigned(hsym) and
  1154. (
  1155. (
  1156. not(m_delphi in current_settings.modeswitches) and
  1157. is_visible_for_object(hsym,tobjectdef(defowner))
  1158. ) or
  1159. (
  1160. { In Delphi, you can repeat members of a parent class. You can't }
  1161. { do this for objects however, and you (obviouly) can't }
  1162. { declare two fields with the same name in a single class }
  1163. (m_delphi in current_settings.modeswitches) and
  1164. (
  1165. is_object(tdef(defowner)) or
  1166. (hsym.owner = self)
  1167. )
  1168. )
  1169. ) then
  1170. begin
  1171. DuplicateSym(hashedid,sym,hsym);
  1172. result:=true;
  1173. end;
  1174. end
  1175. else
  1176. result:=inherited checkduplicate(hashedid,sym);
  1177. end;
  1178. {****************************************************************************
  1179. TAbstractLocalSymtable
  1180. ****************************************************************************}
  1181. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1182. var
  1183. oldtyp : byte;
  1184. begin
  1185. oldtyp:=ppufile.entrytyp;
  1186. ppufile.entrytyp:=subentryid;
  1187. inherited ppuwrite(ppufile);
  1188. ppufile.entrytyp:=oldtyp;
  1189. end;
  1190. function tabstractlocalsymtable.count_locals:longint;
  1191. var
  1192. i : longint;
  1193. sym : tsym;
  1194. begin
  1195. result:=0;
  1196. for i:=0 to SymList.Count-1 do
  1197. begin
  1198. sym:=tsym(SymList[i]);
  1199. { Count only varsyms, but ignore the funcretsym }
  1200. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1201. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1202. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1203. (tstoredsym(sym).refs>0)) then
  1204. inc(result);
  1205. end;
  1206. end;
  1207. {****************************************************************************
  1208. TLocalSymtable
  1209. ****************************************************************************}
  1210. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1211. begin
  1212. inherited create('');
  1213. defowner:=adefowner;
  1214. symtabletype:=localsymtable;
  1215. symtablelevel:=level;
  1216. end;
  1217. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1218. var
  1219. hsym : tsym;
  1220. begin
  1221. if not assigned(defowner) or
  1222. (defowner.typ<>procdef) then
  1223. internalerror(200602042);
  1224. result:=false;
  1225. hsym:=tsym(FindWithHash(hashedid));
  1226. if assigned(hsym) then
  1227. begin
  1228. { a local and the function can have the same
  1229. name in TP and Delphi, but RESULT not }
  1230. if (m_duplicate_names in current_settings.modeswitches) and
  1231. (hsym.typ in [absolutevarsym,localvarsym]) and
  1232. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1233. not((m_result in current_settings.modeswitches) and
  1234. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1235. HideSym(hsym)
  1236. else
  1237. DuplicateSym(hashedid,sym,hsym);
  1238. result:=true;
  1239. exit;
  1240. end;
  1241. { check also parasymtable, this needs to be done here because
  1242. of the special situation with the funcret sym that needs to be
  1243. hidden for tp and delphi modes }
  1244. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1245. if assigned(hsym) then
  1246. begin
  1247. { a local and the function can have the same
  1248. name in TP and Delphi, but RESULT not }
  1249. if (m_duplicate_names in current_settings.modeswitches) and
  1250. (sym.typ in [absolutevarsym,localvarsym]) and
  1251. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1252. not((m_result in current_settings.modeswitches) and
  1253. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1254. Hidesym(sym)
  1255. else
  1256. DuplicateSym(hashedid,sym,hsym);
  1257. result:=true;
  1258. exit;
  1259. end;
  1260. { check ObjectSymtable, skip this for funcret sym because
  1261. that will always be positive because it has the same name
  1262. as the procsym }
  1263. if not is_funcret_sym(sym) and
  1264. (defowner.typ=procdef) and
  1265. assigned(tprocdef(defowner).struct) and
  1266. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1267. (
  1268. not(m_delphi in current_settings.modeswitches) or
  1269. is_object(tprocdef(defowner).struct)
  1270. ) then
  1271. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1272. end;
  1273. {****************************************************************************
  1274. TParaSymtable
  1275. ****************************************************************************}
  1276. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1277. begin
  1278. inherited create('');
  1279. readonly:=false;
  1280. defowner:=adefowner;
  1281. symtabletype:=parasymtable;
  1282. symtablelevel:=level;
  1283. end;
  1284. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1285. begin
  1286. result:=inherited checkduplicate(hashedid,sym);
  1287. if result then
  1288. exit;
  1289. if not(m_duplicate_names in current_settings.modeswitches) and
  1290. assigned(defowner) and (defowner.typ=procdef) and
  1291. assigned(tprocdef(defowner).struct) and
  1292. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1293. (
  1294. not(m_delphi in current_settings.modeswitches) or
  1295. is_object(tprocdef(defowner).struct)
  1296. ) then
  1297. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1298. end;
  1299. procedure tparasymtable.insertdef(def: TDefEntry);
  1300. begin
  1301. if readonly then
  1302. defowner.owner.insertdef(def)
  1303. else
  1304. inherited insertdef(def);
  1305. end;
  1306. {****************************************************************************
  1307. TAbstractUniTSymtable
  1308. ****************************************************************************}
  1309. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1310. begin
  1311. inherited create(n);
  1312. moduleid:=id;
  1313. end;
  1314. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1315. var
  1316. hsym : tsym;
  1317. begin
  1318. result:=false;
  1319. hsym:=tsym(FindWithHash(hashedid));
  1320. if assigned(hsym) then
  1321. begin
  1322. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  1323. exit;
  1324. if hsym.typ=symconst.namespacesym then
  1325. begin
  1326. case sym.typ of
  1327. symconst.namespacesym:;
  1328. symconst.unitsym:
  1329. begin
  1330. 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 }
  1331. tnamespacesym(hsym).unitsym:=tsym(sym);
  1332. end
  1333. else
  1334. HideSym(hsym);
  1335. end;
  1336. end
  1337. else
  1338. { In delphi (contrary to TP) you can have a symbol with the same name as the
  1339. unit, the unit can then not be accessed anymore using
  1340. <unit>.<id>, so we can hide the symbol.
  1341. Do the same if we add a namespace and there is a unit with the same name }
  1342. if (hsym.typ=symconst.unitsym) and
  1343. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  1344. begin
  1345. HideSym(hsym);
  1346. if sym.typ=symconst.namespacesym then
  1347. tnamespacesym(sym).unitsym:=tsym(hsym);
  1348. end
  1349. else
  1350. DuplicateSym(hashedid,sym,hsym);
  1351. result:=true;
  1352. exit;
  1353. end;
  1354. end;
  1355. function tabstractuniTSymtable.iscurrentunit:boolean;
  1356. begin
  1357. result:=assigned(current_module) and
  1358. (
  1359. (current_module.globalsymtable=self) or
  1360. (current_module.localsymtable=self)
  1361. );
  1362. end;
  1363. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  1364. var
  1365. p:integer;
  1366. n,ns:string;
  1367. oldsym:TSymEntry;
  1368. begin
  1369. insert(sym);
  1370. n:=sym.realname;
  1371. p:=pos('.',n);
  1372. ns:='';
  1373. while p>0 do
  1374. begin
  1375. if ns='' then
  1376. ns:=copy(n,1,p-1)
  1377. else
  1378. ns:=ns+'.'+copy(n,1,p-1);
  1379. system.delete(n,1,p);
  1380. oldsym:=Find(upper(ns));
  1381. if not Assigned(oldsym) or (oldsym.typ<>namespacesym) then
  1382. insert(tnamespacesym.create(ns));
  1383. p:=pos('.',n);
  1384. end;
  1385. end;
  1386. {****************************************************************************
  1387. TStaticSymtable
  1388. ****************************************************************************}
  1389. constructor tstaticsymtable.create(const n : string;id:word);
  1390. begin
  1391. inherited create(n,id);
  1392. symtabletype:=staticsymtable;
  1393. symtablelevel:=main_program_level;
  1394. end;
  1395. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1396. begin
  1397. inherited ppuload(ppufile);
  1398. { now we can deref the syms and defs }
  1399. deref;
  1400. end;
  1401. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1402. begin
  1403. inherited ppuwrite(ppufile);
  1404. end;
  1405. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1406. begin
  1407. result:=inherited checkduplicate(hashedid,sym);
  1408. if not result and
  1409. (current_module.localsymtable=self) and
  1410. assigned(current_module.globalsymtable) then
  1411. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1412. end;
  1413. {****************************************************************************
  1414. TGlobalSymtable
  1415. ****************************************************************************}
  1416. constructor tglobalsymtable.create(const n : string;id:word);
  1417. begin
  1418. inherited create(n,id);
  1419. symtabletype:=globalsymtable;
  1420. symtablelevel:=main_program_level;
  1421. end;
  1422. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1423. begin
  1424. inherited ppuload(ppufile);
  1425. { now we can deref the syms and defs }
  1426. deref;
  1427. end;
  1428. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1429. begin
  1430. { write the symtable entries }
  1431. inherited ppuwrite(ppufile);
  1432. end;
  1433. {*****************************************************************************
  1434. tspecializesymtable
  1435. *****************************************************************************}
  1436. function tspecializesymtable.iscurrentunit: boolean;
  1437. begin
  1438. Result := true;
  1439. end;
  1440. {****************************************************************************
  1441. TWITHSYMTABLE
  1442. ****************************************************************************}
  1443. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1444. begin
  1445. inherited create('');
  1446. symtabletype:=withsymtable;
  1447. withrefnode:=refnode;
  1448. { Replace SymList with the passed symlist }
  1449. SymList.free;
  1450. SymList:=ASymList;
  1451. defowner:=aowner;
  1452. end;
  1453. destructor twithsymtable.destroy;
  1454. begin
  1455. withrefnode.free;
  1456. { Disable SymList because we don't Own it }
  1457. SymList:=nil;
  1458. inherited destroy;
  1459. end;
  1460. procedure twithsymtable.clear;
  1461. begin
  1462. { remove no entry from a withsymtable as it is only a pointer to the
  1463. recorddef or objectdef symtable }
  1464. end;
  1465. procedure twithsymtable.insertdef(def:TDefEntry);
  1466. begin
  1467. { Definitions can't be registered in the withsymtable
  1468. because the withsymtable is removed after the with block.
  1469. We can't easily solve it here because the next symtable in the
  1470. stack is not known. }
  1471. internalerror(200602046);
  1472. end;
  1473. {****************************************************************************
  1474. TSTT_ExceptionSymtable
  1475. ****************************************************************************}
  1476. constructor tstt_excepTSymtable.create;
  1477. begin
  1478. inherited create('');
  1479. symtabletype:=stt_excepTSymtable;
  1480. end;
  1481. {****************************************************************************
  1482. TMacroSymtable
  1483. ****************************************************************************}
  1484. constructor tmacrosymtable.create(exported: boolean);
  1485. begin
  1486. inherited create('');
  1487. if exported then
  1488. symtabletype:=exportedmacrosymtable
  1489. else
  1490. symtabletype:=localmacrosymtable;
  1491. symtablelevel:=main_program_level;
  1492. end;
  1493. {****************************************************************************
  1494. TEnumSymtable
  1495. ****************************************************************************}
  1496. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  1497. var
  1498. value: longint;
  1499. def: tenumdef;
  1500. begin
  1501. // defowner = nil only when we are loading from ppu
  1502. if defowner<>nil then
  1503. begin
  1504. { First entry? Then we need to set the minval }
  1505. value:=tenumsym(sym).value;
  1506. def:=tenumdef(defowner);
  1507. if SymList.count=0 then
  1508. begin
  1509. if value>0 then
  1510. def.has_jumps:=true;
  1511. def.setmin(value);
  1512. def.setmax(value);
  1513. end
  1514. else
  1515. begin
  1516. { check for jumps }
  1517. if value>def.max+1 then
  1518. def.has_jumps:=true;
  1519. { update low and high }
  1520. if def.min>value then
  1521. def.setmin(value);
  1522. if def.max<value then
  1523. def.setmax(value);
  1524. end;
  1525. end;
  1526. inherited insert(sym, checkdup);
  1527. end;
  1528. constructor tenumsymtable.create(adefowner: tdef);
  1529. begin
  1530. inherited Create('');
  1531. symtabletype:=enumsymtable;
  1532. defowner:=adefowner;
  1533. end;
  1534. {****************************************************************************
  1535. TArraySymtable
  1536. ****************************************************************************}
  1537. procedure tarraysymtable.insertdef(def: TDefEntry);
  1538. begin
  1539. { Enums must also be available outside the record scope,
  1540. insert in the owner of this symtable }
  1541. if def.typ=enumdef then
  1542. defowner.owner.insertdef(def)
  1543. else
  1544. inherited insertdef(def);
  1545. end;
  1546. constructor tarraysymtable.create(adefowner: tdef);
  1547. begin
  1548. inherited Create('');
  1549. symtabletype:=arraysymtable;
  1550. defowner:=adefowner;
  1551. end;
  1552. {*****************************************************************************
  1553. Helper Routines
  1554. *****************************************************************************}
  1555. function FullTypeName(def,otherdef:tdef):string;
  1556. var
  1557. s1,s2 : string;
  1558. begin
  1559. if def.typ in [objectdef,recorddef] then
  1560. s1:=tabstractrecorddef(def).RttiName
  1561. else
  1562. s1:=def.typename;
  1563. { When the names are the same try to include the unit name }
  1564. if assigned(otherdef) and
  1565. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1566. begin
  1567. s2:=otherdef.typename;
  1568. if upper(s1)=upper(s2) then
  1569. s1:=def.owner.realname^+'.'+s1;
  1570. end;
  1571. FullTypeName:=s1;
  1572. end;
  1573. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  1574. begin
  1575. result:='';
  1576. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  1577. begin
  1578. if (result='') then
  1579. result:=symtable.name^
  1580. else
  1581. result:=symtable.name^+delimiter+result;
  1582. symtable:=symtable.defowner.owner;
  1583. end;
  1584. end;
  1585. procedure incompatibletypes(def1,def2:tdef);
  1586. begin
  1587. { When there is an errordef there is already an error message show }
  1588. if (def2.typ=errordef) or
  1589. (def1.typ=errordef) then
  1590. exit;
  1591. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1592. end;
  1593. procedure hidesym(sym:TSymEntry);
  1594. begin
  1595. sym.realname:='$hidden'+sym.realname;
  1596. tsym(sym).visibility:=vis_hidden;
  1597. end;
  1598. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1599. var
  1600. st : TSymtable;
  1601. begin
  1602. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1603. { Write hint where the original symbol was found }
  1604. st:=finduniTSymtable(origsym.owner);
  1605. with tsym(origsym).fileinfo do
  1606. begin
  1607. if assigned(st) and
  1608. (st.symtabletype=globalsymtable) and
  1609. st.iscurrentunit then
  1610. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1611. else if assigned(st.name) then
  1612. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1613. end;
  1614. { Rename duplicate sym to an unreachable name, but it can be
  1615. inserted in the symtable without errors }
  1616. inc(dupnr);
  1617. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1618. if assigned(dupsym) then
  1619. include(tsym(dupsym).symoptions,sp_implicitrename);
  1620. end;
  1621. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  1622. begin
  1623. result:=false;
  1624. if not assigned(sym) or not (sym is tstoredsym) then
  1625. Internalerror(2011081101);
  1626. { For generics a dummy symbol without the parameter count is created
  1627. if such a symbol not yet exists so that different parts of the
  1628. parser can find that symbol. If that symbol is still a
  1629. undefineddef we replace the generic dummy symbol's
  1630. name with a "dup" name and use the new symbol as the generic dummy
  1631. symbol }
  1632. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  1633. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  1634. (m_delphi in current_settings.modeswitches) then
  1635. begin
  1636. inc(dupnr);
  1637. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  1638. include(tsym(sym).symoptions,sp_implicitrename);
  1639. { we need to find the new symbol now if checking for a dummy }
  1640. include(symoptions,sp_generic_dummy);
  1641. result:=true;
  1642. end;
  1643. end;
  1644. {*****************************************************************************
  1645. Search
  1646. *****************************************************************************}
  1647. procedure addsymref(sym:tsym);
  1648. begin
  1649. { symbol uses count }
  1650. sym.IncRefCount;
  1651. { unit uses count }
  1652. if assigned(current_module) and
  1653. (sym.owner.symtabletype=globalsymtable) then
  1654. begin
  1655. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1656. internalerror(200501152);
  1657. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1658. end;
  1659. end;
  1660. function is_owned_by(childdef,ownerdef:tdef):boolean;
  1661. begin
  1662. result:=childdef=ownerdef;
  1663. if not result and assigned(childdef.owner.defowner) then
  1664. result:=is_owned_by(tdef(childdef.owner.defowner),ownerdef);
  1665. end;
  1666. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  1667. begin
  1668. result:=assigned(childsym) and (childsym.owner=symtable);
  1669. if not result and assigned(childsym) and
  1670. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1671. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  1672. end;
  1673. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  1674. begin
  1675. result:=false;
  1676. if not assigned(def1) or not assigned(def2) then
  1677. exit;
  1678. { for both defs walk to the topmost generic }
  1679. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  1680. def1:=tdef(def1.owner.defowner);
  1681. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  1682. def2:=tdef(def2.owner.defowner);
  1683. result:=def1=def2;
  1684. end;
  1685. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1686. var
  1687. symownerdef : tabstractrecorddef;
  1688. begin
  1689. result:=false;
  1690. { Get objdectdef owner of the symtable for the is_related checks }
  1691. if not assigned(symst) or
  1692. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  1693. internalerror(200810285);
  1694. symownerdef:=tabstractrecorddef(symst.defowner);
  1695. case symvisibility of
  1696. vis_private :
  1697. begin
  1698. { private symbols are allowed when we are in the same
  1699. module as they are defined }
  1700. result:=(
  1701. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1702. (symownerdef.owner.iscurrentunit)
  1703. ) or
  1704. ( // the case of specialize inside the generic declaration and nested types
  1705. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1706. (
  1707. assigned(current_structdef) and
  1708. (
  1709. (current_structdef=symownerdef) or
  1710. (current_structdef.owner.iscurrentunit)
  1711. )
  1712. ) or
  1713. (
  1714. not assigned(current_structdef) and
  1715. (symownerdef.owner.iscurrentunit)
  1716. )
  1717. );
  1718. end;
  1719. vis_strictprivate :
  1720. begin
  1721. result:=assigned(current_structdef) and
  1722. is_owned_by(current_structdef,symownerdef);
  1723. end;
  1724. vis_strictprotected :
  1725. begin
  1726. result:=(
  1727. assigned(current_structdef) and
  1728. (current_structdef.is_related(symownerdef) or
  1729. is_owned_by(current_structdef,symownerdef))
  1730. ) or
  1731. (
  1732. { helpers can access strict protected symbols }
  1733. is_objectpascal_helper(contextobjdef) and
  1734. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1735. );
  1736. end;
  1737. vis_protected :
  1738. begin
  1739. { protected symbols are visible in the module that defines them and
  1740. also visible to related objects. The related object must be defined
  1741. in the current module }
  1742. result:=(
  1743. (
  1744. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1745. (symownerdef.owner.iscurrentunit)
  1746. ) or
  1747. (
  1748. assigned(contextobjdef) and
  1749. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  1750. (contextobjdef.owner.iscurrentunit) and
  1751. contextobjdef.is_related(symownerdef)
  1752. ) or
  1753. ( // the case of specialize inside the generic declaration and nested types
  1754. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1755. (
  1756. assigned(current_structdef) and
  1757. (
  1758. (current_structdef=symownerdef) or
  1759. (current_structdef.owner.iscurrentunit)
  1760. )
  1761. ) or
  1762. (
  1763. not assigned(current_structdef) and
  1764. (symownerdef.owner.iscurrentunit)
  1765. ) or
  1766. (
  1767. { helpers can access protected symbols }
  1768. is_objectpascal_helper(contextobjdef) and
  1769. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1770. )
  1771. )
  1772. );
  1773. end;
  1774. vis_public,
  1775. vis_published :
  1776. result:=true;
  1777. end;
  1778. end;
  1779. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  1780. begin
  1781. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1782. end;
  1783. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  1784. var
  1785. i : longint;
  1786. pd : tprocdef;
  1787. begin
  1788. if sym.typ=procsym then
  1789. begin
  1790. { A procsym is visible, when there is at least one of the procdefs visible }
  1791. result:=false;
  1792. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1793. begin
  1794. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1795. if (pd.owner=sym.owner) and
  1796. is_visible_for_object(pd,contextobjdef) then
  1797. begin
  1798. result:=true;
  1799. exit;
  1800. end;
  1801. end;
  1802. end
  1803. else
  1804. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1805. end;
  1806. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1807. begin
  1808. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,false,sp_none);
  1809. end;
  1810. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchoption:boolean;option:tsymoption):boolean;
  1811. var
  1812. hashedid : THashedIDString;
  1813. contextstructdef : tabstractrecorddef;
  1814. stackitem : psymtablestackitem;
  1815. begin
  1816. result:=false;
  1817. hashedid.id:=s;
  1818. stackitem:=symtablestack.stack;
  1819. while assigned(stackitem) do
  1820. begin
  1821. srsymtable:=stackitem^.symtable;
  1822. if (srsymtable.symtabletype=objectsymtable) then
  1823. begin
  1824. { TODO : implement the search for an option in classes as well }
  1825. if searchoption then
  1826. begin
  1827. result:=false;
  1828. exit;
  1829. end;
  1830. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,true) then
  1831. begin
  1832. result:=true;
  1833. exit;
  1834. end;
  1835. end
  1836. else
  1837. begin
  1838. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1839. if assigned(srsym) then
  1840. begin
  1841. { use the class from withsymtable only when it is
  1842. defined in this unit }
  1843. if (srsymtable.symtabletype=withsymtable) and
  1844. assigned(srsymtable.defowner) and
  1845. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  1846. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1847. (srsymtable.defowner.owner.iscurrentunit) then
  1848. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  1849. else
  1850. contextstructdef:=current_structdef;
  1851. if not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  1852. is_visible_for_object(srsym,contextstructdef) and
  1853. (not searchoption or (option in srsym.symoptions)) then
  1854. begin
  1855. { we need to know if a procedure references symbols
  1856. in the static symtable, because then it can't be
  1857. inlined from outside this unit }
  1858. if assigned(current_procinfo) and
  1859. (srsym.owner.symtabletype=staticsymtable) then
  1860. include(current_procinfo.flags,pi_uses_static_symtable);
  1861. addsymref(srsym);
  1862. result:=true;
  1863. exit;
  1864. end;
  1865. end;
  1866. end;
  1867. stackitem:=stackitem^.next;
  1868. end;
  1869. srsym:=nil;
  1870. srsymtable:=nil;
  1871. end;
  1872. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  1873. srsymtable:TSymtable;option:tsymoption):boolean;
  1874. begin
  1875. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,true,option);
  1876. end;
  1877. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1878. var
  1879. hashedid : THashedIDString;
  1880. stackitem : psymtablestackitem;
  1881. classh : tobjectdef;
  1882. begin
  1883. result:=false;
  1884. hashedid.id:=s;
  1885. stackitem:=symtablestack.stack;
  1886. while assigned(stackitem) do
  1887. begin
  1888. {
  1889. It is not possible to have type symbols in:
  1890. parameters
  1891. Exception are classes, objects, records, generic definitions and specializations
  1892. that have the parameterized types inserted in the symtable.
  1893. }
  1894. srsymtable:=stackitem^.symtable;
  1895. if (srsymtable.symtabletype=ObjectSymtable) then
  1896. begin
  1897. classh:=tobjectdef(srsymtable.defowner);
  1898. while assigned(classh) do
  1899. begin
  1900. srsymtable:=classh.symtable;
  1901. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1902. if assigned(srsym) and
  1903. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1904. is_visible_for_object(srsym,current_structdef) then
  1905. begin
  1906. addsymref(srsym);
  1907. result:=true;
  1908. exit;
  1909. end;
  1910. classh:=classh.childof;
  1911. end;
  1912. end
  1913. else
  1914. begin
  1915. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1916. if assigned(srsym) and
  1917. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1918. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  1919. begin
  1920. { we need to know if a procedure references symbols
  1921. in the static symtable, because then it can't be
  1922. inlined from outside this unit }
  1923. if assigned(current_procinfo) and
  1924. (srsym.owner.symtabletype=staticsymtable) then
  1925. include(current_procinfo.flags,pi_uses_static_symtable);
  1926. addsymref(srsym);
  1927. result:=true;
  1928. exit;
  1929. end;
  1930. end;
  1931. stackitem:=stackitem^.next;
  1932. end;
  1933. result:=false;
  1934. srsym:=nil;
  1935. srsymtable:=nil;
  1936. end;
  1937. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1938. var
  1939. pmod : tmodule;
  1940. begin
  1941. pmod:=tmodule(pm);
  1942. result:=false;
  1943. if assigned(pmod.globalsymtable) then
  1944. begin
  1945. srsym:=tsym(pmod.globalsymtable.Find(s));
  1946. if assigned(srsym) then
  1947. begin
  1948. srsymtable:=pmod.globalsymtable;
  1949. addsymref(srsym);
  1950. result:=true;
  1951. exit;
  1952. end;
  1953. end;
  1954. { If the module is the current unit we also need
  1955. to search the local symtable }
  1956. if (pmod=current_module) and
  1957. assigned(pmod.localsymtable) then
  1958. begin
  1959. srsym:=tsym(pmod.localsymtable.Find(s));
  1960. if assigned(srsym) then
  1961. begin
  1962. srsymtable:=pmod.localsymtable;
  1963. addsymref(srsym);
  1964. result:=true;
  1965. exit;
  1966. end;
  1967. end;
  1968. srsym:=nil;
  1969. srsymtable:=nil;
  1970. end;
  1971. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1972. var
  1973. stackitem : psymtablestackitem;
  1974. begin
  1975. result:=false;
  1976. stackitem:=symtablestack.stack;
  1977. while assigned(stackitem) do
  1978. begin
  1979. srsymtable:=stackitem^.symtable;
  1980. if (srsymtable.symtabletype=globalsymtable) and
  1981. (srsymtable.name^=unitname) then
  1982. begin
  1983. srsym:=tsym(srsymtable.find(symname));
  1984. if not assigned(srsym) then
  1985. break;
  1986. result:=true;
  1987. exit;
  1988. end;
  1989. stackitem:=stackitem^.next;
  1990. end;
  1991. { If the module is the current unit we also need
  1992. to search the local symtable }
  1993. if (current_module.globalsymtable=srsymtable) and
  1994. assigned(current_module.localsymtable) then
  1995. begin
  1996. srsymtable:=current_module.localsymtable;
  1997. srsym:=tsym(srsymtable.find(symname));
  1998. if assigned(srsym) then
  1999. begin
  2000. result:=true;
  2001. exit;
  2002. end;
  2003. end;
  2004. end;
  2005. function find_real_objcclass_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2006. var
  2007. hashedid : THashedIDString;
  2008. stackitem : psymtablestackitem;
  2009. srsymtable : tsymtable;
  2010. srsym : tsym;
  2011. begin
  2012. { not a formal definition -> return it }
  2013. if not(oo_is_formal in pd.objectoptions) then
  2014. begin
  2015. result:=pd;
  2016. exit;
  2017. end;
  2018. hashedid.id:=pd.typesym.name;
  2019. stackitem:=symtablestack.stack;
  2020. while assigned(stackitem) do
  2021. begin
  2022. srsymtable:=stackitem^.symtable;
  2023. { ObjC classes can't appear in generics or as nested class
  2024. definitions }
  2025. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) then
  2026. begin
  2027. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2028. if assigned(srsym) and
  2029. (srsym.typ=typesym) and
  2030. is_objc_class_or_protocol(ttypesym(srsym).typedef) and
  2031. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2032. begin
  2033. { the external name for the formal and the real definition must match }
  2034. if tobjectdef(ttypesym(srsym).typedef).objextname^<>pd.objextname^ then
  2035. begin
  2036. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,pd.objextname^,pd.typename);
  2037. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,tobjectdef(ttypesym(srsym).typedef).objextname^);
  2038. end;
  2039. result:=tobjectdef(ttypesym(srsym).typedef);
  2040. if assigned(current_procinfo) and
  2041. (srsym.owner.symtabletype=staticsymtable) then
  2042. include(current_procinfo.flags,pi_uses_static_symtable);
  2043. addsymref(srsym);
  2044. exit;
  2045. end;
  2046. end;
  2047. stackitem:=stackitem^.next;
  2048. end;
  2049. { nothing found: optionally give an error and return the original
  2050. (empty) one }
  2051. if erroronfailure then
  2052. Message1(sym_e_objc_formal_class_not_resolved,pd.objrealname^);
  2053. result:=pd;
  2054. end;
  2055. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:boolean):boolean;
  2056. var
  2057. hashedid : THashedIDString;
  2058. orgclass : tobjectdef;
  2059. i : longint;
  2060. begin
  2061. orgclass:=classh;
  2062. { in case this is a formal objcclass, first find the real definition }
  2063. if assigned(classh) then
  2064. begin
  2065. if (oo_is_formal in classh.objectoptions) then
  2066. classh:=find_real_objcclass_definition(classh,true);
  2067. { The contextclassh is used for visibility. The classh must be equal to
  2068. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  2069. parent or a class helper. }
  2070. if not (contextclassh.is_related(classh) or
  2071. (assigned(contextclassh.extendeddef) and
  2072. (contextclassh.extendeddef.typ=objectdef) and
  2073. contextclassh.extendeddef.is_related(classh))) then
  2074. internalerror(200811161);
  2075. end;
  2076. result:=false;
  2077. hashedid.id:=s;
  2078. { an Objective-C protocol can inherit from multiple other protocols
  2079. -> uses ImplementedInterfaces instead }
  2080. if is_objcprotocol(classh) then
  2081. begin
  2082. srsymtable:=classh.symtable;
  2083. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2084. if assigned(srsym) and
  2085. is_visible_for_object(srsym,contextclassh) then
  2086. begin
  2087. addsymref(srsym);
  2088. result:=true;
  2089. exit;
  2090. end;
  2091. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2092. begin
  2093. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,false) then
  2094. begin
  2095. result:=true;
  2096. exit;
  2097. end;
  2098. end;
  2099. end
  2100. else
  2101. if is_objectpascal_helper(classh) then
  2102. begin
  2103. { helpers have their own obscure search logic... }
  2104. result:=searchsym_in_helper(classh,contextclassh,s,srsym,srsymtable,false);
  2105. if result then
  2106. exit;
  2107. end
  2108. else
  2109. begin
  2110. while assigned(classh) do
  2111. begin
  2112. { search for a class helper method first if this is an Object
  2113. Pascal class }
  2114. if is_class(classh) and searchhelper then
  2115. begin
  2116. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2117. if result then
  2118. { if the procsym is overloaded we need to use the
  2119. "original" symbol; the helper symbol will be found when
  2120. searching for overloads }
  2121. if (srsym.typ<>procsym) or
  2122. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2123. exit;
  2124. end;
  2125. srsymtable:=classh.symtable;
  2126. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2127. if assigned(srsym) and
  2128. is_visible_for_object(srsym,contextclassh) then
  2129. begin
  2130. addsymref(srsym);
  2131. result:=true;
  2132. exit;
  2133. end;
  2134. classh:=classh.childof;
  2135. end;
  2136. end;
  2137. if is_objcclass(orgclass) then
  2138. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2139. else
  2140. begin
  2141. srsym:=nil;
  2142. srsymtable:=nil;
  2143. end;
  2144. end;
  2145. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2146. var
  2147. hashedid : THashedIDString;
  2148. begin
  2149. result:=false;
  2150. hashedid.id:=s;
  2151. { search for a record helper method first }
  2152. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2153. if result then
  2154. { if the procsym is overloaded we need to use the
  2155. "original" symbol; the helper symbol will be found when
  2156. searching for overloads }
  2157. if (srsym.typ<>procsym) or
  2158. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2159. exit;
  2160. srsymtable:=recordh.symtable;
  2161. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2162. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2163. begin
  2164. addsymref(srsym);
  2165. result:=true;
  2166. exit;
  2167. end;
  2168. srsym:=nil;
  2169. srsymtable:=nil;
  2170. end;
  2171. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2172. var
  2173. def : tdef;
  2174. i : longint;
  2175. begin
  2176. { in case this is a formal objcclass, first find the real definition }
  2177. if assigned(classh) and
  2178. (oo_is_formal in classh.objectoptions) then
  2179. classh:=find_real_objcclass_definition(classh,true);
  2180. result:=false;
  2181. def:=nil;
  2182. while assigned(classh) do
  2183. begin
  2184. for i:=0 to classh.symtable.DefList.Count-1 do
  2185. begin
  2186. def:=tstoreddef(classh.symtable.DefList[i]);
  2187. { Find also all hidden private methods to
  2188. be compatible with delphi, see tw6203 (PFV) }
  2189. if (def.typ=procdef) and
  2190. (po_msgint in tprocdef(def).procoptions) and
  2191. (tprocdef(def).messageinf.i=msgid) then
  2192. begin
  2193. srdef:=def;
  2194. srsym:=tprocdef(def).procsym;
  2195. srsymtable:=classh.symtable;
  2196. addsymref(srsym);
  2197. result:=true;
  2198. exit;
  2199. end;
  2200. end;
  2201. classh:=classh.childof;
  2202. end;
  2203. srdef:=nil;
  2204. srsym:=nil;
  2205. srsymtable:=nil;
  2206. end;
  2207. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2208. var
  2209. def : tdef;
  2210. i : longint;
  2211. begin
  2212. { in case this is a formal objcclass, first find the real definition }
  2213. if assigned(classh) and
  2214. (oo_is_formal in classh.objectoptions) then
  2215. classh:=find_real_objcclass_definition(classh,true);
  2216. result:=false;
  2217. def:=nil;
  2218. while assigned(classh) do
  2219. begin
  2220. for i:=0 to classh.symtable.DefList.Count-1 do
  2221. begin
  2222. def:=tstoreddef(classh.symtable.DefList[i]);
  2223. { Find also all hidden private methods to
  2224. be compatible with delphi, see tw6203 (PFV) }
  2225. if (def.typ=procdef) and
  2226. (po_msgstr in tprocdef(def).procoptions) and
  2227. (tprocdef(def).messageinf.str^=s) then
  2228. begin
  2229. srsym:=tprocdef(def).procsym;
  2230. srsymtable:=classh.symtable;
  2231. addsymref(srsym);
  2232. result:=true;
  2233. exit;
  2234. end;
  2235. end;
  2236. classh:=classh.childof;
  2237. end;
  2238. srsym:=nil;
  2239. srsymtable:=nil;
  2240. end;
  2241. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  2242. var
  2243. hashedid : THashedIDString;
  2244. parentclassh : tobjectdef;
  2245. begin
  2246. result:=false;
  2247. if not is_objectpascal_helper(classh) then
  2248. Internalerror(2011030101);
  2249. hashedid.id:=s;
  2250. { in a helper things are a bit more complex:
  2251. 1. search the symbol in the helper (if not "inherited")
  2252. 2. search the symbol in the extended type
  2253. 3. search the symbol in the parent helpers
  2254. 4. only classes: search the symbol in the parents of the extended type
  2255. }
  2256. if not aHasInherited then
  2257. begin
  2258. { search in the helper itself }
  2259. srsymtable:=classh.symtable;
  2260. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2261. if assigned(srsym) and
  2262. is_visible_for_object(srsym,contextclassh) then
  2263. begin
  2264. addsymref(srsym);
  2265. result:=true;
  2266. exit;
  2267. end;
  2268. end;
  2269. { now search in the extended type itself }
  2270. if classh.extendeddef.typ in [recorddef,objectdef] then
  2271. begin
  2272. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  2273. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2274. if assigned(srsym) and
  2275. is_visible_for_object(srsym,contextclassh) then
  2276. begin
  2277. addsymref(srsym);
  2278. result:=true;
  2279. exit;
  2280. end;
  2281. end;
  2282. { now search in the parent helpers }
  2283. parentclassh:=classh.childof;
  2284. while assigned(parentclassh) do
  2285. begin
  2286. srsymtable:=parentclassh.symtable;
  2287. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2288. if assigned(srsym) and
  2289. is_visible_for_object(srsym,contextclassh) then
  2290. begin
  2291. addsymref(srsym);
  2292. result:=true;
  2293. exit;
  2294. end;
  2295. parentclassh:=parentclassh.childof;
  2296. end;
  2297. if is_class(classh.extendeddef) then
  2298. { now search in the parents of the extended class (with helpers!) }
  2299. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,true);
  2300. { addsymref is already called by searchsym_in_class }
  2301. end;
  2302. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2303. var
  2304. sym : Tprocsym;
  2305. hashedid : THashedIDString;
  2306. curreq,
  2307. besteq : tequaltype;
  2308. currpd,
  2309. bestpd : tprocdef;
  2310. stackitem : psymtablestackitem;
  2311. begin
  2312. hashedid.id:=overloaded_names[assignment_type];
  2313. besteq:=te_incompatible;
  2314. bestpd:=nil;
  2315. stackitem:=symtablestack.stack;
  2316. while assigned(stackitem) do
  2317. begin
  2318. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2319. if sym<>nil then
  2320. begin
  2321. if sym.typ<>procsym then
  2322. internalerror(200402031);
  2323. { if the source type is an alias then this is only the second choice,
  2324. if you mess with this code, check tw4093 }
  2325. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2326. if curreq>besteq then
  2327. begin
  2328. besteq:=curreq;
  2329. bestpd:=currpd;
  2330. if (besteq=te_exact) then
  2331. break;
  2332. end;
  2333. end;
  2334. stackitem:=stackitem^.next;
  2335. end;
  2336. result:=bestpd;
  2337. end;
  2338. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2339. begin
  2340. { search record/object symtable first for a suitable operator }
  2341. if from_def.typ in [recorddef,objectdef] then
  2342. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2343. if to_def.typ in [recorddef,objectdef] then
  2344. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2345. { if type conversion is explicit then search first for explicit
  2346. operator overload and if not found then use implicit operator }
  2347. if explicit then
  2348. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2349. else
  2350. result:=nil;
  2351. if result=nil then
  2352. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2353. { restore symtable stack }
  2354. if to_def.typ in [recorddef,objectdef] then
  2355. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2356. if from_def.typ in [recorddef,objectdef] then
  2357. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2358. end;
  2359. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2360. var
  2361. sym : Tprocsym;
  2362. hashedid : THashedIDString;
  2363. curreq,
  2364. besteq : tequaltype;
  2365. currpd,
  2366. bestpd : tprocdef;
  2367. stackitem : psymtablestackitem;
  2368. begin
  2369. hashedid.id:='enumerator';
  2370. besteq:=te_incompatible;
  2371. bestpd:=nil;
  2372. stackitem:=symtablestack.stack;
  2373. while assigned(stackitem) do
  2374. begin
  2375. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2376. if sym<>nil then
  2377. begin
  2378. if sym.typ<>procsym then
  2379. internalerror(200910241);
  2380. { if the source type is an alias then this is only the second choice,
  2381. if you mess with this code, check tw4093 }
  2382. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2383. if curreq>besteq then
  2384. begin
  2385. besteq:=curreq;
  2386. bestpd:=currpd;
  2387. if (besteq=te_exact) then
  2388. break;
  2389. end;
  2390. end;
  2391. stackitem:=stackitem^.next;
  2392. end;
  2393. result:=bestpd;
  2394. end;
  2395. function search_system_type(const s: TIDString): ttypesym;
  2396. var
  2397. sym : tsym;
  2398. begin
  2399. sym:=tsym(systemunit.Find(s));
  2400. if not assigned(sym) or
  2401. (sym.typ<>typesym) then
  2402. cgmessage1(cg_f_unknown_system_type,s);
  2403. result:=ttypesym(sym);
  2404. end;
  2405. function try_search_system_type(const s: TIDString): ttypesym;
  2406. var
  2407. sym : tsym;
  2408. begin
  2409. sym:=tsym(systemunit.Find(s));
  2410. if not assigned(sym) then
  2411. result:=nil
  2412. else
  2413. begin
  2414. if sym.typ<>typesym then
  2415. cgmessage1(cg_f_unknown_system_type,s);
  2416. result:=ttypesym(sym);
  2417. end;
  2418. end;
  2419. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2420. var
  2421. srsymtable: tsymtable;
  2422. sym: tsym;
  2423. begin
  2424. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2425. (sym.typ=typesym) then
  2426. begin
  2427. result:=ttypesym(sym);
  2428. exit;
  2429. end
  2430. else
  2431. begin
  2432. if throwerror then
  2433. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  2434. result:=nil;
  2435. end;
  2436. end;
  2437. function search_last_objectpascal_helper(pd,contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  2438. var
  2439. s: string;
  2440. list: TFPObjectList;
  2441. i: integer;
  2442. st: tsymtable;
  2443. begin
  2444. result:=false;
  2445. odef:=nil;
  2446. { when there are no helpers active currently then we don't need to do
  2447. anything }
  2448. if current_module.extendeddefs.count=0 then
  2449. exit;
  2450. { no helpers for anonymous types }
  2451. if not assigned(pd.objrealname) or (pd.objrealname^='') then
  2452. exit;
  2453. { if pd is defined inside a procedure we must not use make_mangledname
  2454. (as a helper may not be defined in a procedure this is no problem...)}
  2455. st:=pd.owner;
  2456. while st.symtabletype in [objectsymtable,recordsymtable] do
  2457. st:=st.defowner.owner;
  2458. if st.symtabletype=localsymtable then
  2459. exit;
  2460. { the mangled name is used as the key for tmodule.extendeddefs }
  2461. s:=make_mangledname('',pd.symtable,'');
  2462. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  2463. if assigned(list) and (list.count>0) then
  2464. begin
  2465. i:=list.count-1;
  2466. repeat
  2467. odef:=tobjectdef(list[list.count-1]);
  2468. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2469. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  2470. dec(i);
  2471. until result or (i<0);
  2472. if not result then
  2473. { just to be sure that noone uses odef }
  2474. odef:=nil;
  2475. end;
  2476. end;
  2477. function search_objectpascal_helper(pd,contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2478. var
  2479. hashedid : THashedIDString;
  2480. classh : tobjectdef;
  2481. i : integer;
  2482. pdef : tprocdef;
  2483. begin
  2484. result:=false;
  2485. { if there is no class helper for the class then there is no need to
  2486. search further }
  2487. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  2488. exit;
  2489. hashedid.id:=s;
  2490. repeat
  2491. srsymtable:=classh.symtable;
  2492. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2493. if srsym<>nil then
  2494. begin
  2495. if srsym.typ=propertysym then
  2496. begin
  2497. result:=true;
  2498. exit;
  2499. end;
  2500. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2501. begin
  2502. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2503. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2504. continue;
  2505. { we need to know if a procedure references symbols
  2506. in the static symtable, because then it can't be
  2507. inlined from outside this unit }
  2508. if assigned(current_procinfo) and
  2509. (srsym.owner.symtabletype=staticsymtable) then
  2510. include(current_procinfo.flags,pi_uses_static_symtable);
  2511. { the first found method wins }
  2512. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2513. srsymtable:=srsym.owner;
  2514. addsymref(srsym);
  2515. result:=true;
  2516. exit;
  2517. end;
  2518. end;
  2519. { try the helper parent if available }
  2520. classh:=classh.childof;
  2521. until classh=nil;
  2522. srsym:=nil;
  2523. srsymtable:=nil;
  2524. end;
  2525. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2526. var
  2527. hashedid : THashedIDString;
  2528. stackitem : psymtablestackitem;
  2529. i : longint;
  2530. defowner : tobjectdef;
  2531. begin
  2532. hashedid.id:=class_helper_prefix+s;
  2533. stackitem:=symtablestack.stack;
  2534. while assigned(stackitem) do
  2535. begin
  2536. srsymtable:=stackitem^.symtable;
  2537. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2538. if assigned(srsym) then
  2539. begin
  2540. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2541. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2542. (srsym.typ<>procsym) then
  2543. internalerror(2009111505);
  2544. { check whether this procsym includes a helper for this particular class }
  2545. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2546. begin
  2547. { does pd inherit from (or is the same as) the class
  2548. that this method's category extended?
  2549. Warning: this list contains both category and objcclass methods
  2550. (for id.randommethod), so only check category methods here
  2551. }
  2552. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  2553. if (oo_is_classhelper in defowner.objectoptions) and
  2554. pd.is_related(defowner.childof) then
  2555. begin
  2556. { we need to know if a procedure references symbols
  2557. in the static symtable, because then it can't be
  2558. inlined from outside this unit }
  2559. if assigned(current_procinfo) and
  2560. (srsym.owner.symtabletype=staticsymtable) then
  2561. include(current_procinfo.flags,pi_uses_static_symtable);
  2562. { no need to keep looking. There might be other
  2563. categories that extend this, a parent or child
  2564. class with a method with the same name (either
  2565. overriding this one, or overridden by this one),
  2566. but that doesn't matter as far as the basic
  2567. procsym is concerned.
  2568. }
  2569. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2570. srsymtable:=srsym.owner;
  2571. addsymref(srsym);
  2572. result:=true;
  2573. exit;
  2574. end;
  2575. end;
  2576. end;
  2577. stackitem:=stackitem^.next;
  2578. end;
  2579. srsym:=nil;
  2580. srsymtable:=nil;
  2581. result:=false;
  2582. end;
  2583. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2584. var
  2585. hashedid : THashedIDString;
  2586. stackitem : psymtablestackitem;
  2587. i : longint;
  2588. begin
  2589. hashedid.id:=class_helper_prefix+s;
  2590. stackitem:=symtablestack.stack;
  2591. while assigned(stackitem) do
  2592. begin
  2593. srsymtable:=stackitem^.symtable;
  2594. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2595. if assigned(srsym) then
  2596. begin
  2597. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2598. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2599. (srsym.typ<>procsym) then
  2600. internalerror(2009112005);
  2601. { check whether this procsym includes a helper for this particular class }
  2602. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2603. begin
  2604. { we need to know if a procedure references symbols
  2605. in the static symtable, because then it can't be
  2606. inlined from outside this unit }
  2607. if assigned(current_procinfo) and
  2608. (srsym.owner.symtabletype=staticsymtable) then
  2609. include(current_procinfo.flags,pi_uses_static_symtable);
  2610. { no need to keep looking. There might be other
  2611. methods with the same name, but that doesn't matter
  2612. as far as the basic procsym is concerned.
  2613. }
  2614. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2615. { We need the symtable in which the classhelper-like sym
  2616. is located, not the objectdef. The reason is that the
  2617. callnode will climb the symtablestack until it encounters
  2618. this symtable to start looking for overloads (and it won't
  2619. find the objectsymtable in which this method sym is
  2620. located
  2621. srsymtable:=srsym.owner;
  2622. }
  2623. addsymref(srsym);
  2624. result:=true;
  2625. exit;
  2626. end;
  2627. end;
  2628. stackitem:=stackitem^.next;
  2629. end;
  2630. srsym:=nil;
  2631. srsymtable:=nil;
  2632. result:=false;
  2633. end;
  2634. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  2635. { searches n in symtable of pd and all anchestors }
  2636. var
  2637. hashedid : THashedIDString;
  2638. srsym : tsym;
  2639. orgpd : tabstractrecorddef;
  2640. srsymtable : tsymtable;
  2641. begin
  2642. { in case this is a formal objcclass, first find the real definition }
  2643. if (oo_is_formal in pd.objectoptions) then
  2644. pd:=find_real_objcclass_definition(tobjectdef(pd),true);
  2645. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  2646. exit;
  2647. hashedid.id:=s;
  2648. orgpd:=pd;
  2649. while assigned(pd) do
  2650. begin
  2651. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  2652. if assigned(srsym) then
  2653. begin
  2654. search_struct_member:=srsym;
  2655. exit;
  2656. end;
  2657. if pd.typ=objectdef then
  2658. pd:=tobjectdef(pd).childof
  2659. else
  2660. pd:=nil;
  2661. end;
  2662. { not found, now look for class helpers }
  2663. if is_objcclass(pd) then
  2664. search_objc_helper(tobjectdef(orgpd),s,result,srsymtable)
  2665. else
  2666. result:=nil;
  2667. end;
  2668. function search_macro(const s : string):tsym;
  2669. var
  2670. stackitem : psymtablestackitem;
  2671. hashedid : THashedIDString;
  2672. srsym : tsym;
  2673. begin
  2674. hashedid.id:=s;
  2675. { First search the localmacrosymtable before searching the
  2676. global macrosymtables from the units }
  2677. if assigned(current_module) then
  2678. begin
  2679. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2680. if assigned(srsym) then
  2681. begin
  2682. result:= srsym;
  2683. exit;
  2684. end;
  2685. end;
  2686. stackitem:=macrosymtablestack.stack;
  2687. while assigned(stackitem) do
  2688. begin
  2689. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2690. if assigned(srsym) then
  2691. begin
  2692. result:= srsym;
  2693. exit;
  2694. end;
  2695. stackitem:=stackitem^.next;
  2696. end;
  2697. result:= nil;
  2698. end;
  2699. function defined_macro(const s : string):boolean;
  2700. var
  2701. mac: tmacro;
  2702. begin
  2703. mac:=tmacro(search_macro(s));
  2704. if assigned(mac) then
  2705. begin
  2706. mac.is_used:=true;
  2707. defined_macro:=mac.defined;
  2708. end
  2709. else
  2710. defined_macro:=false;
  2711. end;
  2712. {****************************************************************************
  2713. Object Helpers
  2714. ****************************************************************************}
  2715. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  2716. { returns the default property of a class, searches also anchestors }
  2717. var
  2718. _defaultprop : tpropertysym;
  2719. helperpd : tobjectdef;
  2720. begin
  2721. _defaultprop:=nil;
  2722. { first search in helper's hierarchy }
  2723. if search_last_objectpascal_helper(pd,nil,helperpd) then
  2724. while assigned(helperpd) do
  2725. begin
  2726. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  2727. if assigned(_defaultprop) then
  2728. break;
  2729. helperpd:=helperpd.childof;
  2730. end;
  2731. if assigned(_defaultprop) then
  2732. begin
  2733. search_default_property:=_defaultprop;
  2734. exit;
  2735. end;
  2736. { now search in the type's hierarchy itself }
  2737. while assigned(pd) do
  2738. begin
  2739. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2740. if assigned(_defaultprop) then
  2741. break;
  2742. if (pd.typ=objectdef) then
  2743. pd:=tobjectdef(pd).childof
  2744. else
  2745. break;
  2746. end;
  2747. search_default_property:=_defaultprop;
  2748. end;
  2749. {****************************************************************************
  2750. Macro Helpers
  2751. ****************************************************************************}
  2752. procedure def_system_macro(const name : string);
  2753. var
  2754. mac : tmacro;
  2755. s: string;
  2756. begin
  2757. if name = '' then
  2758. internalerror(2004121202);
  2759. s:= upper(name);
  2760. mac:=tmacro(search_macro(s));
  2761. if not assigned(mac) then
  2762. begin
  2763. mac:=tmacro.create(s);
  2764. if assigned(current_module) then
  2765. current_module.localmacrosymtable.insert(mac)
  2766. else
  2767. initialmacrosymtable.insert(mac);
  2768. end;
  2769. Message1(parser_c_macro_defined,mac.name);
  2770. mac.defined:=true;
  2771. end;
  2772. procedure set_system_macro(const name, value : string);
  2773. var
  2774. mac : tmacro;
  2775. s: string;
  2776. begin
  2777. if name = '' then
  2778. internalerror(2004121203);
  2779. s:= upper(name);
  2780. mac:=tmacro(search_macro(s));
  2781. if not assigned(mac) then
  2782. begin
  2783. mac:=tmacro.create(s);
  2784. if assigned(current_module) then
  2785. current_module.localmacrosymtable.insert(mac)
  2786. else
  2787. initialmacrosymtable.insert(mac);
  2788. end
  2789. else
  2790. begin
  2791. mac.is_compiler_var:=false;
  2792. if assigned(mac.buftext) then
  2793. freemem(mac.buftext,mac.buflen);
  2794. end;
  2795. Message2(parser_c_macro_set_to,mac.name,value);
  2796. mac.buflen:=length(value);
  2797. getmem(mac.buftext,mac.buflen);
  2798. move(value[1],mac.buftext^,mac.buflen);
  2799. mac.defined:=true;
  2800. end;
  2801. procedure set_system_compvar(const name, value : string);
  2802. var
  2803. mac : tmacro;
  2804. s: string;
  2805. begin
  2806. if name = '' then
  2807. internalerror(2004121204);
  2808. s:= upper(name);
  2809. mac:=tmacro(search_macro(s));
  2810. if not assigned(mac) then
  2811. begin
  2812. mac:=tmacro.create(s);
  2813. mac.is_compiler_var:=true;
  2814. if assigned(current_module) then
  2815. current_module.localmacrosymtable.insert(mac)
  2816. else
  2817. initialmacrosymtable.insert(mac);
  2818. end
  2819. else
  2820. begin
  2821. mac.is_compiler_var:=true;
  2822. if assigned(mac.buftext) then
  2823. freemem(mac.buftext,mac.buflen);
  2824. end;
  2825. Message2(parser_c_macro_set_to,mac.name,value);
  2826. mac.buflen:=length(value);
  2827. getmem(mac.buftext,mac.buflen);
  2828. move(value[1],mac.buftext^,mac.buflen);
  2829. mac.defined:=true;
  2830. end;
  2831. procedure undef_system_macro(const name : string);
  2832. var
  2833. mac : tmacro;
  2834. s: string;
  2835. begin
  2836. if name = '' then
  2837. internalerror(2004121205);
  2838. s:= upper(name);
  2839. mac:=tmacro(search_macro(s));
  2840. if not assigned(mac) then
  2841. {If not found, then it's already undefined.}
  2842. else
  2843. begin
  2844. Message1(parser_c_macro_undefined,mac.name);
  2845. mac.defined:=false;
  2846. mac.is_compiler_var:=false;
  2847. { delete old definition }
  2848. if assigned(mac.buftext) then
  2849. begin
  2850. freemem(mac.buftext,mac.buflen);
  2851. mac.buftext:=nil;
  2852. end;
  2853. end;
  2854. end;
  2855. {$ifdef UNITALIASES}
  2856. {****************************************************************************
  2857. TUNIT_ALIAS
  2858. ****************************************************************************}
  2859. constructor tunit_alias.create(const n:string);
  2860. var
  2861. i : longint;
  2862. begin
  2863. i:=pos('=',n);
  2864. if i=0 then
  2865. fail;
  2866. inherited createname(Copy(n,1,i-1));
  2867. newname:=stringdup(Copy(n,i+1,255));
  2868. end;
  2869. destructor tunit_alias.destroy;
  2870. begin
  2871. stringdispose(newname);
  2872. inherited destroy;
  2873. end;
  2874. procedure addunitalias(const n:string);
  2875. begin
  2876. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2877. end;
  2878. function getunitalias(const n:string):string;
  2879. var
  2880. p : punit_alias;
  2881. begin
  2882. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2883. if assigned(p) then
  2884. getunitalias:=punit_alias(p).newname^
  2885. else
  2886. getunitalias:=n;
  2887. end;
  2888. {$endif UNITALIASES}
  2889. {****************************************************************************
  2890. Init/Done Symtable
  2891. ****************************************************************************}
  2892. procedure InitSymtable;
  2893. begin
  2894. { Reset symbolstack }
  2895. symtablestack:=nil;
  2896. systemunit:=nil;
  2897. { create error syms and def }
  2898. generrorsym:=terrorsym.create;
  2899. generrordef:=terrordef.create;
  2900. { macros }
  2901. initialmacrosymtable:=tmacrosymtable.create(false);
  2902. macrosymtablestack:=TSymtablestack.create;
  2903. macrosymtablestack.push(initialmacrosymtable);
  2904. {$ifdef UNITALIASES}
  2905. { unit aliases }
  2906. unitaliases:=TFPHashObjectList.create;
  2907. {$endif}
  2908. { set some global vars to nil, might be important for the ide }
  2909. class_tobject:=nil;
  2910. interface_iunknown:=nil;
  2911. interface_idispatch:=nil;
  2912. rec_tguid:=nil;
  2913. dupnr:=0;
  2914. end;
  2915. procedure DoneSymtable;
  2916. begin
  2917. generrorsym.owner:=nil;
  2918. generrorsym.free;
  2919. generrordef.owner:=nil;
  2920. generrordef.free;
  2921. initialmacrosymtable.free;
  2922. macrosymtablestack.free;
  2923. {$ifdef UNITALIASES}
  2924. unitaliases.free;
  2925. {$endif}
  2926. end;
  2927. end.