slang_rs.cpp revision c383a500aa59423264811be3874461bf8adbfea0
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#include "slang_rs.h"
18
19#include <cstring>
20
21#include "clang/Sema/SemaDiagnostic.h"
22
23#include "slang_rs_backend.h"
24#include "slang_rs_context.h"
25
26using namespace slang;
27
28#define RS_HEADER_SUFFIX  "rsh"
29
30#define ENUM_RS_HEADER()  \
31  RS_HEADER_ENTRY(rs_types) \
32  RS_HEADER_ENTRY(rs_cl) \
33  RS_HEADER_ENTRY(rs_core) \
34  RS_HEADER_ENTRY(rs_math)
35
36#define RS_HEADER_ENTRY(x)  \
37  extern const char x ## _header[]; \
38  extern unsigned x ## _header_size;
39ENUM_RS_HEADER()
40#undef RS_HEADER_ENTRY
41
42void SlangRS::initDiagnostic() {
43  clang::Diagnostic &Diag = getDiagnostics();
44  if (!Diag.setDiagnosticGroupMapping(
45        "implicit-function-declaration", clang::diag::MAP_ERROR))
46    assert(false && "Unable to find option group "
47        "implicit-function-declaration");
48  Diag.setDiagnosticMapping(
49      clang::diag::ext_typecheck_convert_discards_qualifiers,
50      clang::diag::MAP_ERROR);
51  return;
52}
53
54void SlangRS::initPreprocessor() {
55  clang::Preprocessor &PP = getPreprocessor();
56
57  std::string RSH;
58#define RS_HEADER_ENTRY(x)  \
59  RSH.append("#line 1 \"" #x "."RS_HEADER_SUFFIX"\"\n"); \
60  RSH.append(x ## _header, x ## _header_size);
61ENUM_RS_HEADER()
62#undef RS_HEADER_ENTRY
63  PP.setPredefines(RSH);
64
65  return;
66}
67
68void SlangRS::initASTContext() {
69  mRSContext = new RSContext(&getPreprocessor(),
70                             &getASTContext(),
71                             &getTargetInfo());
72  return;
73}
74
75clang::ASTConsumer
76*SlangRS::createBackend(const clang::CodeGenOptions& CodeGenOpts,
77                        llvm::raw_ostream *OS,
78                        Slang::OutputType OT) {
79    return new RSBackend(mRSContext,
80                         getDiagnostics(),
81                         CodeGenOpts,
82                         getTargetOptions(),
83                         mPragmas,
84                         OS,
85                         OT,
86                         getSourceManager(),
87                         mAllowRSPrefix);
88}
89
90bool SlangRS::IsRSHeaderFile(const char *File) {
91#define RS_HEADER_ENTRY(x)  \
92  if (::strcmp(File, #x "."RS_HEADER_SUFFIX) == 0)  \
93    return true;
94ENUM_RS_HEADER()
95#undef RS_HEADER_ENTRY
96  // Deal with rs_graphics.rsh special case
97  if (::strcmp(File, "rs_graphics."RS_HEADER_SUFFIX) == 0)
98    return true;
99  return false;
100}
101
102SlangRS::SlangRS(const std::string &Triple, const std::string &CPU,
103                 const std::vector<std::string> &Features)
104    : Slang(Triple, CPU, Features),
105      mRSContext(NULL),
106      mAllowRSPrefix(false) {
107  return;
108}
109
110bool SlangRS::reflectToJava(const std::string &OutputPathBase,
111                            const std::string &OutputPackageName,
112                            std::string *RealPackageName) {
113  if (mRSContext)
114    return mRSContext->reflectToJava(OutputPathBase,
115                                     OutputPackageName,
116                                     getInputFileName(),
117                                     getOutputFileName(),
118                                     RealPackageName);
119  else
120    return false;
121}
122
123void SlangRS::reset() {
124  Slang::reset();
125  delete mRSContext;
126  mRSContext = NULL;
127  return;
128}
129
130SlangRS::~SlangRS() {
131  delete mRSContext;
132  return;
133}
134