Process function declarations.

This commit is contained in:
Roberto Raggi
2010-11-25 14:55:43 +01:00
parent 4f9be97600
commit e2419153a5
11 changed files with 170 additions and 74 deletions

View File

@@ -62,6 +62,8 @@ class VectorType;
class MatrixType; class MatrixType;
// symbols // symbols
class Symbol;
class Scope;
class Struct; class Struct;
class Function; class Function;
class Argument; class Argument;

View File

@@ -28,6 +28,8 @@
**************************************************************************/ **************************************************************************/
#include "glslengine.h" #include "glslengine.h"
#include "glslsymbols.h"
#include "glsltypes.h"
using namespace GLSL; using namespace GLSL;
@@ -82,6 +84,7 @@ Engine::Engine()
Engine::~Engine() Engine::~Engine()
{ {
qDeleteAll(_symbols);
} }
const QString *Engine::identifier(const QString &s) const QString *Engine::identifier(const QString &s)
@@ -181,3 +184,42 @@ bool GLSL::DiagnosticMessage::isWarning() const
{ {
return _kind == Warning; return _kind == Warning;
} }
Struct *Engine::newStruct(Scope *scope)
{
Struct *s = new Struct(scope);
_symbols.append(s);
return s;
}
Block *Engine::newBlock(Scope *scope)
{
Block *s = new Block(scope);
_symbols.append(s);
return s;
}
Function *Engine::newFunction(Scope *scope)
{
Function *s = new Function(scope);
_symbols.append(s);
return s;
}
Argument *Engine::newArgument(Function *function, const QString &name, const Type *type)
{
Argument *a = new Argument(function);
a->setName(name);
a->setType(type);
_symbols.append(a);
return a;
}
Variable *Engine::newVariable(Scope *scope, const QString &name, const Type *type)
{
Variable *var = new Variable(scope);
var->setName(name);
var->setType(type);
_symbols.append(var);
return var;
}

View File

@@ -98,6 +98,7 @@ public:
const QString *identifier(const char *s, int n); const QString *identifier(const char *s, int n);
QSet<QString> identifiers() const; QSet<QString> identifiers() const;
// types
const UndefinedType *undefinedType(); const UndefinedType *undefinedType();
const VoidType *voidType(); const VoidType *voidType();
const BoolType *boolType(); const BoolType *boolType();
@@ -108,6 +109,13 @@ public:
const VectorType *vectorType(const Type *elementType, int dimension); const VectorType *vectorType(const Type *elementType, int dimension);
const MatrixType *matrixType(const Type *elementType, int columns, int rows); const MatrixType *matrixType(const Type *elementType, int columns, int rows);
// symbols
Struct *newStruct(Scope *scope = 0);
Block *newBlock(Scope *scope = 0);
Function *newFunction(Scope *scope = 0);
Argument *newArgument(Function *function, const QString &name, const Type *type);
Variable *newVariable(Scope *scope, const QString &name, const Type *type);
MemoryPool *pool(); MemoryPool *pool();
QList<DiagnosticMessage> diagnosticMessages() const; QList<DiagnosticMessage> diagnosticMessages() const;
@@ -120,6 +128,7 @@ private:
TypeTable<MatrixType> _matrixTypes; TypeTable<MatrixType> _matrixTypes;
MemoryPool _pool; MemoryPool _pool;
QList<DiagnosticMessage> _diagnosticMessages; QList<DiagnosticMessage> _diagnosticMessages;
QList<Symbol *> _symbols;
}; };
} // namespace GLSL } // namespace GLSL

View File

@@ -30,6 +30,8 @@
#include "glslsemantic.h" #include "glslsemantic.h"
#include "glslengine.h" #include "glslengine.h"
#include "glslparser.h" #include "glslparser.h"
#include "glslsymbols.h"
#include "glsltypes.h"
#include <QtCore/QDebug> #include <QtCore/QDebug>
using namespace GLSL; using namespace GLSL;
@@ -37,6 +39,7 @@ using namespace GLSL;
Semantic::Semantic(Engine *engine) Semantic::Semantic(Engine *engine)
: _engine(engine) : _engine(engine)
, _type(0) , _type(0)
, _scope(0)
{ {
} }
@@ -44,6 +47,13 @@ Semantic::~Semantic()
{ {
} }
Scope *Semantic::switchScope(Scope *scope)
{
Scope *previousScope = _scope;
_scope = scope;
return previousScope;
}
void Semantic::expression(ExpressionAST *ast) void Semantic::expression(ExpressionAST *ast)
{ {
accept(ast); accept(ast);
@@ -68,9 +78,15 @@ void Semantic::declaration(DeclarationAST *ast)
accept(ast); accept(ast);
} }
void Semantic::translationUnit(TranslationUnitAST *ast) Scope *Semantic::translationUnit(TranslationUnitAST *ast)
{ {
accept(ast); Block *globalScope = _engine->newBlock();
Scope *previousScope = switchScope(globalScope);
for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) {
DeclarationAST *decl = it->value;
declaration(decl);
}
return switchScope(previousScope);
} }
void Semantic::functionIdentifier(FunctionIdentifierAST *ast) void Semantic::functionIdentifier(FunctionIdentifierAST *ast)
@@ -78,17 +94,30 @@ void Semantic::functionIdentifier(FunctionIdentifierAST *ast)
accept(ast); accept(ast);
} }
void Semantic::field(StructTypeAST::Field *ast) Symbol *Semantic::field(StructTypeAST::Field *ast)
{ {
accept(ast); // ast->name
const Type *ty = type(ast->type);
QString name;
if (ast->name)
name = *ast->name;
return _engine->newVariable(_scope, name, ty);
}
void Semantic::parameterDeclaration(ParameterDeclarationAST *ast, Function *fun)
{
const Type *ty = type(ast->type);
QString name;
if (ast->name)
name = *ast->name;
Argument *arg = _engine->newArgument(fun, name, ty);
fun->addArgument(arg);
} }
bool Semantic::visit(TranslationUnitAST *ast) bool Semantic::visit(TranslationUnitAST *ast)
{ {
for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) { Q_UNUSED(ast);
DeclarationAST *decl = it->value; Q_ASSERT(!"unreachable");
declaration(decl);
}
return false; return false;
} }
@@ -102,9 +131,8 @@ bool Semantic::visit(FunctionIdentifierAST *ast)
bool Semantic::visit(StructTypeAST::Field *ast) bool Semantic::visit(StructTypeAST::Field *ast)
{ {
// ast->name Q_UNUSED(ast);
const Type *ty = type(ast->type); Q_ASSERT(!"unreachable");
Q_UNUSED(ty);
return false; return false;
} }
@@ -462,11 +490,16 @@ bool Semantic::visit(ArrayTypeAST *ast)
bool Semantic::visit(StructTypeAST *ast) bool Semantic::visit(StructTypeAST *ast)
{ {
// ast->name Struct *s = _engine->newStruct(_scope);
if (ast->name)
s->setName(*ast->name);
Scope *previousScope = switchScope(s);
for (List<StructTypeAST::Field *> *it = ast->fields; it; it = it->next) { for (List<StructTypeAST::Field *> *it = ast->fields; it; it = it->next) {
StructTypeAST::Field *f = it->value; StructTypeAST::Field *f = it->value;
field(f); if (Symbol *member = field(f))
s->add(member);
} }
(void) switchScope(previousScope);
return false; return false;
} }
@@ -493,8 +526,8 @@ bool Semantic::visit(PrecisionDeclarationAST *ast)
bool Semantic::visit(ParameterDeclarationAST *ast) bool Semantic::visit(ParameterDeclarationAST *ast)
{ {
const Type *ty = type(ast->type); Q_UNUSED(ast);
Q_UNUSED(ty); Q_ASSERT(!"unreachable");
return false; return false;
} }
@@ -537,13 +570,23 @@ bool Semantic::visit(InitDeclarationAST *ast)
bool Semantic::visit(FunctionDeclarationAST *ast) bool Semantic::visit(FunctionDeclarationAST *ast)
{ {
const Type *returnType = type(ast->returnType); Function *fun = _engine->newFunction(_scope);
Q_UNUSED(returnType); if (ast->name)
fun->setName(*ast->name);
fun->setReturnType(type(ast->returnType));
for (List<ParameterDeclarationAST *> *it = ast->params; it; it = it->next) { for (List<ParameterDeclarationAST *> *it = ast->params; it; it = it->next) {
ParameterDeclarationAST *decl = it->value; ParameterDeclarationAST *decl = it->value;
declaration(decl); parameterDeclaration(decl, fun);
} }
if (Scope *enclosingScope = fun->scope())
enclosingScope->add(fun);
Scope *previousScope = switchScope(fun);
statement(ast->body); statement(ast->body);
(void) switchScope(previousScope);
return false; return false;
} }

View File

@@ -43,11 +43,14 @@ public:
void statement(StatementAST *ast); void statement(StatementAST *ast);
const Type *type(TypeAST *ast); const Type *type(TypeAST *ast);
void declaration(DeclarationAST *ast); void declaration(DeclarationAST *ast);
void translationUnit(TranslationUnitAST *ast); Scope *translationUnit(TranslationUnitAST *ast);
void functionIdentifier(FunctionIdentifierAST *ast); void functionIdentifier(FunctionIdentifierAST *ast);
void field(StructTypeAST::Field *ast); Symbol *field(StructTypeAST::Field *ast);
void parameterDeclaration(ParameterDeclarationAST *ast, Function *fun);
protected: protected:
Scope *switchScope(Scope *scope);
virtual bool visit(TranslationUnitAST *ast); virtual bool visit(TranslationUnitAST *ast);
virtual bool visit(FunctionIdentifierAST *ast); virtual bool visit(FunctionIdentifierAST *ast);
virtual bool visit(StructTypeAST::Field *ast); virtual bool visit(StructTypeAST::Field *ast);
@@ -96,6 +99,7 @@ protected:
private: private:
Engine *_engine; Engine *_engine;
const Type *_type; const Type *_type;
Scope *_scope;
}; };
} // namespace GLSL } // namespace GLSL

View File

@@ -50,6 +50,16 @@ void Symbol::setScope(Scope *scope)
_scope = scope; _scope = scope;
} }
QString Symbol::name() const
{
return _name;
}
void Symbol::setName(const QString &name)
{
_name = name;
}
Scope::Scope(Scope *enclosingScope) Scope::Scope(Scope *enclosingScope)
: Symbol(enclosingScope) : Symbol(enclosingScope)
{ {

View File

@@ -31,6 +31,7 @@
#define GLSLSYMBOL_H #define GLSLSYMBOL_H
#include "glsl.h" #include "glsl.h"
#include <QtCore/QString>
namespace GLSL { namespace GLSL {
@@ -46,6 +47,9 @@ public:
Scope *scope() const; Scope *scope() const;
void setScope(Scope *scope); void setScope(Scope *scope);
QString name() const;
void setName(const QString &name);
virtual Scope *asScope() { return 0; } virtual Scope *asScope() { return 0; }
virtual Struct *asStruct() { return 0; } virtual Struct *asStruct() { return 0; }
virtual Function *asFunction() { return 0; } virtual Function *asFunction() { return 0; }
@@ -57,6 +61,7 @@ public:
private: private:
Scope *_scope; Scope *_scope;
QString _name;
}; };
class GLSL_EXPORT Scope: public Symbol class GLSL_EXPORT Scope: public Symbol
@@ -65,6 +70,8 @@ public:
Scope(Scope *sscope = 0); Scope(Scope *sscope = 0);
Symbol *lookup(const QString &name) const; Symbol *lookup(const QString &name) const;
virtual void add(Symbol *symbol) = 0;
virtual Symbol *find(const QString &name) const = 0; virtual Symbol *find(const QString &name) const = 0;
virtual Scope *asScope() { return this; } virtual Scope *asScope() { return this; }

View File

@@ -39,16 +39,6 @@ Argument::Argument(Function *scope)
{ {
} }
QString Argument::name() const
{
return _name;
}
void Argument::setName(const QString &name)
{
_name = name;
}
const Type *Argument::type() const const Type *Argument::type() const
{ {
return _type; return _type;
@@ -64,9 +54,9 @@ Block::Block(Scope *enclosingScope)
{ {
} }
void Block::addMember(const QString &name, Symbol *symbol) void Block::add(Symbol *symbol)
{ {
_members.insert(name, symbol); _members.insert(symbol->name(), symbol);
} }
const Type *Block::type() const const Type *Block::type() const
@@ -86,16 +76,6 @@ Variable::Variable(Scope *scope)
{ {
} }
QString Variable::name() const
{
return _name;
}
void Variable::setName(const QString &name)
{
_name = name;
}
const Type *Variable::type() const const Type *Variable::type() const
{ {
return _type; return _type;

View File

@@ -42,16 +42,12 @@ class GLSL_EXPORT Argument: public Symbol
public: public:
Argument(Function *scope); Argument(Function *scope);
QString name() const;
void setName(const QString &name);
virtual const Type *type() const; virtual const Type *type() const;
void setType(const Type *type); void setType(const Type *type);
virtual Argument *asArgument() { return this; } virtual Argument *asArgument() { return this; }
private: private:
QString _name;
const Type *_type; const Type *_type;
}; };
@@ -60,16 +56,12 @@ class GLSL_EXPORT Variable: public Symbol
public: public:
Variable(Scope *scope); Variable(Scope *scope);
QString name() const;
void setName(const QString &name);
virtual const Type *type() const; virtual const Type *type() const;
void setType(const Type *type); void setType(const Type *type);
virtual Variable *asVariable() { return this; } virtual Variable *asVariable() { return this; }
private: private:
QString _name;
const Type *_type; const Type *_type;
}; };
@@ -78,7 +70,7 @@ class GLSL_EXPORT Block: public Scope
public: public:
Block(Scope *enclosingScope = 0); Block(Scope *enclosingScope = 0);
void addMember(const QString &name, Symbol *symbol); void add(Symbol *symbol);
virtual Block *asBlock() { return this; } virtual Block *asBlock() { return this; }

View File

@@ -195,8 +195,18 @@ bool MatrixType::isLessThan(const Type *other) const
return false; return false;
} }
Struct::Struct(Scope *scope) : Symbol(scope) void Struct::add(Symbol *member)
{ {
_members.append(member);
}
Symbol *Struct::find(const QString &name) const
{
foreach (Symbol *s, _members) {
if (s->name() == name)
return s;
}
return 0;
} }
bool Struct::isEqualTo(const Type *other) const bool Struct::isEqualTo(const Type *other) const
@@ -211,21 +221,6 @@ bool Struct::isLessThan(const Type *other) const
return false; return false;
} }
Function::Function(Scope *scope) : Scope(scope)
{
}
QString Function::name() const
{
return _name;
}
void Function::setName(const QString &name)
{
_name = name;
}
const Type *Function::returnType() const const Type *Function::returnType() const
{ {
return _returnType; return _returnType;

View File

@@ -136,10 +136,14 @@ private:
int _rows; int _rows;
}; };
class GLSL_EXPORT Struct: public Type, public Symbol class GLSL_EXPORT Struct: public Type, public Scope
{ {
public: public:
Struct(Scope *scope = 0); Struct(Scope *scope = 0)
: Scope(scope) {}
virtual void add(Symbol *member);
virtual Symbol *find(const QString &name) const;
// as Type // as Type
virtual const Struct *asStructType() const { return this; } virtual const Struct *asStructType() const { return this; }
@@ -148,15 +152,17 @@ public:
// as Symbol // as Symbol
virtual Struct *asStruct() { return this; } // as Symbol virtual Struct *asStruct() { return this; } // as Symbol
virtual const Type *type() const { return this; }
private:
QVector<Symbol *> _members;
}; };
class GLSL_EXPORT Function: public Type, public Scope class GLSL_EXPORT Function: public Type, public Scope
{ {
public: public:
Function(Scope *scope = 0); Function(Scope *scope = 0)
: Scope(scope) {}
QString name() const;
void setName(const QString &name);
const Type *returnType() const; const Type *returnType() const;
void setReturnType(const Type *returnType); void setReturnType(const Type *returnType);
@@ -177,8 +183,14 @@ public:
virtual Symbol *find(const QString &name) const; virtual Symbol *find(const QString &name) const;
virtual void add(Symbol *symbol) {
if (! symbol)
return;
else if (Argument *arg = symbol->asArgument())
addArgument(arg);
}
private: private:
QString _name;
const Type *_returnType; const Type *_returnType;
QVector<Argument *> _arguments; QVector<Argument *> _arguments;
}; };