Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:00

0001 //===--- Designator.h - Initialization Designator ---------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines interfaces used to represent designators (a la
0010 // C99 designated initializers) during parsing.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_SEMA_DESIGNATOR_H
0015 #define LLVM_CLANG_SEMA_DESIGNATOR_H
0016 
0017 #include "clang/Basic/SourceLocation.h"
0018 #include "llvm/ADT/SmallVector.h"
0019 
0020 namespace clang {
0021 
0022 class Expr;
0023 class IdentifierInfo;
0024 
0025 /// Designator - A designator in a C99 designated initializer.
0026 ///
0027 /// This class is a discriminated union which holds the various
0028 /// different sorts of designators possible. A Designation is an array of
0029 /// these.  An example of a designator are things like this:
0030 ///
0031 ///      [8] .field [47]        // C99 designation: 3 designators
0032 ///      [8 ... 47]  field:     // GNU extensions: 2 designators
0033 ///
0034 /// These occur in initializers, e.g.:
0035 ///
0036 ///      int a[10] = {2, 4, [8]=9, 10};
0037 ///
0038 class Designator {
0039   /// A field designator, e.g., ".x = 42".
0040   struct FieldDesignatorInfo {
0041     /// Refers to the field being initialized.
0042     const IdentifierInfo *FieldName;
0043 
0044     /// The location of the '.' in the designated initializer.
0045     SourceLocation DotLoc;
0046 
0047     /// The location of the field name in the designated initializer.
0048     SourceLocation FieldLoc;
0049 
0050     FieldDesignatorInfo(const IdentifierInfo *FieldName, SourceLocation DotLoc,
0051                         SourceLocation FieldLoc)
0052         : FieldName(FieldName), DotLoc(DotLoc), FieldLoc(FieldLoc) {}
0053   };
0054 
0055   /// An array designator, e.g., "[42] = 0".
0056   struct ArrayDesignatorInfo {
0057     Expr *Index;
0058 
0059     // The location of the '[' in the designated initializer.
0060     SourceLocation LBracketLoc;
0061 
0062     // The location of the ']' in the designated initializer.
0063     mutable SourceLocation RBracketLoc;
0064 
0065     ArrayDesignatorInfo(Expr *Index, SourceLocation LBracketLoc)
0066         : Index(Index), LBracketLoc(LBracketLoc) {}
0067   };
0068 
0069   /// An array range designator, e.g. "[42 ... 50] = 1".
0070   struct ArrayRangeDesignatorInfo {
0071     Expr *Start;
0072     Expr *End;
0073 
0074     // The location of the '[' in the designated initializer.
0075     SourceLocation LBracketLoc;
0076 
0077     // The location of the '...' in the designated initializer.
0078     SourceLocation EllipsisLoc;
0079 
0080     // The location of the ']' in the designated initializer.
0081     mutable SourceLocation RBracketLoc;
0082 
0083     ArrayRangeDesignatorInfo(Expr *Start, Expr *End, SourceLocation LBracketLoc,
0084                              SourceLocation EllipsisLoc)
0085         : Start(Start), End(End), LBracketLoc(LBracketLoc),
0086           EllipsisLoc(EllipsisLoc) {}
0087   };
0088 
0089   /// The kind of designator this describes.
0090   enum DesignatorKind {
0091     FieldDesignator,
0092     ArrayDesignator,
0093     ArrayRangeDesignator
0094   };
0095 
0096   DesignatorKind Kind;
0097 
0098   union {
0099     FieldDesignatorInfo FieldInfo;
0100     ArrayDesignatorInfo ArrayInfo;
0101     ArrayRangeDesignatorInfo ArrayRangeInfo;
0102   };
0103 
0104   Designator(DesignatorKind Kind) : Kind(Kind) {}
0105 
0106 public:
0107   bool isFieldDesignator() const { return Kind == FieldDesignator; }
0108   bool isArrayDesignator() const { return Kind == ArrayDesignator; }
0109   bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
0110 
0111   //===--------------------------------------------------------------------===//
0112   // FieldDesignatorInfo
0113 
0114   /// Creates a field designator.
0115   static Designator CreateFieldDesignator(const IdentifierInfo *FieldName,
0116                                           SourceLocation DotLoc,
0117                                           SourceLocation FieldLoc) {
0118     Designator D(FieldDesignator);
0119     new (&D.FieldInfo) FieldDesignatorInfo(FieldName, DotLoc, FieldLoc);
0120     return D;
0121   }
0122 
0123   const IdentifierInfo *getFieldDecl() const {
0124     assert(isFieldDesignator() && "Invalid accessor");
0125     return FieldInfo.FieldName;
0126   }
0127 
0128   SourceLocation getDotLoc() const {
0129     assert(isFieldDesignator() && "Invalid accessor");
0130     return FieldInfo.DotLoc;
0131   }
0132 
0133   SourceLocation getFieldLoc() const {
0134     assert(isFieldDesignator() && "Invalid accessor");
0135     return FieldInfo.FieldLoc;
0136   }
0137 
0138   //===--------------------------------------------------------------------===//
0139   // ArrayDesignatorInfo:
0140 
0141   /// Creates an array designator.
0142   static Designator CreateArrayDesignator(Expr *Index,
0143                                           SourceLocation LBracketLoc) {
0144     Designator D(ArrayDesignator);
0145     new (&D.ArrayInfo) ArrayDesignatorInfo(Index, LBracketLoc);
0146     return D;
0147   }
0148 
0149   Expr *getArrayIndex() const {
0150     assert(isArrayDesignator() && "Invalid accessor");
0151     return ArrayInfo.Index;
0152   }
0153 
0154   SourceLocation getLBracketLoc() const {
0155     assert((isArrayDesignator() || isArrayRangeDesignator()) &&
0156            "Invalid accessor");
0157     return isArrayDesignator() ? ArrayInfo.LBracketLoc
0158                                : ArrayRangeInfo.LBracketLoc;
0159   }
0160 
0161   SourceLocation getRBracketLoc() const {
0162     assert((isArrayDesignator() || isArrayRangeDesignator()) &&
0163            "Invalid accessor");
0164     return isArrayDesignator() ? ArrayInfo.RBracketLoc
0165                                : ArrayRangeInfo.RBracketLoc;
0166   }
0167 
0168   //===--------------------------------------------------------------------===//
0169   // ArrayRangeDesignatorInfo:
0170 
0171   /// Creates a GNU array-range designator.
0172   static Designator CreateArrayRangeDesignator(Expr *Start, Expr *End,
0173                                                SourceLocation LBracketLoc,
0174                                                SourceLocation EllipsisLoc) {
0175     Designator D(ArrayRangeDesignator);
0176     new (&D.ArrayRangeInfo)
0177         ArrayRangeDesignatorInfo(Start, End, LBracketLoc, EllipsisLoc);
0178     return D;
0179   }
0180 
0181   Expr *getArrayRangeStart() const {
0182     assert(isArrayRangeDesignator() && "Invalid accessor");
0183     return ArrayRangeInfo.Start;
0184   }
0185 
0186   Expr *getArrayRangeEnd() const {
0187     assert(isArrayRangeDesignator() && "Invalid accessor");
0188     return ArrayRangeInfo.End;
0189   }
0190 
0191   SourceLocation getEllipsisLoc() const {
0192     assert(isArrayRangeDesignator() && "Invalid accessor");
0193     return ArrayRangeInfo.EllipsisLoc;
0194   }
0195 
0196   void setRBracketLoc(SourceLocation RBracketLoc) const {
0197     assert((isArrayDesignator() || isArrayRangeDesignator()) &&
0198            "Invalid accessor");
0199     if (isArrayDesignator())
0200       ArrayInfo.RBracketLoc = RBracketLoc;
0201     else
0202       ArrayRangeInfo.RBracketLoc = RBracketLoc;
0203   }
0204 };
0205 
0206 /// Designation - Represent a full designation, which is a sequence of
0207 /// designators.  This class is mostly a helper for InitListDesignations.
0208 class Designation {
0209   /// Designators - The actual designators for this initializer.
0210   SmallVector<Designator, 2> Designators;
0211 
0212 public:
0213   /// AddDesignator - Add a designator to the end of this list.
0214   void AddDesignator(Designator D) { Designators.push_back(D); }
0215 
0216   bool empty() const { return Designators.empty(); }
0217 
0218   unsigned getNumDesignators() const { return Designators.size(); }
0219   const Designator &getDesignator(unsigned Idx) const {
0220     assert(Idx < Designators.size());
0221     return Designators[Idx];
0222   }
0223 };
0224 
0225 } // end namespace clang
0226 
0227 #endif