Partial AST and Sema support for C++ try-catch.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61337 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp
index 2308159..079ed4e 100644
--- a/lib/AST/Stmt.cpp
+++ b/lib/AST/Stmt.cpp
@@ -14,6 +14,7 @@
 #include "clang/AST/Stmt.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
+#include "clang/AST/Type.h"
 using namespace clang;
 
 static struct StmtClassNameTable {
@@ -333,3 +334,22 @@
   return &SubStmts[0]+END_EXPR;
 }
 
+// CXXCatchStmt
+Stmt::child_iterator CXXCatchStmt::child_begin() {
+  return &HandlerBlock;
+}
+
+Stmt::child_iterator CXXCatchStmt::child_end() {
+  return &HandlerBlock + 1;
+}
+
+QualType CXXCatchStmt::getCaughtType() {
+  if (ExceptionDecl)
+    return llvm::cast<VarDecl>(ExceptionDecl)->getType();
+  return QualType();
+}
+
+void CXXCatchStmt::Destroy(ASTContext& C) {
+  ExceptionDecl->Destroy(C);
+  Stmt::Destroy(C);
+}
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index 81e158e..61cd89f 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -474,6 +474,17 @@
   OS << "\n";
 }
 
+void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
+  Indent() << "catch (";
+  if (Decl *ExDecl = Node->getExceptionDecl())
+    PrintRawDecl(ExDecl);
+  else
+    OS << "...";
+  OS << ") ";
+  PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
+  OS << "\n";
+}
+
 //===----------------------------------------------------------------------===//
 //  Expr printing methods.
 //===----------------------------------------------------------------------===//
diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp
index d12ecd0..2d6f755 100644
--- a/lib/AST/StmtSerialization.cpp
+++ b/lib/AST/StmtSerialization.cpp
@@ -242,6 +242,9 @@
 
     case CXXDependentNameExprClass:
       return CXXDependentNameExpr::CreateImpl(D, C);
+
+    case CXXCatchStmtClass:
+      return CXXCatchStmt::CreateImpl(D, C);
   }
 }
 
@@ -1523,3 +1526,17 @@
   SourceLocation L = SourceLocation::ReadVal(D);
   return new CXXDependentNameExpr(N, Ty, L);
 }
+
+void CXXCatchStmt::EmitImpl(llvm::Serializer& S) const {
+  S.Emit(CatchLoc);
+  S.EmitOwnedPtr(ExceptionDecl);
+  S.EmitOwnedPtr(HandlerBlock);
+}
+
+CXXCatchStmt *
+CXXCatchStmt::CreateImpl(llvm::Deserializer& D, ASTContext& C) {
+  SourceLocation CatchLoc = SourceLocation::ReadVal(D);
+  Decl *ExDecl = D.ReadOwnedPtr<Decl>(C);
+  Stmt *HandlerBlock = D.ReadOwnedPtr<Stmt>(C);
+  return new CXXCatchStmt(CatchLoc, ExDecl, HandlerBlock);
+}
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index 81a9765..c853ecf 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -1314,7 +1314,8 @@
   DeclTy *ExceptionDecl = 0;
   if (Tok.isNot(tok::ellipsis)) {
     DeclSpec DS;
-    ParseDeclarationSpecifiers(DS);
+    if (ParseCXXTypeSpecifierSeq(DS))
+      return StmtError();
     Declarator ExDecl(DS, Declarator::CXXCatchContext);
     ParseDeclarator(ExDecl);
     ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index f8923b4..c179fe8 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -636,10 +636,10 @@
                                                  ExprTy *SynchExpr, 
                                                  StmtTy *SynchBody);
 
-  //virtual DeclTy *ActOnExceptionDeclarator(Scope *S, Declarator &D);
-  //virtual OwningStmtResult ActOnCXXCatchBlock(SourceLocation CatchLoc,
-  //                                            DeclTy *ExceptionDecl,
-  //                                            StmtArg HandlerBlock);
+  virtual DeclTy *ActOnExceptionDeclarator(Scope *S, Declarator &D);
+  virtual OwningStmtResult ActOnCXXCatchBlock(SourceLocation CatchLoc,
+                                              DeclTy *ExDecl,
+                                              StmtArg HandlerBlock);
   //virtual OwningStmtResult ActOnCXXTryBlock(SourceLocation TryLoc,
   //                                          StmtArg TryBlock,
   //                                          MultiStmtArg Handlers);
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index f2f02e2..4ac7ecb 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -2026,3 +2026,66 @@
   return LinkageSpecDecl::Create(Context, Loc, Language, dcl);
 }
 
+/// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch
+/// handler.
+Sema::DeclTy *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D)
+{
+  QualType ExDeclType = GetTypeForDeclarator(D, S);
+  SourceLocation Begin = D.getDeclSpec().getSourceRange().getBegin();
+
+  bool Invalid = false;
+
+  // Arrays and functions decay.
+  if (ExDeclType->isArrayType())
+    ExDeclType = Context.getArrayDecayedType(ExDeclType);
+  else if (ExDeclType->isFunctionType())
+    ExDeclType = Context.getPointerType(ExDeclType);
+
+  // C++ 15.3p1: The exception-declaration shall not denote an incomplete type.
+  // The exception-declaration shall not denote a pointer or reference to an
+  // incomplete type, other than [cv] void*.
+  QualType BaseType = ExDeclType;
+  int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference
+  if (const PointerType *Ptr = BaseType->getAsPointerType()) {
+    BaseType = Ptr->getPointeeType();
+    Mode = 1;
+  } else if(const ReferenceType *Ref = BaseType->getAsReferenceType()) {
+    BaseType = Ref->getPointeeType();
+    Mode = 2;
+  }
+  if ((Mode == 0 || !BaseType->isVoidType()) && BaseType->isIncompleteType()) {
+    Invalid = true;
+    Diag(Begin, diag::err_catch_incomplete) << BaseType << Mode;
+  }
+
+  IdentifierInfo *II = D.getIdentifier();
+  if (Decl *PrevDecl = LookupDecl(II, Decl::IDNS_Ordinary, S)) {
+    // The scope should be freshly made just for us. There is just no way
+    // it contains any previous declaration.
+    assert(!S->isDeclScope(PrevDecl));
+    if (PrevDecl->isTemplateParameter()) {
+      // Maybe we will complain about the shadowed template parameter.
+      DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
+
+    }
+  }
+
+  VarDecl *ExDecl = VarDecl::Create(Context, CurContext, D.getIdentifierLoc(),
+                                    II, ExDeclType, VarDecl::None, 0, Begin);
+  if (D.getInvalidType() || Invalid)
+    ExDecl->setInvalidDecl();
+
+  if (D.getCXXScopeSpec().isSet()) {
+    Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator)
+      << D.getCXXScopeSpec().getRange();
+    ExDecl->setInvalidDecl();
+  }
+
+  // Add the exception declaration into this scope.
+  S->AddDecl(ExDecl);
+  if (II)
+    IdResolver.AddDecl(ExDecl);
+
+  ProcessDeclAttributes(ExDecl, D);
+  return ExDecl;
+}
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 22a9617..6a322ee 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -951,3 +951,13 @@
     static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
   return SS;
 }
+
+/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block
+/// and creates a proper catch handler from them.
+Action::OwningStmtResult
+Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclTy *ExDecl,
+                         StmtArg HandlerBlock) {
+  // There's nothing to test that ActOnExceptionDecl didn't already test.
+  return Owned(new CXXCatchStmt(CatchLoc, static_cast<VarDecl*>(ExDecl),
+                                static_cast<Stmt*>(HandlerBlock.release())));
+}