symtable.pas 135 KB

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