1*ff7655f0SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ff7655f0SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ff7655f0SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ff7655f0SAndrew Rist * distributed with this work for additional information 6*ff7655f0SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ff7655f0SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ff7655f0SAndrew Rist * "License"); you may not use this file except in compliance 9*ff7655f0SAndrew Rist * with the License. You may obtain a copy of the License at 10*ff7655f0SAndrew Rist * 11*ff7655f0SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ff7655f0SAndrew Rist * 13*ff7655f0SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ff7655f0SAndrew Rist * software distributed under the License is distributed on an 15*ff7655f0SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ff7655f0SAndrew Rist * KIND, either express or implied. See the License for the 17*ff7655f0SAndrew Rist * specific language governing permissions and limitations 18*ff7655f0SAndrew Rist * under the License. 19*ff7655f0SAndrew Rist * 20*ff7655f0SAndrew Rist *************************************************************/ 21*ff7655f0SAndrew Rist 22*ff7655f0SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_codemaker.hxx" 26cdf0e10cSrcweir #include "sal/config.h" 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include "codemaker/commoncpp.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include "codemaker/options.hxx" 31cdf0e10cSrcweir #include "codemaker/typemanager.hxx" 32cdf0e10cSrcweir #include "codemaker/unotype.hxx" 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include "osl/diagnose.h" 35cdf0e10cSrcweir #include "registry/reader.hxx" 36cdf0e10cSrcweir #include "registry/types.h" 37cdf0e10cSrcweir #include "rtl/strbuf.hxx" 38cdf0e10cSrcweir #include "rtl/string.hxx" 39cdf0e10cSrcweir #include "rtl/ustring.hxx" 40cdf0e10cSrcweir #include "sal/types.h" 41cdf0e10cSrcweir 42cdf0e10cSrcweir #include <vector> 43cdf0e10cSrcweir 44cdf0e10cSrcweir namespace codemaker { namespace cpp { 45cdf0e10cSrcweir 46cdf0e10cSrcweir rtl::OString typeToPrefix(TypeManager const & manager, rtl::OString const & type) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir RTTypeClass typeclass = manager.getTypeClass(type); 49cdf0e10cSrcweir if (typeclass == RT_TYPE_INVALID || 50cdf0e10cSrcweir typeclass == RT_TYPE_PUBLISHED) 51cdf0e10cSrcweir return rtl::OString("_"); 52cdf0e10cSrcweir 53cdf0e10cSrcweir static char const * const typeclassPrefix[RT_TYPE_UNION + 1] = { 54cdf0e10cSrcweir "invalid", /* RT_TYPE_INVALID, is here only as placeholder */ 55cdf0e10cSrcweir "interface", /* RT_TYPE_INTERFACE */ 56cdf0e10cSrcweir "module", /* RT_TYPE_MODULE */ 57cdf0e10cSrcweir "struct", /* RT_TYPE_STRUCT */ 58cdf0e10cSrcweir "enum", /* RT_TYPE_ENUM */ 59cdf0e10cSrcweir "exception", /* RT_TYPE_EXCEPTION */ 60cdf0e10cSrcweir "typedef", /* RT_TYPE_TYPEDEF */ 61cdf0e10cSrcweir "service", /* RT_TYPE_SERVICE */ 62cdf0e10cSrcweir "singleton", /* RT_TYPE_SINGLETON */ 63cdf0e10cSrcweir "object", /* RT_TYPE_OBJECT */ 64cdf0e10cSrcweir "constants", /* RT_TYPE_CONSTANTS */ 65cdf0e10cSrcweir "union" /* RT_TYPE_UNION */ 66cdf0e10cSrcweir }; 67cdf0e10cSrcweir 68cdf0e10cSrcweir return rtl::OString(typeclassPrefix[typeclass]); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir rtl::OString scopedCppName(rtl::OString const & type, bool bNoNameSpace, 72cdf0e10cSrcweir bool shortname) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir char c('/'); 75cdf0e10cSrcweir sal_Int32 nPos = type.lastIndexOf( c ); 76cdf0e10cSrcweir if (nPos == -1) { 77cdf0e10cSrcweir nPos = type.lastIndexOf( '.' ); 78cdf0e10cSrcweir if (nPos == -1) 79cdf0e10cSrcweir return type; 80cdf0e10cSrcweir 81cdf0e10cSrcweir c = '.'; 82cdf0e10cSrcweir } 83cdf0e10cSrcweir if (bNoNameSpace) 84cdf0e10cSrcweir return type.copy(nPos+1); 85cdf0e10cSrcweir 86cdf0e10cSrcweir rtl::OStringBuffer tmpBuf(type.getLength()*2); 87cdf0e10cSrcweir nPos = 0; 88cdf0e10cSrcweir do 89cdf0e10cSrcweir { 90cdf0e10cSrcweir tmpBuf.append("::"); 91cdf0e10cSrcweir tmpBuf.append(type.getToken(0, c, nPos)); 92cdf0e10cSrcweir } while( nPos != -1 ); 93cdf0e10cSrcweir 94cdf0e10cSrcweir if (shortname) { 95cdf0e10cSrcweir rtl::OString s(tmpBuf.makeStringAndClear()); 96cdf0e10cSrcweir if (s.indexOf("::com::sun::star") == 0) 97cdf0e10cSrcweir return s.replaceAt(0, 16, "css"); 98cdf0e10cSrcweir else 99cdf0e10cSrcweir return s; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir return tmpBuf.makeStringAndClear(); 103cdf0e10cSrcweir } 104cdf0e10cSrcweir 105cdf0e10cSrcweir 106cdf0e10cSrcweir rtl::OString translateUnoToCppType( 107cdf0e10cSrcweir codemaker::UnoType::Sort sort, RTTypeClass typeClass, 108cdf0e10cSrcweir rtl::OString const & nucleus, bool shortname) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir rtl::OStringBuffer buf; 111cdf0e10cSrcweir if (sort == codemaker::UnoType::SORT_COMPLEX) { 112cdf0e10cSrcweir if (typeClass == RT_TYPE_INTERFACE 113cdf0e10cSrcweir && nucleus == rtl::OString("com/sun/star/uno/XInterface")) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir buf.append(RTL_CONSTASCII_STRINGPARAM("::com::sun::star::uno::XInterface")); 116cdf0e10cSrcweir } else { 117cdf0e10cSrcweir //TODO: check that nucleus is a valid (UTF-8) identifier 118cdf0e10cSrcweir buf.append(nucleus); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir } else { 121cdf0e10cSrcweir static char const * const cppTypes[codemaker::UnoType::SORT_ANY + 1] = { 122cdf0e10cSrcweir "void", "::sal_Bool", "::sal_Int8", "::sal_Int16", "::sal_uInt16", 123cdf0e10cSrcweir "::sal_Int32", "::sal_uInt32", "::sal_Int64", "::sal_uInt64", 124cdf0e10cSrcweir "float", "double", "::sal_Unicode", "::rtl::OUString", 125cdf0e10cSrcweir "::com::sun::star::uno::Type", "::com::sun::star::uno::Any" }; 126cdf0e10cSrcweir buf.append(cppTypes[sort]); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir if (shortname) { 130cdf0e10cSrcweir rtl::OString s(buf.makeStringAndClear()); 131cdf0e10cSrcweir if (s.indexOf("::com::sun::star") == 0) 132cdf0e10cSrcweir return s.replaceAt(0, 16, "css"); 133cdf0e10cSrcweir else 134cdf0e10cSrcweir return s; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir return buf.makeStringAndClear(); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir rtl::OString translateUnoToCppIdentifier( 141cdf0e10cSrcweir rtl::OString const & unoIdentifier, rtl::OString const & prefix, 142cdf0e10cSrcweir IdentifierTranslationMode transmode, rtl::OString const * forbidden) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir if (// Keywords: 145cdf0e10cSrcweir unoIdentifier == "asm" 146cdf0e10cSrcweir || unoIdentifier == "auto" 147cdf0e10cSrcweir || unoIdentifier == "bool" 148cdf0e10cSrcweir || unoIdentifier == "break" 149cdf0e10cSrcweir || unoIdentifier == "case" 150cdf0e10cSrcweir || unoIdentifier == "catch" 151cdf0e10cSrcweir || unoIdentifier == "char" 152cdf0e10cSrcweir || unoIdentifier == "class" 153cdf0e10cSrcweir || unoIdentifier == "const" 154cdf0e10cSrcweir /* unoIdentifier == "const_cast" */ 155cdf0e10cSrcweir || unoIdentifier == "continue" 156cdf0e10cSrcweir || unoIdentifier == "default" 157cdf0e10cSrcweir || unoIdentifier == "delete" 158cdf0e10cSrcweir || unoIdentifier == "do" 159cdf0e10cSrcweir || unoIdentifier == "double" 160cdf0e10cSrcweir /* unoIdentifier == "dynamic_cast" */ 161cdf0e10cSrcweir || unoIdentifier == "else" 162cdf0e10cSrcweir || unoIdentifier == "enum" 163cdf0e10cSrcweir || unoIdentifier == "explicit" 164cdf0e10cSrcweir || unoIdentifier == "export" 165cdf0e10cSrcweir || unoIdentifier == "extern" 166cdf0e10cSrcweir || unoIdentifier == "false" 167cdf0e10cSrcweir || unoIdentifier == "float" 168cdf0e10cSrcweir || unoIdentifier == "for" 169cdf0e10cSrcweir || unoIdentifier == "friend" 170cdf0e10cSrcweir || unoIdentifier == "goto" 171cdf0e10cSrcweir || unoIdentifier == "if" 172cdf0e10cSrcweir || unoIdentifier == "inline" 173cdf0e10cSrcweir || unoIdentifier == "int" 174cdf0e10cSrcweir || unoIdentifier == "long" 175cdf0e10cSrcweir || unoIdentifier == "mutable" 176cdf0e10cSrcweir || unoIdentifier == "namespace" 177cdf0e10cSrcweir || unoIdentifier == "new" 178cdf0e10cSrcweir || unoIdentifier == "operator" 179cdf0e10cSrcweir || unoIdentifier == "private" 180cdf0e10cSrcweir || unoIdentifier == "protected" 181cdf0e10cSrcweir || unoIdentifier == "public" 182cdf0e10cSrcweir || unoIdentifier == "register" 183cdf0e10cSrcweir /* unoIdentifier == "reinterpret_cast" */ 184cdf0e10cSrcweir || unoIdentifier == "return" 185cdf0e10cSrcweir || unoIdentifier == "short" 186cdf0e10cSrcweir || unoIdentifier == "signed" 187cdf0e10cSrcweir || unoIdentifier == "sizeof" 188cdf0e10cSrcweir || unoIdentifier == "static" 189cdf0e10cSrcweir /* unoIdentifier == "static_cast" */ 190cdf0e10cSrcweir || unoIdentifier == "struct" 191cdf0e10cSrcweir || unoIdentifier == "switch" 192cdf0e10cSrcweir || unoIdentifier == "template" 193cdf0e10cSrcweir || unoIdentifier == "this" 194cdf0e10cSrcweir || unoIdentifier == "throw" 195cdf0e10cSrcweir || unoIdentifier == "true" 196cdf0e10cSrcweir || unoIdentifier == "try" 197cdf0e10cSrcweir || unoIdentifier == "typedef" 198cdf0e10cSrcweir || unoIdentifier == "typeid" 199cdf0e10cSrcweir || unoIdentifier == "typename" 200cdf0e10cSrcweir || unoIdentifier == "union" 201cdf0e10cSrcweir || unoIdentifier == "unsigned" 202cdf0e10cSrcweir || unoIdentifier == "using" 203cdf0e10cSrcweir || unoIdentifier == "virtual" 204cdf0e10cSrcweir || unoIdentifier == "void" 205cdf0e10cSrcweir || unoIdentifier == "volatile" 206cdf0e10cSrcweir /* unoIdentifier == "wchar_t" */ 207cdf0e10cSrcweir || unoIdentifier == "while" 208cdf0e10cSrcweir // Alternative representations: 209cdf0e10cSrcweir || unoIdentifier == "and" 210cdf0e10cSrcweir /* unoIdentifier == "and_eq" */ 211cdf0e10cSrcweir || unoIdentifier == "bitand" 212cdf0e10cSrcweir || unoIdentifier == "bitor" 213cdf0e10cSrcweir || unoIdentifier == "compl" 214cdf0e10cSrcweir || unoIdentifier == "not" 215cdf0e10cSrcweir /* unoIdentifier == "not_eq" */ 216cdf0e10cSrcweir || unoIdentifier == "or" 217cdf0e10cSrcweir /* unoIdentifier == "or_eq" */ 218cdf0e10cSrcweir || unoIdentifier == "xor" 219cdf0e10cSrcweir /* unoIdentifier == "xor_eq" */ 220cdf0e10cSrcweir // Standard macros: 221cdf0e10cSrcweir || (transmode != ITM_KEYWORDSONLY 222cdf0e10cSrcweir && (unoIdentifier == "BUFSIZ" 223cdf0e10cSrcweir || unoIdentifier == "CLOCKS_PER_SEC" 224cdf0e10cSrcweir || unoIdentifier == "EDOM" 225cdf0e10cSrcweir || unoIdentifier == "EOF" 226cdf0e10cSrcweir || unoIdentifier == "ERANGE" 227cdf0e10cSrcweir || unoIdentifier == "EXIT_FAILURE" 228cdf0e10cSrcweir || unoIdentifier == "EXIT_SUCCESS" 229cdf0e10cSrcweir || unoIdentifier == "FILENAME_MAX" 230cdf0e10cSrcweir || unoIdentifier == "FOPEN_MAX" 231cdf0e10cSrcweir || unoIdentifier == "HUGE_VAL" 232cdf0e10cSrcweir || unoIdentifier == "LC_ALL" 233cdf0e10cSrcweir || unoIdentifier == "LC_COLLATE" 234cdf0e10cSrcweir || unoIdentifier == "LC_CTYPE" 235cdf0e10cSrcweir || unoIdentifier == "LC_MONETARY" 236cdf0e10cSrcweir || unoIdentifier == "LC_NUMERIC" 237cdf0e10cSrcweir || unoIdentifier == "LC_TIME" 238cdf0e10cSrcweir || unoIdentifier == "L_tmpnam" 239cdf0e10cSrcweir || unoIdentifier == "MB_CUR_MAX" 240cdf0e10cSrcweir || unoIdentifier == "NULL" 241cdf0e10cSrcweir || unoIdentifier == "RAND_MAX" 242cdf0e10cSrcweir || unoIdentifier == "SEEK_CUR" 243cdf0e10cSrcweir || unoIdentifier == "SEEK_END" 244cdf0e10cSrcweir || unoIdentifier == "SEEK_SET" 245cdf0e10cSrcweir || unoIdentifier == "SIGABRT" 246cdf0e10cSrcweir || unoIdentifier == "SIGFPE" 247cdf0e10cSrcweir || unoIdentifier == "SIGILL" 248cdf0e10cSrcweir || unoIdentifier == "SIGINT" 249cdf0e10cSrcweir || unoIdentifier == "SIGSEGV" 250cdf0e10cSrcweir || unoIdentifier == "SIGTERM" 251cdf0e10cSrcweir || unoIdentifier == "SIG_DFL" 252cdf0e10cSrcweir || unoIdentifier == "SIG_ERR" 253cdf0e10cSrcweir || unoIdentifier == "SIG_IGN" 254cdf0e10cSrcweir || unoIdentifier == "TMP_MAX" 255cdf0e10cSrcweir || unoIdentifier == "WCHAR_MAX" 256cdf0e10cSrcweir || unoIdentifier == "WCHAR_MIN" 257cdf0e10cSrcweir || unoIdentifier == "WEOF" 258cdf0e10cSrcweir /* unoIdentifier == "_IOFBF" */ 259cdf0e10cSrcweir /* unoIdentifier == "_IOLBF" */ 260cdf0e10cSrcweir /* unoIdentifier == "_IONBF" */ 261cdf0e10cSrcweir || unoIdentifier == "assert" 262cdf0e10cSrcweir || unoIdentifier == "errno" 263cdf0e10cSrcweir || unoIdentifier == "offsetof" 264cdf0e10cSrcweir || unoIdentifier == "setjmp" 265cdf0e10cSrcweir || unoIdentifier == "stderr" 266cdf0e10cSrcweir || unoIdentifier == "stdin" 267cdf0e10cSrcweir || unoIdentifier == "stdout" 268cdf0e10cSrcweir /* unoIdentifier == "va_arg" */ 269cdf0e10cSrcweir /* unoIdentifier == "va_end" */ 270cdf0e10cSrcweir /* unoIdentifier == "va_start" */ 271cdf0e10cSrcweir // Standard values: 272cdf0e10cSrcweir || unoIdentifier == "CHAR_BIT" 273cdf0e10cSrcweir || unoIdentifier == "CHAR_MAX" 274cdf0e10cSrcweir || unoIdentifier == "CHAR_MIN" 275cdf0e10cSrcweir || unoIdentifier == "DBL_DIG" 276cdf0e10cSrcweir || unoIdentifier == "DBL_EPSILON" 277cdf0e10cSrcweir || unoIdentifier == "DBL_MANT_DIG" 278cdf0e10cSrcweir || unoIdentifier == "DBL_MAX" 279cdf0e10cSrcweir || unoIdentifier == "DBL_MAX_10_EXP" 280cdf0e10cSrcweir || unoIdentifier == "DBL_MAX_EXP" 281cdf0e10cSrcweir || unoIdentifier == "DBL_MIN" 282cdf0e10cSrcweir || unoIdentifier == "DBL_MIN_10_EXP" 283cdf0e10cSrcweir || unoIdentifier == "DBL_MIN_EXP" 284cdf0e10cSrcweir || unoIdentifier == "FLT_DIG" 285cdf0e10cSrcweir || unoIdentifier == "FLT_EPSILON" 286cdf0e10cSrcweir || unoIdentifier == "FLT_MANT_DIG" 287cdf0e10cSrcweir || unoIdentifier == "FLT_MAX" 288cdf0e10cSrcweir || unoIdentifier == "FLT_MAX_10_EXP" 289cdf0e10cSrcweir || unoIdentifier == "FLT_MAX_EXP" 290cdf0e10cSrcweir || unoIdentifier == "FLT_MIN" 291cdf0e10cSrcweir || unoIdentifier == "FLT_MIN_10_EXP" 292cdf0e10cSrcweir || unoIdentifier == "FLT_MIN_EXP" 293cdf0e10cSrcweir || unoIdentifier == "FLT_RADIX" 294cdf0e10cSrcweir || unoIdentifier == "FLT_ROUNDS" 295cdf0e10cSrcweir || unoIdentifier == "INT_MAX" 296cdf0e10cSrcweir || unoIdentifier == "INT_MIN" 297cdf0e10cSrcweir || unoIdentifier == "LDBL_DIG" 298cdf0e10cSrcweir || unoIdentifier == "LDBL_EPSILON" 299cdf0e10cSrcweir || unoIdentifier == "LDBL_MANT_DIG" 300cdf0e10cSrcweir || unoIdentifier == "LDBL_MAX" 301cdf0e10cSrcweir || unoIdentifier == "LDBL_MAX_10_EXP" 302cdf0e10cSrcweir || unoIdentifier == "LDBL_MAX_EXP" 303cdf0e10cSrcweir || unoIdentifier == "LDBL_MIN" 304cdf0e10cSrcweir || unoIdentifier == "LDBL_MIN_10_EXP" 305cdf0e10cSrcweir || unoIdentifier == "LDBL_MIN_EXP" 306cdf0e10cSrcweir || unoIdentifier == "LONG_MAX" 307cdf0e10cSrcweir || unoIdentifier == "LONG_MIN" 308cdf0e10cSrcweir || unoIdentifier == "MB_LEN_MAX" 309cdf0e10cSrcweir || unoIdentifier == "SCHAR_MAX" 310cdf0e10cSrcweir || unoIdentifier == "SCHAR_MIN" 311cdf0e10cSrcweir || unoIdentifier == "SHRT_MAX" 312cdf0e10cSrcweir || unoIdentifier == "SHRT_MIN" 313cdf0e10cSrcweir || unoIdentifier == "UCHAR_MAX" 314cdf0e10cSrcweir || unoIdentifier == "UINT_MAX" 315cdf0e10cSrcweir || unoIdentifier == "ULONG_MAX" 316cdf0e10cSrcweir || unoIdentifier == "USHRT_MAX")) 317cdf0e10cSrcweir || (transmode == ITM_GLOBAL 318cdf0e10cSrcweir && (// Standard types: 319cdf0e10cSrcweir /* unoIdentifier == "clock_t" */ 320cdf0e10cSrcweir /* unoIdentifier == "div_t" */ 321cdf0e10cSrcweir unoIdentifier == "FILE" 322cdf0e10cSrcweir /* unoIdentifier == "fpos_t" */ 323cdf0e10cSrcweir /* unoIdentifier == "jmp_buf" */ 324cdf0e10cSrcweir || unoIdentifier == "lconv" 325cdf0e10cSrcweir /* unoIdentifier == "ldiv_t" */ 326cdf0e10cSrcweir /* unoIdentifier == "mbstate_t" */ 327cdf0e10cSrcweir /* unoIdentifier == "ptrdiff_t" */ 328cdf0e10cSrcweir /* unoIdentifier == "sig_atomic_t" */ 329cdf0e10cSrcweir /* unoIdentifier == "size_t" */ 330cdf0e10cSrcweir /* unoIdentifier == "time_t" */ 331cdf0e10cSrcweir || unoIdentifier == "tm" 332cdf0e10cSrcweir /* unoIdentifier == "va_list" */ 333cdf0e10cSrcweir /* unoIdentifier == "wctrans_t" */ 334cdf0e10cSrcweir /* unoIdentifier == "wctype_t" */ 335cdf0e10cSrcweir /* unoIdentifier == "wint_t" */ 336cdf0e10cSrcweir // Standard namespaces: 337cdf0e10cSrcweir || unoIdentifier == "std")) 338cdf0e10cSrcweir // Others: 339cdf0e10cSrcweir || unoIdentifier == "NDEBUG" 340cdf0e10cSrcweir || (forbidden != 0 && unoIdentifier == *forbidden) ) 341cdf0e10cSrcweir { 342cdf0e10cSrcweir rtl::OStringBuffer buf(prefix); 343cdf0e10cSrcweir buf.append('_'); 344cdf0e10cSrcweir buf.append(unoIdentifier); 345cdf0e10cSrcweir return buf.makeStringAndClear(); 346cdf0e10cSrcweir } else { 347cdf0e10cSrcweir return unoIdentifier; 348cdf0e10cSrcweir } 349cdf0e10cSrcweir } 350cdf0e10cSrcweir 351cdf0e10cSrcweir } } 352