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 :
21 : #include "sal/config.h"
22 :
23 : #include "codemaker/codemaker.hxx"
24 :
25 : #include "codemaker/options.hxx"
26 : #include "codemaker/typemanager.hxx"
27 : #include "codemaker/unotype.hxx"
28 :
29 : #include "osl/diagnose.h"
30 : #include "registry/reader.hxx"
31 : #include "registry/types.h"
32 : #include "rtl/strbuf.h"
33 : #include "rtl/string.h"
34 : #include "rtl/string.hxx"
35 : #include "rtl/ustring.hxx"
36 : #include "sal/types.h"
37 :
38 : #include <vector>
39 :
40 : namespace {
41 :
42 28290 : void checkNoTypeArguments(std::vector< rtl::OString > const & arguments) {
43 28290 : if (!arguments.empty()) {
44 : throw CannotDumpException(
45 0 : rtl::OString(RTL_CONSTASCII_STRINGPARAM("Bad type information")));
46 : //TODO
47 : }
48 28290 : }
49 :
50 : }
51 :
52 : namespace codemaker {
53 :
54 52293 : rtl::OString convertString(rtl::OUString const & string) {
55 52293 : rtl::OString s;
56 52293 : if (!string.convertToString(
57 : &s, RTL_TEXTENCODING_UTF8,
58 : (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
59 52293 : | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
60 : {
61 : throw CannotDumpException(
62 : rtl::OString(
63 : RTL_CONSTASCII_STRINGPARAM(
64 0 : "Failure converting string from UTF-16 to UTF-8")));
65 : }
66 52293 : return s;
67 : }
68 :
69 0 : rtl::OString errorMsg(rtl::OString const & desc, rtl::OString const & type) {
70 0 : rtl::OStringBuffer msg(128);
71 0 : msg.append(desc);
72 0 : msg.append(type);
73 0 : return msg.makeStringAndClear();
74 : }
75 :
76 30250 : codemaker::UnoType::Sort decomposeAndResolve(
77 : TypeManager const & manager, rtl::OString const & type,
78 : bool resolveTypedefs, bool allowVoid, bool allowExtraEntities,
79 : RTTypeClass * typeClass, rtl::OString * name, sal_Int32 * rank,
80 : std::vector< rtl::OString > * arguments)
81 : {
82 : OSL_ASSERT(typeClass != 0 && name != 0 && rank != 0 && arguments != 0);
83 30250 : *rank = 0;
84 30517 : for (rtl::OString t(type);;) {
85 30517 : sal_Int32 n = 0;
86 30517 : *name = codemaker::UnoType::decompose(t, &n, arguments);
87 30517 : if (n > SAL_MAX_INT32 - *rank) {
88 : throw CannotDumpException(
89 : errorMsg(rtl::OString(
90 : RTL_CONSTASCII_STRINGPARAM("Bad type information: ")),
91 0 : type));
92 : //TODO
93 : }
94 30517 : *rank += n;
95 30517 : if (n > 0) {
96 1627 : allowVoid = false;
97 1627 : allowExtraEntities = false;
98 : }
99 30517 : codemaker::UnoType::Sort sort = codemaker::UnoType::getSort(*name);
100 30517 : switch (sort) {
101 : case codemaker::UnoType::SORT_VOID:
102 3382 : if (!allowVoid) {
103 : throw CannotDumpException(
104 : errorMsg(rtl::OString(
105 : RTL_CONSTASCII_STRINGPARAM("Bad type information: ")),
106 0 : type));
107 : //TODO
108 : }
109 : default:
110 23336 : checkNoTypeArguments(*arguments);
111 23336 : *typeClass = RT_TYPE_INVALID;
112 23336 : return sort;
113 :
114 : case codemaker::UnoType::SORT_COMPLEX:
115 7181 : typereg::Reader reader(manager.getTypeReader(*name));
116 7181 : *typeClass = reader.getTypeClass();
117 7181 : switch (*typeClass) {
118 : case RT_TYPE_ENUM:
119 : case RT_TYPE_INTERFACE:
120 4687 : checkNoTypeArguments(*arguments);
121 4687 : return sort;
122 :
123 : case RT_TYPE_STRUCT:
124 8908 : if (!(allowExtraEntities && arguments->empty())
125 2227 : && (arguments->size() > SAL_MAX_UINT16
126 2227 : || (static_cast< sal_uInt16 >(arguments->size())
127 2227 : != reader.getReferenceCount())))
128 : {
129 : throw CannotDumpException(
130 : errorMsg(rtl::OString(
131 : RTL_CONSTASCII_STRINGPARAM("Bad type information: ")),
132 0 : type));
133 : //TODO
134 : }
135 2227 : return sort;
136 :
137 : case RT_TYPE_MODULE:
138 : case RT_TYPE_EXCEPTION:
139 : case RT_TYPE_SERVICE:
140 : case RT_TYPE_SINGLETON:
141 : case RT_TYPE_CONSTANTS:
142 0 : if (!allowExtraEntities) {
143 : throw CannotDumpException(
144 : errorMsg(rtl::OString(
145 : RTL_CONSTASCII_STRINGPARAM("Bad type information: ")),
146 0 : type));
147 : //TODO
148 : }
149 0 : checkNoTypeArguments(*arguments);
150 : //TODO: check reader for consistency
151 0 : return sort;
152 :
153 : case RT_TYPE_TYPEDEF:
154 267 : checkNoTypeArguments(*arguments);
155 1068 : if (reader.getSuperTypeCount() == 1
156 267 : && reader.getFieldCount() == 0
157 267 : && reader.getMethodCount() == 0
158 267 : && reader.getReferenceCount() == 0)
159 : {
160 267 : if (resolveTypedefs) {
161 267 : t = convertString(reader.getSuperTypeName(0));
162 267 : continue;
163 : } else {
164 0 : return sort;
165 : }
166 : }
167 : default:
168 : throw CannotDumpException(
169 : errorMsg(rtl::OString(
170 : RTL_CONSTASCII_STRINGPARAM("Bad type information: ")),
171 0 : type));
172 : //TODO
173 7181 : }
174 : }
175 30250 : }
176 : }
177 :
178 : }
179 :
180 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|