symtable.pas 175 KB

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