blob: 8b98ebf00cec73274bc5ee34082b89d462520132 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Chris Lattner970e54e2006-11-12 00:37:36 +000015#include "clang/AST/Type.h"
Chris Lattnerddc135e2006-11-10 06:34:16 +000016#include "clang/Lex/Preprocessor.h"
17using namespace llvm;
18using namespace clang;
19
20ASTContext::ASTContext(Preprocessor &pp)
21 : PP(pp), Target(pp.getTargetInfo()) {
Chris Lattner970e54e2006-11-12 00:37:36 +000022 InitBuiltinTypes();
23}
24
Chris Lattnerd5973eb2006-11-12 00:53:46 +000025ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
28 delete Types.back();
29 Types.pop_back();
30 }
31}
32
33void ASTContext::InitBuiltinType(TypeRef &R, const char *Name) {
34 Types.push_back((R = new BuiltinType(Name)).getTypePtr());
35}
36
37
Chris Lattner970e54e2006-11-12 00:37:36 +000038void ASTContext::InitBuiltinTypes() {
39 assert(VoidTy.isNull() && "Context reinitialized?");
40
41 // C99 6.2.5p19.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000042 InitBuiltinType(VoidTy, "void");
Chris Lattner970e54e2006-11-12 00:37:36 +000043
44 // C99 6.2.5p2.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000045 InitBuiltinType(BoolTy, "_Bool");
Chris Lattner970e54e2006-11-12 00:37:36 +000046 // C99 6.2.5p3.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000047 InitBuiltinType(CharTy, "char");
Chris Lattner970e54e2006-11-12 00:37:36 +000048 // C99 6.2.5p4.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000049 InitBuiltinType(SignedCharTy, "signed char");
50 InitBuiltinType(ShortTy, "short");
51 InitBuiltinType(IntTy, "int");
52 InitBuiltinType(LongTy, "long");
53 InitBuiltinType(LongLongTy, "long long");
Chris Lattner970e54e2006-11-12 00:37:36 +000054
55 // C99 6.2.5p6.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000056 InitBuiltinType(UnsignedCharTy, "unsigned char");
57 InitBuiltinType(UnsignedShortTy, "unsigned short");
58 InitBuiltinType(UnsignedIntTy, "unsigned int");
59 InitBuiltinType(UnsignedLongTy, "unsigned long");
60 InitBuiltinType(UnsignedLongLongTy, "unsigned long long");
Chris Lattner970e54e2006-11-12 00:37:36 +000061
62 // C99 6.2.5p10.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000063 InitBuiltinType(FloatTy, "float");
64 InitBuiltinType(DoubleTy, "double");
65 InitBuiltinType(LongDoubleTy, "long double");
Chris Lattner970e54e2006-11-12 00:37:36 +000066
67 // C99 6.2.5p11.
Chris Lattnerd5973eb2006-11-12 00:53:46 +000068 InitBuiltinType(FloatComplexTy, "float _Complex");
69 InitBuiltinType(DoubleComplexTy, "double _Complex");
70 InitBuiltinType(LongDoubleComplexTy, "long double _Complex");
Chris Lattner970e54e2006-11-12 00:37:36 +000071}
72
73/// getPointerType - Return the uniqued reference to the type for a pointer to
74/// the specified type.
75TypeRef ASTContext::getPointerType(const TypeRef &T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +000076 // FIXME: This is obviously braindead!
77 // Unique pointers, to guarantee there is only one pointer of a particular
78 // structure.
79 for (unsigned i = 0, e = Types.size(); i != e; ++i)
Chris Lattner47814662006-11-12 00:56:20 +000080 if (PointerType *PTy = dyn_cast<PointerType>(Types[i]))
81 if (PTy->getPointee() == T)
Chris Lattnerd5973eb2006-11-12 00:53:46 +000082 return Types[i];
Chris Lattner970e54e2006-11-12 00:37:36 +000083
84
85 Type *Canonical = 0;
86 if (!T->isCanonical())
87 Canonical = getPointerType(T.getCanonicalType()).getTypePtr();
Chris Lattnerd5973eb2006-11-12 00:53:46 +000088
89 Types.push_back(new PointerType(T, Canonical));
90 return Types.back();
Chris Lattnerddc135e2006-11-10 06:34:16 +000091}
92
Chris Lattneref51c202006-11-10 07:17:23 +000093