Revert "Revert "Handle kernel launch calls""

Bug: 23535985

This reverts commit 5d9263d3a3a7457b9e5fe6e518c0d822dcdfcda6.

This also fixes issues in change list 172074, which caused some
slang tests to fail due to changed hehavior in error handling.

The fix was based on changes originally posted by srhines@.

To keep single-source functionality working, I separated kernel
name logging from the final export processing code. We would
build a map from kernel funciton names to slot numbers as we parse
the script. The map is used when translating a rsParallelFor call
to a call to the internal rsForEachInternal API, so that we can find
the proper slot numbers.

Here is Steve's original commit message.

Only call processExports() after a successful C99 compilation.

This refactoring fixes a bug in the previous single-source patch, where
exported functions/variables/kernels were being processed during the
regular compilation steps. This wastes compile time, since we don't need
to actually prepare for outputting reflected Java/C++ code if the
compile is going to fail for regular C99 reasons.

Change-Id: I81d88731188f4258f12f4c90ae8dd8abc8c2d641
diff --git a/slang_rs_context.h b/slang_rs_context.h
index 8cced4d..8298a67 100644
--- a/slang_rs_context.h
+++ b/slang_rs_context.h
@@ -21,6 +21,7 @@
 #include <list>
 #include <map>
 #include <string>
+#include <vector>
 
 #include "clang/Lex/Preprocessor.h"
 #include "clang/AST/Mangle.h"
@@ -40,7 +41,9 @@
   class ASTContext;
   class TargetInfo;
   class FunctionDecl;
+  class QualType;
   class SourceManager;
+  class TypeDecl;
 }   // namespace clang
 
 namespace slang {
@@ -60,7 +63,7 @@
   typedef std::list<RSExportable*> ExportableList;
   typedef std::list<RSExportVar*> ExportVarList;
   typedef std::list<RSExportFunc*> ExportFuncList;
-  typedef std::list<RSExportForEach*> ExportForEachList;
+  typedef std::vector<RSExportForEach*> ExportForEachVector;
   typedef std::list<RSExportReduce*> ExportReduceList;
   typedef llvm::StringMap<RSExportType*> ExportTypeMap;
 
@@ -97,14 +100,18 @@
   bool processExportFunc(const clang::FunctionDecl *FD);
   bool processExportType(const llvm::StringRef &Name);
 
-  void cleanupForEach();
+  int getForEachSlotNumber(const clang::StringRef& funcName);
+  unsigned mNextSlot;
 
   ExportVarList mExportVars;
   ExportFuncList mExportFuncs;
-  ExportForEachList mExportForEach;
+  std::map<llvm::StringRef, unsigned> mExportForEachMap;
+  ExportForEachVector mExportForEach;
   ExportReduceList mExportReduce;
   ExportTypeMap mExportTypes;
 
+  clang::QualType mAllocationType;
+
  public:
   RSContext(clang::Preprocessor &PP,
             clang::ASTContext &Ctx,
@@ -159,7 +166,13 @@
 
   inline const std::string &getRSPackageName() const { return mRSPackageName; }
 
-  bool processExport();
+  void addAllocationType(const clang::TypeDecl* TD);
+  inline const clang::QualType& getAllocationType() const {
+    return mAllocationType;
+  }
+
+  bool addForEach(const clang::FunctionDecl* FD);
+  bool processExports();
   inline void newExportable(RSExportable *E) {
     if (E != nullptr)
       mExportables.push_back(E);
@@ -192,7 +205,7 @@
   }
   inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
 
-  typedef ExportForEachList::const_iterator const_export_foreach_iterator;
+  typedef ExportForEachVector::const_iterator const_export_foreach_iterator;
   const_export_foreach_iterator export_foreach_begin() const {
     return mExportForEach.begin();
   }
@@ -200,6 +213,7 @@
     return mExportForEach.end();
   }
   inline bool hasExportForEach() const { return !mExportForEach.empty(); }
+  int getForEachSlotNumber(const clang::FunctionDecl* FD);
 
   typedef ExportReduceList::const_iterator const_export_reduce_iterator;
   const_export_reduce_iterator export_reduce_begin() const {