slang_rs.h revision 2e35b136cc2434080fcd682d2f95e53a87675dd4
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 _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_H_
19
20#include "slang.h"
21
22#include <list>
23#include <string>
24#include <utility>
25#include <vector>
26
27#include "llvm/ADT/StringMap.h"
28
29#include "slang_rs_reflect_utils.h"
30
31// Define minimum and maximum target API versions. These correspond to the same
32// API levels used by the standard Android SDK.
33//
34// 12 - Honeycomb MR1
35// 13 - Honeycomb MR2
36// 14 - Ice Cream Sandwich
37// ...
38#define RS_MINIMUM_TARGET_API 12
39#define RS_MAXIMUM_TARGET_API RS_VERSION
40
41namespace clang {
42  class FunctionDecl;
43}
44
45namespace slang {
46  class RSContext;
47  class RSExportRecordType;
48
49class SlangRS : public Slang {
50 private:
51  // Context for RenderScript
52  RSContext *mRSContext;
53
54  bool mAllowRSPrefix;
55
56  unsigned int mTargetAPI;
57
58  // Custom diagnostic identifiers
59  unsigned mDiagErrorInvalidOutputDepParameter;
60  unsigned mDiagErrorODR;
61  unsigned mDiagErrorTargetAPIRange;
62
63  // Collect generated filenames (without the .java) for dependency generation
64  std::vector<std::string> mGeneratedFileNames;
65
66  // FIXME: Should be std::list<RSExportable *> here. But currently we only
67  //        check ODR on record type.
68  //
69  // ReflectedDefinitions maps record type name to a pair:
70  //  <its RSExportRecordType instance,
71  //   the first file contains this record type definition>
72  typedef std::pair<RSExportRecordType*, const char*> ReflectedDefinitionTy;
73  typedef llvm::StringMap<ReflectedDefinitionTy> ReflectedDefinitionListTy;
74  ReflectedDefinitionListTy ReflectedDefinitions;
75
76  // The package name that's really applied will be filled in RealPackageName.
77  bool reflectToJava(const std::string &OutputPathBase,
78                     const std::string &OutputPackageName,
79                     std::string *RealPackageName);
80
81  bool generateBitcodeAccessor(const std::string &OutputPathBase,
82                               const std::string &PackageName);
83
84  // CurInputFile is the pointer to a char array holding the input filename
85  // and is valid before compile() ends.
86  bool checkODR(const char *CurInputFile);
87
88 protected:
89  virtual void initDiagnostic();
90  virtual void initPreprocessor();
91  virtual void initASTContext();
92
93  virtual clang::ASTConsumer
94  *createBackend(const clang::CodeGenOptions& CodeGenOpts,
95                 llvm::raw_ostream *OS,
96                 Slang::OutputType OT);
97
98
99 public:
100  static bool IsRSHeaderFile(const char *File);
101  // FIXME: Determine whether a function is in RS header (i.e., one of the RS
102  //        built-in APIs) should only need its names (we need a "list" of RS
103  //        built-in APIs).
104  static bool IsFunctionInRSHeaderFile(const clang::FunctionDecl *FD,
105                                       const clang::SourceManager &SourceMgr);
106
107  SlangRS();
108
109  // Compile bunch of RS files given in the llvm-rs-cc arguments. Return true if
110  // all given input files are successfully compiled without errors.
111  //
112  // @IOFiles - List of pairs of <input file path, output file path>.
113  //
114  // @DepFiles - List of pairs of <output dep. file path, dependent bitcode
115  //             target>. If @OutputDep is true, this parameter must be given
116  //             with the same number of pairs given in @IOFiles.
117  //
118  // @IncludePaths - User-defined include paths.
119  //
120  // @AdditionalDepTargets - User-defined files added to the dependencies.
121  //
122  // @OutputType - See Slang::OutputType.
123  //
124  // @BitcodeStorage - See BitCodeStorageType in slang_rs_reflect_util.cpp.
125  //
126  // @AllowRSPrefix - true to allow user-defined function prefixed with 'rs'.
127  //
128  // @OutputDep - true if output dependecies file for each input file.
129  //
130  // @JavaReflectionPathBase - The path base for storing reflection files.
131  //
132  // @JavaReflectionPackageName - The package name given by user in command
133  //                              line. This may override the package name
134  //                              specified in the .rs using #pragma.
135  //
136  bool compile(const std::list<std::pair<const char*, const char*> > &IOFiles,
137               const std::list<std::pair<const char*, const char*> > &DepFiles,
138               const std::vector<std::string> &IncludePaths,
139               const std::vector<std::string> &AdditionalDepTargets,
140               Slang::OutputType OutputType, BitCodeStorageType BitcodeStorage,
141               bool AllowRSPrefix, bool OutputDep,
142               unsigned int TargetAPI,
143               const std::string &JavaReflectionPathBase,
144               const std::string &JavaReflectionPackageName);
145
146  virtual void reset();
147
148  virtual ~SlangRS();
149};
150}
151
152#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_H_  NOLINT
153