Coverage Report

Created: 2018-07-21 08:31

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Sema/Designator.h
Line
Count
Source (jump to first uncovered line)
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
18
#include "clang/Basic/SourceLocation.h"
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:
39
  enum DesignatorKind {
40
    FieldDesignator, ArrayDesignator, ArrayRangeDesignator
41
  };
42
private:
43
  DesignatorKind Kind;
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
2.83k
  DesignatorKind getKind() const { return Kind; }
70
  bool isFieldDesignator() const { return Kind == FieldDesignator; }
71
893
  bool isArrayDesignator() const { return Kind == ArrayDesignator; }
72
4
  bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; }
73
74
2.53k
  const IdentifierInfo *getField() const {
75
2.53k
    assert(isFieldDesignator() && "Invalid accessor");
76
2.53k
    return FieldInfo.II;
77
2.53k
  }
78
79
2.53k
  SourceLocation getDotLoc() const {
80
2.53k
    assert(isFieldDesignator() && "Invalid accessor");
81
2.53k
    return SourceLocation::getFromRawEncoding(FieldInfo.DotLoc);
82
2.53k
  }
83
84
2.53k
  SourceLocation getFieldLoc() const {
85
2.53k
    assert(isFieldDesignator() && "Invalid accessor");
86
2.53k
    return SourceLocation::getFromRawEncoding(FieldInfo.NameLoc);
87
2.53k
  }
88
89
275
  Expr *getArrayIndex() const {
90
275
    assert(isArrayDesignator() && "Invalid accessor");
91
275
    return ArrayInfo.Index;
92
275
  }
93
94
27
  Expr *getArrayRangeStart() const {
95
27
    assert(isArrayRangeDesignator() && "Invalid accessor");
96
27
    return ArrayRangeInfo.Start;
97
27
  }
98
27
  Expr *getArrayRangeEnd() const {
99
27
    assert(isArrayRangeDesignator() && "Invalid accessor");
100
27
    return ArrayRangeInfo.End;
101
27
  }
102
103
297
  SourceLocation getLBracketLoc() const {
104
297
    assert((isArrayDesignator() || isArrayRangeDesignator()) &&
105
297
           "Invalid accessor");
106
297
    if (isArrayDesignator())
107
271
      return SourceLocation::getFromRawEncoding(ArrayInfo.LBracketLoc);
108
26
    else
109
26
      return SourceLocation::getFromRawEncoding(ArrayRangeInfo.LBracketLoc);
110
297
  }
111
112
297
  SourceLocation getRBracketLoc() const {
113
297
    assert((isArrayDesignator() || isArrayRangeDesignator()) &&
114
297
           "Invalid accessor");
115
297
    if (isArrayDesignator())
116
271
      return SourceLocation::getFromRawEncoding(ArrayInfo.RBracketLoc);
117
26
    else
118
26
      return SourceLocation::getFromRawEncoding(ArrayRangeInfo.RBracketLoc);
119
297
  }
120
121
27
  SourceLocation getEllipsisLoc() const {
122
27
    assert(isArrayRangeDesignator() && "Invalid accessor");
123
27
    return SourceLocation::getFromRawEncoding(ArrayRangeInfo.EllipsisLoc);
124
27
  }
125
126
  static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc,
127
2.53k
                             SourceLocation NameLoc) {
128
2.53k
    Designator D;
129
2.53k
    D.Kind = FieldDesignator;
130
2.53k
    D.FieldInfo.II = II;
131
2.53k
    D.FieldInfo.DotLoc = DotLoc.getRawEncoding();
132
2.53k
    D.FieldInfo.NameLoc = NameLoc.getRawEncoding();
133
2.53k
    return D;
134
2.53k
  }
135
136
  static Designator getArray(Expr *Index,
137
285
                             SourceLocation LBracketLoc) {
138
285
    Designator D;
139
285
    D.Kind = ArrayDesignator;
140
285
    D.ArrayInfo.Index = Index;
141
285
    D.ArrayInfo.LBracketLoc = LBracketLoc.getRawEncoding();
142
285
    D.ArrayInfo.RBracketLoc = 0;
143
285
    return D;
144
285
  }
145
146
  static Designator getArrayRange(Expr *Start,
147
                                  Expr *End,
148
                                  SourceLocation LBracketLoc,
149
27
                                  SourceLocation EllipsisLoc) {
150
27
    Designator D;
151
27
    D.Kind = ArrayRangeDesignator;
152
27
    D.ArrayRangeInfo.Start = Start;
153
27
    D.ArrayRangeInfo.End = End;
154
27
    D.ArrayRangeInfo.LBracketLoc = LBracketLoc.getRawEncoding();
155
27
    D.ArrayRangeInfo.EllipsisLoc = EllipsisLoc.getRawEncoding();
156
27
    D.ArrayRangeInfo.RBracketLoc = 0;
157
27
    return D;
158
27
  }
159
160
280
  void setRBracketLoc(SourceLocation RBracketLoc) const {
161
280
    assert((isArrayDesignator() || isArrayRangeDesignator()) &&
162
280
           "Invalid accessor");
163
280
    if (isArrayDesignator())
164
261
      ArrayInfo.RBracketLoc = RBracketLoc.getRawEncoding();
165
19
    else
166
19
      ArrayRangeInfo.RBracketLoc = RBracketLoc.getRawEncoding();
167
280
  }
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.
187
2.85k
  void AddDesignator(Designator D) {
188
2.85k
    Designators.push_back(D);
189
2.85k
  }
190
191
0
  bool empty() const { return Designators.empty(); }
192
193
5.86k
  unsigned getNumDesignators() const { return Designators.size(); }
194
3.13k
  const Designator &getDesignator(unsigned Idx) const {
195
3.13k
    assert(Idx < Designators.size());
196
3.13k
    return Designators[Idx];
197
3.13k
  }
198
199
  /// ClearExprs - Null out any expression references, which prevents them from
200
  /// being 'delete'd later.
201
2.60k
  void ClearExprs(Sema &Actions) {}
202
203
  /// FreeExprs - Release any unclaimed memory for the expressions in this
204
  /// designation.
205
0
  void FreeExprs(Sema &Actions) {}
206
};
207
208
} // end namespace clang
209
210
#endif