|
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 "CheckStatement.h" |
|
62 #include "Semantic.h" |
|
63 #include "AST.h" |
|
64 #include "TranslationUnit.h" |
|
65 #include "Scope.h" |
|
66 #include "CoreTypes.h" |
|
67 #include "Control.h" |
|
68 #include "Symbols.h" |
|
69 #include "Names.h" |
|
70 #include "Literals.h" |
|
71 #include <string> |
|
72 |
|
73 using namespace CPlusPlus; |
|
74 |
|
75 CheckStatement::CheckStatement(Semantic *semantic) |
|
76 : SemanticCheck(semantic), |
|
77 _statement(0), |
|
78 _scope(0) |
|
79 { } |
|
80 |
|
81 CheckStatement::~CheckStatement() |
|
82 { } |
|
83 |
|
84 void CheckStatement::check(StatementAST *statement, Scope *scope) |
|
85 { |
|
86 Scope *previousScope = switchScope(scope); |
|
87 StatementAST *previousStatement = switchStatement(statement); |
|
88 accept(statement); |
|
89 (void) switchStatement(previousStatement); |
|
90 (void) switchScope(previousScope); |
|
91 } |
|
92 |
|
93 StatementAST *CheckStatement::switchStatement(StatementAST *statement) |
|
94 { |
|
95 StatementAST *previousStatement = _statement; |
|
96 _statement = statement; |
|
97 return previousStatement; |
|
98 } |
|
99 |
|
100 Scope *CheckStatement::switchScope(Scope *scope) |
|
101 { |
|
102 Scope *previousScope = _scope; |
|
103 _scope = scope; |
|
104 return previousScope; |
|
105 } |
|
106 |
|
107 bool CheckStatement::visit(CaseStatementAST *ast) |
|
108 { |
|
109 FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope); |
|
110 semantic()->check(ast->statement, _scope); |
|
111 return false; |
|
112 } |
|
113 |
|
114 bool CheckStatement::visit(CompoundStatementAST *ast) |
|
115 { |
|
116 Block *block = control()->newBlock(ast->lbrace_token); |
|
117 block->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
118 block->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
119 ast->symbol = block; |
|
120 _scope->enterSymbol(block); |
|
121 Scope *previousScope = switchScope(block->members()); |
|
122 StatementAST *previousStatement = 0; |
|
123 for (StatementListAST *it = ast->statement_list; it; it = it->next) { |
|
124 StatementAST *statement = it->value; |
|
125 semantic()->check(statement, _scope); |
|
126 |
|
127 if (statement && previousStatement) { |
|
128 ExpressionStatementAST *expressionStatement = statement->asExpressionStatement(); |
|
129 CompoundStatementAST *compoundStatement = previousStatement->asCompoundStatement(); |
|
130 if (expressionStatement && ! expressionStatement->expression && compoundStatement && compoundStatement->rbrace_token) |
|
131 translationUnit()->warning(compoundStatement->rbrace_token, "unnecessary semicolon after block"); |
|
132 } |
|
133 |
|
134 previousStatement = statement; |
|
135 } |
|
136 (void) switchScope(previousScope); |
|
137 return false; |
|
138 } |
|
139 |
|
140 bool CheckStatement::visit(DeclarationStatementAST *ast) |
|
141 { |
|
142 semantic()->check(ast->declaration, _scope); |
|
143 return false; |
|
144 } |
|
145 |
|
146 bool CheckStatement::visit(DoStatementAST *ast) |
|
147 { |
|
148 semantic()->check(ast->statement, _scope); |
|
149 FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope); |
|
150 return false; |
|
151 } |
|
152 |
|
153 bool CheckStatement::visit(ExpressionOrDeclarationStatementAST *ast) |
|
154 { |
|
155 // translationUnit()->warning(ast->firstToken(), |
|
156 // "ambiguous expression or declaration statement"); |
|
157 if (ast->declaration) |
|
158 semantic()->check(ast->declaration, _scope); |
|
159 else |
|
160 semantic()->check(ast->expression, _scope); |
|
161 return false; |
|
162 } |
|
163 |
|
164 bool CheckStatement::visit(ExpressionStatementAST *ast) |
|
165 { |
|
166 FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope); |
|
167 return false; |
|
168 } |
|
169 |
|
170 bool CheckStatement::forEachFastEnum(unsigned firstToken, |
|
171 unsigned lastToken, |
|
172 SpecifierListAST *type_specifier_list, |
|
173 DeclaratorAST *declarator, |
|
174 ExpressionAST *initializer, |
|
175 ExpressionAST *expression, |
|
176 StatementAST *statement, |
|
177 Block *&symbol) |
|
178 { |
|
179 Block *block = control()->newBlock(firstToken); |
|
180 block->setStartOffset(tokenAt(firstToken).offset); |
|
181 block->setEndOffset(tokenAt(lastToken).offset); |
|
182 symbol = block; |
|
183 _scope->enterSymbol(block); |
|
184 Scope *previousScope = switchScope(block->members()); |
|
185 if (type_specifier_list && declarator) { |
|
186 FullySpecifiedType ty = semantic()->check(type_specifier_list, _scope); |
|
187 const Name *name = 0; |
|
188 ty = semantic()->check(declarator, ty, _scope, &name); |
|
189 unsigned location = declarator->firstToken(); |
|
190 if (CoreDeclaratorAST *core_declarator = declarator->core_declarator) |
|
191 location = core_declarator->firstToken(); |
|
192 Declaration *decl = control()->newDeclaration(location, name); |
|
193 decl->setType(ty); |
|
194 _scope->enterSymbol(decl); |
|
195 } else { |
|
196 FullySpecifiedType exprTy = semantic()->check(initializer, _scope); |
|
197 (void) exprTy; |
|
198 } |
|
199 |
|
200 FullySpecifiedType exprTy = semantic()->check(expression, _scope); |
|
201 semantic()->check(statement, _scope); |
|
202 (void) switchScope(previousScope); |
|
203 return false; |
|
204 } |
|
205 |
|
206 bool CheckStatement::visit(ForeachStatementAST *ast) |
|
207 { |
|
208 return forEachFastEnum(ast->firstToken(), |
|
209 ast->lastToken(), |
|
210 ast->type_specifier_list, |
|
211 ast->declarator, |
|
212 ast->initializer, |
|
213 ast->expression, |
|
214 ast->statement, |
|
215 ast->symbol); |
|
216 } |
|
217 |
|
218 bool CheckStatement::visit(ObjCFastEnumerationAST *ast) |
|
219 { |
|
220 return forEachFastEnum(ast->firstToken(), |
|
221 ast->lastToken(), |
|
222 ast->type_specifier_list, |
|
223 ast->declarator, |
|
224 ast->initializer, |
|
225 ast->fast_enumeratable_expression, |
|
226 ast->statement, |
|
227 ast->symbol); |
|
228 } |
|
229 |
|
230 bool CheckStatement::visit(ForStatementAST *ast) |
|
231 { |
|
232 Block *block = control()->newBlock(ast->for_token); |
|
233 block->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
234 block->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
235 ast->symbol = block; |
|
236 _scope->enterSymbol(block); |
|
237 Scope *previousScope = switchScope(block->members()); |
|
238 semantic()->check(ast->initializer, _scope); |
|
239 FullySpecifiedType condTy = semantic()->check(ast->condition, _scope); |
|
240 FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope); |
|
241 semantic()->check(ast->statement, _scope); |
|
242 (void) switchScope(previousScope); |
|
243 return false; |
|
244 } |
|
245 |
|
246 bool CheckStatement::visit(IfStatementAST *ast) |
|
247 { |
|
248 Block *block = control()->newBlock(ast->if_token); |
|
249 block->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
250 block->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
251 ast->symbol = block; |
|
252 _scope->enterSymbol(block); |
|
253 Scope *previousScope = switchScope(block->members()); |
|
254 FullySpecifiedType exprTy = semantic()->check(ast->condition, _scope); |
|
255 semantic()->check(ast->statement, _scope); |
|
256 semantic()->check(ast->else_statement, _scope); |
|
257 (void) switchScope(previousScope); |
|
258 return false; |
|
259 } |
|
260 |
|
261 bool CheckStatement::visit(LabeledStatementAST *ast) |
|
262 { |
|
263 semantic()->check(ast->statement, _scope); |
|
264 return false; |
|
265 } |
|
266 |
|
267 bool CheckStatement::visit(BreakStatementAST *) |
|
268 { |
|
269 return false; |
|
270 } |
|
271 |
|
272 bool CheckStatement::visit(ContinueStatementAST *) |
|
273 { |
|
274 return false; |
|
275 } |
|
276 |
|
277 bool CheckStatement::visit(GotoStatementAST *) |
|
278 { |
|
279 return false; |
|
280 } |
|
281 |
|
282 bool CheckStatement::visit(ReturnStatementAST *ast) |
|
283 { |
|
284 FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope); |
|
285 return false; |
|
286 } |
|
287 |
|
288 bool CheckStatement::visit(SwitchStatementAST *ast) |
|
289 { |
|
290 Block *block = control()->newBlock(ast->switch_token); |
|
291 block->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
292 block->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
293 ast->symbol = block; |
|
294 _scope->enterSymbol(block); |
|
295 Scope *previousScope = switchScope(block->members()); |
|
296 FullySpecifiedType condTy = semantic()->check(ast->condition, _scope); |
|
297 semantic()->check(ast->statement, _scope); |
|
298 (void) switchScope(previousScope); |
|
299 return false; |
|
300 } |
|
301 |
|
302 bool CheckStatement::visit(TryBlockStatementAST *ast) |
|
303 { |
|
304 semantic()->check(ast->statement, _scope); |
|
305 for (CatchClauseListAST *it = ast->catch_clause_list; it; it = it->next) { |
|
306 semantic()->check(it->value, _scope); |
|
307 } |
|
308 return false; |
|
309 } |
|
310 |
|
311 bool CheckStatement::visit(CatchClauseAST *ast) |
|
312 { |
|
313 Block *block = control()->newBlock(ast->catch_token); |
|
314 block->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
315 block->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
316 ast->symbol = block; |
|
317 _scope->enterSymbol(block); |
|
318 Scope *previousScope = switchScope(block->members()); |
|
319 semantic()->check(ast->exception_declaration, _scope); |
|
320 semantic()->check(ast->statement, _scope); |
|
321 (void) switchScope(previousScope); |
|
322 return false; |
|
323 } |
|
324 |
|
325 bool CheckStatement::visit(WhileStatementAST *ast) |
|
326 { |
|
327 Block *block = control()->newBlock(ast->while_token); |
|
328 block->setStartOffset(tokenAt(ast->firstToken()).offset); |
|
329 block->setEndOffset(tokenAt(ast->lastToken()).offset); |
|
330 ast->symbol = block; |
|
331 _scope->enterSymbol(block); |
|
332 Scope *previousScope = switchScope(block->members()); |
|
333 FullySpecifiedType condTy = semantic()->check(ast->condition, _scope); |
|
334 semantic()->check(ast->statement, _scope); |
|
335 (void) switchScope(previousScope); |
|
336 return false; |
|
337 } |
|
338 |
|
339 bool CheckStatement::visit(QtMemberDeclarationAST *ast) |
|
340 { |
|
341 const Name *name = 0; |
|
342 |
|
343 if (tokenKind(ast->q_token) == T_Q_D) |
|
344 name = control()->nameId(control()->findOrInsertIdentifier("d")); |
|
345 else |
|
346 name = control()->nameId(control()->findOrInsertIdentifier("q")); |
|
347 |
|
348 FullySpecifiedType declTy = semantic()->check(ast->type_id, _scope); |
|
349 |
|
350 if (tokenKind(ast->q_token) == T_Q_D) { |
|
351 if (NamedType *namedTy = declTy->asNamedType()) { |
|
352 if (const NameId *nameId = namedTy->name()->asNameId()) { |
|
353 std::string privateClass; |
|
354 privateClass += nameId->identifier()->chars(); |
|
355 privateClass += "Private"; |
|
356 |
|
357 const Name *privName = control()->nameId(control()->findOrInsertIdentifier(privateClass.c_str(), |
|
358 privateClass.size())); |
|
359 declTy.setType(control()->namedType(privName)); |
|
360 } |
|
361 } |
|
362 } |
|
363 |
|
364 Declaration *symbol = control()->newDeclaration(/*generated*/ 0, name); |
|
365 symbol->setType(control()->pointerType(declTy)); |
|
366 |
|
367 _scope->enterSymbol(symbol); |
|
368 |
|
369 return false; |
|
370 } |