1 //===--- Designator.h - Initialization Designator ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines interfaces used to represent designators (a la
11 // C99 designated initializers) during parsing.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SEMA_DESIGNATOR_H
16 #define LLVM_CLANG_SEMA_DESIGNATOR_H
18 #include "clang/Basic/SourceLocation.h"
19 #include "llvm/ADT/SmallVector.h"
27 /// Designator - A designator in a C99 designated initializer.
29 /// This class is a discriminated union which holds the various
30 /// different sorts of designators possible. A Designation is an array of
31 /// these. An example of a designator are things like this:
32 /// [8] .field [47] // C99 designation: 3 designators
33 /// [8 ... 47] field: // GNU extensions: 2 designators
34 /// These occur in initializers, e.g.:
35 /// int a[10] = {2, 4, [8]=9, 10};
40 FieldDesignator, ArrayDesignator, ArrayRangeDesignator
45 struct FieldDesignatorInfo {
46 const IdentifierInfo *II;
50 struct ArrayDesignatorInfo {
53 mutable unsigned RBracketLoc;
55 struct ArrayRangeDesignatorInfo {
57 unsigned LBracketLoc, EllipsisLoc;
58 mutable unsigned RBracketLoc;
62 FieldDesignatorInfo FieldInfo;
63 ArrayDesignatorInfo ArrayInfo;
64 ArrayRangeDesignatorInfo ArrayRangeInfo;
69 DesignatorKind getKind() const { return Kind; }
70 bool isFieldDesignator() const { return Kind == FieldDesignator; }
71 bool isArrayDesignator() const { return Kind == ArrayDesignator; }
72 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
74 const IdentifierInfo *getField() const {
75 assert(isFieldDesignator() && "Invalid accessor");
79 SourceLocation getDotLoc() const {
80 assert(isFieldDesignator() && "Invalid accessor");
81 return SourceLocation::getFromRawEncoding(FieldInfo.DotLoc);
84 SourceLocation getFieldLoc() const {
85 assert(isFieldDesignator() && "Invalid accessor");
86 return SourceLocation::getFromRawEncoding(FieldInfo.NameLoc);
89 Expr *getArrayIndex() const {
90 assert(isArrayDesignator() && "Invalid accessor");
91 return ArrayInfo.Index;
94 Expr *getArrayRangeStart() const {
95 assert(isArrayRangeDesignator() && "Invalid accessor");
96 return ArrayRangeInfo.Start;
98 Expr *getArrayRangeEnd() const {
99 assert(isArrayRangeDesignator() && "Invalid accessor");
100 return ArrayRangeInfo.End;
103 SourceLocation getLBracketLoc() const {
104 assert((isArrayDesignator() || isArrayRangeDesignator()) &&
106 if (isArrayDesignator())
107 return SourceLocation::getFromRawEncoding(ArrayInfo.LBracketLoc);
109 return SourceLocation::getFromRawEncoding(ArrayRangeInfo.LBracketLoc);
112 SourceLocation getRBracketLoc() const {
113 assert((isArrayDesignator() || isArrayRangeDesignator()) &&
115 if (isArrayDesignator())
116 return SourceLocation::getFromRawEncoding(ArrayInfo.RBracketLoc);
118 return SourceLocation::getFromRawEncoding(ArrayRangeInfo.RBracketLoc);
121 SourceLocation getEllipsisLoc() const {
122 assert(isArrayRangeDesignator() && "Invalid accessor");
123 return SourceLocation::getFromRawEncoding(ArrayRangeInfo.EllipsisLoc);
126 static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc,
127 SourceLocation NameLoc) {
129 D.Kind = FieldDesignator;
131 D.FieldInfo.DotLoc = DotLoc.getRawEncoding();
132 D.FieldInfo.NameLoc = NameLoc.getRawEncoding();
136 static Designator getArray(Expr *Index,
137 SourceLocation LBracketLoc) {
139 D.Kind = ArrayDesignator;
140 D.ArrayInfo.Index = Index;
141 D.ArrayInfo.LBracketLoc = LBracketLoc.getRawEncoding();
142 D.ArrayInfo.RBracketLoc = 0;
146 static Designator getArrayRange(Expr *Start,
148 SourceLocation LBracketLoc,
149 SourceLocation EllipsisLoc) {
151 D.Kind = ArrayRangeDesignator;
152 D.ArrayRangeInfo.Start = Start;
153 D.ArrayRangeInfo.End = End;
154 D.ArrayRangeInfo.LBracketLoc = LBracketLoc.getRawEncoding();
155 D.ArrayRangeInfo.EllipsisLoc = EllipsisLoc.getRawEncoding();
156 D.ArrayRangeInfo.RBracketLoc = 0;
160 void setRBracketLoc(SourceLocation RBracketLoc) const {
161 assert((isArrayDesignator() || isArrayRangeDesignator()) &&
163 if (isArrayDesignator())
164 ArrayInfo.RBracketLoc = RBracketLoc.getRawEncoding();
166 ArrayRangeInfo.RBracketLoc = RBracketLoc.getRawEncoding();
169 /// ClearExprs - Null out any expression references, which prevents
170 /// them from being 'delete'd later.
171 void ClearExprs(Sema &Actions) {}
173 /// FreeExprs - Release any unclaimed memory for the expressions in
175 void FreeExprs(Sema &Actions) {}
179 /// Designation - Represent a full designation, which is a sequence of
180 /// designators. This class is mostly a helper for InitListDesignations.
182 /// Designators - The actual designators for this initializer.
183 SmallVector<Designator, 2> Designators;
186 /// AddDesignator - Add a designator to the end of this list.
187 void AddDesignator(Designator D) {
188 Designators.push_back(D);
191 bool empty() const { return Designators.empty(); }
193 unsigned getNumDesignators() const { return Designators.size(); }
194 const Designator &getDesignator(unsigned Idx) const {
195 assert(Idx < Designators.size());
196 return Designators[Idx];
199 /// ClearExprs - Null out any expression references, which prevents them from
200 /// being 'delete'd later.
201 void ClearExprs(Sema &Actions) {}
203 /// FreeExprs - Release any unclaimed memory for the expressions in this
205 void FreeExprs(Sema &Actions) {}
208 } // end namespace clang