symtable.pas 180 KB

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