changeset 560:709e223a8eb9

Added code related to symbols. Added class ScopeSymbol. Module inherits from ScopeSymbol now. Added methods classScope() and moduleScope(), and member symbol to class Scope. Added enum SYM. Added member sid to class Symbol. Aggregate and Function inherit from ScopeSymbol now. Added Error to struct Types.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 26 Dec 2007 14:17:01 +0100
parents c4bb948e5cc1
children 302e50e71ec2
files trunk/src/dil/Module.d trunk/src/dil/Scope.d trunk/src/dil/Symbol.d trunk/src/dil/Symbols.d trunk/src/dil/TypeSystem.d
diffstat 5 files changed, 113 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/Module.d	Mon Dec 24 20:39:14 2007 +0100
+++ b/trunk/src/dil/Module.d	Wed Dec 26 14:17:01 2007 +0100
@@ -11,6 +11,8 @@
 import dil.Lexer;
 import dil.File;
 import dil.Scope;
+import dil.Symbol;
+import dil.Symbols;
 import dil.Information;
 import tango.io.FilePath;
 import tango.io.FileConst;
@@ -18,7 +20,7 @@
 
 alias FileConst.PathSeparatorChar dirSep;
 
-class Module
+class Module : ScopeSymbol
 {
   bool isLightweight; /// If true an ImportParser is used instead of a full Parser.
   string filePath; /// Path to the source file.
@@ -37,6 +39,8 @@
 
   this(string filePath, bool isLightweight = false)
   {
+    this.sid = SYM.Module;
+
     this.filePath = filePath;
     this.isLightweight = isLightweight;
   }
--- a/trunk/src/dil/Scope.d	Mon Dec 24 20:39:14 2007 +0100
+++ b/trunk/src/dil/Scope.d	Wed Dec 26 14:17:01 2007 +0100
@@ -3,8 +3,12 @@
   License: GPL3
 +/
 module dil.Scope;
+
 import dil.Symbol;
+import dil.Symbols;
 import dil.Information;
+import dil.Messages;
+import dil.Token;
 import common;
 
 class Scope
@@ -12,6 +16,8 @@
   Scope parent; /// The surrounding scope.
   InfoManager infoMan; /// Collects errors reported during the semantic phase.
 
+  ScopeSymbol symbol; /// The current symbol with the symbol table.
+
   this()
   {
   }
@@ -52,9 +58,32 @@
     return sc;
   }
 
-  import dil.Information;
-  import dil.Messages;
-  import dil.Token;
+  /// Search for the enclosing Class scope.
+  Scope classScope()
+  {
+    auto scop = this;
+    while (scop)
+    {
+      if (scop.symbol.sid == SYM.Class)
+        return scop;
+      scop = scop.parent;
+    }
+    return null;
+  }
+
+  /// Search for the enclosing Module scope.
+  Scope moduleScope()
+  {
+    auto scop = this;
+    while (scop)
+    {
+      if (scop.symbol.sid == SYM.Module)
+        return scop;
+      scop = scop.parent;
+    }
+    return null;
+  }
+
   void error(Token* token, MID mid)
   {
     auto location = token.getLocation();
--- a/trunk/src/dil/Symbol.d	Mon Dec 24 20:39:14 2007 +0100
+++ b/trunk/src/dil/Symbol.d	Wed Dec 26 14:17:01 2007 +0100
@@ -3,9 +3,21 @@
   License: GPL3
 +/
 module dil.Symbol;
+
 import common;
 
+enum SYM
+{
+  Module,
+  Class,
+  Struct,
+  Union,
+  Variable,
+  Function,
+  Type,
+}
+
 class Symbol
 {
-
+  SYM sid;
 }
--- a/trunk/src/dil/Symbols.d	Mon Dec 24 20:39:14 2007 +0100
+++ b/trunk/src/dil/Symbols.d	Wed Dec 26 14:17:01 2007 +0100
@@ -3,10 +3,28 @@
   License: GPL3
 +/
 module dil.Symbols;
+
 import dil.Symbol;
+import dil.SymbolTable;
+import dil.SyntaxTree;
+import dil.Enums;
+import dil.TypeSystem;
+import dil.Identifier;
 import common;
 
-class Aggregate : Symbol
+/// A symbol that has its own scope with a symbol table.
+class ScopeSymbol : Symbol
+{
+  SymbolTable symbolTable; /// The symbol table.
+
+  this()
+  {
+    symbolTable = new SymbolTable;
+  }
+}
+
+/// Aggregates have function and field members.
+class Aggregate : ScopeSymbol
 {
   Function[] funcs;
   Variable[] fields;
@@ -14,25 +32,61 @@
 
 class Class : Aggregate
 {
-
+  this()
+  {
+    this.sid = SYM.Class;
+  }
 }
 
 class Union : Aggregate
 {
-
+  this()
+  {
+    this.sid = SYM.Union;
+  }
 }
 
 class Struct : Aggregate
 {
-
+  this()
+  {
+    this.sid = SYM.Struct;
+  }
 }
 
-class Function : Symbol
+class Function : ScopeSymbol
 {
+  StorageClass stc;
+  LinkageType linkType;
 
+  Type returnType;
+  Identifier* ident;
+  Variable[] params;
+
+  this()
+  {
+    this.sid = SYM.Function;
+  }
 }
 
 class Variable : Symbol
 {
+  StorageClass stc;
+  LinkageType linkType;
 
+  Type type;
+  Identifier* ident;
+  Node varDecl; /// The VariableDeclaration or Parameter node - for source code location.
+
+  this(StorageClass stc, LinkageType linkType,
+       Type type, Identifier* ident, Node varDecl)
+  {
+    this.sid = SYM.Variable;
+
+    this.stc = stc;
+    this.linkType = linkType;
+    this.type = type;
+    this.ident = ident;
+    this.varDecl = varDecl;
+  }
 }
--- a/trunk/src/dil/TypeSystem.d	Mon Dec 24 20:39:14 2007 +0100
+++ b/trunk/src/dil/TypeSystem.d	Wed Dec 26 14:17:01 2007 +0100
@@ -18,6 +18,8 @@
 
   this(Type next, TYP tid)
   {
+    this.sid = SYM.Type;
+
     this.next = next;
     this.tid = tid;
   }
@@ -295,7 +297,7 @@
 
   TypeBasic Size_t, Ptrdiff_t;
   TypePointer Void_ptr;
-  TypeBasic Undefined;
+  TypeBasic Error, Undefined;
 
   /// Allocates an instance of TypeBasic and assigns it to typeName.
   template newTB(char[] typeName)
@@ -340,6 +342,7 @@
       Ptrdiff_t = Int;
     }
     Void_ptr = Void.ptrTo;
+    Error = new TypeBasic(TYP.Error);
     Undefined = new TypeBasic(TYP.Error);
   }
 }