symtable.pas 115 KB

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