summaryrefslogtreecommitdiff
path: root/lib/Sema/TypeLocBuilder.h
blob: 7b241871b67dec37aecafcdb6620eca16409d560 (plain)
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
//===--- TypeLocBuilder.h - Type Source Info collector ----------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This files defines TypeLocBuilder, a class for building TypeLocs
//  bottom-up.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_SEMA_TYPELOCBUILDER_H
#define LLVM_CLANG_SEMA_TYPELOCBUILDER_H

#include "clang/AST/TypeLoc.h"
#include "llvm/ADT/SmallVector.h"
#include "clang/AST/ASTContext.h"
#include "llvm/Support/CrashRecoveryContext.h"

namespace clang {

class TypeLocBuilder {
  enum { InlineCapacity = 8 * sizeof(SourceLocation) };

  /// The underlying location-data buffer.  Data grows from the end
  /// of the buffer backwards.
  char *Buffer;

  /// The capacity of the current buffer.
  size_t Capacity;

  /// The index of the first occupied byte in the buffer.
  size_t Index;

#ifndef NDEBUG
  /// The last type pushed on this builder.
  QualType LastTy;
#endif
    
  /// The inline buffer.
  char InlineBuffer[InlineCapacity];

  llvm::CrashRecoveryContextCleanupRegistrar cleanupBuffer;

 public:
  TypeLocBuilder()
    : Buffer(InlineBuffer), Capacity(InlineCapacity), Index(InlineCapacity),
      cleanupBuffer(llvm::CrashRecoveryContextCleanup::create<TypeLocBuilder>(
            this, llvm::CrashRecoveryContextCleanup::DestructorCleanup))
  {}

  ~TypeLocBuilder() {
    if (Buffer != InlineBuffer)
      delete[] Buffer;
  }

  /// Ensures that this buffer has at least as much capacity as described.
  void reserve(size_t Requested) {
    if (Requested > Capacity)
      // For now, match the request exactly.
      grow(Requested);
  }

  /// Pushes a copy of the given TypeLoc onto this builder.  The builder
  /// must be empty for this to work.
  void pushFullCopy(TypeLoc L) {
    size_t Size = L.getFullDataSize();
    TypeLoc Copy = pushFullUninitializedImpl(L.getType(), Size);
    memcpy(Copy.getOpaqueData(), L.getOpaqueData(), Size);
  }

  /// Pushes uninitialized space for the given type.  The builder must
  /// be empty.
  TypeLoc pushFullUninitialized(QualType T) {
    return pushFullUninitializedImpl(T, TypeLoc::getFullDataSizeForType(T));
  }

  /// Pushes space for a typespec TypeLoc.  Invalidates any TypeLocs
  /// previously retrieved from this builder.
  TypeSpecTypeLoc pushTypeSpec(QualType T) {
    size_t LocalSize = TypeSpecTypeLoc::LocalDataSize;
    return cast<TypeSpecTypeLoc>(pushImpl(T, LocalSize));
  }

  /// Resets this builder to the newly-initialized state.
  void clear() {
#ifndef NDEBUG
    LastTy = QualType();
#endif
    Index = Capacity;
  }  

  /// Pushes space for a new TypeLoc of the given type.  Invalidates
  /// any TypeLocs previously retrieved from this builder.
  template <class TyLocType> TyLocType push(QualType T) {
    size_t LocalSize = cast<TyLocType>(TypeLoc(T, 0)).getLocalDataSize();
    return cast<TyLocType>(pushImpl(T, LocalSize));
  }

  /// Creates a TypeSourceInfo for the given type.
  TypeSourceInfo *getTypeSourceInfo(ASTContext& Context, QualType T) {
#ifndef NDEBUG
    assert(T == LastTy && "type doesn't match last type pushed!");
#endif

    size_t FullDataSize = Capacity - Index;
    TypeSourceInfo *DI = Context.CreateTypeSourceInfo(T, FullDataSize);
    memcpy(DI->getTypeLoc().getOpaqueData(), &Buffer[Index], FullDataSize);
    return DI;
  }

  /// \brief Copies the type-location information to the given AST context and 
  /// returns a \c TypeLoc referring into the AST context.
  TypeLoc getTypeLocInContext(ASTContext &Context, QualType T) {
#ifndef NDEBUG
    assert(T == LastTy && "type doesn't match last type pushed!");
#endif
    
    size_t FullDataSize = Capacity - Index;
    void *Mem = Context.Allocate(FullDataSize);
    memcpy(Mem, &Buffer[Index], FullDataSize);
    return TypeLoc(T, Mem);
  }

private:
  TypeLoc pushImpl(QualType T, size_t LocalSize) {
#ifndef NDEBUG
    QualType TLast = TypeLoc(T, 0).getNextTypeLoc().getType();
    assert(TLast == LastTy &&
           "mismatch between last type and new type's inner type");
    LastTy = T;
#endif

    // If we need to grow, grow by a factor of 2.
    if (LocalSize > Index) {
      size_t RequiredCapacity = Capacity + (LocalSize - Index);
      size_t NewCapacity = Capacity * 2;
      while (RequiredCapacity > NewCapacity)
        NewCapacity *= 2;
      grow(NewCapacity);
    }

    Index -= LocalSize;

    return getTypeLoc(T);
  }

  /// Grow to the given capacity.
  void grow(size_t NewCapacity) {
    assert(NewCapacity > Capacity);

    // Allocate the new buffer and copy the old data into it.
    char *NewBuffer = new char[NewCapacity];
    unsigned NewIndex = Index + NewCapacity - Capacity;
    memcpy(&NewBuffer[NewIndex],
           &Buffer[Index],
           Capacity - Index);

    if (Buffer != InlineBuffer)
      delete[] Buffer;

    Buffer = NewBuffer;
    Capacity = NewCapacity;
    Index = NewIndex;
  }

  TypeLoc pushFullUninitializedImpl(QualType T, size_t Size) {
#ifndef NDEBUG
    assert(LastTy.isNull() && "pushing full on non-empty TypeLocBuilder");
    LastTy = T;
#endif
    assert(Index == Capacity && "pushing full on non-empty TypeLocBuilder");

    reserve(Size);
    Index -= Size;

    return getTypeLoc(T);
  }


  // This is private because, when we kill off TypeSourceInfo in favor
  // of TypeLoc, we'll want an interface that creates a TypeLoc given
  // an ASTContext, and we don't want people to think they can just
  // use this as an equivalent.
  TypeLoc getTypeLoc(QualType T) {
#ifndef NDEBUG
    assert(LastTy == T && "type doesn't match last type pushed!");
#endif
    return TypeLoc(T, &Buffer[Index]);
  }
};

}

#endif