symtable.pas 167 KB

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