symtable.pas 132 KB

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