2004-08-07 Anirban Bhattacharjee <banirban@novell.com>
[mono.git] / mcs / mbas / codegen.cs
index 44afcb2c5e26e699905827073f83a3ab3fbc418e..0ece01942c1f1f45d48a6dce6968b80eedc79609 100644 (file)
@@ -14,7 +14,7 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Diagnostics.SymbolStore;
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
 
        /// <summary>
        ///    Code generator class.
@@ -310,6 +310,11 @@ namespace Mono.CSharp {
                ///  Whether we are inside an unsafe block
                /// </summary>
                public bool InUnsafe;
+               
+               /// <summary>
+               ///  Whether we are inside an unsafe block
+               /// </summary>
+               public bool InvokingOwnOverload;                
 
                /// <summary>
                ///   Location for this EmitContext
@@ -332,6 +337,8 @@ namespace Mono.CSharp {
                ///   we relax the rules
                /// </summary>
                public bool InEnumContext;
+               
+               public string BlockName;
 
                protected Stack FlowStack;
                
@@ -349,6 +356,8 @@ namespace Mono.CSharp {
                        ReturnType = return_type;
                        IsConstructor = is_constructor;
                        CurrentBlock = null;
+                       BlockName = "";
+                       InvokingOwnOverload = false;
                        
                        if (parent != null){
                                // Can only be null for the ResolveType contexts.
@@ -487,12 +496,22 @@ namespace Mono.CSharp {
                                CurrentBranching.SetParameterAssigned (number);
                }
 
+               // These are two overloaded methods for EmitTopBlock
+               // since in MonoBasic functions we need the Function name
+               // along with its top block, in order to be able to
+               // retrieve the return value when there is no explicit 
+               // 'Return' statement
                public void EmitTopBlock (Block block, InternalParameters ip, Location loc)
                {
-                       bool has_ret = false;
+                       EmitTopBlock (block, "", ip, loc);
+               }
 
-//                     Console.WriteLine ("Emitting: " + loc);
+               public void EmitTopBlock (Block block, string bname, InternalParameters ip, Location loc)
+               {
+                       bool has_ret = false;
 
+                       //Console.WriteLine ("Emitting: '{0}", bname);
+                       BlockName = bname;
                        if (CodeGen.SymbolWriter != null)
                                Mark (loc);
 
@@ -535,11 +554,18 @@ namespace Mono.CSharp {
 
                        if (ReturnType != null && !has_ret){
                                //
-                               // FIXME: we need full flow analysis to implement this
-                               // correctly and emit an error instead of a warning.
+                               // mcs here would report an error (and justly so), but functions without
+                               // an explicit return value are perfectly legal in MonoBasic
                                //
-                               //
-                               Report.Error (161, loc, "Not all code paths return a value");
+                               
+                               VariableInfo vi = block.GetVariableInfo (bname);
+                               if (vi != null) 
+                               {
+                                       ig.Emit (OpCodes.Ldloc, vi.LocalBuilder);
+                                       ig.Emit (OpCodes.Ret);
+                               }
+                               else
+                                       Report.Error (-200, "This is not supposed to happen !");
                                return;
                        }