tools/icheck/parser/src/shared/cplusplus/CheckExpression.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     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 "CheckExpression.h"
       
    62 #include "Semantic.h"
       
    63 #include "TranslationUnit.h"
       
    64 #include "AST.h"
       
    65 #include "Scope.h"
       
    66 #include "Literals.h"
       
    67 #include "CoreTypes.h"
       
    68 #include "Symbols.h"
       
    69 #include "Control.h"
       
    70 
       
    71 using namespace CPlusPlus;
       
    72 
       
    73 CheckExpression::CheckExpression(Semantic *semantic)
       
    74     : SemanticCheck(semantic),
       
    75       _expression(0),
       
    76       _scope(0),
       
    77       _checkOldStyleCasts(false)
       
    78 { }
       
    79 
       
    80 CheckExpression::~CheckExpression()
       
    81 { }
       
    82 
       
    83 FullySpecifiedType CheckExpression::check(ExpressionAST *expression, Scope *scope)
       
    84 {
       
    85     FullySpecifiedType previousType = switchFullySpecifiedType(FullySpecifiedType());
       
    86     Scope *previousScope = switchScope(scope);
       
    87     ExpressionAST *previousExpression = switchExpression(expression);
       
    88     accept(expression);
       
    89     (void) switchExpression(previousExpression);
       
    90     (void) switchScope(previousScope);
       
    91     return switchFullySpecifiedType(previousType);
       
    92 }
       
    93 
       
    94 ExpressionAST *CheckExpression::switchExpression(ExpressionAST *expression)
       
    95 {
       
    96     ExpressionAST *previousExpression = _expression;
       
    97     _expression = expression;
       
    98     return previousExpression;
       
    99 }
       
   100 
       
   101 FullySpecifiedType CheckExpression::switchFullySpecifiedType(const FullySpecifiedType &type)
       
   102 {
       
   103     FullySpecifiedType previousType = _fullySpecifiedType;
       
   104     _fullySpecifiedType = type;
       
   105     return previousType;
       
   106 }
       
   107 
       
   108 Scope *CheckExpression::switchScope(Scope *scope)
       
   109 {
       
   110     Scope *previousScope = _scope;
       
   111     _scope = scope;
       
   112     return previousScope;
       
   113 }
       
   114 
       
   115 bool CheckExpression::visit(BinaryExpressionAST *ast)
       
   116 {
       
   117     FullySpecifiedType leftExprTy = semantic()->check(ast->left_expression, _scope);
       
   118     FullySpecifiedType rightExprTy = semantic()->check(ast->right_expression, _scope);
       
   119     return false;
       
   120 }
       
   121 
       
   122 bool CheckExpression::visit(CastExpressionAST *ast)
       
   123 {
       
   124     FullySpecifiedType castTy = semantic()->check(ast->type_id, _scope);
       
   125     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   126     if (_checkOldStyleCasts && ! castTy->isVoidType())
       
   127         translationUnit()->warning(ast->firstToken(),
       
   128                                    "ugly old style cast");
       
   129     return false;
       
   130 }
       
   131 
       
   132 bool CheckExpression::visit(ConditionAST *ast)
       
   133 {
       
   134     FullySpecifiedType typeSpecTy = semantic()->check(ast->type_specifier_list, _scope);
       
   135     const Name *name = 0;
       
   136     FullySpecifiedType declTy = semantic()->check(ast->declarator, typeSpecTy.qualifiedType(),
       
   137                                                   _scope, &name);
       
   138     Declaration *decl = control()->newDeclaration(ast->declarator->firstToken(), name);
       
   139     decl->setType(declTy);
       
   140     _scope->enterSymbol(decl);
       
   141     return false;
       
   142 }
       
   143 
       
   144 bool CheckExpression::visit(ConditionalExpressionAST *ast)
       
   145 {
       
   146     FullySpecifiedType condTy = semantic()->check(ast->condition, _scope);
       
   147     FullySpecifiedType leftExprTy = semantic()->check(ast->left_expression, _scope);
       
   148     FullySpecifiedType rightExprTy = semantic()->check(ast->right_expression, _scope);
       
   149     return false;
       
   150 }
       
   151 
       
   152 bool CheckExpression::visit(CppCastExpressionAST *ast)
       
   153 {
       
   154     FullySpecifiedType typeIdTy = semantic()->check(ast->type_id, _scope);
       
   155     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   156     return false;
       
   157 }
       
   158 
       
   159 bool CheckExpression::visit(DeleteExpressionAST *ast)
       
   160 {
       
   161     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   162     return false;
       
   163 }
       
   164 
       
   165 bool CheckExpression::visit(ArrayInitializerAST *ast)
       
   166 {
       
   167     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
       
   168         FullySpecifiedType exprTy = semantic()->check(it->value, _scope);
       
   169     }
       
   170     return false;
       
   171 }
       
   172 
       
   173 bool CheckExpression::visit(QualifiedNameAST *ast)
       
   174 {
       
   175     (void) semantic()->check(ast, _scope);
       
   176     return false;
       
   177 }
       
   178 
       
   179 bool CheckExpression::visit(OperatorFunctionIdAST *ast)
       
   180 {
       
   181     (void) semantic()->check(ast, _scope);
       
   182     return false;
       
   183 }
       
   184 
       
   185 bool CheckExpression::visit(ConversionFunctionIdAST *ast)
       
   186 {
       
   187     (void) semantic()->check(ast, _scope);
       
   188     return false;
       
   189 }
       
   190 
       
   191 bool CheckExpression::visit(SimpleNameAST *ast)
       
   192 {
       
   193     (void) semantic()->check(ast, _scope);
       
   194     return false;
       
   195 }
       
   196 
       
   197 bool CheckExpression::visit(DestructorNameAST *ast)
       
   198 {
       
   199     (void) semantic()->check(ast, _scope);
       
   200     return false;
       
   201 }
       
   202 
       
   203 bool CheckExpression::visit(TemplateIdAST *ast)
       
   204 {
       
   205     (void) semantic()->check(ast, _scope);
       
   206     return false;
       
   207 }
       
   208 
       
   209 bool CheckExpression::visit(NewExpressionAST *ast)
       
   210 {
       
   211     if (ast->new_placement) {
       
   212         for (ExpressionListAST *it = ast->new_placement->expression_list; it; it = it->next) {
       
   213             FullySpecifiedType exprTy = semantic()->check(it->value, _scope);
       
   214         }
       
   215     }
       
   216 
       
   217     FullySpecifiedType typeIdTy = semantic()->check(ast->type_id, _scope);
       
   218 
       
   219     if (ast->new_type_id) {
       
   220         FullySpecifiedType ty = semantic()->check(ast->new_type_id->type_specifier_list, _scope);
       
   221 
       
   222         for (NewArrayDeclaratorListAST *it = ast->new_type_id->new_array_declarator_list; it; it = it->next) {
       
   223             if (NewArrayDeclaratorAST *declarator = it->value) {
       
   224                 FullySpecifiedType exprTy = semantic()->check(declarator->expression, _scope);
       
   225             }
       
   226         }
       
   227     }
       
   228 
       
   229     // ### process new-initializer
       
   230     if (ast->new_initializer) {
       
   231         FullySpecifiedType exprTy = semantic()->check(ast->new_initializer->expression, _scope);
       
   232     }
       
   233 
       
   234     return false;
       
   235 }
       
   236 
       
   237 bool CheckExpression::visit(TypeidExpressionAST *ast)
       
   238 {
       
   239     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   240     return false;
       
   241 }
       
   242 
       
   243 bool CheckExpression::visit(TypenameCallExpressionAST *ast)
       
   244 {
       
   245     (void) semantic()->check(ast->name, _scope);
       
   246 
       
   247     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
       
   248         FullySpecifiedType exprTy = semantic()->check(it->value, _scope);
       
   249         (void) exprTy;
       
   250     }
       
   251     return false;
       
   252 }
       
   253 
       
   254 bool CheckExpression::visit(TypeConstructorCallAST *ast)
       
   255 {
       
   256     FullySpecifiedType typeSpecTy = semantic()->check(ast->type_specifier_list, _scope);
       
   257     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
       
   258         FullySpecifiedType exprTy = semantic()->check(it->value, _scope);
       
   259     }
       
   260     return false;
       
   261 }
       
   262 
       
   263 bool CheckExpression::visit(PostfixExpressionAST *ast)
       
   264 {
       
   265     FullySpecifiedType exprTy = semantic()->check(ast->base_expression, _scope);
       
   266     for (PostfixListAST *it = ast->postfix_expression_list; it; it = it->next) {
       
   267         accept(it->value); // ### not exactly.
       
   268     }
       
   269     return false;
       
   270 }
       
   271 
       
   272 bool CheckExpression::visit(SizeofExpressionAST *ast)
       
   273 {
       
   274     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   275     return false;
       
   276 }
       
   277 
       
   278 bool CheckExpression::visit(NumericLiteralAST *)
       
   279 {
       
   280     _fullySpecifiedType.setType(control()->integerType(IntegerType::Int));
       
   281     return false;
       
   282 }
       
   283 
       
   284 bool CheckExpression::visit(BoolLiteralAST *)
       
   285 {
       
   286     _fullySpecifiedType.setType(control()->integerType(IntegerType::Bool));
       
   287     return false;
       
   288 }
       
   289 
       
   290 bool CheckExpression::visit(StringLiteralAST *)
       
   291 {
       
   292     IntegerType *charTy = control()->integerType(IntegerType::Char);
       
   293     _fullySpecifiedType.setType(control()->pointerType(charTy));
       
   294     return false;
       
   295 }
       
   296 
       
   297 bool CheckExpression::visit(ThisExpressionAST *)
       
   298 {
       
   299     return false;
       
   300 }
       
   301 
       
   302 bool CheckExpression::visit(NestedExpressionAST *ast)
       
   303 {
       
   304     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   305     return false;
       
   306 }
       
   307 
       
   308 bool CheckExpression::visit(ThrowExpressionAST *ast)
       
   309 {
       
   310     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   311     return false;
       
   312 }
       
   313 
       
   314 bool CheckExpression::visit(TypeIdAST *ast)
       
   315 {
       
   316     FullySpecifiedType typeSpecTy = semantic()->check(ast->type_specifier_list, _scope);
       
   317     FullySpecifiedType declTy = semantic()->check(ast->declarator, typeSpecTy.qualifiedType(), _scope);
       
   318     _fullySpecifiedType = declTy;
       
   319     return false;
       
   320 }
       
   321 
       
   322 bool CheckExpression::visit(UnaryExpressionAST *ast)
       
   323 {
       
   324     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   325     return false;
       
   326 }
       
   327 
       
   328 bool CheckExpression::visit(QtMethodAST *ast)
       
   329 {
       
   330     const Name *name = 0;
       
   331     Scope dummy;
       
   332     FullySpecifiedType methTy = semantic()->check(ast->declarator, FullySpecifiedType(),
       
   333                                                   &dummy, &name);
       
   334     Function *fty = methTy->asFunctionType();
       
   335     if (! fty)
       
   336         translationUnit()->warning(ast->firstToken(), "expected a function declarator");
       
   337     else {
       
   338         for (unsigned i = 0; i < fty->argumentCount(); ++i) {
       
   339             Symbol *arg = fty->argumentAt(i);
       
   340             if (arg->name())
       
   341                 translationUnit()->warning(arg->sourceLocation(),
       
   342                                            "argument should be anonymous");
       
   343         }
       
   344     }
       
   345     return false;
       
   346 }
       
   347 
       
   348 bool CheckExpression::visit(CompoundLiteralAST *ast)
       
   349 {
       
   350     /*FullySpecifiedType exprTy = */ semantic()->check(ast->type_id, _scope);
       
   351     /*FullySpecifiedType initTy = */ semantic()->check(ast->initializer, _scope);
       
   352     return false;
       
   353 }
       
   354 
       
   355 bool CheckExpression::visit(CallAST *ast)
       
   356 {
       
   357     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
       
   358         FullySpecifiedType exprTy = semantic()->check(it->value, _scope);
       
   359     }
       
   360     return false;
       
   361 }
       
   362 
       
   363 bool CheckExpression::visit(ArrayAccessAST *ast)
       
   364 {
       
   365     FullySpecifiedType exprTy = semantic()->check(ast->expression, _scope);
       
   366     return false;
       
   367 }
       
   368 
       
   369 bool CheckExpression::visit(PostIncrDecrAST *)
       
   370 {
       
   371     return false;
       
   372 }
       
   373 
       
   374 bool CheckExpression::visit(MemberAccessAST *ast)
       
   375 {
       
   376     (void) semantic()->check(ast->member_name, _scope);
       
   377     return false;
       
   378 }
       
   379 
       
   380 bool CheckExpression::visit(ObjCMessageExpressionAST *ast)
       
   381 {
       
   382     (void) semantic()->check(ast->receiver_expression, _scope);
       
   383     (void) semantic()->check(ast->selector, _scope);
       
   384 
       
   385     accept(ast->argument_list); // ### not necessary.
       
   386     return false;
       
   387 }
       
   388 
       
   389 bool CheckExpression::visit(ObjCEncodeExpressionAST * /*ast*/)
       
   390 {
       
   391     // TODO: visit the type name, but store the type here? (EV)
       
   392     return true;
       
   393 }
       
   394 
       
   395 bool CheckExpression::visit(ObjCSelectorExpressionAST *ast)
       
   396 {
       
   397     (void) semantic()->check(ast->selector, _scope);
       
   398     return false;
       
   399 }
       
   400 
       
   401