TranslationUnit.h 7.04 KB
Newer Older
con's avatar
con committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef CPLUSPLUS_TRANSLATIONUNIT_H
#define CPLUSPLUS_TRANSLATIONUNIT_H

#include "CPlusPlusForwardDeclarations.h"
#include "ASTfwd.h"
#include "Token.h"
27
#include "DiagnosticClient.h"
28 29
#include <cstdio>
#include <vector>
30 31 32 33 34 35 36 37 38 39 40 41

#if !(__cplusplus > 199711L || __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER >= 1600 || defined( _LIBCPP_VERSION ))
#define USE_TR1
#endif

#if defined(_MSC_VER) && _MSC_VER < 1600
#  include <map>
#elif defined(USE_TR1)
#  include <tr1/unordered_map>
#else
#  include <unordered_map>
#endif
Roberto Raggi's avatar
Roberto Raggi committed
42 43

namespace CPlusPlus {
con's avatar
con committed
44 45 46 47 48 49 50

class CPLUSPLUS_EXPORT TranslationUnit
{
    TranslationUnit(const TranslationUnit &other);
    void operator =(const TranslationUnit &other);

public:
Roberto Raggi's avatar
Roberto Raggi committed
51
    TranslationUnit(Control *control, const StringLiteral *fileId);
con's avatar
con committed
52 53 54 55
    ~TranslationUnit();

    Control *control() const;

Roberto Raggi's avatar
Roberto Raggi committed
56
    const StringLiteral *fileId() const;
con's avatar
con committed
57 58 59 60 61 62 63 64 65
    const char *fileName() const;
    unsigned fileNameLength() const;

    const char *firstSourceChar() const;
    const char *lastSourceChar() const;
    unsigned sourceLength() const;

    void setSource(const char *source, unsigned size);

66 67 68 69 70
    unsigned tokenCount() const { return _tokens ? unsigned(_tokens->size()) : unsigned(0); }
    const Token &tokenAt(unsigned index) const
    { return _tokens && index < tokenCount() ? (*_tokens)[index] : nullToken; }

    int tokenKind(unsigned index) const { return tokenAt(index).f.kind; }
71
    const char *spell(unsigned index) const;
con's avatar
con committed
72

73 74 75
    unsigned commentCount() const;
    const Token &commentAt(unsigned index) const;

con's avatar
con committed
76
    unsigned matchingBrace(unsigned index) const;
Roberto Raggi's avatar
Roberto Raggi committed
77 78 79 80
    const Identifier *identifier(unsigned index) const;
    const Literal *literal(unsigned index) const;
    const StringLiteral *stringLiteral(unsigned index) const;
    const NumericLiteral *numericLiteral(unsigned index) const;
con's avatar
con committed
81 82

    MemoryPool *memoryPool() const;
83
    AST *ast() const;
con's avatar
con committed
84

85
    bool blockErrors() const { return f._blockErrors; }
86 87 88 89 90 91
    bool blockErrors(bool block)
    {
        const bool previous = f._blockErrors;
        f._blockErrors = block;
        return previous;
    }
con's avatar
con committed
92 93 94 95 96

    void warning(unsigned index, const char *fmt, ...);
    void error(unsigned index, const char *fmt, ...);
    void fatal(unsigned index, const char *fmt, ...);

97 98 99
    void message(DiagnosticClient::Level level, unsigned index,
                 const char *format, va_list ap);

con's avatar
con committed
100 101 102 103 104 105 106
    bool isTokenized() const;
    void tokenize();

    bool skipFunctionBody() const;
    void setSkipFunctionBody(bool skipFunctionBody);

    bool isParsed() const;
107 108 109 110 111

    enum ParseMode {
        ParseTranlationUnit,
        ParseDeclaration,
        ParseExpression,
112
        ParseDeclarator,
113 114 115 116
        ParseStatement
    };

    bool parse(ParseMode mode = ParseTranlationUnit);
con's avatar
con committed
117 118 119 120

    void resetAST();
    void release();

Roberto Raggi's avatar
Roberto Raggi committed
121 122
    void getTokenStartPosition(unsigned index, unsigned *line,
                               unsigned *column = 0,
Roberto Raggi's avatar
Roberto Raggi committed
123
                               const StringLiteral **fileName = 0) const;
Roberto Raggi's avatar
Roberto Raggi committed
124 125 126

    void getTokenEndPosition(unsigned index, unsigned *line,
                             unsigned *column = 0,
Roberto Raggi's avatar
Roberto Raggi committed
127
                             const StringLiteral **fileName = 0) const;
Roberto Raggi's avatar
Roberto Raggi committed
128

con's avatar
con committed
129 130 131
    void getPosition(unsigned offset,
                     unsigned *line,
                     unsigned *column = 0,
Roberto Raggi's avatar
Roberto Raggi committed
132
                     const StringLiteral **fileName = 0) const;
con's avatar
con committed
133 134 135 136

    void getTokenPosition(unsigned index,
                          unsigned *line,
                          unsigned *column = 0,
Roberto Raggi's avatar
Roberto Raggi committed
137
                          const StringLiteral **fileName = 0) const;
con's avatar
con committed
138 139 140 141

    void pushLineOffset(unsigned offset);
    void pushPreprocessorLine(unsigned offset,
                              unsigned line,
Roberto Raggi's avatar
Roberto Raggi committed
142
                              const StringLiteral *fileName);
con's avatar
con committed
143

144 145
    unsigned findPreviousLineOffset(unsigned tokenIndex) const;

146 147
    bool maybeSplitGreaterGreaterToken(unsigned tokenIndex);

148 149 150
    LanguageFeatures languageFeatures() const { return _languageFeatures; }
    void setLanguageFeatures(LanguageFeatures features) { _languageFeatures = features; }

151
private:
con's avatar
con committed
152 153 154
    struct PPLine {
        unsigned offset;
        unsigned line;
Roberto Raggi's avatar
Roberto Raggi committed
155
        const StringLiteral *fileName;
con's avatar
con committed
156 157 158

        PPLine(unsigned offset = 0,
               unsigned line = 0,
Roberto Raggi's avatar
Roberto Raggi committed
159
               const StringLiteral *fileName = 0)
con's avatar
con committed
160 161 162 163 164 165 166 167 168 169 170 171 172
            : offset(offset), line(line), fileName(fileName)
        { }

        bool operator == (const PPLine &other) const
        { return offset == other.offset; }

        bool operator != (const PPLine &other) const
        { return offset != other.offset; }

        bool operator < (const PPLine &other) const
        { return offset < other.offset; }
    };

173
    void releaseTokensAndComments();
con's avatar
con committed
174 175 176 177 178
    unsigned findLineNumber(unsigned offset) const;
    unsigned findColumnNumber(unsigned offset, unsigned lineNumber) const;
    PPLine findPreprocessorLine(unsigned offset) const;
    void showErrorLine(unsigned index, unsigned column, FILE *out);

179 180
    static const Token nullToken;

con's avatar
con committed
181
    Control *_control;
Roberto Raggi's avatar
Roberto Raggi committed
182
    const StringLiteral *_fileId;
con's avatar
con committed
183 184
    const char *_firstSourceChar;
    const char *_lastSourceChar;
185
    std::vector<Token> *_tokens;
186
    std::vector<Token> *_comments;
con's avatar
con committed
187 188
    std::vector<unsigned> _lineOffsets;
    std::vector<PPLine> _ppLines;
189 190 191 192 193 194 195 196 197
#if defined(_MSC_VER) && _MSC_VER < 1600
    // MSVC2008 and earlier do not implement TR1.
    typedef std::map<unsigned, std::pair<unsigned, unsigned> > TokenLineColumn;
#elif defined(USE_TR1)
    typedef std::tr1::unordered_map<unsigned, std::pair<unsigned, unsigned> > TokenLineColumn;
#else
    typedef std::unordered_map<unsigned, std::pair<unsigned, unsigned> > TokenLineColumn;
#endif
    TokenLineColumn _expandedLineColumn;
con's avatar
con committed
198
    MemoryPool *_pool;
199
    AST *_ast;
con's avatar
con committed
200
    TranslationUnit *_previousTranslationUnit;
201 202 203 204 205 206
    struct Flags {
        unsigned _tokenized: 1;
        unsigned _parsed: 1;
        unsigned _blockErrors: 1;
        unsigned _skipFunctionBody: 1;
    };
con's avatar
con committed
207 208
    union {
        unsigned _flags;
209
        Flags f;
con's avatar
con committed
210
    };
211
    LanguageFeatures _languageFeatures;
con's avatar
con committed
212 213
};

214
} // namespace CPlusPlus
Roberto Raggi's avatar
Roberto Raggi committed
215

con's avatar
con committed
216 217

#endif // CPLUSPLUS_TRANSLATIONUNIT_H