Home | History | Annotate | Download | only in slang
      1 /*
      2  * Copyright 2010-2012, 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 #include "slang_version.h"
     31 
     32 namespace slang {
     33   class RSContext;
     34   class RSExportRecordType;
     35 
     36 class SlangRS : public Slang {
     37  private:
     38   // Context for Renderscript
     39   RSContext *mRSContext;
     40 
     41   bool mAllowRSPrefix;
     42 
     43   unsigned int mTargetAPI;
     44 
     45   bool mIsFilterscript;
     46 
     47   // Custom diagnostic identifiers
     48   unsigned mDiagErrorInvalidOutputDepParameter;
     49   unsigned mDiagErrorODR;
     50   unsigned mDiagErrorTargetAPIRange;
     51 
     52   // Collect generated filenames (without the .java) for dependency generation
     53   std::vector<std::string> mGeneratedFileNames;
     54 
     55   // FIXME: Should be std::list<RSExportable *> here. But currently we only
     56   //        check ODR on record type.
     57   //
     58   // ReflectedDefinitions maps record type name to a pair:
     59   //  <its RSExportRecordType instance,
     60   //   the first file contains this record type definition>
     61   typedef std::pair<RSExportRecordType*, const char*> ReflectedDefinitionTy;
     62   typedef llvm::StringMap<ReflectedDefinitionTy> ReflectedDefinitionListTy;
     63   ReflectedDefinitionListTy ReflectedDefinitions;
     64 
     65   // The package name that's really applied will be filled in RealPackageName.
     66   bool reflectToJava(const std::string &OutputPathBase,
     67                      const std::string &OutputPackageName,
     68                      const std::string &RSPackageName,
     69                      std::string *RealPackageName);
     70 
     71   bool generateBitcodeAccessor(const std::string &OutputPathBase,
     72                                const std::string &PackageName);
     73 
     74   // CurInputFile is the pointer to a char array holding the input filename
     75   // and is valid before compile() ends.
     76   bool checkODR(const char *CurInputFile);
     77 
     78   // Returns true if this is a Filterscript file.
     79   static bool isFilterscript(const char *Filename);
     80 
     81  protected:
     82   virtual void initDiagnostic();
     83   virtual void initPreprocessor();
     84   virtual void initASTContext();
     85 
     86   virtual clang::ASTConsumer
     87   *createBackend(const clang::CodeGenOptions& CodeGenOpts,
     88                  llvm::raw_ostream *OS,
     89                  Slang::OutputType OT);
     90 
     91 
     92  public:
     93   static bool IsRSHeaderFile(const char *File);
     94   // FIXME: Determine whether a location is in RS header (i.e., one of the RS
     95   //        built-in APIs) should only need its names (we need a "list" of RS
     96   //        built-in APIs).
     97   static bool IsLocInRSHeaderFile(const clang::SourceLocation &Loc,
     98                                   const clang::SourceManager &SourceMgr);
     99 
    100   SlangRS();
    101 
    102   // Compile bunch of RS files given in the llvm-rs-cc arguments. Return true if
    103   // all given input files are successfully compiled without errors.
    104   //
    105   // @IOFiles - List of pairs of <input file path, output file path>.
    106   //
    107   // @DepFiles - List of pairs of <output dep. file path, dependent bitcode
    108   //             target>. If @OutputDep is true, this parameter must be given
    109   //             with the same number of pairs given in @IOFiles.
    110   //
    111   // @IncludePaths - User-defined include paths.
    112   //
    113   // @AdditionalDepTargets - User-defined files added to the dependencies.
    114   //
    115   // @OutputType - See Slang::OutputType.
    116   //
    117   // @BitcodeStorage - See BitCodeStorageType in slang_rs_reflect_util.cpp.
    118   //
    119   // @AllowRSPrefix - true to allow user-defined function prefixed with 'rs'.
    120   //
    121   // @OutputDep - true if output dependecies file for each input file.
    122   //
    123   // @JavaReflectionPathBase - The path base for storing reflection files.
    124   //
    125   // @EmitDebug - true to allow debug metadata emission
    126   //
    127   // @OptimizationLevel - code generation optimization level: None is recommended for
    128   //                      interactive debugging. The default is Aggresive.
    129   //
    130   // @JavaReflectionPackageName - The package name given by user in command
    131   //                              line. This may override the package name
    132   //                              specified in the .rs using #pragma.
    133   //
    134   // @RSPackageName - The RS package name supplied by the command line. This
    135   //                  can override the default value of
    136   //                  "android.renderscript" used by the normal APIs.
    137   //
    138   bool compile(const std::list<std::pair<const char*, const char*> > &IOFiles,
    139                const std::list<std::pair<const char*, const char*> > &DepFiles,
    140                const std::vector<std::string> &IncludePaths,
    141                const std::vector<std::string> &AdditionalDepTargets,
    142                Slang::OutputType OutputType, BitCodeStorageType BitcodeStorage,
    143                bool AllowRSPrefix, bool OutputDep,
    144                unsigned int TargetAPI, bool EmitDebug,
    145                llvm::CodeGenOpt::Level OptimizationLevel,
    146                const std::string &JavaReflectionPathBase,
    147                const std::string &JavaReflectionPackageName,
    148                const std::string &RSPackageName);
    149 
    150   virtual void reset();
    151 
    152   virtual ~SlangRS();
    153 
    154   virtual void makeModuleVisible(clang::Module *Mod,
    155                                  clang::Module::NameVisibilityKind Visibility,
    156                                  clang::SourceLocation ImportLoc) { }
    157 };
    158 }  // namespace slang
    159 
    160 #endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_H_  NOLINT
    161