symtable.pas 179 KB

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