clang  6.0.0svn
Designator.h
Go to the documentation of this file.
1 //===--- Designator.h - Initialization Designator ---------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines interfaces used to represent designators (a la
11 // C99 designated initializers) during parsing.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_SEMA_DESIGNATOR_H
16 #define LLVM_CLANG_SEMA_DESIGNATOR_H
17 
19 #include "llvm/ADT/SmallVector.h"
20 
21 namespace clang {
22 
23 class Expr;
24 class IdentifierInfo;
25 class Sema;
26 
27 /// Designator - A designator in a C99 designated initializer.
28 ///
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};
36 ///
37 class Designator {
38 public:
41  };
42 private:
44 
45  struct FieldDesignatorInfo {
46  const IdentifierInfo *II;
47  unsigned DotLoc;
48  unsigned NameLoc;
49  };
50  struct ArrayDesignatorInfo {
51  Expr *Index;
52  unsigned LBracketLoc;
53  mutable unsigned RBracketLoc;
54  };
55  struct ArrayRangeDesignatorInfo {
56  Expr *Start, *End;
57  unsigned LBracketLoc, EllipsisLoc;
58  mutable unsigned RBracketLoc;
59  };
60 
61  union {
62  FieldDesignatorInfo FieldInfo;
63  ArrayDesignatorInfo ArrayInfo;
64  ArrayRangeDesignatorInfo ArrayRangeInfo;
65  };
66 
67 public:
68 
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; }
73 
74  const IdentifierInfo *getField() const {
75  assert(isFieldDesignator() && "Invalid accessor");
76  return FieldInfo.II;
77  }
78 
80  assert(isFieldDesignator() && "Invalid accessor");
82  }
83 
85  assert(isFieldDesignator() && "Invalid accessor");
87  }
88 
89  Expr *getArrayIndex() const {
90  assert(isArrayDesignator() && "Invalid accessor");
91  return ArrayInfo.Index;
92  }
93 
95  assert(isArrayRangeDesignator() && "Invalid accessor");
96  return ArrayRangeInfo.Start;
97  }
99  assert(isArrayRangeDesignator() && "Invalid accessor");
100  return ArrayRangeInfo.End;
101  }
102 
104  assert((isArrayDesignator() || isArrayRangeDesignator()) &&
105  "Invalid accessor");
106  if (isArrayDesignator())
107  return SourceLocation::getFromRawEncoding(ArrayInfo.LBracketLoc);
108  else
110  }
111 
113  assert((isArrayDesignator() || isArrayRangeDesignator()) &&
114  "Invalid accessor");
115  if (isArrayDesignator())
116  return SourceLocation::getFromRawEncoding(ArrayInfo.RBracketLoc);
117  else
119  }
120 
122  assert(isArrayRangeDesignator() && "Invalid accessor");
124  }
125 
127  SourceLocation NameLoc) {
128  Designator D;
129  D.Kind = FieldDesignator;
130  D.FieldInfo.II = II;
131  D.FieldInfo.DotLoc = DotLoc.getRawEncoding();
132  D.FieldInfo.NameLoc = NameLoc.getRawEncoding();
133  return D;
134  }
135 
136  static Designator getArray(Expr *Index,
137  SourceLocation LBracketLoc) {
138  Designator D;
139  D.Kind = ArrayDesignator;
140  D.ArrayInfo.Index = Index;
141  D.ArrayInfo.LBracketLoc = LBracketLoc.getRawEncoding();
142  D.ArrayInfo.RBracketLoc = 0;
143  return D;
144  }
145 
147  Expr *End,
148  SourceLocation LBracketLoc,
149  SourceLocation EllipsisLoc) {
150  Designator D;
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;
157  return D;
158  }
159 
160  void setRBracketLoc(SourceLocation RBracketLoc) const {
161  assert((isArrayDesignator() || isArrayRangeDesignator()) &&
162  "Invalid accessor");
163  if (isArrayDesignator())
164  ArrayInfo.RBracketLoc = RBracketLoc.getRawEncoding();
165  else
166  ArrayRangeInfo.RBracketLoc = RBracketLoc.getRawEncoding();
167  }
168 
169  /// ClearExprs - Null out any expression references, which prevents
170  /// them from being 'delete'd later.
171  void ClearExprs(Sema &Actions) {}
172 
173  /// FreeExprs - Release any unclaimed memory for the expressions in
174  /// this designator.
175  void FreeExprs(Sema &Actions) {}
176 };
177 
178 
179 /// Designation - Represent a full designation, which is a sequence of
180 /// designators. This class is mostly a helper for InitListDesignations.
181 class Designation {
182  /// Designators - The actual designators for this initializer.
183  SmallVector<Designator, 2> Designators;
184 
185 public:
186  /// AddDesignator - Add a designator to the end of this list.
188  Designators.push_back(D);
189  }
190 
191  bool empty() const { return Designators.empty(); }
192 
193  unsigned getNumDesignators() const { return Designators.size(); }
194  const Designator &getDesignator(unsigned Idx) const {
195  assert(Idx < Designators.size());
196  return Designators[Idx];
197  }
198 
199  /// ClearExprs - Null out any expression references, which prevents them from
200  /// being 'delete'd later.
201  void ClearExprs(Sema &Actions) {}
202 
203  /// FreeExprs - Release any unclaimed memory for the expressions in this
204  /// designation.
205  void FreeExprs(Sema &Actions) {}
206 };
207 
208 } // end namespace clang
209 
210 #endif
void ClearExprs(Sema &Actions)
ClearExprs - Null out any expression references, which prevents them from being &#39;delete&#39;d later...
Definition: Designator.h:171
SourceLocation getEllipsisLoc() const
Definition: Designator.h:121
unsigned getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it...
ArrayRangeDesignatorInfo ArrayRangeInfo
Definition: Designator.h:64
const IdentifierInfo * getField() const
Definition: Designator.h:74
bool isArrayRangeDesignator() const
Definition: Designator.h:72
void setRBracketLoc(SourceLocation RBracketLoc) const
Definition: Designator.h:160
Expr * getArrayRangeStart() const
Definition: Designator.h:94
One of these records is kept for each identifier that is lexed.
FieldDesignatorInfo FieldInfo
Definition: Designator.h:62
static SourceLocation getFromRawEncoding(unsigned Encoding)
Turn a raw encoding of a SourceLocation object into a real SourceLocation.
ArrayDesignatorInfo ArrayInfo
Definition: Designator.h:63
static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc, SourceLocation NameLoc)
Definition: Designator.h:126
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:274
unsigned getNumDesignators() const
Definition: Designator.h:193
Expr - This represents one expression.
Definition: Expr.h:106
SourceLocation End
bool isArrayDesignator() const
Definition: Designator.h:71
void ClearExprs(Sema &Actions)
ClearExprs - Null out any expression references, which prevents them from being &#39;delete&#39;d later...
Definition: Designator.h:201
Expr * getArrayRangeEnd() const
Definition: Designator.h:98
bool empty() const
Definition: Designator.h:191
Expr * getArrayIndex() const
Definition: Designator.h:89
void FreeExprs(Sema &Actions)
FreeExprs - Release any unclaimed memory for the expressions in this designation. ...
Definition: Designator.h:205
Kind
const Designator & getDesignator(unsigned Idx) const
Definition: Designator.h:194
Encodes a location in the source.
SourceLocation getFieldLoc() const
Definition: Designator.h:84
bool isFieldDesignator() const
Definition: Designator.h:70
SourceLocation getLBracketLoc() const
Definition: Designator.h:103
Dataflow Directional Tag Classes.
void AddDesignator(Designator D)
AddDesignator - Add a designator to the end of this list.
Definition: Designator.h:187
DesignatorKind getKind() const
Definition: Designator.h:69
Defines the clang::SourceLocation class and associated facilities.
SourceLocation getRBracketLoc() const
Definition: Designator.h:112
void FreeExprs(Sema &Actions)
FreeExprs - Release any unclaimed memory for the expressions in this designator.
Definition: Designator.h:175
Designation - Represent a full designation, which is a sequence of designators.
Definition: Designator.h:181
Designator - A designator in a C99 designated initializer.
Definition: Designator.h:37
static Designator getArray(Expr *Index, SourceLocation LBracketLoc)
Definition: Designator.h:136
SourceLocation getDotLoc() const
Definition: Designator.h:79
static Designator getArrayRange(Expr *Start, Expr *End, SourceLocation LBracketLoc, SourceLocation EllipsisLoc)
Definition: Designator.h:146