Files
qt-creator/src/libs/glsl/glslsemantic.cpp

664 lines
16 KiB
C++
Raw Normal View History

2010-11-25 10:29:57 +01:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "glslsemantic.h"
2010-11-25 13:06:41 +01:00
#include "glslengine.h"
2010-11-25 13:27:03 +01:00
#include "glslparser.h"
2010-11-25 14:55:43 +01:00
#include "glslsymbols.h"
#include "glsltypes.h"
2010-11-25 13:27:03 +01:00
#include <QtCore/QDebug>
2010-11-25 10:29:57 +01:00
using namespace GLSL;
2010-11-25 13:06:41 +01:00
Semantic::Semantic(Engine *engine)
: _engine(engine)
2010-11-25 14:55:43 +01:00
, _scope(0)
2010-11-26 12:08:43 +01:00
, _type(0)
2010-11-25 10:29:57 +01:00
{
}
Semantic::~Semantic()
{
}
2010-11-25 14:55:43 +01:00
Scope *Semantic::switchScope(Scope *scope)
{
Scope *previousScope = _scope;
_scope = scope;
return previousScope;
}
2010-11-26 12:08:43 +01:00
Semantic::ExprResult Semantic::expression(ExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
Semantic::ExprResult r(_engine->undefinedType());
std::swap(_expr, r);
2010-11-25 10:29:57 +01:00
accept(ast);
2010-11-26 12:08:43 +01:00
std::swap(_expr, r);
return r;
2010-11-25 10:29:57 +01:00
}
void Semantic::statement(StatementAST *ast)
2010-11-25 10:29:57 +01:00
{
accept(ast);
}
2010-11-25 13:06:41 +01:00
const Type *Semantic::type(TypeAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:06:41 +01:00
const Type *t = _engine->undefinedType();
std::swap(_type, t);
2010-11-25 10:29:57 +01:00
accept(ast);
2010-11-25 13:06:41 +01:00
std::swap(_type, t);
return t;
2010-11-25 10:29:57 +01:00
}
void Semantic::declaration(DeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
accept(ast);
}
2010-11-25 14:55:43 +01:00
Scope *Semantic::translationUnit(TranslationUnitAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 11:33:49 +01:00
Namespace *globalScope = _engine->newNamespace();
2010-11-25 14:55:43 +01:00
Scope *previousScope = switchScope(globalScope);
for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) {
DeclarationAST *decl = it->value;
declaration(decl);
}
return switchScope(previousScope);
2010-11-25 10:29:57 +01:00
}
void Semantic::functionIdentifier(FunctionIdentifierAST *ast)
2010-11-25 10:29:57 +01:00
{
accept(ast);
}
2010-11-25 14:55:43 +01:00
Symbol *Semantic::field(StructTypeAST::Field *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 14:55:43 +01:00
// 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);
2010-11-25 10:29:57 +01:00
}
bool Semantic::visit(TranslationUnitAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 14:55:43 +01:00
Q_UNUSED(ast);
Q_ASSERT(!"unreachable");
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(FunctionIdentifierAST *ast)
2010-11-25 10:29:57 +01:00
{
// ast->name
2010-11-25 13:06:41 +01:00
const Type *ty = type(ast->type);
Q_UNUSED(ty);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(StructTypeAST::Field *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 14:55:43 +01:00
Q_UNUSED(ast);
Q_ASSERT(!"unreachable");
2010-11-25 10:29:57 +01:00
return false;
}
// expressions
bool Semantic::visit(IdentifierExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:30:12 +01:00
if (ast->name) {
if (Symbol *s = _scope->lookup(*ast->name)) {
_expr.type = s->type();
} else {
if (ast->name->startsWith(QLatin1String("gl_")) || ast->name->startsWith(QLatin1String("qgl_"))) {
// ### well, at least for now.
} else {
_engine->error(ast->lineno, QString("Undefined symbol `%1'").arg(*ast->name));
}
}
}
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(LiteralExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
Q_UNUSED(ast);
return false;
}
bool Semantic::visit(BinaryExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult left = expression(ast->left);
ExprResult right = expression(ast->right);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(UnaryExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(TernaryExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult first = expression(ast->first);
ExprResult second = expression(ast->second);
ExprResult third = expression(ast->third);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(AssignmentExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult variable = expression(ast->variable);
ExprResult value = expression(ast->value);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(MemberAccessExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
// ast->field
return false;
}
bool Semantic::visit(FunctionCallExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
functionIdentifier(ast->id);
for (List<ExpressionAST *> *it = ast->arguments; it; it = it->next) {
2010-11-26 12:08:43 +01:00
ExprResult arg = expression(it->value);
2010-11-25 10:29:57 +01:00
}
return false;
}
bool Semantic::visit(DeclarationExpressionAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:06:41 +01:00
const Type *ty = type(ast->type);
Q_UNUSED(ty);
2010-11-25 10:29:57 +01:00
// ast->name
2010-11-26 12:08:43 +01:00
ExprResult initializer = expression(ast->initializer);
2010-11-25 10:29:57 +01:00
return false;
}
// statements
bool Semantic::visit(ExpressionStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(CompoundStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 11:33:49 +01:00
Scope *previousScope = switchScope(_engine->newBlock(_scope));
for (List<StatementAST *> *it = ast->statements; it; it = it->next) {
StatementAST *stmt = it->value;
2010-11-25 10:29:57 +01:00
statement(stmt);
}
2010-11-26 11:33:49 +01:00
(void) switchScope(previousScope);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(IfStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult condition = expression(ast->condition);
2010-11-25 10:29:57 +01:00
statement(ast->thenClause);
statement(ast->elseClause);
return false;
}
bool Semantic::visit(WhileStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult condition = expression(ast->condition);
2010-11-25 10:29:57 +01:00
statement(ast->body);
return false;
}
bool Semantic::visit(DoStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
statement(ast->body);
2010-11-26 12:08:43 +01:00
ExprResult condition = expression(ast->condition);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(ForStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
statement(ast->init);
2010-11-26 12:08:43 +01:00
ExprResult condition = expression(ast->condition);
ExprResult increment = expression(ast->increment);
2010-11-25 10:29:57 +01:00
statement(ast->body);
return false;
}
bool Semantic::visit(JumpStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
Q_UNUSED(ast);
return false;
}
bool Semantic::visit(ReturnStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(SwitchStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
statement(ast->body);
return false;
}
bool Semantic::visit(CaseLabelStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:08:43 +01:00
ExprResult expr = expression(ast->expr);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(DeclarationStatementAST *ast)
2010-11-25 10:29:57 +01:00
{
declaration(ast->decl);
2010-11-25 10:29:57 +01:00
return false;
}
// types
bool Semantic::visit(BasicTypeAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:27:03 +01:00
switch (ast->token) {
case Parser::T_VOID:
_type = _engine->voidType();
break;
case Parser::T_BOOL:
_type = _engine->boolType();
break;
case Parser::T_INT:
_type = _engine->intType();
break;
case Parser::T_UINT:
_type = _engine->uintType();
break;
case Parser::T_FLOAT:
_type = _engine->floatType();
break;
case Parser::T_DOUBLE:
_type = _engine->doubleType();
break;
// bvec
case Parser::T_BVEC2:
_type = _engine->vectorType(_engine->boolType(), 2);
break;
case Parser::T_BVEC3:
_type = _engine->vectorType(_engine->boolType(), 3);
break;
case Parser::T_BVEC4:
_type = _engine->vectorType(_engine->boolType(), 4);
break;
// ivec
case Parser::T_IVEC2:
_type = _engine->vectorType(_engine->intType(), 2);
break;
case Parser::T_IVEC3:
_type = _engine->vectorType(_engine->intType(), 3);
break;
case Parser::T_IVEC4:
_type = _engine->vectorType(_engine->intType(), 4);
break;
// uvec
case Parser::T_UVEC2:
_type = _engine->vectorType(_engine->uintType(), 2);
break;
case Parser::T_UVEC3:
_type = _engine->vectorType(_engine->uintType(), 3);
break;
case Parser::T_UVEC4:
_type = _engine->vectorType(_engine->uintType(), 4);
break;
// vec
case Parser::T_VEC2:
_type = _engine->vectorType(_engine->floatType(), 2);
break;
case Parser::T_VEC3:
_type = _engine->vectorType(_engine->floatType(), 3);
break;
case Parser::T_VEC4:
_type = _engine->vectorType(_engine->floatType(), 4);
break;
// dvec
case Parser::T_DVEC2:
_type = _engine->vectorType(_engine->doubleType(), 2);
break;
case Parser::T_DVEC3:
_type = _engine->vectorType(_engine->doubleType(), 3);
break;
case Parser::T_DVEC4:
_type = _engine->vectorType(_engine->doubleType(), 4);
break;
// mat2
case Parser::T_MAT2:
case Parser::T_MAT2X2:
_type = _engine->matrixType(_engine->floatType(), 2, 2);
break;
case Parser::T_MAT2X3:
_type = _engine->matrixType(_engine->floatType(), 2, 3);
break;
case Parser::T_MAT2X4:
_type = _engine->matrixType(_engine->floatType(), 2, 4);
break;
// mat3
case Parser::T_MAT3X2:
_type = _engine->matrixType(_engine->floatType(), 3, 2);
break;
case Parser::T_MAT3:
case Parser::T_MAT3X3:
_type = _engine->matrixType(_engine->floatType(), 3, 3);
break;
case Parser::T_MAT3X4:
_type = _engine->matrixType(_engine->floatType(), 3, 4);
break;
// mat4
case Parser::T_MAT4X2:
_type = _engine->matrixType(_engine->floatType(), 4, 2);
break;
case Parser::T_MAT4X3:
_type = _engine->matrixType(_engine->floatType(), 4, 3);
break;
case Parser::T_MAT4:
case Parser::T_MAT4X4:
_type = _engine->matrixType(_engine->floatType(), 4, 4);
break;
// dmat2
case Parser::T_DMAT2:
case Parser::T_DMAT2X2:
_type = _engine->matrixType(_engine->doubleType(), 2, 2);
break;
case Parser::T_DMAT2X3:
_type = _engine->matrixType(_engine->doubleType(), 2, 3);
break;
case Parser::T_DMAT2X4:
_type = _engine->matrixType(_engine->doubleType(), 2, 4);
break;
// dmat3
case Parser::T_DMAT3X2:
_type = _engine->matrixType(_engine->doubleType(), 3, 2);
break;
case Parser::T_DMAT3:
case Parser::T_DMAT3X3:
_type = _engine->matrixType(_engine->doubleType(), 3, 3);
break;
case Parser::T_DMAT3X4:
_type = _engine->matrixType(_engine->doubleType(), 3, 4);
break;
// dmat4
case Parser::T_DMAT4X2:
_type = _engine->matrixType(_engine->doubleType(), 4, 2);
break;
case Parser::T_DMAT4X3:
_type = _engine->matrixType(_engine->doubleType(), 4, 3);
break;
case Parser::T_DMAT4:
case Parser::T_DMAT4X4:
_type = _engine->matrixType(_engine->doubleType(), 4, 4);
break;
// samplers
case Parser::T_SAMPLER1D:
case Parser::T_SAMPLER2D:
case Parser::T_SAMPLER3D:
case Parser::T_SAMPLERCUBE:
case Parser::T_SAMPLER1DSHADOW:
case Parser::T_SAMPLER2DSHADOW:
case Parser::T_SAMPLERCUBESHADOW:
case Parser::T_SAMPLER1DARRAY:
case Parser::T_SAMPLER2DARRAY:
case Parser::T_SAMPLER1DARRAYSHADOW:
case Parser::T_SAMPLER2DARRAYSHADOW:
case Parser::T_SAMPLERCUBEARRAY:
case Parser::T_SAMPLERCUBEARRAYSHADOW:
case Parser::T_SAMPLER2DRECT:
case Parser::T_SAMPLER2DRECTSHADOW:
case Parser::T_SAMPLERBUFFER:
case Parser::T_SAMPLER2DMS:
case Parser::T_SAMPLER2DMSARRAY:
case Parser::T_ISAMPLER1D:
case Parser::T_ISAMPLER2D:
case Parser::T_ISAMPLER3D:
case Parser::T_ISAMPLERCUBE:
case Parser::T_ISAMPLER1DARRAY:
case Parser::T_ISAMPLER2DARRAY:
case Parser::T_ISAMPLERCUBEARRAY:
case Parser::T_ISAMPLER2DRECT:
case Parser::T_ISAMPLERBUFFER:
case Parser::T_ISAMPLER2DMS:
case Parser::T_ISAMPLER2DMSARRAY:
case Parser::T_USAMPLER1D:
case Parser::T_USAMPLER2D:
case Parser::T_USAMPLER3D:
case Parser::T_USAMPLERCUBE:
case Parser::T_USAMPLER1DARRAY:
case Parser::T_USAMPLER2DARRAY:
case Parser::T_USAMPLERCUBEARRAY:
case Parser::T_USAMPLER2DRECT:
case Parser::T_USAMPLERBUFFER:
case Parser::T_USAMPLER2DMS:
case Parser::T_USAMPLER2DMSARRAY:
_type = _engine->samplerType(ast->token);
break;
2010-11-25 13:27:03 +01:00
default:
2010-11-26 12:20:53 +01:00
_engine->error(ast->lineno, QString("Unknown type `%1'").arg(QLatin1String(GLSLParserTable::spell[ast->token])));
2010-11-25 13:27:03 +01:00
}
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(NamedTypeAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-26 12:20:53 +01:00
if (ast->name) {
if (Symbol *s = _scope->lookup(*ast->name)) {
if (Struct *ty = s->asStruct()) {
_expr.type = ty;
_expr.isConstant = false;
return false;
}
}
_engine->error(ast->lineno, QString("Undefined type `%1'").arg(*ast->name));
}
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(ArrayTypeAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:06:41 +01:00
const Type *elementType = type(ast->elementType);
Q_UNUSED(elementType);
2010-11-26 12:08:43 +01:00
ExprResult size = expression(ast->size);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(StructTypeAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 14:55:43 +01:00
Struct *s = _engine->newStruct(_scope);
if (ast->name)
s->setName(*ast->name);
2010-11-26 12:20:53 +01:00
if (Scope *e = s->scope())
e->add(s);
2010-11-25 14:55:43 +01:00
Scope *previousScope = switchScope(s);
for (List<StructTypeAST::Field *> *it = ast->fields; it; it = it->next) {
StructTypeAST::Field *f = it->value;
2010-11-25 14:55:43 +01:00
if (Symbol *member = field(f))
s->add(member);
2010-11-25 10:29:57 +01:00
}
2010-11-25 14:55:43 +01:00
(void) switchScope(previousScope);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(QualifiedTypeAST *ast)
2010-11-25 10:29:57 +01:00
{
accept(ast->type);
for (List<LayoutQualifier *> *it = ast->layout_list; it; it = it->next) {
LayoutQualifier *q = it->value;
// q->name;
// q->number;
Q_UNUSED(q);
}
return false;
}
// declarations
bool Semantic::visit(PrecisionDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:06:41 +01:00
const Type *ty = type(ast->type);
Q_UNUSED(ty);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(ParameterDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 14:55:43 +01:00
Q_UNUSED(ast);
Q_ASSERT(!"unreachable");
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(VariableDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:06:41 +01:00
const Type *ty = type(ast->type);
2010-11-26 12:08:43 +01:00
ExprResult initializer = expression(ast->initializer);
if (ast->name) {
Variable *var = _engine->newVariable(_scope, *ast->name, ty);
_scope->add(var);
}
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(TypeDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 13:06:41 +01:00
const Type *ty = type(ast->type);
Q_UNUSED(ty);
2010-11-25 10:29:57 +01:00
return false;
}
bool Semantic::visit(TypeAndVariableDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
declaration(ast->typeDecl);
declaration(ast->varDecl);
return false;
}
bool Semantic::visit(InvariantDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
Q_UNUSED(ast);
return false;
}
bool Semantic::visit(InitDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
for (List<DeclarationAST *> *it = ast->decls; it; it = it->next) {
DeclarationAST *decl = it->value;
2010-11-25 10:29:57 +01:00
declaration(decl);
}
return false;
}
bool Semantic::visit(FunctionDeclarationAST *ast)
2010-11-25 10:29:57 +01:00
{
2010-11-25 14:55:43 +01:00
Function *fun = _engine->newFunction(_scope);
if (ast->name)
fun->setName(*ast->name);
fun->setReturnType(type(ast->returnType));
for (List<ParameterDeclarationAST *> *it = ast->params; it; it = it->next) {
ParameterDeclarationAST *decl = it->value;
2010-11-25 14:55:43 +01:00
parameterDeclaration(decl, fun);
2010-11-25 10:29:57 +01:00
}
2010-11-25 14:55:43 +01:00
if (Scope *enclosingScope = fun->scope())
enclosingScope->add(fun);
Scope *previousScope = switchScope(fun);
2010-11-25 10:29:57 +01:00
statement(ast->body);
2010-11-25 14:55:43 +01:00
(void) switchScope(previousScope);
2010-11-25 10:29:57 +01:00
return false;
}