[opengtl-commits] [190] introduce FunctionMemberAccessorExpression an accessor to call function members of a structure |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/opengtl-commits Archives
]
Revision: 190
Author: cyrille
Date: 2008-06-14 00:48:58 +0200 (Sat, 14 Jun 2008)
Log Message:
-----------
introduce FunctionMemberAccessorExpression an accessor to call function members of a structure
Modified Paths:
--------------
trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp
trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.h
trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h
trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h
Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp 2008-06-13 22:13:26 UTC (rev 189)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp 2008-06-13 22:48:58 UTC (rev 190)
@@ -28,6 +28,7 @@
#include "../VariableNG_p.h"
#include "../Visitor_p.h"
#include "../Type.h"
+#include "../Type_p.h"
using namespace GTLCore::AST;
@@ -36,7 +37,7 @@
GTLCore::ExpressionResult AccessorExpression::generateValue( GenerationContext& _gc, llvm::BasicBlock* _bb ) const
{
llvm::Value* ptr_ = pointer( _gc, _bb );
- GTL_DEBUG( *ptr_ << " " << *ptr_->getType() );
+ GTL_ASSERT(ptr_);
const Visitor* visitor = Visitor::getVisitorFor( type() );
return GTLCore::ExpressionResult( visitor->get( _gc, _bb, ptr_ ) );
}
@@ -44,14 +45,15 @@
llvm::BasicBlock* AccessorExpression::assignValue( GenerationContext& _gc, llvm::BasicBlock* _bb, const Expression* _rhs )
{
llvm::Value* ptr_ = pointer( _gc, _bb );
+ GTL_ASSERT(ptr_);
const Visitor* visitor = Visitor::getVisitorFor( type() );
return visitor->set( _gc, _bb, ptr_, type(), _rhs->generateValue( _gc, _bb).value(), _rhs->type() );
}
//------------------- ArraySizeAccessorExpression -------------------//
-ArraySizeAccessorExpression::ArraySizeAccessorExpression(AccessorExpression* _accessorExpression )
- : m_accessorExpression( _accessorExpression )
+ArraySizeAccessorExpression::ArraySizeAccessorExpression(AccessorExpression* _parent )
+ : m_parent( _parent )
{
}
@@ -71,14 +73,14 @@
std::vector<llvm::Value*> indexes;
indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the structure of the array
indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the size of the array
- return new llvm::GetElementPtrInst( m_accessorExpression->pointer( _gc, _bb),
+ return new llvm::GetElementPtrInst( m_parent->pointer( _gc, _bb),
indexes.begin(), indexes.end(), "", _bb);
}
//------------------- StructAccessorExpression -------------------//
-StructAccessorExpression::StructAccessorExpression(AccessorExpression* _accessorExpression, int _index) : m_accessorExpression(_accessorExpression), m_index(_index)
+StructAccessorExpression::StructAccessorExpression(AccessorExpression* _parent, int _index) : m_parent(_parent), m_index(_index)
{
}
@@ -88,22 +90,22 @@
std::vector<llvm::Value*> indexes;
indexes.push_back( _gc.codeGenerator()->integerToConstant(0));
indexes.push_back( _gc.codeGenerator()->integerToConstant(m_index));
- return new llvm::GetElementPtrInst( m_accessorExpression->pointer( _gc, _bb) , indexes.begin(), indexes.end(), "", _bb);
+ return new llvm::GetElementPtrInst( m_parent->pointer( _gc, _bb) , indexes.begin(), indexes.end(), "", _bb);
}
bool StructAccessorExpression::isConstant() const
{
- return m_accessorExpression->isConstant();
+ return m_parent->isConstant();
}
const GTLCore::Type* StructAccessorExpression::type() const
{
- return (*m_accessorExpression->type()->structDataMembers())[ m_index].type();
+ return (*m_parent->type()->structDataMembers())[ m_index].type();
}
//------------------- ArrayAccessorExpression -------------------//
-ArrayAccessorExpression::ArrayAccessorExpression(AccessorExpression * _accessorExpression, Expression* _index) : m_accessorExpression(_accessorExpression), m_index(_index)
+ArrayAccessorExpression::ArrayAccessorExpression(AccessorExpression * _parent, Expression* _index) : m_parent(_parent), m_index(_index)
{
}
@@ -111,17 +113,17 @@
{
GTL_DEBUG("ArrayAccessorExpression::pointer");
llvm::Value* index = m_index->generateValue(_gc, _bb).value();
- return _gc.codeGenerator()->accessArrayValue(_bb, m_accessorExpression->pointer( _gc, _bb) , index);
+ return _gc.codeGenerator()->accessArrayValue(_bb, m_parent->pointer( _gc, _bb) , index);
}
bool ArrayAccessorExpression::isConstant() const
{
- return m_accessorExpression->isConstant();
+ return m_parent->isConstant();
}
const GTLCore::Type* ArrayAccessorExpression::type() const
{
- return m_accessorExpression->type()->arrayType();
+ return m_parent->type()->arrayType();
}
//------------------- VariableAccessorExpression -------------------//
@@ -145,3 +147,32 @@
{
return m_variable->type();
}
+
+//------------------- FunctionMemberAccessorExpression -------------------//
+
+FunctionMemberAccessorExpression::FunctionMemberAccessorExpression( AccessorExpression * _parent,
+ const Type::StructFunctionMember* _member,
+ const std::list<AST::Expression*>& _arguments )
+ : m_parent( _parent ), m_member(_member), m_arguments(_arguments)
+{
+}
+
+bool FunctionMemberAccessorExpression::isConstant() const
+{
+ return true;
+}
+
+const GTLCore::Type* FunctionMemberAccessorExpression::type() const
+{
+ return m_member->returnType();
+}
+GTLCore::ExpressionResult FunctionMemberAccessorExpression::generateValue( GenerationContext& _gc, llvm::BasicBlock* bb ) const
+{
+ GTL_ASSERT( false ); // TODO implement
+ return GTLCore::ExpressionResult();
+}
+
+llvm::Value* FunctionMemberAccessorExpression::pointer(GenerationContext& _gc, llvm::BasicBlock* bb) const
+{
+ return 0;
+}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.h 2008-06-13 22:13:26 UTC (rev 189)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.h 2008-06-13 22:48:58 UTC (rev 190)
@@ -21,6 +21,7 @@
#define _AST_ACCESSOR_EXPRESSION_H_
#include "Expression.h"
+#include "../Type.h"
namespace GTLCore {
@@ -34,33 +35,33 @@
};
class ArraySizeAccessorExpression : public AccessorExpression {
public:
- ArraySizeAccessorExpression(AccessorExpression* _accessorExpression );
+ ArraySizeAccessorExpression(AccessorExpression* _parent );
virtual bool isConstant() const;
virtual const GTLCore::Type* type() const;
virtual llvm::Value* pointer(GenerationContext& _gc, llvm::BasicBlock* bb) const;
private:
- AccessorExpression * m_accessorExpression;
+ AccessorExpression * m_parent;
};
class StructAccessorExpression : public AccessorExpression {
public:
- StructAccessorExpression(AccessorExpression* _accessorExpression, int _index);
+ StructAccessorExpression(AccessorExpression* _parent, int _index);
virtual bool isConstant() const;
virtual const GTLCore::Type* type() const;
public:
virtual llvm::Value* pointer(GenerationContext& _gc, llvm::BasicBlock* bb) const;
private:
- AccessorExpression * m_accessorExpression;
+ AccessorExpression * m_parent;
int m_index;
};
class ArrayAccessorExpression : public AccessorExpression {
public:
- ArrayAccessorExpression(AccessorExpression * _accessorExpression, Expression* _index);
+ ArrayAccessorExpression(AccessorExpression * _parent, Expression* _index);
virtual bool isConstant() const;
virtual const GTLCore::Type* type() const;
public:
virtual llvm::Value* pointer(GenerationContext& _gc, llvm::BasicBlock* bb) const;
private:
- AccessorExpression * m_accessorExpression;
+ AccessorExpression * m_parent;
Expression* m_index;
};
class VariableAccessorExpression : public AccessorExpression {
@@ -73,6 +74,21 @@
private:
GTLCore::VariableNG* m_variable;
};
+ class FunctionMemberAccessorExpression : public AccessorExpression {
+ public:
+ FunctionMemberAccessorExpression( AccessorExpression * _parent,
+ const Type::StructFunctionMember* _member,
+ const std::list<AST::Expression*>& _arguments );
+ virtual bool isConstant() const;
+ virtual const GTLCore::Type* type() const;
+ public:
+ virtual GTLCore::ExpressionResult generateValue( GenerationContext& _gc, llvm::BasicBlock* bb ) const;
+ virtual llvm::Value* pointer(GenerationContext& _gc, llvm::BasicBlock* bb) const;
+ private:
+ AccessorExpression* m_parent;
+ const Type::StructFunctionMember* m_member;
+ const std::list<AST::Expression*> m_arguments;
+ };
}
}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp 2008-06-13 22:13:26 UTC (rev 189)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp 2008-06-13 22:48:58 UTC (rev 190)
@@ -156,7 +156,20 @@
} else if( isOfType( d->currentToken, GTLCore::Token::IDENTIFIER ) ) {
GTLCore::String name = d->currentToken.string;
getNextToken();
- return parseMemberArrayExpression( new AST::StructAccessorExpression( _expression , _expression->type()->d->memberToIndex( name ) ), _constantExpression);
+ if( d->currentToken.type == GTLCore::Token::STARTBRACKET )
+ {
+ getNextToken(); // Eat the start bracket
+ const Type::StructFunctionMember* sfm = _expression->type()->d->functionMember( name );
+ if( not sfm )
+ {
+ reportError("Unknown member: '" + name + "' for structure " + _expression->type()->structName(), d->currentToken );
+ } else {
+ std::list<AST::Expression*> arguments = parseArguments( sfm->name(), sfm->parameters() );
+ return new AST::FunctionMemberAccessorExpression(_expression, sfm, arguments);
+ }
+ } else {
+ return parseMemberArrayExpression( new AST::StructAccessorExpression( _expression , _expression->type()->d->memberToIndex( name ) ), _constantExpression);
+ }
}
} else if( d->currentToken.type == GTLCore::Token::STARTBOXBRACKET )
{
@@ -167,9 +180,6 @@
getNextToken();
return parseMemberArrayExpression( new AST::ArrayAccessorExpression( _expression , expr ), _constantExpression);
}
- } else if( d->currentToken.type == GTLCore::Token::STARTBRACKET )
- {
- GTL_ASSERT(false);
} else {
return _expression;
}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h 2008-06-13 22:13:26 UTC (rev 189)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h 2008-06-13 22:48:58 UTC (rev 190)
@@ -29,7 +29,6 @@
namespace GTLCore {
class ArraySize;
- class MemberArray;
class VariableNG;
class Type;
class ScopedName;
Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp 2008-06-13 22:13:26 UTC (rev 189)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp 2008-06-13 22:48:58 UTC (rev 190)
@@ -150,3 +150,17 @@
}
return -1;
}
+
+const Type::StructFunctionMember* Type::Private::functionMember( const GTLCore::String& _name) const
+{
+ if(not structFunctionMembers) return 0;
+ for( std::vector<GTLCore::Type::StructFunctionMember>::const_iterator it = structFunctionMembers->begin();
+ it != structFunctionMembers->end(); ++it)
+ {
+ if( it->name() == _name)
+ {
+ return &*it;
+ }
+ }
+ return 0;
+}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h 2008-06-13 22:13:26 UTC (rev 189)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h 2008-06-13 22:48:58 UTC (rev 190)
@@ -63,6 +63,10 @@
Private* const d;
};
+ /**
+ * @internal
+ * Private information of the type object
+ */
struct Type::Private {
friend class Type;
public:
@@ -72,6 +76,10 @@
const llvm::Type * type() const { return m_type; }
void addFunctionMember( const StructFunctionMember& );
int memberToIndex(const GTLCore::String&);
+ /**
+ * @return the function member associated with that name, or null if none
+ */
+ const Type::StructFunctionMember* functionMember( const GTLCore::String&) const;
private:
void setType( const llvm::Type* _type);
const llvm::Type* m_type;