symtable.pas 187 KB

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