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