summaryrefslogtreecommitdiff
path: root/lib/AST/ASTContext.cpp
blob: 21eb32cd8ac0c070544f2def6cedac4c3a35fd0c (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
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
//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file implements the ASTContext interface.
//
//===----------------------------------------------------------------------===//

#include "flang/AST/ASTContext.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/Support/ErrorHandling.h"


namespace flang {

ASTContext::ASTContext(llvm::SourceMgr &SM, LangOptions LangOpts)
  : SrcMgr(SM), LastSDM(0), LanguageOptions(LangOpts) {
  TUDecl = TranslationUnitDecl::Create(*this);
  InitBuiltinTypes();
}

ASTContext::~ASTContext() {
  // Release the DenseMaps associated with DeclContext objects.
  // FIXME: Is this the ideal solution?
  ReleaseDeclContextMaps();
}

void ASTContext::InitBuiltinTypes() {
  // [R404]
  VoidTy = QualType(new (*this, TypeAlignment) VoidType(), 0);

  auto Opts = getLangOpts();
  auto ByteKind = Type::Int1;
  auto IntKind = Opts.DefaultInt8? Type::Int8 : Type::Int4;
  auto RealKind = Opts.DefaultReal8? Type::Real8 : Type::Real4;
  auto DblKind = Type::Real8;
  if(Opts.DefaultReal8 && !Opts.DefaultDouble8)
    DblKind = Type::Real16;

  IntegerTy = QualType(getBuiltinType(BuiltinType::Integer, IntKind), 0);

  RealTy = QualType(getBuiltinType(BuiltinType::Real, RealKind), 0);
  DoublePrecisionTy = QualType(getBuiltinType(BuiltinType::Real, DblKind,
                                              false, true), 0);

  ComplexTy = QualType(getBuiltinType(BuiltinType::Complex, RealKind), 0);
  DoubleComplexTy = QualType(getBuiltinType(BuiltinType::Complex, DblKind,
                                            false, true), 0);

  LogicalTy = QualType(getBuiltinType(BuiltinType::Logical, IntKind), 0);
  ByteTy = QualType(getBuiltinType(BuiltinType::Logical, ByteKind,
                                   false, false, true), 0);

  CharacterTy = QualType(getCharacterType(1), 0);
  NoLengthCharacterTy = QualType(getCharacterType(0), 0);
}

const llvm::fltSemantics&  ASTContext::getFPTypeSemantics(QualType Type) {
  switch(Type->getBuiltinTypeKind()) {
  case BuiltinType::Real4:  return llvm::APFloat::IEEEsingle;
  case BuiltinType::Real8:  return llvm::APFloat::IEEEdouble;
  case BuiltinType::Real16: return llvm::APFloat::IEEEquad;
  default: break;
  }
  llvm_unreachable("invalid real type");
}

unsigned ASTContext::getTypeKindBitWidth(BuiltinType::TypeKind Kind) const {
  switch(Kind) {
  case BuiltinType::Int1: return 8;
  case BuiltinType::Int2: return 16;
  case BuiltinType::Int4: return 32;
  case BuiltinType::Int8: return 64;
  case BuiltinType::Real4: return 32;
  case BuiltinType::Real8: return 64;
  case BuiltinType::Real16: return 128;
  }
  llvm_unreachable("invalid built in type kind");
  return 0;
}

BuiltinType::TypeKind ASTContext::getSelectedIntKind(int64_t Range) const {
  if(Range <= 2)
    return BuiltinType::Int1;
  else if(Range <= 4)
    return BuiltinType::Int2;
  else if(Range <= 9)
    return BuiltinType::Int4;
  else if(Range <= 18)
    return BuiltinType::Int8;
  // NB: add Range <= 38 for Int16
  return BuiltinType::NoKind;
}

//===----------------------------------------------------------------------===//
//                   Type creation/memoization methods
//===----------------------------------------------------------------------===//

QualType ASTContext::getExtQualType(const Type *BaseType, Qualifiers Quals) const {
  // Check if we've already instantiated this type.
  llvm::FoldingSetNodeID ID;
  ExtQuals::Profile(ID, BaseType, Quals);
  void *InsertPos = 0;
  if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
    assert(EQ->getQualifiers() == Quals);
    return QualType(EQ, 0);
  }

  // If the base type is not canonical, make the appropriate canonical type.
  QualType Canon;
  if (!BaseType->isCanonicalUnqualified()) {
    SplitQualType CanonSplit = BaseType->getCanonicalTypeInternal().split();
    CanonSplit.second.addConsistentQualifiers(Quals);
    Canon = getExtQualType(CanonSplit.first, CanonSplit.second);

    // Re-find the insert position.
    (void) ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos);
  }

  ExtQuals *EQ = new (*this, TypeAlignment) ExtQuals(BaseType, Canon, Quals);
  ExtQualNodes.InsertNode(EQ, InsertPos);
  return QualType(EQ, 0);
}

QualType ASTContext::getQualTypeOtherKind(QualType Type, QualType KindType) {
  auto BTy = Type->asBuiltinType();
  auto DesiredBTy = KindType->asBuiltinType();
  if(BTy->getBuiltinTypeKind() == DesiredBTy->getBuiltinTypeKind())
    return Type;
  auto Split = Type.split();
  return getExtQualType(getBuiltinType(BTy->getTypeSpec(), DesiredBTy->getBuiltinTypeKind(),
                                       DesiredBTy->isKindExplicitlySpecified(),
                                       DesiredBTy->isDoublePrecisionKindSpecified(),
                                       DesiredBTy->isByteKindSpecified()), Split.second);
}

// NB: this assumes that real and complex have have the same default kind.
QualType ASTContext::getComplexTypeElementType(QualType Type) {
  assert(Type->isComplexType());
  if(Type->getBuiltinTypeKind() != RealTy->getBuiltinTypeKind())
    return getQualTypeOtherKind(RealTy, Type);
  return RealTy;
}

QualType ASTContext::getComplexType(QualType ElementType) {
  assert(ElementType->isRealType());
  if(ElementType->getBuiltinTypeKind() != ComplexTy->getBuiltinTypeKind())
    return getQualTypeOtherKind(ComplexTy, ElementType);
  return ComplexTy;
}

QualType ASTContext::getTypeWithQualifers(QualType Type, Qualifiers Quals) {
  auto Split = Type.split();
  return getExtQualType(Split.first, Quals);
}

BuiltinType *ASTContext::getBuiltinType(BuiltinType::TypeSpec TS,
                                        BuiltinType::TypeKind Kind,
                                        bool IsKindExplicitlySpecified,
                                        bool IsDoublePrecisionKindSpecified,
                                        bool IsByteKindSpecified) {
  llvm::FoldingSetNodeID ID;
  BuiltinType::Profile(ID, TS, Kind, IsKindExplicitlySpecified,
                       IsDoublePrecisionKindSpecified,
                       IsByteKindSpecified);

  void *InsertPos = 0;
  if (auto BT = BuiltinTypes.FindNodeOrInsertPos(ID, InsertPos))
    return BT;

  auto BT = new (*this, TypeAlignment) BuiltinType(TS, Kind, IsKindExplicitlySpecified,
                                                   IsDoublePrecisionKindSpecified,
                                                   IsByteKindSpecified);
  Types.push_back(BT);
  BuiltinTypes.InsertNode(BT, InsertPos);
  return BT;
}

CharacterType *ASTContext::getCharacterType(uint64_t Length) const {
  llvm::FoldingSetNodeID ID;
  CharacterType::Profile(ID, Length);

  void *InsertPos = 0;
  if (auto CT = CharTypes.FindNodeOrInsertPos(ID, InsertPos))
    return CT;

  auto CT = new (*this, TypeAlignment) CharacterType(Length);
  Types.push_back(CT);
  CharTypes.InsertNode(CT, InsertPos);
  return CT;
}

/// getPointerType - Return the uniqued reference to the type for a pointer to
/// the specified type.
PointerType *ASTContext::getPointerType(const Type *Ty, unsigned NumDims) {
  // Unique pointers, to guarantee there is only one pointer of a particular
  // structure.
  llvm::FoldingSetNodeID ID;
  PointerType::Profile(ID, Ty, NumDims);

  void *InsertPos = 0;
  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
    return PT;

  PointerType *New = new (*this, TypeAlignment) PointerType(Ty, NumDims);
  Types.push_back(New);
  PointerTypes.InsertNode(New, InsertPos);
  return New;
}

/// getArrayType - Return the unique reference to the type for an array of the
/// specified element type.
QualType ASTContext::getArrayType(QualType EltTy,
                                  ArrayRef<ArraySpec*> Dims) {
  ArrayType *New = new (*this, TypeAlignment) ArrayType(*this, Type::Array, EltTy,
                                                        QualType(), Dims);
  Types.push_back(New);
  return QualType(New, 0);
}

QualType ASTContext::getFunctionType(QualType ResultType, const FunctionDecl *Prototype) {
  llvm::FoldingSetNodeID ID;
  FunctionType::Profile(ID, ResultType, Prototype);

  void *InsertPos = 0;
  FunctionType *Result = FunctionTypes.FindNodeOrInsertPos(ID, InsertPos);
  if(!Result) {
    Result = FunctionType::Create(*this, ResultType, Prototype);
    Types.push_back(Result);
    FunctionTypes.InsertNode(Result, InsertPos);
  }
  return QualType(Result, 0);
}

/// getTypeDeclTypeSlow - Return the unique reference to the type for the
/// specified type declaration.
QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
  assert(Decl && "Passed null for Decl param");
  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");

  const RecordDecl *Record = dyn_cast<RecordDecl>(Decl);
  if (!Record) {
    llvm_unreachable("TypeDecl without a type?");
    return QualType(Decl->TypeForDecl, 0);
  }

  return getRecordType(Record);
}

QualType ASTContext::getRecordType(const RecordDecl *Record) {
  if (Record->TypeForDecl) return QualType(Record->TypeForDecl, 0);

  SmallVector<FieldDecl*, 8> Fields;
  unsigned Idx = 0;
  for(auto I = Record->decls_begin(), End = Record->decls_end(); I != End; ++I, ++Idx) {
    if(auto Field = dyn_cast<FieldDecl>(*I)) {
      Fields.push_back(Field);
      Field->setIndex(Idx);
    }
  }
  RecordType *newType = new (*this, TypeAlignment) RecordType(*this, Record, Fields);
  Record->TypeForDecl = newType;
  Types.push_back(newType);
  return QualType(newType, 0);
}

} //namespace flang