ASTContext.h revision 716c7304ff5d27a95e1e7823acd1d09d5ec3e37f
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
55f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file was developed by Chris Lattner and is distributed under
65f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// the University of Illinois Open Source License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_AST_ASTCONTEXT_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_AST_ASTCONTEXT_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Builtins.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Expr.h"
19464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "clang/AST/RecordLayout.h"
20464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "clang/AST/Type.h"
21464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "llvm/ADT/DenseMap.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class TargetInfo;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ASTContext - This class holds long-lived AST nodes (such as types and
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// decls) that can be referred to throughout the semantic analysis of a file.
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass ASTContext {
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::vector<Type*> Types;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<ComplexType> ComplexTypes;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<PointerType> PointerTypes;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<ReferenceType> ReferenceTypes;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<ArrayType> ArrayTypes;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<VectorType> VectorTypes;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
38464175bba1318bef7905122e9fda20cff926df78Chris Lattner  llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
3971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  RecordDecl *CFConstantStringTypeDecl;
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  TargetInfo &Target;
4271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  IdentifierTable &Idents;
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Builtin::Context BuiltinInfo;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Builtin Types.
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType VoidTy;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType BoolTy;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType CharTy;
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType UnsignedLongLongTy;
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType FloatTy, DoubleTy, LongDoubleTy;
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  ASTContext(TargetInfo &t, IdentifierTable &idents) :
5671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    CFConstantStringTypeDecl(0), Target(t), Idents(idents) {
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinTypes();
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BuiltinInfo.InitializeBuiltins(idents, Target);
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ~ASTContext();
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
63d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
64464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
65464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //                           Type Constructors
66464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
67d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getComplexType - Return the uniqued reference to the type for a complex
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// number with the specified element type.
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getComplexType(QualType T);
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getPointerType - Return the uniqued reference to the type for a pointer to
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the specified type.
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getPointerType(QualType T);
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getReferenceType - Return the uniqued reference to the type for a
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// reference to the specified type.
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getReferenceType(QualType T);
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getArrayType - Return the unique reference to the type for an array of the
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified element type.
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getArrayType(QualType EltTy, ArrayType::ArraySizeModifier ASM,
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                        unsigned EltTypeQuals, Expr *NumElts);
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// getVectorType - Return the unique reference to a vector type of
8673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// the specified element type and size. VectorType must be a built-in type.
8773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType getVectorType(QualType VectorType, unsigned NumElts);
8873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
8973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// getOCUVectorType - Return the unique reference to an OCU vector type of
9073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// the specified element type and size. VectorType must be a built-in type.
9173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getFunctionTypeNoProto(QualType ResultTy);
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFunctionType - Return a normal function type with a typed argument
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// list.  isVariadic indicates whether the argument list includes '...'.
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           unsigned NumArgs, bool isVariadic);
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getTypedefType - Return the unique reference to the type for the
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified typename decl.
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getTypedefType(TypedefDecl *Decl);
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
106d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  /// getTypeOfType - GCC extension.
1078d1a3b8ca1e5fcc4567b5a6f51d82be2e460de1cSteve Naroff  QualType getTypeOfExpr(Expr *e);
108d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  QualType getTypeOfType(QualType t);
109d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getTagDeclType - Return the unique reference to the type for the
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified TagDecl (struct/union/class/enum) decl.
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getTagDeclType(TagDecl *Decl);
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getSizeType() const;
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1188b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1198b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1208b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  QualType getPointerDiffType() const;
1218b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
12271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  // getCFConstantStringType - Return the type used for constant CFStrings.
12371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  QualType getCFConstantStringType();
12471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
125464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
126464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //                         Type Sizing and Analysis
127464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
128464175bba1318bef7905122e9fda20cff926df78Chris Lattner
129464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getTypeInfo - Get the size and alignment of the specified complete type in
130464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// bits.
131464175bba1318bef7905122e9fda20cff926df78Chris Lattner  std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
132464175bba1318bef7905122e9fda20cff926df78Chris Lattner
133464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getTypeSize - Return the size of the specified type, in bits.  This method
134464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// does not work on incomplete types.
135464175bba1318bef7905122e9fda20cff926df78Chris Lattner  uint64_t getTypeSize(QualType T, SourceLocation L) {
136464175bba1318bef7905122e9fda20cff926df78Chris Lattner    return getTypeInfo(T, L).first;
137464175bba1318bef7905122e9fda20cff926df78Chris Lattner  }
138464175bba1318bef7905122e9fda20cff926df78Chris Lattner
139464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
140464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// method does not work on incomplete types.
141464175bba1318bef7905122e9fda20cff926df78Chris Lattner  unsigned getTypeAlign(QualType T, SourceLocation L) {
142464175bba1318bef7905122e9fda20cff926df78Chris Lattner    return getTypeInfo(T, L).second;
143464175bba1318bef7905122e9fda20cff926df78Chris Lattner  }
144464175bba1318bef7905122e9fda20cff926df78Chris Lattner
145464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getRecordLayout - Get or compute information about the layout of the
146464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// specified record (struct/union/class), which indicates its size and field
147464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// position information.
148464175bba1318bef7905122e9fda20cff926df78Chris Lattner  const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
149464175bba1318bef7905122e9fda20cff926df78Chris Lattner
150464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
151464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //                            Type Operators
152464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
153464175bba1318bef7905122e9fda20cff926df78Chris Lattner
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getIntegerBitwidth - Return the bitwidth of the specified integer type
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// according to the target.  'Loc' specifies the source location that
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// requires evaluation of this property.
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned getIntegerBitwidth(QualType T, SourceLocation Loc);
158464175bba1318bef7905122e9fda20cff926df78Chris Lattner
159464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
160464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static QualType maxIntegerType(QualType lhs, QualType rhs);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
163f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  /// maxFloatingType - Returns the highest ranked float type. Handles 3
164f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  /// different combos: float/float, float/complex, complex/complex.
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static QualType maxFloatingType(QualType lt, QualType rt);
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
167716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
168716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// point or a complex type (based on typeDomain/typeSize).
169716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// 'typeDomain' is a real floating point or complex type.
170716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// 'typeSize' is a real floating point or complex type.
171f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
172f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff                                             QualType typeDomain) const;
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void operator=(const ASTContext&); // DO NOT IMPLEMENT
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void InitBuiltinTypes();
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
184