tools/icheck/parser/src/shared/cplusplus/TranslationUnit.h
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 #ifndef CPLUSPLUS_TRANSLATIONUNIT_H
       
    62 #define CPLUSPLUS_TRANSLATIONUNIT_H
       
    63 
       
    64 #include "CPlusPlusForwardDeclarations.h"
       
    65 #include "ASTfwd.h"
       
    66 #include "Token.h"
       
    67 #include "Array.h"
       
    68 #include <stdio.h> // for FILE*
       
    69 #include <vector> // ### remove me
       
    70 
       
    71 
       
    72 namespace CPlusPlus {
       
    73 
       
    74 class CPLUSPLUS_EXPORT TranslationUnit
       
    75 {
       
    76     TranslationUnit(const TranslationUnit &other);
       
    77     void operator =(const TranslationUnit &other);
       
    78 
       
    79 public:
       
    80     TranslationUnit(Control *control, const StringLiteral *fileId);
       
    81     ~TranslationUnit();
       
    82 
       
    83     Control *control() const;
       
    84 
       
    85     const StringLiteral *fileId() const;
       
    86     const char *fileName() const;
       
    87     unsigned fileNameLength() const;
       
    88 
       
    89     const char *firstSourceChar() const;
       
    90     const char *lastSourceChar() const;
       
    91     unsigned sourceLength() const;
       
    92 
       
    93     void setSource(const char *source, unsigned size);
       
    94 
       
    95     unsigned tokenCount() const;
       
    96     const Token &tokenAt(unsigned index) const;
       
    97     int tokenKind(unsigned index) const;
       
    98     const char *spell(unsigned index) const;
       
    99 
       
   100     unsigned matchingBrace(unsigned index) const;
       
   101     const Identifier *identifier(unsigned index) const;
       
   102     const Literal *literal(unsigned index) const;
       
   103     const StringLiteral *stringLiteral(unsigned index) const;
       
   104     const NumericLiteral *numericLiteral(unsigned index) const;
       
   105 
       
   106     MemoryPool *memoryPool() const;
       
   107     AST *ast() const;
       
   108 
       
   109     bool blockErrors(bool block);
       
   110 
       
   111     bool qtMocRunEnabled() const;
       
   112     void setQtMocRunEnabled(bool onoff);
       
   113 
       
   114     bool objCEnabled() const;
       
   115     void setObjCEnabled(bool onoff);
       
   116 
       
   117     void warning(unsigned index, const char *fmt, ...);
       
   118     void error(unsigned index, const char *fmt, ...);
       
   119     void fatal(unsigned index, const char *fmt, ...);
       
   120 
       
   121     bool isTokenized() const;
       
   122     void tokenize();
       
   123 
       
   124     bool skipFunctionBody() const;
       
   125     void setSkipFunctionBody(bool skipFunctionBody);
       
   126 
       
   127     bool isParsed() const;
       
   128 
       
   129     enum ParseMode {
       
   130         ParseTranlationUnit,
       
   131         ParseDeclaration,
       
   132         ParseExpression,
       
   133         ParseDeclarator,
       
   134         ParseStatement
       
   135     };
       
   136 
       
   137     bool parse(ParseMode mode = ParseTranlationUnit);
       
   138 
       
   139     void resetAST();
       
   140     void release();
       
   141 
       
   142     void getTokenStartPosition(unsigned index, unsigned *line,
       
   143                                unsigned *column = 0,
       
   144                                const StringLiteral **fileName = 0) const;
       
   145 
       
   146     void getTokenEndPosition(unsigned index, unsigned *line,
       
   147                              unsigned *column = 0,
       
   148                              const StringLiteral **fileName = 0) const;
       
   149 
       
   150     void getPosition(unsigned offset,
       
   151                      unsigned *line,
       
   152                      unsigned *column = 0,
       
   153                      const StringLiteral **fileName = 0) const;
       
   154 
       
   155     void getTokenPosition(unsigned index,
       
   156                           unsigned *line,
       
   157                           unsigned *column = 0,
       
   158                           const StringLiteral **fileName = 0) const;
       
   159 
       
   160     void pushLineOffset(unsigned offset);
       
   161     void pushPreprocessorLine(unsigned offset,
       
   162                               unsigned line,
       
   163                               const StringLiteral *fileName);
       
   164 
       
   165     unsigned findPreviousLineOffset(unsigned tokenIndex) const;
       
   166 
       
   167 public:
       
   168     struct PPLine {
       
   169         unsigned offset;
       
   170         unsigned line;
       
   171         const StringLiteral *fileName;
       
   172 
       
   173         PPLine(unsigned offset = 0,
       
   174                unsigned line = 0,
       
   175                const StringLiteral *fileName = 0)
       
   176             : offset(offset), line(line), fileName(fileName)
       
   177         { }
       
   178 
       
   179         bool operator == (const PPLine &other) const
       
   180         { return offset == other.offset; }
       
   181 
       
   182         bool operator != (const PPLine &other) const
       
   183         { return offset != other.offset; }
       
   184 
       
   185         bool operator < (const PPLine &other) const
       
   186         { return offset < other.offset; }
       
   187     };
       
   188 
       
   189 private:
       
   190     unsigned findLineNumber(unsigned offset) const;
       
   191     unsigned findColumnNumber(unsigned offset, unsigned lineNumber) const;
       
   192     PPLine findPreprocessorLine(unsigned offset) const;
       
   193     void showErrorLine(unsigned index, unsigned column, FILE *out);
       
   194 
       
   195     Control *_control;
       
   196     const StringLiteral *_fileId;
       
   197     const char *_firstSourceChar;
       
   198     const char *_lastSourceChar;
       
   199     Array<Token, 8> *_tokens;
       
   200     std::vector<unsigned> _lineOffsets;
       
   201     std::vector<PPLine> _ppLines;
       
   202     MemoryPool *_pool;
       
   203     AST *_ast;
       
   204     TranslationUnit *_previousTranslationUnit;
       
   205     struct Flags {
       
   206         unsigned _tokenized: 1;
       
   207         unsigned _parsed: 1;
       
   208         unsigned _blockErrors: 1;
       
   209         unsigned _skipFunctionBody: 1;
       
   210         unsigned _qtMocRunEnabled: 1;
       
   211         unsigned _objCEnabled: 1;
       
   212     };
       
   213     union {
       
   214         unsigned _flags;
       
   215         Flags f;
       
   216     };
       
   217 };
       
   218 
       
   219 } // end of namespace CPlusPlus
       
   220 
       
   221 
       
   222 #endif // CPLUSPLUS_TRANSLATIONUNIT_H