Commit d540dfd4 authored by Jürg Billeter's avatar Jürg Billeter

Add Subroutine class

parent fb3a3163
......@@ -136,6 +136,7 @@ libvalacore_la_VALASOURCES = \
valastringliteral.vala \
valastruct.vala \
valastructvaluetype.vala \
valasubroutine.vala \
valaswitchlabel.vala \
valaswitchsection.vala \
valaswitchstatement.vala \
......
......@@ -25,12 +25,7 @@ using GLib;
/**
* Represents a class or instance constructor.
*/
public class Vala.Constructor : Symbol {
/**
* The body of this constructor.
*/
public Block body { get; set; }
public class Vala.Constructor : Subroutine {
/**
* Specifies the generated `this` parameter for instance methods.
*/
......
......@@ -25,12 +25,7 @@ using GLib;
/**
* Represents a class or instance destructor.
*/
public class Vala.Destructor : Symbol {
/**
* The body of this constructor.
*/
public Block body { get; set; }
public class Vala.Destructor : Subroutine {
/**
* Specifies the generated `this` parameter for instance methods.
*/
......
......@@ -173,6 +173,10 @@ public class Vala.FlowAnalyzer : CodeVisitor {
}
}
visit_subroutine (m);
}
void visit_subroutine (Subroutine m) {
if (m.body == null) {
return;
}
......@@ -183,7 +187,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
m.return_block.connect (m.exit_block);
if (context.profile == Profile.DOVA && !(m.return_type is VoidType)) {
if (context.profile == Profile.DOVA && m.result_var != null) {
// ensure result is defined at end of method
var result_ma = new MemberAccess.simple ("result", m.source_reference);
result_ma.symbol_reference = m.result_var;
......@@ -204,8 +208,8 @@ public class Vala.FlowAnalyzer : CodeVisitor {
if (current_block != null) {
// end of method body reachable
if (context.profile != Profile.DOVA && !(m.return_type is VoidType)) {
Report.error (m.source_reference, "missing return statement at end of method or lambda body");
if (context.profile != Profile.DOVA && m.result_var != null) {
Report.error (m.source_reference, "missing return statement at end of subroutine body");
m.error = true;
}
......@@ -499,45 +503,7 @@ public class Vala.FlowAnalyzer : CodeVisitor {
}
public override void visit_property_accessor (PropertyAccessor acc) {
if (acc.body == null) {
return;
}
acc.entry_block = new BasicBlock.entry ();
acc.return_block = new BasicBlock ();
acc.exit_block = new BasicBlock.exit ();
acc.return_block.connect (acc.exit_block);
if (context.profile == Profile.DOVA && acc.readable) {
// ensure result is defined at end of method
var result_ma = new MemberAccess.simple ("result", acc.source_reference);
result_ma.symbol_reference = acc.result_var;
acc.return_block.add_node (result_ma);
}
current_block = new BasicBlock ();
acc.entry_block.connect (current_block);
jump_stack.add (new JumpTarget.return_target (acc.return_block));
jump_stack.add (new JumpTarget.exit_target (acc.exit_block));
acc.accept_children (this);
jump_stack.remove_at (jump_stack.size - 1);
if (current_block != null) {
// end of property accessor body reachable
if (context.profile != Profile.DOVA && acc.readable) {
Report.error (acc.source_reference, "missing return statement at end of property getter body");
acc.error = true;
}
current_block.connect (acc.return_block);
}
analyze_body (acc.entry_block);
visit_subroutine (acc);
}
public override void visit_block (Block b) {
......
......@@ -27,7 +27,7 @@ using GLib;
/**
* Represents a type or namespace method.
*/
public class Vala.Method : Symbol {
public class Vala.Method : Subroutine {
List<TypeParameter> type_parameters;
public const string DEFAULT_SENTINEL = "NULL";
......@@ -42,22 +42,6 @@ public class Vala.Method : Symbol {
_return_type.parent_node = this;
}
}
public Block body {
get { return _body; }
set {
_body = value;
if (_body != null) {
_body.owner = scope;
}
}
}
public BasicBlock entry_block { get; set; }
public BasicBlock return_block { get; set; }
public BasicBlock exit_block { get; set; }
/**
* Specifies whether this method may only be called with an instance of
......@@ -159,11 +143,6 @@ public class Vala.Method : Symbol {
*/
public Parameter this_parameter { get; set; }
/**
* Specifies the generated `result` variable for postconditions.
*/
public LocalVariable result_var { get; set; }
/**
* Specifies the position of the instance parameter in the C function.
*/
......@@ -243,7 +222,6 @@ public class Vala.Method : Symbol {
private List<Expression> preconditions;
private List<Expression> postconditions;
private DataType _return_type;
private Block _body;
private weak Method _base_method;
private Method _base_interface_method;
......
......@@ -25,7 +25,7 @@ using GLib;
/**
* Represents a get or set accessor of a property in the source code.
*/
public class Vala.PropertyAccessor : Symbol {
public class Vala.PropertyAccessor : Subroutine {
/**
* The corresponding property.
*/
......@@ -62,25 +62,6 @@ public class Vala.PropertyAccessor : Symbol {
*/
public bool construction { get; set; }
/**
* The accessor body.
*/
public Block? body {
get { return _body; }
set {
_body = value;
if (_body != null) {
_body.owner = scope;
}
}
}
public BasicBlock entry_block { get; set; }
public BasicBlock return_block { get; set; }
public BasicBlock exit_block { get; set; }
/**
* True if the body was automatically generated
*/
......@@ -91,11 +72,6 @@ public class Vala.PropertyAccessor : Symbol {
*/
public Parameter value_parameter { get; set; }
/**
* Specifies the generated `result' variable in a get accessor.
*/
public LocalVariable? result_var { get; set; }
public virtual string get_default_cname () {
var t = (TypeSymbol) prop.parent_symbol;
......@@ -119,7 +95,6 @@ public class Vala.PropertyAccessor : Symbol {
private DataType _value_type;
private string? _cname;
private Block _body;
/**
* Creates a new property accessor.
......
/* valasubroutine.vala
*
* Copyright (C) 2010 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
public abstract class Vala.Subroutine : Symbol {
Block _body;
public BasicBlock entry_block { get; set; }
public BasicBlock return_block { get; set; }
public BasicBlock exit_block { get; set; }
/**
* Specifies the generated `result` variable for postconditions.
*/
public LocalVariable result_var { get; set; }
protected Subroutine (string? name, SourceReference? source_reference, Comment? comment = null) {
base (name, source_reference, comment);
}
public Block body {
get { return _body; }
set {
_body = value;
if (_body != null) {
_body.owner = scope;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment