symtable.pas 175 KB

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