aboutsummaryrefslogtreecommitdiffstats
path: root/slang_rs_context.cpp
blob: cd4c8af2bae19bf255b2e85fbe06241890f1b4dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright 2010, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "slang_rs_context.h"

#include <string>

#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Type.h"

#include "clang/Basic/Linkage.h"
#include "clang/Basic/TargetInfo.h"

#include "clang/Index/ASTLocation.h"

#include "llvm/LLVMContext.h"

#include "llvm/Target/TargetData.h"

#include "slang.h"
#include "slang_rs_export_func.h"
#include "slang_rs_export_type.h"
#include "slang_rs_export_var.h"
#include "slang_rs_exportable.h"
#include "slang_rs_pragma_handler.h"
#include "slang_rs_reflection.h"

namespace slang {

RSContext::RSContext(clang::Preprocessor &PP,
                     clang::ASTContext &Ctx,
                     const clang::TargetInfo &Target)
    : mPP(PP),
      mCtx(Ctx),
      mTarget(Target),
      mTargetData(NULL),
      mLLVMContext(llvm::getGlobalContext()),
      mExportAllNonStaticVars(true),
      mExportAllNonStaticFuncs(false),
      mLicenseNote(NULL) {
  // For #pragma rs export_var
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaExportVarHandler(this));

  // For #pragma rs export_var_all
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaExportVarAllHandler(this));

  // For #pragma rs export_func
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaExportFuncHandler(this));

  // For #pragma rs export_func_all
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaExportFuncAllHandler(this));

  // For #pragma rs export_type
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaExportTypeHandler(this));

  // For #pragma rs java_package_name
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaJavaPackageNameHandler(this));

  // For #pragma rs set_reflect_license
  PP.AddPragmaHandler(
      "rs", RSPragmaHandler::CreatePragmaReflectLicenseHandler(this));

  // Prepare target data
  mTargetData = new llvm::TargetData(Target.getTargetDescription());

  return;
}

bool RSContext::processExportVar(const clang::VarDecl *VD) {
  assert(!VD->getName().empty() && "Variable name should not be empty");

  // TODO(zonr): some check on variable

  RSExportType *ET = RSExportType::CreateFromDecl(this, VD);
  if (!ET)
    return false;

  RSExportVar *EV = new RSExportVar(this, VD, ET);
  if (EV == NULL)
    return false;
  else
    mExportVars.push_back(EV);

  return true;
}

bool RSContext::processExportFunc(const clang::FunctionDecl *FD) {
  assert(!FD->getName().empty() && "Function name should not be empty");

  if (!FD->isThisDeclarationADefinition())
    return false;

  if (FD->getStorageClass() != clang::SC_None) {
    fprintf(stderr, "RSContext::processExportFunc : cannot export extern or "
                    "static function '%s'\n", FD->getName().str().c_str());
    return false;
  }

  RSExportFunc *EF = RSExportFunc::Create(this, FD);
  if (EF == NULL)
    return false;
  else
    mExportFuncs.push_back(EF);

  return true;
}


bool RSContext::processExportType(const llvm::StringRef &Name) {
  clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl();

  assert(TUDecl != NULL && "Translation unit declaration (top-level "
                           "declaration) is null object");

  const clang::IdentifierInfo *II = mPP.getIdentifierInfo(Name);
  if (II == NULL)
    // TODO(zonr): alert identifier @Name mark as an exportable type cannot be
    //             found
    return false;

  clang::DeclContext::lookup_const_result R = TUDecl->lookup(II);
  RSExportType *ET = NULL;

  RSExportPointerType::IntegerType = mCtx.IntTy.getTypePtr();

  for (clang::DeclContext::lookup_const_iterator I = R.first, E = R.second;
       I != E;
       I++) {
    clang::NamedDecl *const ND = *I;
    const clang::Type *T = NULL;

    switch (ND->getKind()) {
      case clang::Decl::Typedef: {
        T = static_cast<const clang::TypedefDecl*>(
            ND)->getCanonicalDecl()->getUnderlyingType().getTypePtr();
        break;
      }
      case clang::Decl::Record: {
        T = static_cast<const clang::RecordDecl*>(ND)->getTypeForDecl();
        break;
      }
      default: {
        // unsupported, skip
        break;
      }
    }

    if (T != NULL)
      ET = RSExportType::Create(this, T);
  }

  RSExportPointerType::IntegerType = NULL;

  return (ET != NULL);
}

void RSContext::processExport() {
  if (mNeedExportVars.empty() &&
      mNeedExportFuncs.empty() &&
      !mExportAllNonStaticVars && !mExportAllNonStaticFuncs) {
    fprintf(stderr, "note: No reflection because there are no #pragma "
                    "rs export_var(...), #pragma rs export_func(...), "
                    "#pragma rs export_var_all, or #pragma rs "
                    "export_func_all\n");
  }

  // Export variable
  clang::TranslationUnitDecl *TUDecl = mCtx.getTranslationUnitDecl();
  for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
           DE = TUDecl->decls_end();
       DI != TUDecl->decls_end();
       DI++) {
    if (DI->getKind() == clang::Decl::Var) {
      clang::VarDecl *VD = (clang::VarDecl*) (*DI);
      if (mExportAllNonStaticVars &&
          (VD->getLinkage() == clang::ExternalLinkage)) {
        if (!processExportVar(VD)) {
          fprintf(stderr, "RSContext::processExport : failed to export var "
                          "'%s'\n", VD->getNameAsString().c_str());
        }
      } else {
        NeedExportVarSet::iterator EI = mNeedExportVars.find(VD->getName());
        if (EI != mNeedExportVars.end() && processExportVar(VD)) {
          mNeedExportVars.erase(EI);
        }
      }
    } else if (DI->getKind() == clang::Decl::Function) {
      // Export functions
      clang::FunctionDecl *FD = (clang::FunctionDecl*) (*DI);
      if (mExportAllNonStaticFuncs &&
          (FD->getLinkage() == clang::ExternalLinkage)) {
        if (!processExportFunc(FD)) {
          fprintf(stderr, "RSContext::processExport : failed to export func "
                          "'%s'\n", FD->getNameAsString().c_str());
        }
      } else {
        NeedExportFuncSet::iterator EI = mNeedExportFuncs.find(FD->getName());
        if (EI != mNeedExportFuncs.end() && processExportFunc(FD))
          mNeedExportFuncs.erase(EI);
      }
    }
  }

  // Finally, export type forcely set to be exported by user
  for (NeedExportTypeSet::const_iterator EI = mNeedExportTypes.begin(),
           EE = mNeedExportTypes.end();
       EI != EE;
       EI++) {
    if (!processExportType(EI->getKey())) {
      fprintf(stderr, "RSContext::processExport : failed to export type "
              "'%s'\n", EI->getKey().str().c_str());
    }
  }

  return;
}

bool RSContext::insertExportType(const llvm::StringRef &TypeName,
                                 RSExportType *ET) {
  ExportTypeMap::value_type *NewItem =
      ExportTypeMap::value_type::Create(TypeName.begin(),
                                        TypeName.end(),
                                        mExportTypes.getAllocator(),
                                        ET);

  if (mExportTypes.insert(NewItem)) {
    return true;
  } else {
    free(NewItem);
    return false;
  }
}

bool RSContext::reflectToJava(const std::string &OutputPathBase,
                              const std::string &OutputPackageName,
                              const std::string &InputFileName,
                              const std::string &OutputBCFileName,
                              std::string *RealPackageName) {
  if (RealPackageName != NULL)
    RealPackageName->clear();

  const std::string &PackageName =
      ((OutputPackageName.empty()) ? mReflectJavaPackageName :
                                     OutputPackageName);
  if (PackageName.empty())
    // no package name, just return
    return true;

  // Copy back the really applied package name
  RealPackageName->assign(PackageName);

  RSReflection *R = new RSReflection(this);
  bool ret = R->reflect(OutputPathBase, PackageName,
                        InputFileName, OutputBCFileName);
  if (!ret)
    fprintf(stderr, "RSContext::reflectToJava : failed to do reflection "
                    "(%s)\n", R->getLastError());
  delete R;
  return ret;
}

RSContext::~RSContext() {
  delete mLicenseNote;
  delete mTargetData;
  for (ExportableList::iterator I = mExportables.begin(),
          E = mExportables.end();
       I != E;
       I++) {
    if (!(*I)->isKeep())
      delete *I;
  }
}

}  // namespace slang