[r368]: / cmajor++ / Cm / Cm.Core / Variable.hpp  Maximize  Restore  History

Download this file

121 lines (105 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#ifndef CM_CORE_VARIABLE_INCLUDED
#define CM_CORE_VARIABLE_INCLUDED
#include <Cm.Core/Specifier.hpp>
#include <Cm.Core/Type.hpp>
namespace Cm { namespace Core {
class ClassType;
typedef std::shared_ptr<ClassType> ClassTypePtr;
class Variable: public Object
{
public:
Variable(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, Specifiers specifiers_, const ObjectPtr& typeExpr_);
virtual void Dispose();
ObjectPtr TypeExpr() const { return typeExpr; }
virtual bool IsVariable() const { return true; }
virtual std::string ToString() const { return assemblyName; }
virtual void TypeCheckName(TypeCheckContext& context);
virtual void TypeCheckSignature(TypeCheckContext& context);
virtual void TypeCheckBody(TypeCheckContext& context);
virtual TypePtr GetType() const { return type.lock(); }
virtual bool IsBoolExpr() const { return GetType()->IsBoolType(); }
virtual ArgumentCategory GetArgumentCategory() const { return ArgumentCategory::lvalue; }
virtual bool HasTrivialDestructor() const;
virtual void SetFunction(Function* function);
virtual Specifiers GetSpecifiers() const { return specifiers; }
virtual Specifiers GetAccessSpecifiers() const { return specifiers & Specifiers::access; }
void SetSpecifiers(Specifiers specifiers_) { specifiers = specifiers_; }
const std::string& AssemblyName() const { return assemblyName; }
void SetAssemblyName(const std::string& assemblyName_) { assemblyName = assemblyName_; }
virtual std::string ObjectKindStr() const { return "variable"; }
virtual ObjectKind GetObjectKind() const { return ObjectKind::variable; }
virtual GenResult Gen(Ir& ir, GenFlags flags);
private:
Specifiers specifiers;
ObjectPtr typeExpr;
WeakTypePtr type;
std::string assemblyName;
};
typedef std::shared_ptr<Variable> VariablePtr;
class LocalVariable: public Variable
{
public:
LocalVariable(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, const ObjectPtr& typeExpr_);
virtual Specifiers GetAccessSpecifiers() const { return Specifiers::public_; }
virtual ObjectPtr Clone(CloneContext& context) const;
virtual bool IsLocalVariable() const { return true; }
virtual void CollectLocalVariables(LocalVariableList& vars);
virtual void TypeCheckBody(TypeCheckContext& context);
bool Collected() const { return collected; }
void SetCollected() { collected = true; }
private:
bool collected;
};
class MemberVariable: public Variable
{
public:
MemberVariable(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, Specifiers specifiers_,
const ObjectPtr& typeExpr_);
virtual void Dispose();
virtual ObjectPtr Clone(CloneContext& context) const;
virtual bool IsMemberVariable() const { return true; }
int VarIndex() const { return varIndex; }
void SetVarIndex(int varIndex_) { varIndex = varIndex_; }
void SetClassType(const ClassTypePtr& ownerClassType_) { ownerClassType = ownerClassType_; }
virtual ClassTypePtr GetClassType() const { return ownerClassType.lock(); }
virtual bool IsClassLevelObject() const { return true; }
virtual void TypeCheckSignature(TypeCheckContext& context);
virtual void TypeCheckBody(TypeCheckContext& context);
virtual bool IsStatic() const { return (GetSpecifiers() & Specifiers::static_) != Specifiers::none; }
virtual bool IsExternal() const { return (GetSpecifiers() & Specifiers::external) != Specifiers::none; }
virtual void GenerateIntermediateCode(CodeFormatter& formatter);
virtual void GenerateLibraryDeclarations(CodeFormatter& formatter);
void RegisterDestructor(Ir& ir);
void SetInitializedVar() { isInitializedVar = true; }
private:
int varIndex;
WeakClassTypePtr ownerClassType;
Llvm::Ir::ObjectPtr destructionNode;
bool isInitializedVar;
};
typedef std::shared_ptr<MemberVariable> MemberVariablePtr;
class ThisVariable: public LocalVariable
{
public:
ThisVariable(const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, const TypePtr& classType_);
virtual void Dispose();
virtual ObjectPtr Clone(CloneContext& context) const;
virtual bool IsThisVariable() const { return true; }
virtual void CollectLocalVariables(LocalVariableList& vars) {}
virtual void TypeCheckBody(TypeCheckContext& context);
virtual GenResult Gen(Ir& ir, GenFlags flags);
private:
WeakTypePtr classType;
};
typedef std::shared_ptr<LocalVariable> LocalVariablePtr;
typedef std::vector<LocalVariablePtr> LocalVariableList;
typedef std::shared_ptr<MemberVariable> MemberVariablePtr;
typedef std::vector<MemberVariablePtr> MemberVariableList;
} } // namespace Cm::Core
#endif // CM_CORE_VARIABLE_INCLUDED