Branch data 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 <idlc/astunion.hxx>
21 : : #include <idlc/astbasetype.hxx>
22 : : #include <idlc/errorhandler.hxx>
23 : :
24 : : #include "registry/version.h"
25 : : #include "registry/writer.hxx"
26 : :
27 : : using namespace ::rtl;
28 : :
29 : 0 : AstUnion::AstUnion(const ::rtl::OString& name, AstType* pDiscType, AstScope* pScope)
30 : : : AstStruct(NT_union, name, NULL, pScope)
31 : : , m_pDiscriminantType(pDiscType)
32 : 0 : , m_discExprType(ET_long)
33 : : {
34 : : AstBaseType* pBaseType;
35 : :
36 [ # # ]: 0 : if ( !pDiscType )
37 : : {
38 : 0 : m_pDiscriminantType = NULL;
39 : 0 : m_discExprType = ET_none;
40 : 0 : return;
41 : : }
42 : : /*
43 : : * If the discriminator type is a predefined type
44 : : * then install the equivalent coercion target type in
45 : : * the pd_udisc_type field.
46 : : */
47 [ # # ]: 0 : if ( pDiscType->getNodeType() == NT_predefined )
48 : : {
49 : 0 : pBaseType = (AstBaseType*)pDiscType;
50 [ # # ]: 0 : if ( !pBaseType )
51 : : {
52 : 0 : m_pDiscriminantType = NULL;
53 : 0 : m_discExprType = ET_none;
54 : 0 : return;
55 : : }
56 : 0 : m_pDiscriminantType = pDiscType;
57 [ # # ]: 0 : switch (pBaseType->getExprType())
58 : : {
59 : : case ET_long:
60 : : case ET_ulong:
61 : : case ET_short:
62 : : case ET_ushort:
63 : : case ET_char:
64 : : case ET_boolean:
65 : 0 : m_discExprType = pBaseType->getExprType();
66 : 0 : break;
67 : : default:
68 : 0 : m_discExprType = ET_none;
69 : 0 : m_pDiscriminantType = NULL;
70 : 0 : break;
71 : : }
72 : : } else
73 [ # # ]: 0 : if (pDiscType->getNodeType() == NT_enum)
74 : : {
75 : 0 : m_discExprType = ET_any;
76 : 0 : m_pDiscriminantType = pDiscType;
77 : : } else
78 : : {
79 : 0 : m_discExprType = ET_none;
80 : 0 : m_pDiscriminantType = NULL;
81 : : }
82 : :
83 [ # # ]: 0 : if ( !m_pDiscriminantType )
84 [ # # ][ # # ]: 0 : idlc()->error()->error2(EIDL_DISC_TYPE, this, pDiscType);
85 : : }
86 : :
87 : 0 : AstUnion::~AstUnion()
88 : : {
89 [ # # ]: 0 : }
90 : :
91 : 0 : AstDeclaration* AstUnion::addDeclaration(AstDeclaration* pDecl)
92 : : {
93 [ # # ]: 0 : if ( pDecl->getNodeType() == NT_union_branch )
94 : : {
95 : 0 : AstUnionBranch* pBranch = (AstUnionBranch*)pDecl;
96 [ # # ]: 0 : if ( lookupBranch(pBranch) )
97 : : {
98 : 0 : idlc()->error()->error2(EIDL_MULTIPLE_BRANCH, this, pDecl);
99 : 0 : return NULL;
100 : : }
101 : : }
102 : :
103 : 0 : return AstScope::addDeclaration(pDecl);
104 : : }
105 : :
106 : 0 : AstUnionBranch* AstUnion::lookupBranch(AstUnionBranch* pBranch)
107 : : {
108 : 0 : AstUnionLabel* pLabel = NULL;
109 : :
110 [ # # ]: 0 : if ( pBranch )
111 : 0 : pLabel = pBranch->getLabel();
112 : :
113 [ # # ]: 0 : if ( pLabel )
114 : : {
115 [ # # ]: 0 : if (pLabel->getLabelKind() == UL_default)
116 : 0 : return lookupDefault();
117 [ # # ]: 0 : if (m_discExprType == ET_any)
118 : : /* CONVENTION: indicates enum discr */
119 : 0 : return lookupEnum(pBranch);
120 : 0 : return lookupLabel(pBranch);
121 : : }
122 : 0 : return NULL;
123 : : }
124 : :
125 : 0 : AstUnionBranch* AstUnion::lookupDefault(sal_Bool bReportError)
126 : : {
127 : 0 : DeclList::const_iterator iter = getIteratorBegin();
128 : 0 : DeclList::const_iterator end = getIteratorEnd();
129 : 0 : AstUnionBranch *pBranch = NULL;
130 : 0 : AstDeclaration *pDecl = NULL;
131 : :
132 [ # # ]: 0 : while ( iter != end )
133 : : {
134 : 0 : pDecl = *iter;
135 [ # # ]: 0 : if ( pDecl->getNodeType() == NT_union_branch )
136 : : {
137 : 0 : pBranch = (AstUnionBranch*)pDecl;
138 [ # # ]: 0 : if (pBranch == NULL)
139 : : {
140 : 0 : ++iter;
141 : 0 : continue;
142 : : }
143 [ # # # # ]: 0 : if ( pBranch->getLabel() != NULL &&
[ # # ]
144 : 0 : pBranch->getLabel()->getLabelKind() == UL_default)
145 : : {
146 [ # # ]: 0 : if ( bReportError )
147 [ # # ][ # # ]: 0 : idlc()->error()->error2(EIDL_MULTIPLE_BRANCH, this, pBranch);
148 : 0 : return pBranch;
149 : : }
150 : : }
151 : 0 : ++iter;
152 : : }
153 : 0 : return NULL;
154 : : }
155 : :
156 : 0 : AstUnionBranch* AstUnion::lookupLabel(AstUnionBranch* pBranch)
157 : : {
158 : 0 : AstUnionLabel* pLabel = pBranch->getLabel();
159 : :
160 [ # # ]: 0 : if ( !pLabel->getLabelValue() )
161 : 0 : return pBranch;
162 : : // pLabel->getLabelValue()->setExprValue(pLabel->getLabelValue()->coerce(m_discExprType, sal_False));
163 : : AstExprValue* pLabelValue = pLabel->getLabelValue()->coerce(
164 [ # # ]: 0 : m_discExprType, sal_False);
165 [ # # ]: 0 : if ( !pLabelValue )
166 : : {
167 [ # # ][ # # ]: 0 : idlc()->error()->evalError(pLabel->getLabelValue());
168 : 0 : return pBranch;
169 : : } else
170 : : {
171 : 0 : pLabel->getLabelValue()->setExprValue(pLabelValue);
172 : : }
173 : :
174 : 0 : DeclList::const_iterator iter = getIteratorBegin();
175 : 0 : DeclList::const_iterator end = getIteratorEnd();
176 : 0 : AstUnionBranch* pB = NULL;
177 : 0 : AstDeclaration* pDecl = NULL;
178 : :
179 [ # # ]: 0 : while ( iter != end )
180 : : {
181 : 0 : pDecl = *iter;
182 [ # # ]: 0 : if ( pDecl->getNodeType() == NT_union_branch )
183 : : {
184 : 0 : pB = (AstUnionBranch*)pDecl;
185 [ # # ]: 0 : if ( !pB )
186 : : {
187 : 0 : ++iter;
188 : 0 : continue;
189 : : }
190 [ # # # # ]: 0 : if ( pB->getLabel() != NULL &&
[ # # ][ # # ]
191 : 0 : pB->getLabel()->getLabelKind() == UL_label &&
192 [ # # ]: 0 : pB->getLabel()->getLabelValue()->compare(pLabel->getLabelValue()) )
193 : : {
194 [ # # ][ # # ]: 0 : idlc()->error()->error2(EIDL_MULTIPLE_BRANCH, this, pBranch);
195 : 0 : return pBranch;
196 : : }
197 : : }
198 : 0 : ++iter;
199 : : }
200 : 0 : return NULL;
201 : : }
202 : :
203 : 0 : AstUnionBranch* AstUnion::lookupEnum(AstUnionBranch* pBranch)
204 : : {
205 [ # # ]: 0 : AstDeclaration const * pType = resolveTypedefs(m_pDiscriminantType);
206 [ # # ]: 0 : if ( pType->getNodeType() != NT_enum )
207 : 0 : return NULL;
208 : :
209 : 0 : AstUnionLabel* pLabel = pBranch->getLabel();
210 : 0 : AstExpression* pExpr = pLabel->getLabelValue();
211 [ # # ]: 0 : if ( !pExpr )
212 : 0 : return pBranch;
213 : :
214 : : /*
215 : : * Expecting a symbol label
216 : : */
217 [ # # ]: 0 : if ( pExpr->getCombOperator() != EC_symbol)
218 : : {
219 [ # # ][ # # ]: 0 : idlc()->error()->enumValExpected(this);
220 : 0 : return pBranch;
221 : : }
222 : :
223 : : /*
224 : : * See if the symbol defines a constant in the discriminator enum
225 : : */
226 : 0 : AstEnum* pEnum = (AstEnum*)pType;
227 [ # # ]: 0 : AstDeclaration* pDecl = pEnum->lookupByName(*pExpr->getSymbolicName());
228 [ # # ][ # # ]: 0 : if ( pDecl == NULL || pDecl->getScope() != pEnum)
[ # # ][ # # ]
229 : : {
230 [ # # ][ # # ]: 0 : idlc()->error()->enumValLookupFailure(this, pEnum, *pExpr->getSymbolicName());
231 : 0 : return pBranch;
232 : : }
233 : :
234 : :
235 : 0 : DeclList::const_iterator iter = getIteratorBegin();
236 : 0 : DeclList::const_iterator end = getIteratorEnd();
237 : 0 : AstUnionBranch* pB = NULL;
238 : 0 : pDecl = NULL;
239 : :
240 [ # # ]: 0 : while ( iter != end )
241 : : {
242 : 0 : pDecl = *iter;
243 [ # # ]: 0 : if ( pDecl->getNodeType() == NT_union_branch )
244 : : {
245 : 0 : pB = (AstUnionBranch*)pDecl;
246 [ # # ]: 0 : if ( !pB )
247 : : {
248 : 0 : ++iter;
249 : 0 : continue;
250 : : }
251 [ # # # # ]: 0 : if ( pB->getLabel() != NULL &&
[ # # ][ # # ]
252 : 0 : pB->getLabel()->getLabelKind() == UL_label &&
253 [ # # ]: 0 : pB->getLabel()->getLabelValue()->compare(pLabel->getLabelValue()) )
254 : : {
255 [ # # ][ # # ]: 0 : idlc()->error()->error2(EIDL_MULTIPLE_BRANCH, this, pBranch);
256 : 0 : return pBranch;
257 : : }
258 : : }
259 : 0 : ++iter;
260 : : }
261 : 0 : return NULL;
262 : : }
263 : :
264 : 0 : sal_Bool AstUnion::dump(RegistryKey& rKey)
265 : : {
266 [ # # ]: 0 : RegistryKey localKey;
267 [ # # ][ # # ]: 0 : if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey))
[ # # ]
268 : : {
269 : : fprintf(stderr, "%s: warning, could not create key '%s' in '%s'\n",
270 [ # # ][ # # ]: 0 : idlc()->getOptions()->getProgramName().getStr(),
271 [ # # ][ # # ]: 0 : getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
[ # # ]
272 : 0 : return sal_False;
273 : : }
274 : :
275 [ # # ]: 0 : sal_uInt16 nMember = getNodeCount(NT_union_branch);
276 : :
277 : 0 : OUString emptyStr;
278 : : typereg::Writer aBlob(
279 : 0 : TYPEREG_VERSION_0, getDocumentation(), emptyStr, RT_TYPE_UNION,
280 [ # # ]: 0 : false, OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), 1,
281 [ # # # # ]: 0 : nMember, 0, 0);
282 : : aBlob.setSuperTypeName(
283 : : 0,
284 : : OStringToOUString(
285 [ # # ][ # # ]: 0 : getDiscrimantType()->getScopedName(), RTL_TEXTENCODING_UTF8));
286 : :
287 [ # # ]: 0 : if ( nMember > 0 )
288 : : {
289 : 0 : DeclList::const_iterator iter = getIteratorBegin();
290 : 0 : DeclList::const_iterator end = getIteratorEnd();
291 : 0 : AstDeclaration* pDecl = NULL;
292 : 0 : AstUnionBranch* pBranch = NULL;
293 [ # # ]: 0 : AstUnionBranch* pDefault = lookupDefault(sal_False);
294 : 0 : AstUnionLabel* pLabel = NULL;
295 : 0 : AstExprValue* pExprValue = NULL;
296 : 0 : RTConstValue aConst;
297 : 0 : sal_uInt16 index = 0;
298 [ # # ]: 0 : if ( pDefault )
299 : 0 : index = 1;
300 : :
301 : 0 : sal_Int64 disc = 0;
302 [ # # ]: 0 : while ( iter != end )
303 : : {
304 : 0 : pDecl = *iter;
305 [ # # ]: 0 : if ( pDecl->getNodeType() == NT_union_branch )
306 : : {
307 : 0 : pBranch = (AstUnionBranch*)pDecl;
308 [ # # ]: 0 : if (pBranch == pDefault)
309 : : {
310 : 0 : ++iter;
311 : 0 : continue;
312 : : }
313 : :
314 : 0 : pLabel = pBranch->getLabel();
315 [ # # ]: 0 : pExprValue = pLabel->getLabelValue()->coerce(ET_hyper, sal_False);
316 : 0 : aConst.m_type = RT_TYPE_INT64;
317 : 0 : aConst.m_value.aHyper = pExprValue->u.hval;
318 [ # # ]: 0 : if ( aConst.m_value.aHyper > disc )
319 : 0 : disc = aConst.m_value.aHyper;
320 : :
321 : : aBlob.setFieldData(
322 : 0 : index++, pBranch->getDocumentation(), emptyStr, RT_ACCESS_READWRITE,
323 : : OStringToOUString(
324 : 0 : pBranch->getLocalName(), RTL_TEXTENCODING_UTF8),
325 : : OStringToOUString(
326 [ # # ]: 0 : pBranch->getType()->getRelativName(),
327 : : RTL_TEXTENCODING_UTF8),
328 [ # # # # : 0 : aConst);
# # ]
329 : : }
330 : 0 : ++iter;
331 : : }
332 : :
333 [ # # ]: 0 : if ( pDefault )
334 : : {
335 : 0 : aConst.m_type = RT_TYPE_INT64;
336 : 0 : aConst.m_value.aHyper = disc + 1;
337 : : aBlob.setFieldData(
338 : 0 : 0, pDefault->getDocumentation(), emptyStr, RT_ACCESS_DEFAULT,
339 : : OStringToOUString(
340 : 0 : pDefault->getLocalName(), RTL_TEXTENCODING_UTF8),
341 : : OStringToOUString(
342 [ # # ]: 0 : pDefault->getType()->getRelativName(),
343 : : RTL_TEXTENCODING_UTF8),
344 [ # # # # : 0 : aConst);
# # ]
345 : 0 : }
346 : : }
347 : :
348 : : sal_uInt32 aBlobSize;
349 [ # # ]: 0 : void const * pBlob = aBlob.getBlob(&aBlobSize);
350 : :
351 [ # # ]: 0 : if (localKey.setValue(OUString(), RG_VALUETYPE_BINARY,
352 [ # # ]: 0 : (RegValue)pBlob, aBlobSize))
353 : : {
354 : : fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
355 [ # # ][ # # ]: 0 : idlc()->getOptions()->getProgramName().getStr(),
356 [ # # ][ # # ]: 0 : getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
[ # # ]
357 : 0 : return sal_False;
358 : : }
359 : :
360 [ # # ]: 0 : return sal_True;
361 : : }
362 : :
363 : 0 : AstUnionBranch::AstUnionBranch(AstUnionLabel* pLabel, AstType const * pType, const ::rtl::OString& name, AstScope* pScope)
364 : : : AstMember(NT_union_branch, pType, name, pScope)
365 : 0 : , m_pLabel(pLabel)
366 : : {
367 : 0 : }
368 : :
369 : 0 : AstUnionBranch::~AstUnionBranch()
370 : : {
371 [ # # ]: 0 : if ( m_pLabel )
372 [ # # ][ # # ]: 0 : delete m_pLabel;
373 [ # # ]: 0 : }
374 : :
375 : 0 : AstUnionLabel::AstUnionLabel(UnionLabel labelKind, AstExpression* pExpr)
376 : : : m_label(labelKind)
377 : 0 : , m_pLabelValue(pExpr)
378 : : {
379 [ # # ]: 0 : if ( m_pLabelValue )
380 : 0 : m_pLabelValue->evaluate(EK_const);
381 : 0 : }
382 : :
383 : 0 : AstUnionLabel::~AstUnionLabel()
384 : : {
385 [ # # ]: 0 : if ( m_pLabelValue )
386 [ # # ]: 0 : delete m_pLabelValue;
387 [ # # ]: 0 : }
388 : :
389 : :
390 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|