Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include "codemaker/commoncpp.hxx"
23 :
24 : #include "codemaker/options.hxx"
25 : #include "codemaker/typemanager.hxx"
26 : #include "codemaker/unotype.hxx"
27 :
28 : #include "osl/diagnose.h"
29 : #include "rtl/strbuf.hxx"
30 : #include "rtl/string.hxx"
31 : #include "rtl/ustring.hxx"
32 : #include "sal/types.h"
33 :
34 : #include <vector>
35 :
36 : namespace codemaker { namespace cpp {
37 :
38 0 : OString scopedCppName(OString const & type, bool ns_alias)
39 : {
40 0 : char c('/');
41 0 : sal_Int32 nPos = type.lastIndexOf( c );
42 0 : if (nPos == -1) {
43 0 : nPos = type.lastIndexOf( '.' );
44 0 : if (nPos == -1)
45 0 : return type;
46 :
47 0 : c = '.';
48 : }
49 :
50 0 : OStringBuffer tmpBuf(type.getLength()*2);
51 0 : nPos = 0;
52 0 : do
53 : {
54 0 : tmpBuf.append("::" + type.getToken(0, c, nPos));
55 0 : } while( nPos != -1 );
56 :
57 0 : OString s(tmpBuf.makeStringAndClear());
58 0 : if (ns_alias && s.startsWith("::com::sun::star::", &s))
59 : {
60 0 : s = "::css::" + s; // nicer shorthand
61 : }
62 :
63 0 : return s;
64 : }
65 :
66 :
67 0 : OString translateUnoToCppType(
68 : codemaker::UnoType::Sort sort, OUString const & nucleus)
69 : {
70 0 : OStringBuffer buf;
71 0 : if (sort <= codemaker::UnoType::SORT_ANY) {
72 : static char const * const cppTypes[codemaker::UnoType::SORT_ANY + 1] = {
73 : "void", "::sal_Bool", "::sal_Int8", "::sal_Int16", "::sal_uInt16",
74 : "::sal_Int32", "::sal_uInt32", "::sal_Int64", "::sal_uInt64",
75 : "float", "double", "::sal_Unicode", "rtl::OUString",
76 : "::css::uno::Type", "::css::uno::Any" };
77 0 : buf.append(cppTypes[sort]);
78 : } else {
79 0 : if (sort == codemaker::UnoType::SORT_INTERFACE_TYPE
80 0 : && nucleus == "com.sun.star.uno.XInterface")
81 : {
82 0 : buf.append("::css::uno::XInterface");
83 : } else {
84 : //TODO: check that nucleus is a valid (UTF-8) identifier
85 0 : buf.append(u2b(nucleus));
86 : }
87 : }
88 0 : return buf.makeStringAndClear();
89 : }
90 :
91 0 : OString translateUnoToCppIdentifier(
92 : OString const & unoIdentifier, OString const & prefix,
93 : IdentifierTranslationMode transmode, OString const * forbidden)
94 : {
95 0 : if (// Keywords:
96 0 : unoIdentifier == "asm"
97 0 : || unoIdentifier == "auto"
98 0 : || unoIdentifier == "bool"
99 0 : || unoIdentifier == "break"
100 0 : || unoIdentifier == "case"
101 0 : || unoIdentifier == "catch"
102 0 : || unoIdentifier == "char"
103 0 : || unoIdentifier == "class"
104 0 : || unoIdentifier == "const"
105 : /* unoIdentifier == "const_cast" */
106 0 : || unoIdentifier == "continue"
107 0 : || unoIdentifier == "default"
108 0 : || unoIdentifier == "delete"
109 0 : || unoIdentifier == "do"
110 0 : || unoIdentifier == "double"
111 : /* unoIdentifier == "dynamic_cast" */
112 0 : || unoIdentifier == "else"
113 0 : || unoIdentifier == "enum"
114 0 : || unoIdentifier == "explicit"
115 0 : || unoIdentifier == "export"
116 0 : || unoIdentifier == "extern"
117 0 : || unoIdentifier == "false"
118 0 : || unoIdentifier == "float"
119 0 : || unoIdentifier == "for"
120 0 : || unoIdentifier == "friend"
121 0 : || unoIdentifier == "goto"
122 0 : || unoIdentifier == "if"
123 0 : || unoIdentifier == "inline"
124 0 : || unoIdentifier == "int"
125 0 : || unoIdentifier == "long"
126 0 : || unoIdentifier == "mutable"
127 0 : || unoIdentifier == "namespace"
128 0 : || unoIdentifier == "new"
129 0 : || unoIdentifier == "operator"
130 0 : || unoIdentifier == "private"
131 0 : || unoIdentifier == "protected"
132 0 : || unoIdentifier == "public"
133 0 : || unoIdentifier == "register"
134 : /* unoIdentifier == "reinterpret_cast" */
135 0 : || unoIdentifier == "return"
136 0 : || unoIdentifier == "short"
137 0 : || unoIdentifier == "signed"
138 0 : || unoIdentifier == "sizeof"
139 0 : || unoIdentifier == "static"
140 : /* unoIdentifier == "static_cast" */
141 0 : || unoIdentifier == "struct"
142 0 : || unoIdentifier == "switch"
143 0 : || unoIdentifier == "template"
144 0 : || unoIdentifier == "this"
145 0 : || unoIdentifier == "throw"
146 0 : || unoIdentifier == "true"
147 0 : || unoIdentifier == "try"
148 0 : || unoIdentifier == "typedef"
149 0 : || unoIdentifier == "typeid"
150 0 : || unoIdentifier == "typename"
151 0 : || unoIdentifier == "union"
152 0 : || unoIdentifier == "unsigned"
153 0 : || unoIdentifier == "using"
154 0 : || unoIdentifier == "virtual"
155 0 : || unoIdentifier == "void"
156 0 : || unoIdentifier == "volatile"
157 : /* unoIdentifier == "wchar_t" */
158 0 : || unoIdentifier == "while"
159 : // Alternative representations:
160 0 : || unoIdentifier == "and"
161 : /* unoIdentifier == "and_eq" */
162 0 : || unoIdentifier == "bitand"
163 0 : || unoIdentifier == "bitor"
164 0 : || unoIdentifier == "compl"
165 0 : || unoIdentifier == "not"
166 : /* unoIdentifier == "not_eq" */
167 0 : || unoIdentifier == "or"
168 : /* unoIdentifier == "or_eq" */
169 0 : || unoIdentifier == "xor"
170 : /* unoIdentifier == "xor_eq" */
171 : // Standard macros:
172 0 : || (transmode != ITM_KEYWORDSONLY
173 0 : && (unoIdentifier == "BUFSIZ"
174 0 : || unoIdentifier == "CLOCKS_PER_SEC"
175 0 : || unoIdentifier == "EDOM"
176 0 : || unoIdentifier == "EOF"
177 0 : || unoIdentifier == "ERANGE"
178 0 : || unoIdentifier == "EXIT_FAILURE"
179 0 : || unoIdentifier == "EXIT_SUCCESS"
180 0 : || unoIdentifier == "FILENAME_MAX"
181 0 : || unoIdentifier == "FOPEN_MAX"
182 0 : || unoIdentifier == "HUGE_VAL"
183 0 : || unoIdentifier == "LC_ALL"
184 0 : || unoIdentifier == "LC_COLLATE"
185 0 : || unoIdentifier == "LC_CTYPE"
186 0 : || unoIdentifier == "LC_MONETARY"
187 0 : || unoIdentifier == "LC_NUMERIC"
188 0 : || unoIdentifier == "LC_TIME"
189 0 : || unoIdentifier == "L_tmpnam"
190 0 : || unoIdentifier == "MB_CUR_MAX"
191 0 : || unoIdentifier == "NULL"
192 0 : || unoIdentifier == "RAND_MAX"
193 0 : || unoIdentifier == "SEEK_CUR"
194 0 : || unoIdentifier == "SEEK_END"
195 0 : || unoIdentifier == "SEEK_SET"
196 0 : || unoIdentifier == "SIGABRT"
197 0 : || unoIdentifier == "SIGFPE"
198 0 : || unoIdentifier == "SIGILL"
199 0 : || unoIdentifier == "SIGINT"
200 0 : || unoIdentifier == "SIGSEGV"
201 0 : || unoIdentifier == "SIGTERM"
202 0 : || unoIdentifier == "SIG_DFL"
203 0 : || unoIdentifier == "SIG_ERR"
204 0 : || unoIdentifier == "SIG_IGN"
205 0 : || unoIdentifier == "TMP_MAX"
206 0 : || unoIdentifier == "WCHAR_MAX"
207 0 : || unoIdentifier == "WCHAR_MIN"
208 0 : || unoIdentifier == "WEOF"
209 : /* unoIdentifier == "_IOFBF" */
210 : /* unoIdentifier == "_IOLBF" */
211 : /* unoIdentifier == "_IONBF" */
212 0 : || unoIdentifier == "assert"
213 0 : || unoIdentifier == "errno"
214 0 : || unoIdentifier == "offsetof"
215 0 : || unoIdentifier == "setjmp"
216 0 : || unoIdentifier == "stderr"
217 0 : || unoIdentifier == "stdin"
218 0 : || unoIdentifier == "stdout"
219 : /* unoIdentifier == "va_arg" */
220 : /* unoIdentifier == "va_end" */
221 : /* unoIdentifier == "va_start" */
222 : // Standard values:
223 0 : || unoIdentifier == "CHAR_BIT"
224 0 : || unoIdentifier == "CHAR_MAX"
225 0 : || unoIdentifier == "CHAR_MIN"
226 0 : || unoIdentifier == "DBL_DIG"
227 0 : || unoIdentifier == "DBL_EPSILON"
228 0 : || unoIdentifier == "DBL_MANT_DIG"
229 0 : || unoIdentifier == "DBL_MAX"
230 0 : || unoIdentifier == "DBL_MAX_10_EXP"
231 0 : || unoIdentifier == "DBL_MAX_EXP"
232 0 : || unoIdentifier == "DBL_MIN"
233 0 : || unoIdentifier == "DBL_MIN_10_EXP"
234 0 : || unoIdentifier == "DBL_MIN_EXP"
235 0 : || unoIdentifier == "FLT_DIG"
236 0 : || unoIdentifier == "FLT_EPSILON"
237 0 : || unoIdentifier == "FLT_MANT_DIG"
238 0 : || unoIdentifier == "FLT_MAX"
239 0 : || unoIdentifier == "FLT_MAX_10_EXP"
240 0 : || unoIdentifier == "FLT_MAX_EXP"
241 0 : || unoIdentifier == "FLT_MIN"
242 0 : || unoIdentifier == "FLT_MIN_10_EXP"
243 0 : || unoIdentifier == "FLT_MIN_EXP"
244 0 : || unoIdentifier == "FLT_RADIX"
245 0 : || unoIdentifier == "FLT_ROUNDS"
246 0 : || unoIdentifier == "INT_MAX"
247 0 : || unoIdentifier == "INT_MIN"
248 0 : || unoIdentifier == "LDBL_DIG"
249 0 : || unoIdentifier == "LDBL_EPSILON"
250 0 : || unoIdentifier == "LDBL_MANT_DIG"
251 0 : || unoIdentifier == "LDBL_MAX"
252 0 : || unoIdentifier == "LDBL_MAX_10_EXP"
253 0 : || unoIdentifier == "LDBL_MAX_EXP"
254 0 : || unoIdentifier == "LDBL_MIN"
255 0 : || unoIdentifier == "LDBL_MIN_10_EXP"
256 0 : || unoIdentifier == "LDBL_MIN_EXP"
257 0 : || unoIdentifier == "LONG_MAX"
258 0 : || unoIdentifier == "LONG_MIN"
259 0 : || unoIdentifier == "MB_LEN_MAX"
260 0 : || unoIdentifier == "SCHAR_MAX"
261 0 : || unoIdentifier == "SCHAR_MIN"
262 0 : || unoIdentifier == "SHRT_MAX"
263 0 : || unoIdentifier == "SHRT_MIN"
264 0 : || unoIdentifier == "UCHAR_MAX"
265 0 : || unoIdentifier == "UINT_MAX"
266 0 : || unoIdentifier == "ULONG_MAX"
267 0 : || unoIdentifier == "USHRT_MAX"))
268 0 : || (transmode == ITM_GLOBAL
269 0 : && (// Standard types:
270 : /* unoIdentifier == "clock_t" */
271 : /* unoIdentifier == "div_t" */
272 0 : unoIdentifier == "FILE"
273 : /* unoIdentifier == "fpos_t" */
274 : /* unoIdentifier == "jmp_buf" */
275 0 : || unoIdentifier == "lconv"
276 : /* unoIdentifier == "ldiv_t" */
277 : /* unoIdentifier == "mbstate_t" */
278 : /* unoIdentifier == "ptrdiff_t" */
279 : /* unoIdentifier == "sig_atomic_t" */
280 : /* unoIdentifier == "size_t" */
281 : /* unoIdentifier == "time_t" */
282 0 : || unoIdentifier == "tm"
283 : /* unoIdentifier == "va_list" */
284 : /* unoIdentifier == "wctrans_t" */
285 : /* unoIdentifier == "wctype_t" */
286 : /* unoIdentifier == "wint_t" */
287 : // Standard namespaces:
288 0 : || unoIdentifier == "std"))
289 : // Others:
290 0 : || unoIdentifier == "NDEBUG"
291 0 : || (forbidden != 0 && unoIdentifier == *forbidden) )
292 : {
293 0 : return prefix + "_" + unoIdentifier;
294 : } else {
295 0 : return unoIdentifier;
296 : }
297 : }
298 :
299 : } }
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|