Added GLSL::OverloadSet.

This commit is contained in:
Roberto Raggi
2010-11-26 11:33:49 +01:00
parent f891916afb
commit 57e8816001
10 changed files with 166 additions and 2 deletions

View File

@@ -72,6 +72,8 @@ class Function;
class Argument; class Argument;
class Block; class Block;
class Variable; class Variable;
class OverloadSet;
class Namespace;
class AST; class AST;
template <typename T> class List; template <typename T> class List;

View File

@@ -179,6 +179,24 @@ void Engine::addDiagnosticMessage(const DiagnosticMessage &m)
_diagnosticMessages.append(m); _diagnosticMessages.append(m);
} }
void Engine::warning(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Warning);
m.setLine(line);
m.setMessage(message);
addDiagnosticMessage(m);
}
void Engine::error(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Error);
m.setLine(line);
m.setMessage(message);
addDiagnosticMessage(m);
}
QSet<QString> Engine::identifiers() const QSet<QString> Engine::identifiers() const
{ {
return _identifiers; return _identifiers;
@@ -194,6 +212,13 @@ bool GLSL::DiagnosticMessage::isWarning() const
return _kind == Warning; return _kind == Warning;
} }
Namespace *Engine::newNamespace()
{
Namespace *s = new Namespace();
_symbols.append(s);
return s;
}
Struct *Engine::newStruct(Scope *scope) Struct *Engine::newStruct(Scope *scope)
{ {
Struct *s = new Struct(scope); Struct *s = new Struct(scope);
@@ -232,3 +257,4 @@ Variable *Engine::newVariable(Scope *scope, const QString &name, const Type *typ
_symbols.append(var); _symbols.append(var);
return var; return var;
} }

View File

@@ -111,6 +111,7 @@ public:
const MatrixType *matrixType(const Type *elementType, int columns, int rows); const MatrixType *matrixType(const Type *elementType, int columns, int rows);
// symbols // symbols
Namespace *newNamespace();
Struct *newStruct(Scope *scope = 0); Struct *newStruct(Scope *scope = 0);
Block *newBlock(Scope *scope = 0); Block *newBlock(Scope *scope = 0);
Function *newFunction(Scope *scope = 0); Function *newFunction(Scope *scope = 0);
@@ -122,6 +123,8 @@ public:
QList<DiagnosticMessage> diagnosticMessages() const; QList<DiagnosticMessage> diagnosticMessages() const;
void clearDiagnosticMessages(); void clearDiagnosticMessages();
void addDiagnosticMessage(const DiagnosticMessage &m); void addDiagnosticMessage(const DiagnosticMessage &m);
void warning(int line, const QString &message);
void error(int line, const QString &message);
private: private:
QSet<QString> _identifiers; QSet<QString> _identifiers;

View File

@@ -80,7 +80,7 @@ void Semantic::declaration(DeclarationAST *ast)
Scope *Semantic::translationUnit(TranslationUnitAST *ast) Scope *Semantic::translationUnit(TranslationUnitAST *ast)
{ {
Block *globalScope = _engine->newBlock(); Namespace *globalScope = _engine->newNamespace();
Scope *previousScope = switchScope(globalScope); Scope *previousScope = switchScope(globalScope);
for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) { for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) {
DeclarationAST *decl = it->value; DeclarationAST *decl = it->value;
@@ -216,10 +216,12 @@ bool Semantic::visit(ExpressionStatementAST *ast)
bool Semantic::visit(CompoundStatementAST *ast) bool Semantic::visit(CompoundStatementAST *ast)
{ {
Scope *previousScope = switchScope(_engine->newBlock(_scope));
for (List<StatementAST *> *it = ast->statements; it; it = it->next) { for (List<StatementAST *> *it = ast->statements; it; it = it->next) {
StatementAST *stmt = it->value; StatementAST *stmt = it->value;
statement(stmt); statement(stmt);
} }
(void) switchScope(previousScope);
return false; return false;
} }

View File

@@ -56,6 +56,8 @@ public:
virtual Argument *asArgument() { return 0; } virtual Argument *asArgument() { return 0; }
virtual Block *asBlock() { return 0; } virtual Block *asBlock() { return 0; }
virtual Variable *asVariable() { return 0; } virtual Variable *asVariable() { return 0; }
virtual OverloadSet *asOverloadSet() { return 0; }
virtual Namespace *asNamespace() { return 0; }
virtual const Type *type() const = 0; virtual const Type *type() const = 0;

View File

@@ -29,7 +29,7 @@
#include "glsltypes.h" #include "glsltypes.h"
#include "glslsymbols.h" #include "glslsymbols.h"
#include <QtCore/qglobal.h> #include <QtCore/QDebug>
using namespace GLSL; using namespace GLSL;
@@ -85,3 +85,45 @@ void Variable::setType(const Type *type)
{ {
_type = type; _type = type;
} }
Namespace::Namespace()
{
}
Namespace::~Namespace()
{
qDeleteAll(_overloadSets);
}
void Namespace::add(Symbol *symbol)
{
Symbol *&sym = _members[symbol->name()];
if (! sym)
sym = symbol;
else if (Function *fun = symbol->asFunction()) {
if (OverloadSet *o = sym->asOverloadSet()) {
o->addFunction(fun);
} else if (Function *firstFunction = sym->asFunction()) {
OverloadSet *o = new OverloadSet(this);
_overloadSets.append(o);
o->addFunction(firstFunction);
o->addFunction(fun);
sym = o;
}
else {
// ### warning? return false?
}
} else {
// ### warning? return false?
}
}
const Type *Namespace::type() const
{
return 0;
}
Symbol *Namespace::find(const QString &name) const
{
return _members.value(name);
}

View File

@@ -81,6 +81,24 @@ private:
QHash<QString, Symbol *> _members; QHash<QString, Symbol *> _members;
}; };
class GLSL_EXPORT Namespace: public Scope
{
public:
Namespace();
virtual ~Namespace();
void add(Symbol *symbol);
virtual Namespace *asNamespace() { return this; }
virtual const Type *type() const;
virtual Symbol *find(const QString &name) const;
private:
QHash<QString, Symbol *> _members;
QVector<OverloadSet *> _overloadSets;
};
} // end of namespace GLSL } // end of namespace GLSL
#endif // GLSLSYMBOLS_H #endif // GLSLSYMBOLS_H

View File

@@ -51,6 +51,7 @@ public:
virtual const MatrixType *asMatrixType() const { return 0; } virtual const MatrixType *asMatrixType() const { return 0; }
virtual const ArrayType *asArrayType() const { return 0; } virtual const ArrayType *asArrayType() const { return 0; }
virtual const SamplerType *asSamplerType() const { return 0; } virtual const SamplerType *asSamplerType() const { return 0; }
virtual const OverloadSet *asOverloadSetType() const { return 0; }
virtual const Struct *asStructType() const { return 0; } virtual const Struct *asStructType() const { return 0; }
virtual const Function *asFunctionType() const { return 0; } virtual const Function *asFunctionType() const { return 0; }

View File

@@ -390,3 +390,48 @@ bool SamplerType::isLessThan(const Type *other) const
Q_ASSERT(samp != 0); Q_ASSERT(samp != 0);
return _kind < samp->kind(); return _kind < samp->kind();
} }
OverloadSet::OverloadSet(Scope *enclosingScope)
: Scope(enclosingScope)
{
}
QVector<Function *> OverloadSet::functions() const
{
return _functions;
}
void OverloadSet::addFunction(Function *function)
{
_functions.append(function);
}
const Type *OverloadSet::type() const
{
return this;
}
Symbol *OverloadSet::find(const QString &) const
{
return 0;
}
void OverloadSet::add(Symbol *symbol)
{
if (symbol) {
if (Function *fun = symbol->asFunction())
addFunction(fun);
}
}
bool OverloadSet::isEqualTo(const Type *other) const
{
Q_UNUSED(other);
return false;
}
bool OverloadSet::isLessThan(const Type *other) const
{
Q_UNUSED(other);
return false;
}

View File

@@ -249,6 +249,29 @@ private:
int _kind; int _kind;
}; };
class GLSL_EXPORT OverloadSet: public Type, public Scope
{
public:
OverloadSet(Scope *enclosingScope = 0);
QVector<Function *> functions() const;
void addFunction(Function *function);
// as symbol
virtual OverloadSet *asOverloadSet() { return this; }
virtual const Type *type() const;
virtual Symbol *find(const QString &name) const;
virtual void add(Symbol *symbol);
// as type
virtual const OverloadSet *asOverloadSetType() const { return this; }
virtual bool isEqualTo(const Type *other) const;
virtual bool isLessThan(const Type *other) const;
private:
QVector<Function *> _functions;
};
} // end of namespace GLSL } // end of namespace GLSL
#endif // GLSLTYPES_H #endif // GLSLTYPES_H