clang 23.0.0git
Designator.h
Go to the documentation of this file.
1//===--- Designator.h - Initialization Designator ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines interfaces used to represent designators (a la
10// C99 designated initializers) during parsing.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SEMA_DESIGNATOR_H
15#define LLVM_CLANG_SEMA_DESIGNATOR_H
16
18#include "llvm/ADT/SmallVector.h"
19
20namespace clang {
21
22class Expr;
23class IdentifierInfo;
24
25/// Designator - A designator in a C99 designated initializer.
26///
27/// This class is a discriminated union which holds the various
28/// different sorts of designators possible. A Designation is an array of
29/// these. An example of a designator are things like this:
30///
31/// [8] .field [47] // C99 designation: 3 designators
32/// [8 ... 47] field: // GNU extensions: 2 designators
33///
34/// These occur in initializers, e.g.:
35///
36/// int a[10] = {2, 4, [8]=9, 10};
37///
38class Designator {
39 /// A field designator, e.g., ".x = 42".
40 struct FieldDesignatorInfo {
41 /// Refers to the field being initialized.
42 const IdentifierInfo *FieldName;
43
44 /// The location of the '.' in the designated initializer.
45 SourceLocation DotLoc;
46
47 /// The location of the field name in the designated initializer.
48 SourceLocation FieldLoc;
49
50 FieldDesignatorInfo(const IdentifierInfo *FieldName, SourceLocation DotLoc,
51 SourceLocation FieldLoc)
52 : FieldName(FieldName), DotLoc(DotLoc), FieldLoc(FieldLoc) {}
53 };
54
55 /// An array designator, e.g., "[42] = 0".
56 struct ArrayDesignatorInfo {
57 Expr *Index;
58
59 // The location of the '[' in the designated initializer.
60 SourceLocation LBracketLoc;
61
62 // The location of the ']' in the designated initializer.
63 mutable SourceLocation RBracketLoc;
64
65 ArrayDesignatorInfo(Expr *Index, SourceLocation LBracketLoc)
66 : Index(Index), LBracketLoc(LBracketLoc) {}
67 };
68
69 /// An array range designator, e.g. "[42 ... 50] = 1".
70 struct ArrayRangeDesignatorInfo {
71 Expr *Start;
72 Expr *End;
73
74 // The location of the '[' in the designated initializer.
75 SourceLocation LBracketLoc;
76
77 // The location of the '...' in the designated initializer.
78 SourceLocation EllipsisLoc;
79
80 // The location of the ']' in the designated initializer.
81 mutable SourceLocation RBracketLoc;
82
83 ArrayRangeDesignatorInfo(Expr *Start, Expr *End, SourceLocation LBracketLoc,
84 SourceLocation EllipsisLoc)
85 : Start(Start), End(End), LBracketLoc(LBracketLoc),
86 EllipsisLoc(EllipsisLoc) {}
87 };
88
89 /// The kind of designator this describes.
90 enum DesignatorKind {
91 FieldDesignator,
92 ArrayDesignator,
93 ArrayRangeDesignator
94 };
95
96 DesignatorKind Kind;
97
98 union {
99 FieldDesignatorInfo FieldInfo;
100 ArrayDesignatorInfo ArrayInfo;
101 ArrayRangeDesignatorInfo ArrayRangeInfo;
102 };
103
104 Designator(DesignatorKind Kind) : Kind(Kind) {}
105
106public:
107 bool isFieldDesignator() const { return Kind == FieldDesignator; }
108 bool isArrayDesignator() const { return Kind == ArrayDesignator; }
109 bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
110
111 //===--------------------------------------------------------------------===//
112 // FieldDesignatorInfo
113
114 /// Creates a field designator.
115 static Designator CreateFieldDesignator(const IdentifierInfo *FieldName,
116 SourceLocation DotLoc,
117 SourceLocation FieldLoc) {
118 Designator D(FieldDesignator);
119 new (&D.FieldInfo) FieldDesignatorInfo(FieldName, DotLoc, FieldLoc);
120 return D;
121 }
122
124 assert(isFieldDesignator() && "Invalid accessor");
125 return FieldInfo.FieldName;
126 }
127
129 assert(isFieldDesignator() && "Invalid accessor");
130 return FieldInfo.DotLoc;
131 }
132
134 assert(isFieldDesignator() && "Invalid accessor");
135 return FieldInfo.FieldLoc;
136 }
137
138 /// Returns the start location of this designator. A leading field designator
139 /// has no '.', so its location collapses to the field-name location.
141 if (isFieldDesignator())
142 return getDotLoc().isValid() ? getDotLoc() : getFieldLoc();
143 return getLBracketLoc();
144 }
145
146 /// Returns the end location of this designator.
150
151 //===--------------------------------------------------------------------===//
152 // ArrayDesignatorInfo:
153
154 /// Creates an array designator.
155 static Designator CreateArrayDesignator(Expr *Index,
156 SourceLocation LBracketLoc) {
157 Designator D(ArrayDesignator);
158 new (&D.ArrayInfo) ArrayDesignatorInfo(Index, LBracketLoc);
159 return D;
160 }
161
163 assert(isArrayDesignator() && "Invalid accessor");
164 return ArrayInfo.Index;
165 }
166
169 "Invalid accessor");
170 return isArrayDesignator() ? ArrayInfo.LBracketLoc
171 : ArrayRangeInfo.LBracketLoc;
172 }
173
176 "Invalid accessor");
177 return isArrayDesignator() ? ArrayInfo.RBracketLoc
178 : ArrayRangeInfo.RBracketLoc;
179 }
180
181 //===--------------------------------------------------------------------===//
182 // ArrayRangeDesignatorInfo:
183
184 /// Creates a GNU array-range designator.
185 static Designator CreateArrayRangeDesignator(Expr *Start, Expr *End,
186 SourceLocation LBracketLoc,
187 SourceLocation EllipsisLoc) {
188 Designator D(ArrayRangeDesignator);
189 new (&D.ArrayRangeInfo)
190 ArrayRangeDesignatorInfo(Start, End, LBracketLoc, EllipsisLoc);
191 return D;
192 }
193
195 assert(isArrayRangeDesignator() && "Invalid accessor");
196 return ArrayRangeInfo.Start;
197 }
198
200 assert(isArrayRangeDesignator() && "Invalid accessor");
201 return ArrayRangeInfo.End;
202 }
203
205 assert(isArrayRangeDesignator() && "Invalid accessor");
206 return ArrayRangeInfo.EllipsisLoc;
207 }
208
209 void setRBracketLoc(SourceLocation RBracketLoc) const {
211 "Invalid accessor");
212 if (isArrayDesignator())
213 ArrayInfo.RBracketLoc = RBracketLoc;
214 else
215 ArrayRangeInfo.RBracketLoc = RBracketLoc;
216 }
217};
218
219/// Designation - Represent a full designation, which is a sequence of
220/// designators. This class is mostly a helper for InitListDesignations.
222 /// Designators - The actual designators for this initializer.
223 SmallVector<Designator, 2> Designators;
224
225public:
226 /// AddDesignator - Add a designator to the end of this list.
227 void AddDesignator(Designator D) { Designators.push_back(D); }
228
229 bool empty() const { return Designators.empty(); }
230
231 unsigned getNumDesignators() const { return Designators.size(); }
232 const Designator &getDesignator(unsigned Idx) const {
233 assert(Idx < Designators.size());
234 return Designators[Idx];
235 }
236};
237
238} // end namespace clang
239
240#endif
Defines the clang::SourceLocation class and associated facilities.
Designation - Represent a full designation, which is a sequence of designators.
Definition Designator.h:221
const Designator & getDesignator(unsigned Idx) const
Definition Designator.h:232
bool empty() const
Definition Designator.h:229
unsigned getNumDesignators() const
Definition Designator.h:231
void AddDesignator(Designator D)
AddDesignator - Add a designator to the end of this list.
Definition Designator.h:227
Designator - A designator in a C99 designated initializer.
Definition Designator.h:38
SourceLocation getFieldLoc() const
Definition Designator.h:133
SourceLocation getDotLoc() const
Definition Designator.h:128
Expr * getArrayRangeStart() const
Definition Designator.h:194
bool isArrayDesignator() const
Definition Designator.h:108
static Designator CreateArrayRangeDesignator(Expr *Start, Expr *End, SourceLocation LBracketLoc, SourceLocation EllipsisLoc)
Creates a GNU array-range designator.
Definition Designator.h:185
SourceLocation getEndLoc() const
Returns the end location of this designator.
Definition Designator.h:147
SourceLocation getLBracketLoc() const
Definition Designator.h:167
static Designator CreateArrayDesignator(Expr *Index, SourceLocation LBracketLoc)
Creates an array designator.
Definition Designator.h:155
void setRBracketLoc(SourceLocation RBracketLoc) const
Definition Designator.h:209
bool isArrayRangeDesignator() const
Definition Designator.h:109
bool isFieldDesignator() const
Definition Designator.h:107
SourceLocation getRBracketLoc() const
Definition Designator.h:174
ArrayRangeDesignatorInfo ArrayRangeInfo
Definition Designator.h:101
SourceLocation getEllipsisLoc() const
Definition Designator.h:204
ArrayDesignatorInfo ArrayInfo
Definition Designator.h:100
Expr * getArrayRangeEnd() const
Definition Designator.h:199
const IdentifierInfo * getFieldDecl() const
Definition Designator.h:123
SourceLocation getBeginLoc() const
Returns the start location of this designator.
Definition Designator.h:140
FieldDesignatorInfo FieldInfo
Definition Designator.h:99
Expr * getArrayIndex() const
Definition Designator.h:162
static Designator CreateFieldDesignator(const IdentifierInfo *FieldName, SourceLocation DotLoc, SourceLocation FieldLoc)
Creates a field designator.
Definition Designator.h:115
This represents one expression.
Definition Expr.h:112
One of these records is kept for each identifier that is lexed.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
The JSON file list parser is used to communicate input to InstallAPI.