symtable.pas 178 KB

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