CGDecl.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
  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 contains code to emit Decl nodes as LLVM code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenFunction.h"
  14. #include "clang/AST/AST.h"
  15. #include "llvm/Type.h"
  16. using namespace clang;
  17. using namespace CodeGen;
  18. void CodeGenFunction::EmitDecl(const Decl &D) {
  19. switch (D.getKind()) {
  20. default: assert(0 && "Unknown decl kind!");
  21. case Decl::FileVariable:
  22. assert(0 && "Should not see file-scope variables inside a function!");
  23. case Decl::ParmVariable:
  24. assert(0 && "Parmdecls should not be in declstmts!");
  25. case Decl::Typedef: // typedef int X;
  26. case Decl::Function: // void X();
  27. case Decl::Struct: // struct X;
  28. case Decl::Union: // union X;
  29. case Decl::Class: // class X;
  30. case Decl::Enum: // enum X;
  31. // None of these decls require codegen support.
  32. return;
  33. case Decl::BlockVariable:
  34. return EmitBlockVarDecl(cast<BlockVarDecl>(D));
  35. case Decl::EnumConstant:
  36. return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
  37. }
  38. }
  39. void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
  40. assert(0 && "FIXME: Enum constant decls not implemented yet!");
  41. }
  42. /// EmitBlockVarDecl - This method handles emission of any variable declaration
  43. /// inside a function, including static vars etc.
  44. void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) {
  45. switch (D.getStorageClass()) {
  46. case VarDecl::Static:
  47. assert(0 && "FIXME: local static vars not implemented yet");
  48. case VarDecl::Extern:
  49. assert(0 && "FIXME: should call up to codegenmodule");
  50. default:
  51. assert((D.getStorageClass() == VarDecl::None ||
  52. D.getStorageClass() == VarDecl::Auto ||
  53. D.getStorageClass() == VarDecl::Register) &&
  54. "Unknown storage class");
  55. return EmitLocalBlockVarDecl(D);
  56. }
  57. }
  58. /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
  59. /// variable declaration with auto, register, or no storage class specifier.
  60. /// These turn into simple stack objects.
  61. void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) {
  62. QualType Ty = D.getCanonicalType();
  63. llvm::Value *DeclPtr;
  64. if (Ty->isConstantSizeType()) {
  65. // A normal fixed sized variable becomes an alloca in the entry block.
  66. const llvm::Type *LTy = ConvertType(Ty);
  67. // TODO: Alignment
  68. DeclPtr = CreateTempAlloca(LTy, D.getName());
  69. } else {
  70. // TODO: Create a dynamic alloca.
  71. assert(0 && "FIXME: Local VLAs not implemented yet");
  72. }
  73. llvm::Value *&DMEntry = LocalDeclMap[&D];
  74. assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
  75. DMEntry = DeclPtr;
  76. // FIXME: Evaluate initializer.
  77. }
  78. /// Emit an alloca for the specified parameter and set up LocalDeclMap.
  79. void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
  80. QualType Ty = D.getCanonicalType();
  81. llvm::Value *DeclPtr;
  82. if (!Ty->isConstantSizeType()) {
  83. // Variable sized values always are passed by-reference.
  84. DeclPtr = Arg;
  85. } else {
  86. // A fixed sized first class variable becomes an alloca in the entry block.
  87. const llvm::Type *LTy = ConvertType(Ty);
  88. if (LTy->isFirstClassType()) {
  89. // TODO: Alignment
  90. DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
  91. AllocaInsertPt);
  92. // Store the initial value into the alloca.
  93. Builder.CreateStore(Arg, DeclPtr);
  94. } else {
  95. // Otherwise, if this is an aggregate, just use the input pointer.
  96. DeclPtr = Arg;
  97. }
  98. Arg->setName(D.getName());
  99. }
  100. llvm::Value *&DMEntry = LocalDeclMap[&D];
  101. assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
  102. DMEntry = DeclPtr;
  103. }