symtable.pas 140 KB

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