symtable.pas 140 KB

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