File: Synopsis/TypeAnalysis/Type.hh
  1//
  2// Copyright (C) 2005 Stefan Seefeld
  3// All rights reserved.
  4// Licensed to the public under the terms of the GNU LGPL (>= 2),
  5// see the file COPYING for details.
  6//
  7#ifndef Synopsis_TypeAnalysis_Type_hh_
  8#define Synopsis_TypeAnalysis_Type_hh_
  9
 10#include <Synopsis/TypeAnalysis/Visitor.hh>
 11#include <string>
 12#include <ostream>
 13#include <iterator>
 14
 15namespace Synopsis
 16{
 17namespace TypeAnalysis
 18{
 19
 20class Type
 21{
 22public:
 23  Type(std::string const &name) : my_name(name), my_refcounter(1) {}
 24  virtual ~Type() {}
 25  const std::string &name() const { return my_name;}
 26  virtual void accept(Visitor *visitor) = 0;
 27  virtual void ref() const { ++my_refcounter;}
 28  virtual void deref() const { if (--my_refcounter) delete this;}
 29  // TODO: add 'new' / 'delete' operators for optimization.
 30private:
 31  const std::string my_name;
 32  mutable size_t    my_refcounter;
 33};
 34
 35class BuiltinType : public Type
 36{
 37public:
 38  BuiltinType(std::string const &name) : Type(name) {}
 39  virtual void accept(Visitor *visitor) { visitor->visit(this);}
 40  // BuiltinType is preallocated and thus is destructed at program termination.
 41  virtual void ref() const {}
 42  virtual void deref() const {}
 43};
 44
 45extern BuiltinType BOOL;
 46extern BuiltinType CHAR;
 47extern BuiltinType WCHAR;
 48extern BuiltinType SHORT;
 49extern BuiltinType INT;
 50extern BuiltinType LONG;
 51extern BuiltinType FLOAT;
 52extern BuiltinType DOUBLE;
 53extern BuiltinType UCHAR;
 54extern BuiltinType USHORT;
 55extern BuiltinType UINT;
 56extern BuiltinType ULONG;
 57extern BuiltinType SCHAR;
 58extern BuiltinType SSHORT;
 59extern BuiltinType SINT;
 60extern BuiltinType SLONG;
 61
 62class Enum : public Type
 63{
 64public:
 65  Enum(std::string const &name) : Type(name) {}
 66  virtual void accept(Visitor *visitor) { visitor->visit(this);}
 67};
 68
 69class Compound : public Type
 70{
 71public:
 72  Compound(std::string const &name) : Type(name) {}
 73};
 74
 75class Class : public Compound
 76{
 77public:
 78  enum Kind { STRUCT, CLASS};
 79
 80  Class(Kind kind, std::string const &name) : Compound(name), my_kind(kind) {}
 81  virtual void accept(Visitor *visitor) { visitor->visit(this);}
 82
 83private:
 84  Kind my_kind;
 85};
 86
 87class Union : public Compound
 88{
 89public:
 90  Union(std::string const &name) : Compound(name) {}
 91  virtual void accept(Visitor *visitor) { visitor->visit(this);}
 92
 93private:
 94};
 95
 96class CVType : public Type
 97{
 98public:
 99  enum CVQualifier { NONE=0x0, CONST=0x1, VOLATILE=0x2};
100
101  CVType(Type const *type, CVQualifier q)
102    : Type(names[q]), my_type(type), my_qual(q) {}
103  virtual void accept(Visitor *visitor) { visitor->visit(this);}
104
105private:
106  static std::string const names[4];
107
108  Type const *my_type;
109  CVQualifier my_qual;
110};
111
112class Pointer : public Type
113{
114public:
115  Pointer(Type const *type) : Type("*"), my_type(type) {}
116  virtual void accept(Visitor *visitor) { visitor->visit(this);}
117
118private:
119  Type const *my_type;
120};
121
122class Reference : public Type
123{
124public:
125  Reference(Type const *type) : Type("&"), my_type(type) {}
126  virtual void accept(Visitor *visitor) { visitor->visit(this);}
127
128private:
129  Type const *my_type;
130};
131
132class Array : public Type
133{
134public:
135  Array(Type const *type) : Type("[]"), my_type(type) {}
136  virtual void accept(Visitor *visitor) { visitor->visit(this);}
137
138private:
139  Type const *my_type;
140};
141
142class Function : public Type
143{
144public:
145  Function() : Type("") {}
146  virtual void accept(Visitor *visitor) { visitor->visit(this);}
147
148private:
149  Type const *my_type;
150};
151
152class PointerToMember : public Type
153{
154public:
155  PointerToMember() : Type("") {}
156  virtual void accept(Visitor *visitor) { visitor->visit(this);}
157
158private:
159  Type const *my_container;
160  Type const *my_member;
161};
162
163}
164}
165
166#endif