summaryrefslogtreecommitdiff
path: root/vala/valasemanticanalyzer.vala
diff options
context:
space:
mode:
authorLuca Bruno <lucabru@src.gnome.org>2011-08-06 10:38:59 +0200
committerRico Tzschichholz <ricotz@ubuntu.com>2019-03-11 13:46:57 +0100
commit2d66c4833dad492849fad5535226677865b30c6c (patch)
tree30e2c2b387f99122109ef1d0099bee23141af0d6 /vala/valasemanticanalyzer.vala
parent270361907182a4c8b4b291fd1a505d47dd8bcaae (diff)
downloadvala-2d66c4833dad492849fad5535226677865b30c6c.tar.gz
Make the semantic analyzer be stateless
Diffstat (limited to 'vala/valasemanticanalyzer.vala')
-rw-r--r--vala/valasemanticanalyzer.vala261
1 files changed, 119 insertions, 142 deletions
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 485ab222d..3f3388851 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -30,108 +30,6 @@ using GLib;
public class Vala.SemanticAnalyzer : CodeVisitor {
CodeContext context;
- public Symbol current_symbol { get; set; }
- public SourceFile current_source_file { get; set; }
-
- public TypeSymbol? current_type_symbol {
- get {
- var sym = current_symbol;
- while (sym != null) {
- if (sym is TypeSymbol) {
- return (TypeSymbol) sym;
- }
- sym = sym.parent_symbol;
- }
- return null;
- }
- }
-
- public Class? current_class {
- get { return current_type_symbol as Class; }
- }
-
-
- public Struct? current_struct {
- get { return current_type_symbol as Struct; }
- }
-
- public Method? current_method {
- get {
- unowned Symbol sym = current_symbol;
- while (sym is Block) {
- sym = sym.parent_symbol;
- }
- return sym as Method;
- }
- }
-
- public Method? current_async_method {
- get {
- unowned Symbol sym = current_symbol;
- while (sym is Block || sym is Method) {
- var m = sym as Method;
- if (m != null && m.coroutine) {
- break;
- }
-
- sym = sym.parent_symbol;
- }
- return sym as Method;
- }
- }
-
- public PropertyAccessor? current_property_accessor {
- get {
- unowned Symbol sym = current_symbol;
- while (sym is Block) {
- sym = sym.parent_symbol;
- }
- return sym as PropertyAccessor;
- }
- }
-
- public Symbol? current_method_or_property_accessor {
- get {
- unowned Symbol sym = current_symbol;
- while (sym is Block) {
- sym = sym.parent_symbol;
- }
- if (sym is Method) {
- return sym;
- } else if (sym is PropertyAccessor) {
- return sym;
- } else {
- return null;
- }
- }
- }
-
- public DataType? current_return_type {
- get {
- var m = current_method;
- if (m != null) {
- return m.return_type;
- }
-
- var acc = current_property_accessor;
- if (acc != null) {
- if (acc.readable) {
- return acc.value_type;
- } else {
- return void_type;
- }
- }
-
- if (is_in_constructor () || is_in_destructor ()) {
- return void_type;
- }
-
- return null;
- }
- }
-
- public Block insert_block;
-
public DataType void_type = new VoidType ();
public DataType bool_type;
public DataType string_type;
@@ -219,7 +117,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
gsource_type = (Class) glib_ns.scope.lookup ("Source");
}
- current_symbol = root_symbol;
context.root.check (context);
context.accept (this);
@@ -227,11 +124,122 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
public override void visit_source_file (SourceFile file) {
- current_source_file = file;
-
file.check (context);
}
+ public unowned Symbol? get_current_symbol (CodeNode node) {
+ while (node != null && !(node is Symbol)) {
+ node = node.parent_node;
+ }
+ return (Symbol) node;
+ }
+
+ public unowned Symbol? get_current_non_local_symbol (CodeNode node) {
+ while (node != null && (!(node is Symbol) || is_local_symbol ((Symbol) node))) {
+ node = node.parent_node;
+ }
+ return (Symbol) node;
+ }
+
+ public bool is_local_symbol (Symbol sym) {
+ if (sym is LocalVariable || (sym is Constant && sym.parent_symbol is Block)) {
+ return true;
+ }
+ return false;
+ }
+
+ public unowned TypeSymbol? get_current_type_symbol (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
+ while (sym != null && !(sym is TypeSymbol)) {
+ sym = sym.parent_symbol;
+ }
+ return (TypeSymbol) sym;
+ }
+
+ public unowned Class? get_current_class (CodeNode node) {
+ return get_current_type_symbol (node) as Class;
+ }
+
+
+ public unowned Struct? get_current_struct (CodeNode node) {
+ return get_current_type_symbol (node) as Struct;
+ }
+
+ public unowned Method? get_current_method (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
+ while (sym != null && !(sym is Method)) {
+ sym = sym.parent_symbol;
+ }
+ return sym as Method;
+ }
+
+ public unowned Method? get_current_async_method (CodeNode node) {
+ unowned Method m = get_current_method (node);
+ while (m != null && !m.coroutine) {
+ m = get_current_method (m.parent_symbol);
+ }
+ return m;
+ }
+
+ public unowned PropertyAccessor? get_current_property_accessor (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
+ while (sym != null && !(sym is PropertyAccessor)) {
+ sym = sym.parent_symbol;
+ }
+ return sym as PropertyAccessor;
+ }
+
+ public unowned Symbol? get_current_method_or_property_accessor (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
+ while (sym != null && !(sym is Method) && !(sym is PropertyAccessor)) {
+ sym = sym.parent_symbol;
+ }
+ if (sym is Method) {
+ return sym;
+ } else if (sym is PropertyAccessor) {
+ return sym;
+ } else {
+ return null;
+ }
+ }
+
+ public unowned DataType? get_current_return_type (CodeNode node) {
+ unowned Method m = get_current_method (node);
+ if (m != null) {
+ return m.return_type;
+ }
+
+ unowned PropertyAccessor acc = get_current_property_accessor (node);
+ if (acc != null) {
+ if (acc.readable) {
+ return acc.value_type;
+ } else {
+ return void_type;
+ }
+ }
+
+ if (is_in_constructor (node) || is_in_destructor (node)) {
+ return void_type;
+ }
+
+ return null;
+ }
+
+ public unowned Block? get_current_block (CodeNode node) {
+ while (node != null && !(node is Block)) {
+ node = node.parent_node;
+ }
+ return (Block) node;
+ }
+
+ public unowned Block? get_insert_block (CodeNode node) {
+ unowned Block? block = get_current_block (node);
+ if (block is ForeachStatement) {
+ block = block.parent_symbol as Block;
+ }
+ return block;
+ }
+
// check whether type is at least as accessible as the specified symbol
public bool is_type_accessible (Symbol sym, DataType type) {
return type.is_accessible (sym);
@@ -903,8 +911,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return actual_type;
}
- public bool is_in_instance_method () {
- var sym = current_symbol;
+ public bool is_in_instance_method (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
while (sym != null) {
if (sym is CreationMethod) {
return true;
@@ -1021,39 +1029,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
}
- public Method? find_current_method () {
- var sym = current_symbol;
- while (sym != null) {
- if (sym is Method) {
- return (Method) sym;
- }
- sym = sym.parent_symbol;
- }
- return null;
- }
-
- public Method? find_parent_method (Symbol sym) {
- while (sym is Block) {
- sym = sym.parent_symbol;
- }
- return sym as Method;
- }
-
- public Symbol? find_parent_method_or_property_accessor (Symbol sym) {
- while (sym is Block) {
- sym = sym.parent_symbol;
- }
- if (sym is Method) {
- return sym;
- } else if (sym is PropertyAccessor) {
- return sym;
- } else {
- return null;
- }
- }
-
- public bool is_in_constructor () {
- var sym = current_symbol;
+ public bool is_in_constructor (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
while (sym != null) {
if (sym is Constructor) {
return true;
@@ -1063,8 +1040,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return false;
}
- public bool is_in_destructor () {
- var sym = current_symbol;
+ public bool is_in_destructor (CodeNode node) {
+ unowned Symbol sym = get_current_symbol (node);
while (sym != null) {
if (sym is Destructor) {
return true;