symtable.pas 158 KB

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