symtable.pas 123 KB

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