symtable.pas 159 KB

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