1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <codemaker/commoncpp.hxx>
24 #include <codemaker/options.hxx>
25 #include <codemaker/typemanager.hxx>
26 #include <codemaker/unotype.hxx>
28 #include <rtl/strbuf.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/ustring.hxx>
31 #include <sal/types.h>
32 #include <o3tl/string_view.hxx>
36 namespace codemaker::cpp
{
38 OString
scopedCppName(OString
const & type
, bool ns_alias
)
41 sal_Int32 nPos
= type
.lastIndexOf( c
);
43 nPos
= type
.lastIndexOf( '.' );
50 OStringBuffer
tmpBuf(type
.getLength()*2);
54 tmpBuf
.append(OString::Concat("::") + o3tl::getToken(type
, 0, c
, nPos
));
55 } while( nPos
!= -1 );
57 OString
s(tmpBuf
.makeStringAndClear());
58 std::string_view rest
;
59 if (ns_alias
&& s
.startsWith("::com::sun::star::", &rest
))
61 s
= OString::Concat("::css::") + rest
; // nicer shorthand
67 OString
translateUnoToCppType(
68 codemaker::UnoType::Sort sort
, std::u16string_view nucleus
)
71 if (sort
<= codemaker::UnoType::Sort::Any
) {
72 static char const * const cppTypes
[static_cast<int>(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 buf
.append(cppTypes
[static_cast<int>(sort
)]);
79 if (sort
== codemaker::UnoType::Sort::Interface
80 && nucleus
== u
"com.sun.star.uno.XInterface")
82 buf
.append("::css::uno::XInterface");
84 //TODO: check that nucleus is a valid (UTF-8) identifier
85 buf
.append(u2b(nucleus
));
88 return buf
.makeStringAndClear();
91 OString
translateUnoToCppIdentifier(
92 OString
const & unoIdentifier
, std::string_view prefix
,
93 IdentifierTranslationMode transmode
, OString
const * forbidden
)
96 unoIdentifier
== "asm"
97 || unoIdentifier
== "auto"
98 || unoIdentifier
== "bool"
99 || unoIdentifier
== "break"
100 || unoIdentifier
== "case"
101 || unoIdentifier
== "catch"
102 || unoIdentifier
== "char"
103 || unoIdentifier
== "class"
104 || unoIdentifier
== "const"
105 /* unoIdentifier == "const_cast" */
106 || unoIdentifier
== "continue"
107 || unoIdentifier
== "default"
108 || unoIdentifier
== "delete"
109 || unoIdentifier
== "do"
110 || unoIdentifier
== "double"
111 /* unoIdentifier == "dynamic_cast" */
112 || unoIdentifier
== "else"
113 || unoIdentifier
== "enum"
114 || unoIdentifier
== "explicit"
115 || unoIdentifier
== "export"
116 || unoIdentifier
== "extern"
117 || unoIdentifier
== "false"
118 || unoIdentifier
== "float"
119 || unoIdentifier
== "for"
120 || unoIdentifier
== "friend"
121 || unoIdentifier
== "goto"
122 || unoIdentifier
== "if"
123 || unoIdentifier
== "inline"
124 || unoIdentifier
== "int"
125 || unoIdentifier
== "long"
126 || unoIdentifier
== "mutable"
127 || unoIdentifier
== "namespace"
128 || unoIdentifier
== "new"
129 || unoIdentifier
== "operator"
130 || unoIdentifier
== "private"
131 || unoIdentifier
== "protected"
132 || unoIdentifier
== "public"
133 || unoIdentifier
== "register"
134 /* unoIdentifier == "reinterpret_cast" */
135 || unoIdentifier
== "return"
136 || unoIdentifier
== "short"
137 || unoIdentifier
== "signed"
138 || unoIdentifier
== "sizeof"
139 || unoIdentifier
== "static"
140 /* unoIdentifier == "static_cast" */
141 || unoIdentifier
== "struct"
142 || unoIdentifier
== "switch"
143 || unoIdentifier
== "template"
144 || unoIdentifier
== "this"
145 || unoIdentifier
== "throw"
146 || unoIdentifier
== "true"
147 || unoIdentifier
== "try"
148 || unoIdentifier
== "typedef"
149 || unoIdentifier
== "typeid"
150 || unoIdentifier
== "typename"
151 || unoIdentifier
== "union"
152 || unoIdentifier
== "unsigned"
153 || unoIdentifier
== "using"
154 || unoIdentifier
== "virtual"
155 || unoIdentifier
== "void"
156 || unoIdentifier
== "volatile"
157 /* unoIdentifier == "wchar_t" */
158 || unoIdentifier
== "while"
159 // Alternative representations:
160 || unoIdentifier
== "and"
161 /* unoIdentifier == "and_eq" */
162 || unoIdentifier
== "bitand"
163 || unoIdentifier
== "bitor"
164 || unoIdentifier
== "compl"
165 || unoIdentifier
== "not"
166 /* unoIdentifier == "not_eq" */
167 || unoIdentifier
== "or"
168 /* unoIdentifier == "or_eq" */
169 || unoIdentifier
== "xor"
170 /* unoIdentifier == "xor_eq" */
171 || (transmode
!= IdentifierTranslationMode::KeywordsOnly
173 && ((unoIdentifier
== "BUFSIZ"
174 || unoIdentifier
== "CLOCKS_PER_SEC"
175 || unoIdentifier
== "EDOM"
176 || unoIdentifier
== "EOF"
177 || unoIdentifier
== "ERANGE"
178 || unoIdentifier
== "EXIT_FAILURE"
179 || unoIdentifier
== "EXIT_SUCCESS"
180 || unoIdentifier
== "FILENAME_MAX"
181 || unoIdentifier
== "FOPEN_MAX"
182 || unoIdentifier
== "HUGE_VAL"
183 || unoIdentifier
== "LC_ALL"
184 || unoIdentifier
== "LC_COLLATE"
185 || unoIdentifier
== "LC_CTYPE"
186 || unoIdentifier
== "LC_MONETARY"
187 || unoIdentifier
== "LC_NUMERIC"
188 || unoIdentifier
== "LC_TIME"
189 || unoIdentifier
== "L_tmpnam"
190 || unoIdentifier
== "MB_CUR_MAX"
191 || unoIdentifier
== "NULL"
192 || unoIdentifier
== "RAND_MAX"
193 || unoIdentifier
== "SEEK_CUR"
194 || unoIdentifier
== "SEEK_END"
195 || unoIdentifier
== "SEEK_SET"
196 || unoIdentifier
== "SIGABRT"
197 || unoIdentifier
== "SIGFPE"
198 || unoIdentifier
== "SIGILL"
199 || unoIdentifier
== "SIGINT"
200 || unoIdentifier
== "SIGSEGV"
201 || unoIdentifier
== "SIGTERM"
202 || unoIdentifier
== "SIG_DFL"
203 || unoIdentifier
== "SIG_ERR"
204 || unoIdentifier
== "SIG_IGN"
205 || unoIdentifier
== "TMP_MAX"
206 || unoIdentifier
== "WCHAR_MAX"
207 || unoIdentifier
== "WCHAR_MIN"
208 || unoIdentifier
== "WEOF"
209 /* unoIdentifier == "_IOFBF" */
210 /* unoIdentifier == "_IOLBF" */
211 /* unoIdentifier == "_IONBF" */
212 || unoIdentifier
== "assert"
213 || unoIdentifier
== "errno"
214 || unoIdentifier
== "offsetof"
215 || unoIdentifier
== "setjmp"
216 || unoIdentifier
== "stderr"
217 || unoIdentifier
== "stdin"
218 || unoIdentifier
== "stdout"
219 /* unoIdentifier == "va_arg" */
220 /* unoIdentifier == "va_end" */
221 /* unoIdentifier == "va_start" */
223 || unoIdentifier
== "CHAR_BIT"
224 || unoIdentifier
== "CHAR_MAX"
225 || unoIdentifier
== "CHAR_MIN"
226 || unoIdentifier
== "DBL_DIG"
227 || unoIdentifier
== "DBL_EPSILON"
228 || unoIdentifier
== "DBL_MANT_DIG"
229 || unoIdentifier
== "DBL_MAX"
230 || unoIdentifier
== "DBL_MAX_10_EXP"
231 || unoIdentifier
== "DBL_MAX_EXP"
232 || unoIdentifier
== "DBL_MIN"
233 || unoIdentifier
== "DBL_MIN_10_EXP"
234 || unoIdentifier
== "DBL_MIN_EXP"
235 || unoIdentifier
== "FLT_DIG"
236 || unoIdentifier
== "FLT_EPSILON"
237 || unoIdentifier
== "FLT_MANT_DIG"
238 || unoIdentifier
== "FLT_MAX"
239 || unoIdentifier
== "FLT_MAX_10_EXP"
240 || unoIdentifier
== "FLT_MAX_EXP"
241 || unoIdentifier
== "FLT_MIN"
242 || unoIdentifier
== "FLT_MIN_10_EXP"
243 || unoIdentifier
== "FLT_MIN_EXP"
244 || unoIdentifier
== "FLT_RADIX"
245 || unoIdentifier
== "FLT_ROUNDS"
246 || unoIdentifier
== "INT_MAX"
247 || unoIdentifier
== "INT_MIN"
248 || unoIdentifier
== "LDBL_DIG"
249 || unoIdentifier
== "LDBL_EPSILON"
250 || unoIdentifier
== "LDBL_MANT_DIG"
251 || unoIdentifier
== "LDBL_MAX"
252 || unoIdentifier
== "LDBL_MAX_10_EXP"
253 || unoIdentifier
== "LDBL_MAX_EXP"
254 || unoIdentifier
== "LDBL_MIN"
255 || unoIdentifier
== "LDBL_MIN_10_EXP"
256 || unoIdentifier
== "LDBL_MIN_EXP"
257 || unoIdentifier
== "LONG_MAX"
258 || unoIdentifier
== "LONG_MIN"
259 || unoIdentifier
== "MB_LEN_MAX"
260 || unoIdentifier
== "SCHAR_MAX"
261 || unoIdentifier
== "SCHAR_MIN"
262 || unoIdentifier
== "SHRT_MAX"
263 || unoIdentifier
== "SHRT_MIN"
264 || unoIdentifier
== "UCHAR_MAX"
265 || unoIdentifier
== "UINT_MAX"
266 || unoIdentifier
== "ULONG_MAX"
267 || unoIdentifier
== "USHRT_MAX")
268 || (transmode
== IdentifierTranslationMode::Global
269 && (// Standard types:
270 /* unoIdentifier == "clock_t" */
271 /* unoIdentifier == "div_t" */
272 unoIdentifier
== "FILE"
273 /* unoIdentifier == "fpos_t" */
274 /* unoIdentifier == "jmp_buf" */
275 || 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 || unoIdentifier
== "tm"
283 /* unoIdentifier == "va_list" */
284 /* unoIdentifier == "wctrans_t" */
285 /* unoIdentifier == "wctype_t" */
286 /* unoIdentifier == "wint_t" */
287 // Standard namespaces:
288 || unoIdentifier
== "std"))
290 || unoIdentifier
== "NDEBUG"
291 || (forbidden
!= nullptr && unoIdentifier
== *forbidden
))) )
293 return OString::Concat(prefix
) + "_" + unoIdentifier
;
295 return unoIdentifier
;
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */