Skip to content

Commit

Permalink
Refactoring to support different kinds of variable scopes
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Ballance <matt.ballance@gmail.com>
  • Loading branch information
mballance committed Oct 6, 2024
1 parent 6b44a0c commit 198aac8
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/TypeConstraintBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void TypeConstraintBlock::addConstraint(
m_constraints.push_back(ITypeConstraintUP(c, owned));
}

std::vector<ITypeVarUP> TypeConstraintBlock::m_null;

}
} /* namespace vsc */

14 changes: 13 additions & 1 deletion src/TypeConstraintBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace vsc {
namespace dm {

class TypeConstraintBlock : public ITypeConstraintBlock {
class TypeConstraintBlock :
public virtual ITypeConstraintBlock {
public:
TypeConstraintBlock(const std::string &name);

Expand All @@ -29,9 +30,20 @@ class TypeConstraintBlock : public ITypeConstraintBlock {
return m_constraints;
}

virtual int32_t addVariable(ITypeVar *var, bool owned=true) override {
return -1;
}

virtual int32_t getNumVariables() override { return 0; }

virtual const std::vector<ITypeVarUP> &getVariables() const override {
return m_null;
}

virtual void accept(IVisitor *v) override { v->visitTypeConstraintBlock(this); }

private:
static std::vector<ITypeVarUP> m_null;
std::string m_name;
std::vector<ITypeConstraintUP> m_constraints;
};
Expand Down
14 changes: 10 additions & 4 deletions src/TypeConstraintForeach.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ class TypeConstraintForeach :
return m_body.get();
}

virtual void addVariable(ITypeField *var, bool owned=true) override {
m_variables.push_back(ITypeFieldUP(var, owned));
virtual int32_t addVariable(ITypeVar *var, bool owned=true) override {
int32_t ret = m_variables.size();
m_variables.push_back(ITypeVarUP(var, owned));
return ret;
}

virtual const std::vector<ITypeFieldUP> &getVariables() const override {
virtual int32_t getNumVariables() override {
return m_variables.size();
}

virtual const std::vector<ITypeVarUP> &getVariables() const override {
return m_variables;
}

Expand All @@ -64,7 +70,7 @@ class TypeConstraintForeach :
private:
ITypeExprUP m_target;
ITypeConstraintUP m_body;
std::vector<ITypeFieldUP> m_variables;
std::vector<ITypeVarUP> m_variables;

};

Expand Down
2 changes: 2 additions & 0 deletions src/TypeConstraintScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void TypeConstraintScope::addConstraint(
m_constraints.push_back(ITypeConstraintUP(c, owned));
}

std::vector<ITypeVarUP> TypeConstraintScope::m_null;

}
} /* namespace vsc */

14 changes: 13 additions & 1 deletion src/TypeConstraintScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace vsc {
namespace dm {

class TypeConstraintScope : public ITypeConstraintScope {
class TypeConstraintScope :
public virtual ITypeConstraintScope {
public:
TypeConstraintScope();

Expand All @@ -25,9 +26,20 @@ class TypeConstraintScope : public ITypeConstraintScope {
return m_constraints;
}

virtual int32_t addVariable(ITypeVar *var, bool owned=true) override {
return -1;
}

virtual int32_t getNumVariables() override { return 0; }

virtual const std::vector<ITypeVarUP> &getVariables() const override {
return m_null;
}

virtual void accept(IVisitor *v) override { v->visitTypeConstraintScope(this); }

private:
static std::vector<ITypeVarUP> m_null;
std::vector<ITypeConstraintUP> m_constraints;
};

Expand Down
4 changes: 2 additions & 2 deletions src/include/vsc/dm/ITypeConstraintForeach.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

#pragma once
#include "vsc/dm/ITypeConstraint.h"
#include "vsc/dm/ITypeConstraintVarScope.h"
#include "vsc/dm/ITypeExpr.h"
#include "vsc/dm/ITypeVarScope.h"

namespace vsc {
namespace dm {

class ITypeConstraintForeach :
public virtual ITypeConstraint,
public virtual ITypeConstraintVarScope {
public virtual ITypeVarScope {
public:

virtual ~ITypeConstraintForeach() { }
Expand Down
5 changes: 4 additions & 1 deletion src/include/vsc/dm/ITypeConstraintScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
#pragma once
#include <vector>
#include "vsc/dm/ITypeConstraint.h"
#include "vsc/dm/ITypeVarScope.h"

namespace vsc {
namespace dm {

class ITypeConstraintScope;
using ITypeConstraintScopeUP=UP<ITypeConstraintScope>;
class ITypeConstraintScope : public ITypeConstraint {
class ITypeConstraintScope :
public virtual ITypeVarScope,
public virtual ITypeConstraint {
public:

virtual ~ITypeConstraintScope() { }
Expand Down
10 changes: 2 additions & 8 deletions src/include/vsc/dm/ITypeField.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "vsc/dm/IAccept.h"
#include "vsc/dm/IDataType.h"
#include "vsc/dm/TypeFieldAttr.h"
#include "vsc/dm/ITypeVar.h"
#include "vsc/dm/IModelVal.h"
#include "vsc/dm/IModelBuildContext.h"

Expand All @@ -37,6 +38,7 @@ class IDataTypeStruct;
class ITypeField;
using ITypeFieldUP=UP<ITypeField>;
class ITypeField :
public virtual ITypeVar,
public virtual IAccept {
public:

Expand All @@ -56,14 +58,6 @@ class ITypeField :

virtual int32_t getByteSize() const = 0;

virtual const std::string &name() const = 0;

virtual IDataType *getDataType() const = 0;

template <class T> T *getDataTypeT() const {
return dynamic_cast<T *>(getDataType());
}

virtual bool isDataTypeOwned() const = 0;

virtual void setDataType(IDataType *t, bool owned=false) = 0;
Expand Down
51 changes: 51 additions & 0 deletions src/include/vsc/dm/ITypeVar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* ITypeVar.h
*
* Copyright 2023 Matthew Ballance and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on:
* Author:
*/
#pragma once
#include <string>
#include "vsc/dm/IAccept.h"
#include "vsc/dm/IDataType.h"
#include "vsc/dm/ITypeExpr.h"

namespace vsc {
namespace dm {

class ITypeVar;
using ITypeVarUP=UP<ITypeVar>;
class ITypeVar :
public virtual IAccept {
public:

virtual ~ITypeVar() { }

virtual const std::string &name() const = 0;

virtual vsc::dm::IDataType *getDataType() const = 0;

template <class T> T *getDataTypeT() const {
return dynamic_cast<T *>(getDataType());
}

};

} /* namespace dm */
} /* namespace vsc */


Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* ITypeConstraintVarScope.h
* ITypeVarScope.h
*
* Copyright 2023 Matthew Ballance and Contributors
*
Expand All @@ -20,21 +20,23 @@
*/
#pragma once
#include <vector>
#include "vsc/dm/ITypeField.h"
#include "vsc/dm/ITypeVar.h"

namespace vsc {
namespace dm {



class ITypeConstraintVarScope {
class ITypeVarScope {
public:

virtual ~ITypeConstraintVarScope() { }
virtual ~ITypeVarScope() { }

virtual void addVariable(ITypeField *var, bool owned=true) = 0;
virtual int32_t addVariable(ITypeVar *var, bool owned=true) = 0;

virtual const std::vector<ITypeFieldUP> &getVariables() const = 0;
virtual int32_t getNumVariables() = 0;

virtual const std::vector<ITypeVarUP> &getVariables() const = 0;

};

Expand Down
2 changes: 1 addition & 1 deletion src/include/vsc/dm/impl/VisitorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class VisitorBase : public virtual IVisitor {
}

virtual void visitTypeConstraintForeach(ITypeConstraintForeach *c) override {
for (std::vector<ITypeFieldUP>::const_iterator
for (std::vector<ITypeVarUP>::const_iterator
it=c->getVariables().begin();
it!=c->getVariables().end(); it++) {
(*it)->accept(m_this);
Expand Down

0 comments on commit 198aac8

Please sign in to comment.