clang  12.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 
20 namespace clang {
21 
22 class Expr;
23 class IdentifierInfo;
24 class Sema;
25 
26 /// Designator - A designator in a C99 designated initializer.
27 ///
28 /// This class is a discriminated union which holds the various
29 /// different sorts of designators possible. A Designation is an array of
30 /// these. An example of a designator are things like this:
31 /// [8] .field [47] // C99 designation: 3 designators
32 /// [8 ... 47] field: // GNU extensions: 2 designators
33 /// These occur in initializers, e.g.:
34 /// int a[10] = {2, 4, [8]=9, 10};
35 ///
36 class Designator {
37 public:
40  };
41 private:
42  Designator() {};
43 
45 
46  struct FieldDesignatorInfo {
47  const IdentifierInfo *II;
48  SourceLocation DotLoc;
49  SourceLocation NameLoc;
50  };
51  struct ArrayDesignatorInfo {
52  Expr *Index;
53  SourceLocation LBracketLoc;
54  mutable SourceLocation RBracketLoc;
55  };
56  struct ArrayRangeDesignatorInfo {
57  Expr *Start, *End;
58  SourceLocation LBracketLoc, EllipsisLoc;
59  mutable SourceLocation RBracketLoc;
60  };
61 
62  union {
63  FieldDesignatorInfo FieldInfo;
64  ArrayDesignatorInfo ArrayInfo;
65  ArrayRangeDesignatorInfo ArrayRangeInfo;
66  };
67 
68 public:
69 
70  DesignatorKind getKind() const { return Kind; }
71  bool isFieldDesignator() const { return Kind == FieldDesignator; }
72  bool isArrayDesignator() const { return Kind == ArrayDesignator; }
73  bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
74 
75  const IdentifierInfo *getField() const {
76  assert(isFieldDesignator() && "Invalid accessor");
77  return FieldInfo.II;
78  }
79 
81  assert(isFieldDesignator() && "Invalid accessor");
82  return FieldInfo.DotLoc;
83  }
84 
86  assert(isFieldDesignator() && "Invalid accessor");
87  return FieldInfo.NameLoc;
88  }
89 
90  Expr *getArrayIndex() const {
91  assert(isArrayDesignator() && "Invalid accessor");
92  return ArrayInfo.Index;
93  }
94 
96  assert(isArrayRangeDesignator() && "Invalid accessor");
97  return ArrayRangeInfo.Start;
98  }
100  assert(isArrayRangeDesignator() && "Invalid accessor");
101  return ArrayRangeInfo.End;
102  }
103 
105  assert((isArrayDesignator() || isArrayRangeDesignator()) &&
106  "Invalid accessor");
107  if (isArrayDesignator())
108  return ArrayInfo.LBracketLoc;
109  else
110  return ArrayRangeInfo.LBracketLoc;
111  }
112 
114  assert((isArrayDesignator() || isArrayRangeDesignator()) &&
115  "Invalid accessor");
116  if (isArrayDesignator())
117  return ArrayInfo.RBracketLoc;
118  else
119  return ArrayRangeInfo.RBracketLoc;
120  }
121 
123  assert(isArrayRangeDesignator() && "Invalid accessor");
124  return ArrayRangeInfo.EllipsisLoc;
125  }
126 
128  SourceLocation NameLoc) {
129  Designator D;
130  D.Kind = FieldDesignator;
131  new (&D.FieldInfo) FieldDesignatorInfo;
132  D.FieldInfo.II = II;
133  D.FieldInfo.DotLoc = DotLoc;
134  D.FieldInfo.NameLoc = NameLoc;
135  return D;
136  }
137 
138  static Designator getArray(Expr *Index,
139  SourceLocation LBracketLoc) {
140  Designator D;
141  D.Kind = ArrayDesignator;
142  new (&D.ArrayInfo) ArrayDesignatorInfo;
143  D.ArrayInfo.Index = Index;
144  D.ArrayInfo.LBracketLoc = LBracketLoc;
145  D.ArrayInfo.RBracketLoc = SourceLocation();
146  return D;
147  }
148 
150  Expr *End,
151  SourceLocation LBracketLoc,
152  SourceLocation EllipsisLoc) {
153  Designator D;
154  D.Kind = ArrayRangeDesignator;
155  new (&D.ArrayRangeInfo) ArrayRangeDesignatorInfo;
156  D.ArrayRangeInfo.Start = Start;
157  D.ArrayRangeInfo.End = End;
158  D.ArrayRangeInfo.LBracketLoc = LBracketLoc;
159  D.ArrayRangeInfo.EllipsisLoc = EllipsisLoc;
160  D.ArrayRangeInfo.RBracketLoc = SourceLocation();
161  return D;
162  }
163 
164  void setRBracketLoc(SourceLocation RBracketLoc) const {
165  assert((isArrayDesignator() || isArrayRangeDesignator()) &&
166  "Invalid accessor");
167  if (isArrayDesignator())
168  ArrayInfo.RBracketLoc = RBracketLoc;
169  else
170  ArrayRangeInfo.RBracketLoc = RBracketLoc;
171  }
172 
173  /// ClearExprs - Null out any expression references, which prevents
174  /// them from being 'delete'd later.
175  void ClearExprs(Sema &Actions) {}
176 
177  /// FreeExprs - Release any unclaimed memory for the expressions in
178  /// this designator.
179  void FreeExprs(Sema &Actions) {}
180 };
181 
182 
183 /// Designation - Represent a full designation, which is a sequence of
184 /// designators. This class is mostly a helper for InitListDesignations.
185 class Designation {
186  /// Designators - The actual designators for this initializer.
187  SmallVector<Designator, 2> Designators;
188 
189 public:
190  /// AddDesignator - Add a designator to the end of this list.
192  Designators.push_back(D);
193  }
194 
195  bool empty() const { return Designators.empty(); }
196 
197  unsigned getNumDesignators() const { return Designators.size(); }
198  const Designator &getDesignator(unsigned Idx) const {
199  assert(Idx < Designators.size());
200  return Designators[Idx];
201  }
202 
203  /// ClearExprs - Null out any expression references, which prevents them from
204  /// being 'delete'd later.
205  void ClearExprs(Sema &Actions) {}
206 
207  /// FreeExprs - Release any unclaimed memory for the expressions in this
208  /// designation.
209  void FreeExprs(Sema &Actions) {}
210 };
211 
212 } // end namespace clang
213 
214 #endif
void ClearExprs(Sema &Actions)
ClearExprs - Null out any expression references, which prevents them from being 'delete'd later.
Definition: Designator.h:175
SourceLocation getEllipsisLoc() const
Definition: Designator.h:122
ArrayRangeDesignatorInfo ArrayRangeInfo
Definition: Designator.h:65
const IdentifierInfo * getField() const
Definition: Designator.h:75
bool isArrayRangeDesignator() const
Definition: Designator.h:73
void setRBracketLoc(SourceLocation RBracketLoc) const
Definition: Designator.h:164
Expr * getArrayRangeStart() const
Definition: Designator.h:95
One of these records is kept for each identifier that is lexed.
FieldDesignatorInfo FieldInfo
Definition: Designator.h:63
ArrayDesignatorInfo ArrayInfo
Definition: Designator.h:64
static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc, SourceLocation NameLoc)
Definition: Designator.h:127
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:340
unsigned getNumDesignators() const
Definition: Designator.h:197
This represents one expression.
Definition: Expr.h:109
SourceLocation End
bool isArrayDesignator() const
Definition: Designator.h:72
void ClearExprs(Sema &Actions)
ClearExprs - Null out any expression references, which prevents them from being 'delete'd later.
Definition: Designator.h:205
Expr * getArrayRangeEnd() const
Definition: Designator.h:99
bool empty() const
Definition: Designator.h:195
Expr * getArrayIndex() const
Definition: Designator.h:90
void FreeExprs(Sema &Actions)
FreeExprs - Release any unclaimed memory for the expressions in this designation.
Definition: Designator.h:209
Kind
const Designator & getDesignator(unsigned Idx) const
Definition: Designator.h:198
Encodes a location in the source.
SourceLocation getFieldLoc() const
Definition: Designator.h:85
bool isFieldDesignator() const
Definition: Designator.h:71
SourceLocation getLBracketLoc() const
Definition: Designator.h:104
Dataflow Directional Tag Classes.
void AddDesignator(Designator D)
AddDesignator - Add a designator to the end of this list.
Definition: Designator.h:191
DesignatorKind getKind() const
Definition: Designator.h:70
Defines the clang::SourceLocation class and associated facilities.
SourceLocation getRBracketLoc() const
Definition: Designator.h:113
void FreeExprs(Sema &Actions)
FreeExprs - Release any unclaimed memory for the expressions in this designator.
Definition: Designator.h:179
Designation - Represent a full designation, which is a sequence of designators.
Definition: Designator.h:185
Designator - A designator in a C99 designated initializer.
Definition: Designator.h:36
static Designator getArray(Expr *Index, SourceLocation LBracketLoc)
Definition: Designator.h:138
SourceLocation getDotLoc() const
Definition: Designator.h:80
static Designator getArrayRange(Expr *Start, Expr *End, SourceLocation LBracketLoc, SourceLocation EllipsisLoc)
Definition: Designator.h:149