aboutsummaryrefslogtreecommitdiff
path: root/lib/AST/TypeLoc.cpp
blob: c24477ae81c5f4ba471c3cab36dce00103e87022 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//===--- TypeLoc.cpp - Type Source Info Wrapper -----------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the TypeLoc subclasses implementations.
//
//===----------------------------------------------------------------------===//

#include "clang/AST/TypeLocVisitor.h"
using namespace clang;

//===----------------------------------------------------------------------===//
// TypeLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

/// \brief Return the source range for the visited TypeSpecLoc.
class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
public:
#define ABSTRACT_TYPELOC(CLASS)
#define TYPELOC(CLASS, PARENT, TYPE) \
    SourceRange Visit##CLASS(CLASS TyLoc) { return TyLoc.getSourceRange(); }
#include "clang/AST/TypeLocNodes.def"

  SourceRange VisitTypeLoc(TypeLoc TyLoc) {
    assert(0 && "A typeloc wrapper was not handled!");
    return SourceRange();
  }
};

}

SourceRange TypeLoc::getSourceRange() const {
  if (isNull())
    return SourceRange();
  return TypeLocRanger().Visit(*this);
}

/// \brief Returns the size of type source info data block for the given type.
unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
  return TypeLoc(Ty, 0).getFullDataSize();
}

/// \brief Find the TypeSpecLoc that is part of this TypeLoc.
TypeSpecLoc TypeLoc::getTypeSpecLoc() const {
  if (isNull())
    return TypeSpecLoc();

  if (const DeclaratorLoc *DL = dyn_cast<DeclaratorLoc>(this))
    return DL->getTypeSpecLoc();
  return cast<TypeSpecLoc>(*this);
}

/// \brief Find the TypeSpecLoc that is part of this TypeLoc and return its
/// SourceRange.
SourceRange TypeLoc::getTypeSpecRange() const {
  return getTypeSpecLoc().getSourceRange();
}

namespace {

/// \brief Report the full source info data size for the visited TypeLoc.
class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
public:
#define ABSTRACT_TYPELOC(CLASS)
#define TYPELOC(CLASS, PARENT, TYPE) \
    unsigned Visit##CLASS(CLASS TyLoc) { return TyLoc.getFullDataSize(); }
#include "clang/AST/TypeLocNodes.def"

  unsigned VisitTypeLoc(TypeLoc TyLoc) {
    assert(0 && "A type loc wrapper was not handled!");
    return 0;
  }
};

}

/// \brief Returns the size of the type source info data block.
unsigned TypeLoc::getFullDataSize() const {
  if (isNull()) return 0;
  return TypeSizer().Visit(*this);
}

namespace {

/// \brief Return the "next" TypeLoc for the visited TypeLoc, e.g for "int*" the
/// TypeLoc is a PointerLoc and next TypeLoc is for "int".
class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
public:
#define TYPELOC(CLASS, PARENT, TYPE)
#define DECLARATOR_TYPELOC(CLASS, TYPE) \
    TypeLoc Visit##CLASS(CLASS TyLoc);
#include "clang/AST/TypeLocNodes.def"

  TypeLoc VisitTypeSpecLoc(TypeLoc TyLoc) { return TypeLoc(); }
  TypeLoc VisitObjCProtocolListLoc(ObjCProtocolListLoc TL);

  TypeLoc VisitTypeLoc(TypeLoc TyLoc) {
    assert(0 && "A declarator loc wrapper was not handled!");
    return TypeLoc();
  }
};

}

TypeLoc NextLoc::VisitObjCProtocolListLoc(ObjCProtocolListLoc TL) {
  return TL.getBaseTypeLoc();
}

TypeLoc NextLoc::VisitPointerLoc(PointerLoc TL) {
  return TL.getPointeeLoc();
}
TypeLoc NextLoc::VisitMemberPointerLoc(MemberPointerLoc TL) {
  return TL.getPointeeLoc();
}
TypeLoc NextLoc::VisitBlockPointerLoc(BlockPointerLoc TL) {
  return TL.getPointeeLoc();
}
TypeLoc NextLoc::VisitReferenceLoc(ReferenceLoc TL) {
  return TL.getPointeeLoc();
}
TypeLoc NextLoc::VisitFunctionLoc(FunctionLoc TL) {
  return TL.getResultLoc();
}
TypeLoc NextLoc::VisitArrayLoc(ArrayLoc TL) {
  return TL.getElementLoc();
}

/// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
/// TypeLoc is a PointerLoc and next TypeLoc is for "int".
TypeLoc TypeLoc::getNextTypeLoc() const {
  return NextLoc().Visit(*this);
}

//===----------------------------------------------------------------------===//
// TypeSpecLoc Implementation
//===----------------------------------------------------------------------===//

namespace {
class TypeSpecChecker : public TypeLocVisitor<TypeSpecChecker, bool> {
public:
  bool VisitTypeSpecLoc(TypeSpecLoc TyLoc) { return true; }
};

}

bool TypeSpecLoc::classof(const TypeLoc *TL) {
  return TypeSpecChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// DeclaratorLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

/// \brief Return the TypeSpecLoc for the visited DeclaratorLoc.
class TypeSpecGetter : public TypeLocVisitor<TypeSpecGetter, TypeSpecLoc> {
public:
#define TYPELOC(CLASS, PARENT, TYPE)
#define DECLARATOR_TYPELOC(CLASS, TYPE) \
    TypeSpecLoc Visit##CLASS(CLASS TyLoc) { return TyLoc.getTypeSpecLoc(); }
#include "clang/AST/TypeLocNodes.def"

  TypeSpecLoc VisitTypeLoc(TypeLoc TyLoc) {
    assert(0 && "A declarator loc wrapper was not handled!");
    return TypeSpecLoc();
  }
};

}

/// \brief Find the TypeSpecLoc that is part of this DeclaratorLoc.
TypeSpecLoc DeclaratorLoc::getTypeSpecLoc() const {
  return TypeSpecGetter().Visit(*this);
}

namespace {

class DeclaratorLocChecker : public TypeLocVisitor<DeclaratorLocChecker, bool> {
public:
  bool VisitDeclaratorLoc(DeclaratorLoc TyLoc) { return true; }
};

}

bool DeclaratorLoc::classof(const TypeLoc *TL) {
  return DeclaratorLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// DefaultTypeSpecLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class DefaultTypeSpecLocChecker :
                        public TypeLocVisitor<DefaultTypeSpecLocChecker, bool> {
public:
  bool VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) { return true; }
};

}

bool DefaultTypeSpecLoc::classof(const TypeLoc *TL) {
  return DefaultTypeSpecLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// TypedefLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class TypedefLocChecker : public TypeLocVisitor<TypedefLocChecker, bool> {
public:
  bool VisitTypedefLoc(TypedefLoc TyLoc) { return true; }
};

}

bool TypedefLoc::classof(const TypeLoc *TL) {
  return TypedefLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// ObjCInterfaceLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class ObjCInterfaceLocChecker :
  public TypeLocVisitor<ObjCInterfaceLocChecker, bool> {
public:
  bool VisitObjCInterfaceLoc(ObjCInterfaceLoc TyLoc) { return true; }
};

}

bool ObjCInterfaceLoc::classof(const TypeLoc *TL) {
  return ObjCInterfaceLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// ObjCProtocolListLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class ObjCProtocolListLocChecker :
  public TypeLocVisitor<ObjCProtocolListLocChecker, bool> {
public:
  bool VisitObjCProtocolListLoc(ObjCProtocolListLoc TyLoc) { return true; }
};

}

bool ObjCProtocolListLoc::classof(const TypeLoc *TL) {
  return ObjCProtocolListLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// PointerLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class PointerLocChecker : public TypeLocVisitor<PointerLocChecker, bool> {
public:
  bool VisitPointerLoc(PointerLoc TyLoc) { return true; }
};

}

bool PointerLoc::classof(const TypeLoc *TL) {
  return PointerLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// BlockPointerLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class BlockPointerLocChecker :
           public TypeLocVisitor<BlockPointerLocChecker, bool> {
public:
  bool VisitBlockPointerLoc(BlockPointerLoc TyLoc) { return true; }
};

}

bool BlockPointerLoc::classof(const TypeLoc *TL) {
  return BlockPointerLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// MemberPointerLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class MemberPointerLocChecker :
           public TypeLocVisitor<MemberPointerLocChecker, bool> {
public:
  bool VisitMemberPointerLoc(MemberPointerLoc TyLoc) { return true; }
};

}

bool MemberPointerLoc::classof(const TypeLoc *TL) {
  return MemberPointerLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// ReferenceLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class ReferenceLocChecker : public TypeLocVisitor<ReferenceLocChecker, bool> {
public:
  bool VisitReferenceLoc(ReferenceLoc TyLoc) { return true; }
};

}

bool ReferenceLoc::classof(const TypeLoc *TL) {
  return ReferenceLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// FunctionLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class FunctionLocChecker : public TypeLocVisitor<FunctionLocChecker, bool> {
public:
  bool VisitFunctionLoc(FunctionLoc TyLoc) { return true; }
};

}

bool FunctionLoc::classof(const TypeLoc *TL) {
  return FunctionLocChecker().Visit(*TL);
}

//===----------------------------------------------------------------------===//
// ArrayLoc Implementation
//===----------------------------------------------------------------------===//

namespace {

class ArrayLocChecker : public TypeLocVisitor<ArrayLocChecker, bool> {
public:
  bool VisitArrayLoc(ArrayLoc TyLoc) { return true; }
};

}

bool ArrayLoc::classof(const TypeLoc *TL) {
  return ArrayLocChecker().Visit(*TL);
}