Home | History | Annotate | Download | only in slang
      1 /*
      2  * Copyright 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 #include <sys/stat.h>
     18 
     19 #include <cstdarg>
     20 #include <cctype>
     21 
     22 #include <algorithm>
     23 #include <sstream>
     24 #include <string>
     25 #include <utility>
     26 
     27 #include "os_sep.h"
     28 #include "slang_rs_context.h"
     29 #include "slang_rs_export_var.h"
     30 #include "slang_rs_export_foreach.h"
     31 #include "slang_rs_export_func.h"
     32 #include "slang_rs_reflect_utils.h"
     33 #include "slang_version.h"
     34 #include "slang_utils.h"
     35 
     36 #include "slang_rs_reflection_base.h"
     37 
     38 
     39 
     40 using namespace std;
     41 
     42 namespace slang {
     43 
     44 static const char *const gApacheLicenseNote =
     45 "/*\n"
     46 " * Copyright (C) 2012 The Android Open Source Project\n"
     47 " *\n"
     48 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
     49 " * you may not use this file except in compliance with the License.\n"
     50 " * You may obtain a copy of the License at\n"
     51 " *\n"
     52 " *      http://www.apache.org/licenses/LICENSE-2.0\n"
     53 " *\n"
     54 " * Unless required by applicable law or agreed to in writing, software\n"
     55 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
     56 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
     57 " * See the License for the specific language governing permissions and\n"
     58 " * limitations under the License.\n"
     59 " */\n"
     60 "\n";
     61 
     62 
     63 RSReflectionBase::RSReflectionBase(const RSContext *con) {
     64   mRSContext = con;
     65   mLicenseNote = gApacheLicenseNote;
     66 
     67 }
     68 
     69 RSReflectionBase::~RSReflectionBase() {
     70 
     71 }
     72 
     73 /*
     74 bool RSReflectionBase::openFile(const string &name, string &errorMsg) {
     75     if(!mUseStdout) {
     76         mOF.clear();
     77         if(!SlangUtils::CreateDirectoryWithParents(mOutputPath, &errorMsg)) {
     78             return false;
     79         }
     80 
     81         string cf(mOutputPath + OS_PATH_SEPARATOR_STR + name);
     82         mOF.open(cf.c_str());
     83         if(!mOF.good()) {
     84             errorMsg = "failed to open file '" + cf + "' for write";
     85             return false;
     86         }
     87     }
     88     return true;
     89 }
     90 */
     91 
     92 void RSReflectionBase::startFile(const string &filename) {
     93   if(mVerbose) {
     94     printf("Generating %s\n", filename.c_str());
     95   }
     96 
     97   // License
     98   write(mLicenseNote);
     99 
    100   // Notice of generated file
    101   write("/*");
    102   write(" * This file is auto-generated. DO NOT MODIFY!");
    103   write(" * The source Renderscript file: " + mInputFileName);
    104   write(" */");
    105   write("");
    106 }
    107 
    108 // remove path plus .rs from filename to generate class name
    109 string RSReflectionBase::stripRS(const string &s) const {
    110   string tmp(s);
    111   size_t pos = tmp.rfind(".rs");
    112   if(pos != string::npos) {
    113     tmp.erase(pos);
    114   }
    115   pos = tmp.rfind("/");
    116   if (pos != string::npos) {
    117     tmp.erase(0, pos+1);
    118   }
    119   return tmp;
    120 }
    121 
    122 void RSReflectionBase::write(const std::string &t) {
    123   //printf("%s%s\n", mIndent.c_str(), t.c_str());
    124   mText.push_back(mIndent + t);
    125 }
    126 
    127 void RSReflectionBase::write(const std::stringstream &t) {
    128   mText.push_back(mIndent + t.str());
    129 }
    130 
    131 
    132 void RSReflectionBase::incIndent() {
    133   mIndent.append("    ");
    134 }
    135 
    136 void RSReflectionBase::decIndent() {
    137   mIndent.erase(0, 4);
    138 }
    139 
    140 bool RSReflectionBase::writeFile(const string &filename, const vector< string > &txt) {
    141   FILE *pfin = fopen((mOutputPath + filename).c_str(), "wt");
    142   if (pfin == NULL) {
    143     fprintf(stderr, "Error: could not write file %s\n", filename.c_str());
    144     return false;
    145   }
    146 
    147   for(size_t ct=0; ct < txt.size(); ct++) {
    148     fprintf(pfin, "%s\n", txt[ct].c_str());
    149   }
    150   fclose(pfin);
    151   return true;
    152 }
    153 
    154 
    155 string RSReflectionBase::genInitValue(const clang::APValue &Val, bool asBool) {
    156   stringstream tmp;
    157   switch (Val.getKind()) {
    158     case clang::APValue::Int: {
    159       llvm::APInt api = Val.getInt();
    160       if(asBool) {
    161         tmp << ((api.getSExtValue() == 0) ? "false" : "true");
    162       } else {
    163         tmp << api.getSExtValue();
    164         if (api.getBitWidth() > 32) {
    165           tmp << "L";
    166         }
    167       }
    168       break;
    169     }
    170 
    171     case clang::APValue::Float: {
    172       llvm::APFloat apf = Val.getFloat();
    173       if (&apf.getSemantics() == &llvm::APFloat::IEEEsingle) {
    174         tmp << apf.convertToFloat() << "f";
    175       } else {
    176         tmp << apf.convertToDouble();
    177       }
    178       break;
    179     }
    180 
    181     case clang::APValue::ComplexInt:
    182     case clang::APValue::ComplexFloat:
    183     case clang::APValue::LValue:
    184     case clang::APValue::Vector: {
    185       slangAssert(false && "Primitive type cannot have such kind of initializer");
    186       break;
    187     }
    188 
    189     default: {
    190       slangAssert(false && "Unknown kind of initializer");
    191     }
    192   }
    193   return tmp.str();
    194 }
    195 
    196 
    197 }
    198