|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com> |
|
42 // |
|
43 // Permission is hereby granted, free of charge, to any person obtaining a copy |
|
44 // of this software and associated documentation files (the "Software"), to deal |
|
45 // in the Software without restriction, including without limitation the rights |
|
46 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
47 // copies of the Software, and to permit persons to whom the Software is |
|
48 // furnished to do so, subject to the following conditions: |
|
49 // |
|
50 // The above copyright notice and this permission notice shall be included in |
|
51 // all copies or substantial portions of the Software. |
|
52 // |
|
53 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
54 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
55 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
56 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
57 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
58 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
59 // THE SOFTWARE. |
|
60 |
|
61 #include "CheckSpecifier.h" |
|
62 #include "Semantic.h" |
|
63 #include "AST.h" |
|
64 #include "Token.h" |
|
65 #include "TranslationUnit.h" |
|
66 #include "Literals.h" |
|
67 #include "Names.h" |
|
68 #include "CoreTypes.h" |
|
69 #include "Symbols.h" |
|
70 #include "Control.h" |
|
71 #include "Scope.h" |
|
72 |
|
73 using namespace CPlusPlus; |
|
74 |
|
75 CheckSpecifier::CheckSpecifier(Semantic *semantic) |
|
76 : SemanticCheck(semantic), |
|
77 _specifier(0), |
|
78 _scope(0) |
|
79 { } |
|
80 |
|
81 CheckSpecifier::~CheckSpecifier() |
|
82 { } |
|
83 |
|
84 FullySpecifiedType CheckSpecifier::check(SpecifierListAST *specifier, Scope *scope) |
|
85 { |
|
86 FullySpecifiedType previousType = switchFullySpecifiedType(FullySpecifiedType()); |
|
87 Scope *previousScope = switchScope(scope); |
|
88 SpecifierListAST *previousSpecifier = switchSpecifier(specifier); |
|
89 accept(specifier); |
|
90 (void) switchSpecifier(previousSpecifier); |
|
91 (void) switchScope(previousScope); |
|
92 return switchFullySpecifiedType(previousType); |
|
93 } |
|
94 |
|
95 FullySpecifiedType CheckSpecifier::check(ObjCTypeNameAST *typeName, Scope *scope) |
|
96 { |
|
97 FullySpecifiedType previousType = switchFullySpecifiedType(FullySpecifiedType()); |
|
98 Scope *previousScope = switchScope(scope); |
|
99 |
|
100 accept(typeName); |
|
101 |
|
102 (void) switchScope(previousScope); |
|
103 return switchFullySpecifiedType(previousType); |
|
104 } |
|
105 |
|
106 SpecifierListAST *CheckSpecifier::switchSpecifier(SpecifierListAST *specifier) |
|
107 { |
|
108 SpecifierListAST *previousSpecifier = _specifier; |
|
109 _specifier = specifier; |
|
110 return previousSpecifier; |
|
111 } |
|
112 |
|
113 FullySpecifiedType CheckSpecifier::switchFullySpecifiedType(const FullySpecifiedType &type) |
|
114 { |
|
115 FullySpecifiedType previousType = _fullySpecifiedType; |
|
116 _fullySpecifiedType = type; |
|
117 return previousType; |
|
118 } |
|
119 |
|
120 Scope *CheckSpecifier::switchScope(Scope *scope) |
|
121 { |
|
122 Scope *previousScope = _scope; |
|
123 _scope = scope; |
|
124 return previousScope; |
|
125 } |
|
126 |
|
127 bool CheckSpecifier::visit(SimpleSpecifierAST *ast) |
|
128 { |
|
129 switch (tokenKind(ast->specifier_token)) { |
|
130 case T_CONST: |
|
131 if (_fullySpecifiedType.isConst()) |
|
132 translationUnit()->error(ast->specifier_token, |
|
133 "duplicate `%s'", spell(ast->specifier_token)); |
|
134 _fullySpecifiedType.setConst(true); |
|
135 break; |
|
136 |
|
137 case T_VOLATILE: |
|
138 if (_fullySpecifiedType.isVolatile()) |
|
139 translationUnit()->error(ast->specifier_token, |
|
140 "duplicate `%s'", spell(ast->specifier_token)); |
|
141 _fullySpecifiedType.setVolatile(true); |
|
142 break; |
|
143 |
|
144 case T_FRIEND: |
|
145 if (_fullySpecifiedType.isFriend()) |
|
146 translationUnit()->error(ast->specifier_token, |
|
147 "duplicate `%s'", spell(ast->specifier_token)); |
|
148 _fullySpecifiedType.setFriend(true); |
|
149 break; |
|
150 |
|
151 case T_REGISTER: |
|
152 if (_fullySpecifiedType.isRegister()) |
|
153 translationUnit()->error(ast->specifier_token, |
|
154 "duplicate `%s'", spell(ast->specifier_token)); |
|
155 _fullySpecifiedType.setRegister(true); |
|
156 break; |
|
157 |
|
158 case T_STATIC: |
|
159 if (_fullySpecifiedType.isStatic()) |
|
160 translationUnit()->error(ast->specifier_token, |
|
161 "duplicate `%s'", spell(ast->specifier_token)); |
|
162 _fullySpecifiedType.setStatic(true); |
|
163 break; |
|
164 |
|
165 case T_EXTERN: |
|
166 if (_fullySpecifiedType.isExtern()) |
|
167 translationUnit()->error(ast->specifier_token, |
|
168 "duplicate `%s'", spell(ast->specifier_token)); |
|
169 _fullySpecifiedType.setExtern(true); |
|
170 break; |
|
171 |
|
172 case T_MUTABLE: |
|
173 if (_fullySpecifiedType.isMutable()) |
|
174 translationUnit()->error(ast->specifier_token, |
|
175 "duplicate `%s'", spell(ast->specifier_token)); |
|
176 _fullySpecifiedType.setMutable(true); |
|
177 break; |
|
178 |
|
179 case T_TYPEDEF: |
|
180 if (_fullySpecifiedType.isTypedef()) |
|
181 translationUnit()->error(ast->specifier_token, |
|
182 "duplicate `%s'", spell(ast->specifier_token)); |
|
183 _fullySpecifiedType.setTypedef(true); |
|
184 break; |
|
185 |
|
186 case T_INLINE: |
|
187 if (_fullySpecifiedType.isInline()) |
|
188 translationUnit()->error(ast->specifier_token, |
|
189 "duplicate `%s'", spell(ast->specifier_token)); |
|
190 _fullySpecifiedType.setInline(true); |
|
191 break; |
|
192 |
|
193 case T_VIRTUAL: |
|
194 if (_fullySpecifiedType.isVirtual()) |
|
195 translationUnit()->error(ast->specifier_token, |
|
196 "duplicate `%s'", spell(ast->specifier_token)); |
|
197 _fullySpecifiedType.setVirtual(true); |
|
198 break; |
|
199 |
|
200 case T_EXPLICIT: |
|
201 if (_fullySpecifiedType.isExplicit()) |
|
202 translationUnit()->error(ast->specifier_token, |
|
203 "duplicate `%s'", spell(ast->specifier_token)); |
|
204 _fullySpecifiedType.setExplicit(true); |
|
205 break; |
|
206 |
|
207 case T_SIGNED: |
|
208 if (_fullySpecifiedType.isSigned()) |
|
209 translationUnit()->error(ast->specifier_token, |
|
210 "duplicate `%s'", spell(ast->specifier_token)); |
|
211 _fullySpecifiedType.setSigned(true); |
|
212 break; |
|
213 |
|
214 case T_UNSIGNED: |
|
215 if (_fullySpecifiedType.isUnsigned()) |
|
216 translationUnit()->error(ast->specifier_token, |
|
217 "duplicate `%s'", spell(ast->specifier_token)); |
|
218 _fullySpecifiedType.setUnsigned(true); |
|
219 break; |
|
220 |
|
221 case T_CHAR: |
|
222 if (_fullySpecifiedType) |
|
223 translationUnit()->error(ast->specifier_token, |
|
224 "duplicate data type in declaration"); |
|
225 _fullySpecifiedType.setType(control()->integerType(IntegerType::Char)); |
|
226 break; |
|
227 |
|
228 case T_WCHAR_T: |
|
229 if (_fullySpecifiedType) |
|
230 translationUnit()->error(ast->specifier_token, |
|
231 "duplicate data type in declaration"); |
|
232 _fullySpecifiedType.setType(control()->integerType(IntegerType::WideChar)); |
|
233 break; |
|
234 |
|
235 case T_BOOL: |
|
236 if (_fullySpecifiedType) |
|
237 translationUnit()->error(ast->specifier_token, |
|
238 "duplicate data type in declaration"); |
|
239 _fullySpecifiedType.setType(control()->integerType(IntegerType::Bool)); |
|
240 break; |
|
241 |
|
242 case T_SHORT: |
|
243 if (_fullySpecifiedType) { |
|
244 IntegerType *intType = control()->integerType(IntegerType::Int); |
|
245 if (_fullySpecifiedType.type() != intType) |
|
246 translationUnit()->error(ast->specifier_token, |
|
247 "duplicate data type in declaration"); |
|
248 } |
|
249 _fullySpecifiedType.setType(control()->integerType(IntegerType::Short)); |
|
250 break; |
|
251 |
|
252 case T_INT: |
|
253 if (_fullySpecifiedType) { |
|
254 Type *tp = _fullySpecifiedType.type(); |
|
255 IntegerType *shortType = control()->integerType(IntegerType::Short); |
|
256 IntegerType *longType = control()->integerType(IntegerType::Long); |
|
257 IntegerType *longLongType = control()->integerType(IntegerType::LongLong); |
|
258 if (tp == shortType || tp == longType || tp == longLongType) |
|
259 break; |
|
260 translationUnit()->error(ast->specifier_token, |
|
261 "duplicate data type in declaration"); |
|
262 } |
|
263 _fullySpecifiedType.setType(control()->integerType(IntegerType::Int)); |
|
264 break; |
|
265 |
|
266 case T_LONG: |
|
267 if (_fullySpecifiedType) { |
|
268 Type *tp = _fullySpecifiedType.type(); |
|
269 IntegerType *intType = control()->integerType(IntegerType::Int); |
|
270 IntegerType *longType = control()->integerType(IntegerType::Long); |
|
271 FloatType *doubleType = control()->floatType(FloatType::Double); |
|
272 if (tp == longType) { |
|
273 _fullySpecifiedType.setType(control()->integerType(IntegerType::LongLong)); |
|
274 break; |
|
275 } else if (tp == doubleType) { |
|
276 _fullySpecifiedType.setType(control()->floatType(FloatType::LongDouble)); |
|
277 break; |
|
278 } else if (tp != intType) { |
|
279 translationUnit()->error(ast->specifier_token, |
|
280 "duplicate data type in declaration"); |
|
281 } |
|
282 } |
|
283 _fullySpecifiedType.setType(control()->integerType(IntegerType::Long)); |
|
284 break; |
|
285 |
|
286 case T_FLOAT: |
|
287 if (_fullySpecifiedType) |
|
288 translationUnit()->error(ast->specifier_token, |
|
289 "duplicate data type in declaration"); |
|
290 _fullySpecifiedType.setType(control()->floatType(FloatType::Float)); |
|
291 break; |
|
292 |
|
293 case T_DOUBLE: |
|
294 if (_fullySpecifiedType) { |
|
295 IntegerType *longType = control()->integerType(IntegerType::Long); |
|
296 if (_fullySpecifiedType.type() == longType) { |
|
297 _fullySpecifiedType.setType(control()->floatType(FloatType::LongDouble)); |
|
298 break; |
|
299 } |
|
300 translationUnit()->error(ast->specifier_token, |
|
301 "duplicate data type in declaration"); |
|
302 } |
|
303 _fullySpecifiedType.setType(control()->floatType(FloatType::Double)); |
|
304 break; |
|
305 |
|
306 case T_VOID: |
|
307 if (_fullySpecifiedType) |
|
308 translationUnit()->error(ast->specifier_token, |
|
309 "duplicate data type in declaration"); |
|
310 _fullySpecifiedType.setType(control()->voidType()); |
|
311 break; |
|
312 |
|
313 default: |
|
314 break; |
|
315 } // switch |
|
316 |
|
317 return false; |
|
318 } |
|
319 |
|
320 bool CheckSpecifier::visit(ClassSpecifierAST *ast) |
|
321 { |
|
322 unsigned sourceLocation = ast->firstToken(); |
|
323 |
|
324 if (ast->name) |
|
325 sourceLocation = ast->name->firstToken(); |
|
326 |
|
327 const Name *className = semantic()->check(ast->name, _scope); |
|
328 Class *klass = control()->newClass(sourceLocation, className); |
|
329 klass->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
330 klass->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
331 ast->symbol = klass; |
|
332 unsigned classKey = tokenKind(ast->classkey_token); |
|
333 if (classKey == T_CLASS) |
|
334 klass->setClassKey(Class::ClassKey); |
|
335 else if (classKey == T_STRUCT) |
|
336 klass->setClassKey(Class::StructKey); |
|
337 else if (classKey == T_UNION) |
|
338 klass->setClassKey(Class::UnionKey); |
|
339 klass->setVisibility(semantic()->currentVisibility()); |
|
340 _scope->enterSymbol(klass); |
|
341 _fullySpecifiedType.setType(klass); |
|
342 |
|
343 for (BaseSpecifierListAST *it = ast->base_clause_list; it; it = it->next) { |
|
344 BaseSpecifierAST *base = it->value; |
|
345 const Name *baseClassName = semantic()->check(base->name, _scope); |
|
346 BaseClass *baseClass = control()->newBaseClass(ast->firstToken(), baseClassName); |
|
347 base->symbol = baseClass; |
|
348 if (base->virtual_token) |
|
349 baseClass->setVirtual(true); |
|
350 if (base->access_specifier_token) { |
|
351 int accessSpecifier = tokenKind(base->access_specifier_token); |
|
352 int visibility = semantic()->visibilityForAccessSpecifier(accessSpecifier); |
|
353 baseClass->setVisibility(visibility); |
|
354 } |
|
355 klass->addBaseClass(baseClass); |
|
356 } |
|
357 |
|
358 int visibility = semantic()->visibilityForClassKey(classKey); |
|
359 int previousVisibility = semantic()->switchVisibility(visibility); |
|
360 int previousMethodKey = semantic()->switchMethodKey(Function::NormalMethod); |
|
361 |
|
362 DeclarationAST *previousDeclaration = 0; |
|
363 for (DeclarationListAST *it = ast->member_specifier_list; it; it = it->next) { |
|
364 DeclarationAST *declaration = it->value; |
|
365 semantic()->check(declaration, klass->members()); |
|
366 |
|
367 if (previousDeclaration && declaration && |
|
368 declaration->asEmptyDeclaration() != 0 && |
|
369 previousDeclaration->asFunctionDefinition() != 0) |
|
370 translationUnit()->warning(declaration->firstToken(), "unnecessary semicolon after function body"); |
|
371 |
|
372 previousDeclaration = declaration; |
|
373 } |
|
374 |
|
375 (void) semantic()->switchMethodKey(previousMethodKey); |
|
376 (void) semantic()->switchVisibility(previousVisibility); |
|
377 |
|
378 return false; |
|
379 } |
|
380 |
|
381 bool CheckSpecifier::visit(NamedTypeSpecifierAST *ast) |
|
382 { |
|
383 const Name *name = semantic()->check(ast->name, _scope); |
|
384 _fullySpecifiedType.setType(control()->namedType(name)); |
|
385 return false; |
|
386 } |
|
387 |
|
388 bool CheckSpecifier::visit(ElaboratedTypeSpecifierAST *ast) |
|
389 { |
|
390 const Name *name = semantic()->check(ast->name, _scope); |
|
391 _fullySpecifiedType.setType(control()->namedType(name)); |
|
392 return false; |
|
393 } |
|
394 |
|
395 bool CheckSpecifier::visit(EnumSpecifierAST *ast) |
|
396 { |
|
397 unsigned sourceLocation = ast->firstToken(); |
|
398 if (ast->name) |
|
399 sourceLocation = ast->name->firstToken(); |
|
400 |
|
401 const Name *name = semantic()->check(ast->name, _scope); |
|
402 Enum *e = control()->newEnum(sourceLocation, name); |
|
403 e->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
404 e->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
405 e->setVisibility(semantic()->currentVisibility()); |
|
406 _scope->enterSymbol(e); |
|
407 _fullySpecifiedType.setType(e); |
|
408 for (EnumeratorListAST *it = ast->enumerator_list; it; it = it->next) { |
|
409 EnumeratorAST *enumerator = it->value; |
|
410 const Identifier *id = identifier(enumerator->identifier_token); |
|
411 if (! id) |
|
412 continue; |
|
413 const NameId *enumeratorName = control()->nameId(id); |
|
414 Declaration *decl = control()->newDeclaration(enumerator->firstToken(), |
|
415 enumeratorName); |
|
416 e->addMember(decl); |
|
417 } |
|
418 return false; |
|
419 } |
|
420 |
|
421 bool CheckSpecifier::visit(TypeofSpecifierAST *ast) |
|
422 { |
|
423 semantic()->check(ast->expression, _scope); |
|
424 return false; |
|
425 } |
|
426 |
|
427 bool CheckSpecifier::visit(AttributeSpecifierAST * /*ast*/) |
|
428 { |
|
429 return false; |
|
430 } |
|
431 |
|
432 bool CheckSpecifier::visit(ObjCTypeNameAST * /*ast*/) |
|
433 { |
|
434 // TODO: implement this (EV) |
|
435 // _fullySpecifiedType = FullySpecifiedType(); |
|
436 return true; |
|
437 } |
|
438 |
|
439 |