implement support for basic codegen of global variables with no initializers.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39795 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CodeGen/CGDecl.cpp b/CodeGen/CGDecl.cpp
index 1cce124..d86f305 100644
--- a/CodeGen/CGDecl.cpp
+++ b/CodeGen/CGDecl.cpp
@@ -19,7 +19,6 @@
 
 
 void CodeGenFunction::EmitDecl(const Decl &D) {
-
   switch (D.getKind()) {
   default: assert(0 && "Unknown decl kind!");
   case Decl::FileVariable:
diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp
index cdc3e63..b610d3e 100644
--- a/CodeGen/CodeGenModule.cpp
+++ b/CodeGen/CodeGenModule.cpp
@@ -48,12 +48,45 @@
                                           0, D->getName(), &getModule());
 }
 
-void CodeGenModule::EmitFunction(FunctionDecl *FD) {
+void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
   // If this is not a prototype, emit the body.
   if (FD->getBody())
     CodeGenFunction(*this).GenerateCode(FD);
 }
 
+void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
+  llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D));
+  
+  // If the storage class is external and there is no initializer, just leave it
+  // as a declaration.
+  if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
+    return;
+
+  // Otherwise, convert the initializer, or use zero if appropriate.
+  llvm::Constant *Init;
+  if (D->getInit() == 0)
+    Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
+  else
+    assert(D->getInit() == 0 && "FIXME: Global variable initializers unimp!");
+    
+  GV->setInitializer(Init);
+  
+  // Set the llvm linkage type as appropriate.
+  // FIXME: This isn't right.  This should handle common linkage and other
+  // stuff.
+  switch (D->getStorageClass()) {
+  case VarDecl::Auto:
+  case VarDecl::Register:
+    assert(0 && "Can't have auto or register globals");
+  case VarDecl::None:
+  case VarDecl::Extern:
+    // todo: common
+    break;
+  case VarDecl::Static:
+    GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
+    break;
+  }
+}
 
 
 llvm::Function *CodeGenModule::getMemCpyFn() {
diff --git a/CodeGen/CodeGenModule.h b/CodeGen/CodeGenModule.h
index 885fb97..9d85470 100644
--- a/CodeGen/CodeGenModule.h
+++ b/CodeGen/CodeGenModule.h
@@ -27,6 +27,7 @@
   class ASTContext;
   class FunctionDecl;
   class Decl;
+  class FileVarDecl;
     
 namespace CodeGen {
 
@@ -50,7 +51,8 @@
   
   llvm::Function *getMemCpyFn();
   
-  void EmitFunction(FunctionDecl *FD);
+  void EmitFunction(const FunctionDecl *FD);
+  void EmitGlobalVar(const FileVarDecl *D);
   
   void PrintStats() {}
 };
diff --git a/CodeGen/ModuleBuilder.cpp b/CodeGen/ModuleBuilder.cpp
index 16b13d1..e525514 100644
--- a/CodeGen/ModuleBuilder.cpp
+++ b/CodeGen/ModuleBuilder.cpp
@@ -32,6 +32,12 @@
   static_cast<CodeGenModule*>(B)->EmitFunction(D);
 }
 
+/// CodeGenGlobalVar - Emit the specified global variable to LLVM.
+void clang::CodeGen::CodeGenGlobalVar(BuilderTy *Builder, FileVarDecl *D) {
+  static_cast<CodeGenModule*>(Builder)->EmitGlobalVar(D);
+}
+
+
 /// PrintStats - Emit statistic information to stderr.
 ///
 void clang::CodeGen::PrintStats(BuilderTy *B) {