slang_rs.h revision fcda2352b9e140529f8f3c89f05b10a70c0048b2
1/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _SLANG_COMPILER_SLANG_RS_HPP
18#define _SLANG_COMPILER_SLANG_RS_HPP
19
20#include "slang.h"
21
22#include <list>
23#include <vector>
24#include <string>
25
26#include "llvm/ADT/StringMap.h"
27
28#include "slang_rs_reflect_utils.h"
29
30namespace clang {
31  class FunctionDecl;
32}
33
34namespace slang {
35  class RSContext;
36  class RSExportRecordType;
37
38class SlangRS : public Slang {
39 private:
40  // Context for RenderScript
41  RSContext *mRSContext;
42
43  bool mAllowRSPrefix;
44
45  // Custom diagnostic identifiers
46  unsigned mDiagErrorInvalidOutputDepParameter;
47  unsigned mDiagErrorODR;
48
49  // FIXME: Should be std::list<RSExportable *> here. But currently we only
50  //        check ODR on record type.
51  //
52  // ReflectedDefinitions maps record type name to a pair:
53  //  <its RSExportRecordType instance,
54  //   the first file contains this record type definition>
55  typedef std::pair<RSExportRecordType*, const char*> ReflectedDefinitionTy;
56  typedef llvm::StringMap<ReflectedDefinitionTy> ReflectedDefinitionListTy;
57  ReflectedDefinitionListTy ReflectedDefinitions;
58
59  // The package name that's really applied will be filled in RealPackageName.
60  bool reflectToJava(const std::string &OutputPathBase,
61                     const std::string &OutputPackageName,
62                     std::string *RealPackageName);
63
64  bool generateBitcodeAccessor(const std::string &OutputPathBase,
65                               const std::string &PackageName);
66
67  // CurInputFile is the pointer to a char array holding the input filename
68  // and is valid before compile() ends.
69  bool checkODR(const char *CurInputFile);
70
71 protected:
72  virtual void initDiagnostic();
73  virtual void initPreprocessor();
74  virtual void initASTContext();
75
76  virtual clang::ASTConsumer
77  *createBackend(const clang::CodeGenOptions& CodeGenOpts,
78                 llvm::raw_ostream *OS,
79                 Slang::OutputType OT);
80
81
82 public:
83  static bool IsRSHeaderFile(const char *File);
84  // FIXME: Determine whether a function is in RS header (i.e., one of the RS
85  //        built-in APIs) should only need its names (we need a "list" of RS
86  //        built-in APIs).
87  static bool IsFunctionInRSHeaderFile(const clang::FunctionDecl *FD,
88                                       const clang::SourceManager &SourceMgr);
89
90  SlangRS();
91
92  // Compile bunch of RS files given in the llvm-rs-cc arguments. Return true if
93  // all given input files are successfully compiled without errors.
94  //
95  // @IOFiles - List of pairs of <input file path, output file path>.
96  //
97  // @DepFiles - List of pairs of <output dep. file path, dependent bitcode
98  //             target>. If @OutputDep is true, this parameter must be given
99  //             with the same number of pairs given in @IOFiles.
100  //
101  // @IncludePaths - User-defined include paths.
102  //
103  // @AdditionalDepTargets - User-defined files added to the dependencies.
104  //
105  // @OutputType - See Slang::OutputType.
106  //
107  // @BitcodeStorage - See BitCodeStorageType in slang_rs_reflect_util.cpp.
108  //
109  // @AllowRSPrefix - true to allow user-defined function prefixed with 'rs'.
110  //
111  // @OutputDep - true if output dependecies file for each input file.
112  //
113  // @JavaReflectionPathBase - The path base for storing reflection files.
114  //
115  // @JavaReflectionPackageName - The package name given by user in command
116  //                              line. This may override the package name
117  //                              specified in the .rs using #pragma.
118  //
119  bool compile(const std::list<std::pair<const char*, const char*> > &IOFiles,
120               const std::list<std::pair<const char*, const char*> > &DepFiles,
121               const std::vector<std::string> &IncludePaths,
122               const std::vector<std::string> &AdditionalDepTargets,
123               Slang::OutputType OutputType, BitCodeStorageType BitcodeStorage,
124               bool AllowRSPrefix, bool OutputDep,
125               const std::string &JavaReflectionPathBase,
126               const std::string &JavaReflectionPackageName);
127
128  virtual void reset();
129
130  virtual ~SlangRS();
131};
132}
133
134#endif  // _SLANG_COMPILER_SLANG_RS_HPP
135