2008-12-02 12:01:29 +01:00
|
|
|
// 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"
|
2010-08-10 17:30:20 +02:00
|
|
|
#include "DiagnosticClient.h"
|
2010-03-18 15:21:07 +01:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <vector>
|
2013-08-23 11:52:46 +02:00
|
|
|
|
|
|
|
|
#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
|
2009-10-20 11:21:25 +02:00
|
|
|
|
|
|
|
|
namespace CPlusPlus {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
class CPLUSPLUS_EXPORT TranslationUnit
|
|
|
|
|
{
|
|
|
|
|
TranslationUnit(const TranslationUnit &other);
|
|
|
|
|
void operator =(const TranslationUnit &other);
|
|
|
|
|
|
|
|
|
|
public:
|
2009-12-01 11:33:13 +01:00
|
|
|
TranslationUnit(Control *control, const StringLiteral *fileId);
|
2008-12-02 12:01:29 +01:00
|
|
|
~TranslationUnit();
|
|
|
|
|
|
|
|
|
|
Control *control() const;
|
|
|
|
|
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral *fileId() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
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);
|
|
|
|
|
|
2013-10-22 11:24:29 +02:00
|
|
|
unsigned tokenCount() const { return _tokens ? unsigned(_tokens->size()) : unsigned(0); }
|
|
|
|
|
const Token &tokenAt(unsigned index) const
|
|
|
|
|
{ return _tokens && index < tokenCount() ? (*_tokens)[index] : nullToken; }
|
|
|
|
|
|
2013-12-13 21:03:40 +01:00
|
|
|
int tokenKind(unsigned index) const { return tokenAt(index).kind(); }
|
2009-02-19 13:01:01 +01:00
|
|
|
const char *spell(unsigned index) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-08-24 09:05:19 +02:00
|
|
|
unsigned commentCount() const;
|
|
|
|
|
const Token &commentAt(unsigned index) const;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
unsigned matchingBrace(unsigned index) const;
|
2009-12-01 11:33:13 +01:00
|
|
|
const Identifier *identifier(unsigned index) const;
|
|
|
|
|
const Literal *literal(unsigned index) const;
|
|
|
|
|
const StringLiteral *stringLiteral(unsigned index) const;
|
|
|
|
|
const NumericLiteral *numericLiteral(unsigned index) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
MemoryPool *memoryPool() const;
|
2008-12-03 14:01:19 +01:00
|
|
|
AST *ast() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-12-10 20:42:47 +02:00
|
|
|
bool blockErrors() const { return f._blockErrors; }
|
2013-04-16 15:38:59 +02:00
|
|
|
bool blockErrors(bool block)
|
|
|
|
|
{
|
|
|
|
|
const bool previous = f._blockErrors;
|
|
|
|
|
f._blockErrors = block;
|
|
|
|
|
return previous;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
void warning(unsigned index, const char *fmt, ...);
|
|
|
|
|
void error(unsigned index, const char *fmt, ...);
|
|
|
|
|
void fatal(unsigned index, const char *fmt, ...);
|
|
|
|
|
|
2010-08-10 17:30:20 +02:00
|
|
|
void message(DiagnosticClient::Level level, unsigned index,
|
|
|
|
|
const char *format, va_list ap);
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool isTokenized() const;
|
|
|
|
|
void tokenize();
|
|
|
|
|
|
|
|
|
|
bool skipFunctionBody() const;
|
|
|
|
|
void setSkipFunctionBody(bool skipFunctionBody);
|
|
|
|
|
|
|
|
|
|
bool isParsed() const;
|
2008-12-03 14:01:19 +01:00
|
|
|
|
|
|
|
|
enum ParseMode {
|
|
|
|
|
ParseTranlationUnit,
|
|
|
|
|
ParseDeclaration,
|
|
|
|
|
ParseExpression,
|
2009-03-30 15:07:30 +02:00
|
|
|
ParseDeclarator,
|
2008-12-03 14:01:19 +01:00
|
|
|
ParseStatement
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool parse(ParseMode mode = ParseTranlationUnit);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
void resetAST();
|
|
|
|
|
void release();
|
|
|
|
|
|
2009-06-24 16:40:30 +02:00
|
|
|
void getTokenStartPosition(unsigned index, unsigned *line,
|
|
|
|
|
unsigned *column = 0,
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral **fileName = 0) const;
|
2009-06-24 16:40:30 +02:00
|
|
|
|
|
|
|
|
void getTokenEndPosition(unsigned index, unsigned *line,
|
|
|
|
|
unsigned *column = 0,
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral **fileName = 0) const;
|
2009-06-24 16:40:30 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void getPosition(unsigned offset,
|
|
|
|
|
unsigned *line,
|
|
|
|
|
unsigned *column = 0,
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral **fileName = 0) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
void getTokenPosition(unsigned index,
|
|
|
|
|
unsigned *line,
|
|
|
|
|
unsigned *column = 0,
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral **fileName = 0) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
void pushLineOffset(unsigned offset);
|
|
|
|
|
void pushPreprocessorLine(unsigned offset,
|
|
|
|
|
unsigned line,
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral *fileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-06-02 14:56:03 +02:00
|
|
|
unsigned findPreviousLineOffset(unsigned tokenIndex) const;
|
|
|
|
|
|
2012-09-18 12:57:51 +02:00
|
|
|
bool maybeSplitGreaterGreaterToken(unsigned tokenIndex);
|
|
|
|
|
|
2013-10-06 02:41:22 +02:00
|
|
|
LanguageFeatures languageFeatures() const { return _languageFeatures; }
|
|
|
|
|
void setLanguageFeatures(LanguageFeatures features) { _languageFeatures = features; }
|
|
|
|
|
|
2013-01-03 16:04:34 +01:00
|
|
|
private:
|
2008-12-02 12:01:29 +01:00
|
|
|
struct PPLine {
|
|
|
|
|
unsigned offset;
|
|
|
|
|
unsigned line;
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral *fileName;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
PPLine(unsigned offset = 0,
|
|
|
|
|
unsigned line = 0,
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral *fileName = 0)
|
2008-12-02 12:01:29 +01:00
|
|
|
: 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; }
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-22 11:24:29 +02:00
|
|
|
void releaseTokensAndComments();
|
2008-12-02 12:01:29 +01:00
|
|
|
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);
|
|
|
|
|
|
2013-10-22 11:24:29 +02:00
|
|
|
static const Token nullToken;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Control *_control;
|
2009-12-01 11:33:13 +01:00
|
|
|
const StringLiteral *_fileId;
|
2008-12-02 12:01:29 +01:00
|
|
|
const char *_firstSourceChar;
|
|
|
|
|
const char *_lastSourceChar;
|
2010-03-18 15:21:07 +01:00
|
|
|
std::vector<Token> *_tokens;
|
2011-08-24 09:05:19 +02:00
|
|
|
std::vector<Token> *_comments;
|
2008-12-02 12:01:29 +01:00
|
|
|
std::vector<unsigned> _lineOffsets;
|
|
|
|
|
std::vector<PPLine> _ppLines;
|
2013-08-23 11:52:46 +02:00
|
|
|
#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;
|
2008-12-02 12:01:29 +01:00
|
|
|
MemoryPool *_pool;
|
2008-12-03 14:01:19 +01:00
|
|
|
AST *_ast;
|
2008-12-02 12:01:29 +01:00
|
|
|
TranslationUnit *_previousTranslationUnit;
|
2009-07-27 21:47:03 +02:00
|
|
|
struct Flags {
|
|
|
|
|
unsigned _tokenized: 1;
|
|
|
|
|
unsigned _parsed: 1;
|
|
|
|
|
unsigned _blockErrors: 1;
|
|
|
|
|
unsigned _skipFunctionBody: 1;
|
|
|
|
|
};
|
2008-12-02 12:01:29 +01:00
|
|
|
union {
|
|
|
|
|
unsigned _flags;
|
2009-07-27 21:47:03 +02:00
|
|
|
Flags f;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
2013-10-06 02:41:22 +02:00
|
|
|
LanguageFeatures _languageFeatures;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2011-02-04 09:52:39 +01:00
|
|
|
} // namespace CPlusPlus
|
2009-10-20 11:21:25 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#endif // CPLUSPLUS_TRANSLATIONUNIT_H
|