Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 323 lines (292 sloc) 11.981 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/*
* Copyright (c) 2003-2008 The University of Wroclaw.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the University may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE UNIVERSITY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using Nemerle.Compiler;
using Nemerle.Collections;

namespace Nemerle.Core
{
  macro DebugPrint(expression, printFunction = <[ System.Console.WriteLine ]>)
  {
    def code = expression.ToString() + " ==> ";
    <[
def result = $expression;
        $printFunction($(code : string) + result.ToString());
    ]>
  }
  
  macro assert(cond, message = <[ "" ]>)
  {
    match (cond) {
      | <[ false ]> =>
        // skip cond in this case so it gets 'a type
        <[ throw AssertionException ($(cond.Location.File : string),
                                     $(cond.Location.Line : int),
                                     "", $message) ]>
      | _ =>
        <[ unless ($cond)
             throw AssertionException ($(cond.Location.File : string),
                                       $(cond.Location.Line : int),
                                       $(cond.ToString () : string),
                                       $message) ]>
    }
  }
  
  /// <summary>Checks for a condition; if the condition is false, outputs a specified message and displays a message
  /// box that shows the call stack. The message include code of condition.</summary>
  /// <param name="condition">The conditional expression to evaluate. If the condition is true, the specified message is not sent and the message box is not displayed.</param>
  /// <param name="message">The message to send to the <see cref="P:System.Diagnostics.Trace.Listeners"/> collection.</param>
  /// <remark>Used System.Diagnostics.Debug.Assert() to display message</remark>
  macro assert2(condition, message = <[ "" ]>)
  {
    match (condition)
    {
      | <[ false ]> => // skip condition in this case so it gets 'a type
        <[ System.Diagnostics.Debug.Assert(false, ($message).ToString()) ]>
        
      | _ => <[
unless ($condition)
        {
          def msg = "Assertion: " + $(condition.ToString() : string)
                    + "\nAt: " + $(condition.Location.File : string) + " failed.";
          System.Diagnostics.Debug.Fail(msg, ($message).ToString())
        } ]>
    }
  }
}

namespace Nemerle.Assertions
{
  /// Enforces that given parameter is not a null value.
  ///
  /// Performs runtime check at every call to enclosing method.
  ///
  /// Example: Foo([NotNull] o : object) : void { ... }
[Nemerle.MacroUsage (MacroPhase.WithTypedMembers, MacroTargets.Parameter,
Inherited = true, AllowMultiple = false)]
  macro NotNull(_ : TypeBuilder, m : MethodBuilder, p : ParameterBuilder)
  {
    if (p.ty.CanBeNull)
    {
      def name = <[ $(p.AsParsed().PName : name) ]>;
      def nameLoc = p.NameLocation;
      def loc = m.Body.Location;
      def paramName = p.Name.ToString();
      def msg = $<#The ``NotNull'' contract of parameter ``$paramName'' has been violated. See $nameLoc.#>;
      def condition = if (p.ty.Fix().IsValueType) name
                      else <[ $name : object ]>;

      m.Body = <[
when ($condition == null)
        {
          //System.Diagnostics.Debug.Fail($(msg : string));
          throw System.ArgumentNullException($(paramName : string), $(msg : string))
        }
          
        $(m.Body)
      ]>;
      
      m.Body.Location = loc;
    }
    else
      Message.Warning(p.Location,
        $"The ``NotNull'' contract for parameter ``$(p.Name)'' has no effect. Instance of type ``$(p.ty)'' can't be null reference.");
  }

  /// Example: foo ([Requires (value != 4)] i : int) : void { ... }
[Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
Nemerle.MacroTargets.Parameter,
Inherited = true, AllowMultiple = true)]
  macro Requires (_ : TypeBuilder, m : ParsedMethod, p : ParsedParameter, assertion, other = null)
  syntax ("requires", assertion, Optional ("otherwise", other))
  {
    def loc = m.Body.Location;
    def check =
      if (other != null)
        <[ unless ($assertion) $other ]>
      else
        <[ assert($assertion, "The ``Requires'' contract of parameter `" +
                   $(p.Name : string) + "' has been violated.") ]>;
    
    def newBody = Util.locate(loc, <[
def $("value" : usesite) = $(p.PName : name);
      $check;
      $(m.Body)
    ]>);
    m.Body = newBody;
  }

  /** Enforces given boolean condition at method invocation beginning.

It checks at runtime, that given condition is true at the beginning
of each method invocation. The `otherwise' section allows to specify
what should happen when condition is false (for example throw some
exception).
Example: [Requires (i != 4 &amp;&amp; boo ())]
foo (i : int) : void
{ ... }
or
foo (i : int) : void
requires i > 0
{ ... }

after opening Nemerle.Assertions namespace
*/
[Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
Nemerle.MacroTargets.Method,
Inherited = true, AllowMultiple = true)]
  macro Requires (_ : TypeBuilder, m : ParsedMethod, assertion, other = null)
  syntax ("requires", assertion, Optional ("otherwise", other))
  {
    def check =
      if (other != null)
        <[ unless ($assertion) $other ]>
      else
        <[ assert($assertion, "The ``Requires'' contract of method `" +
                   $(m.Name : string) + "' has been violated.") ]>;
        
    def newBody = Util.locate(m.Body.Location, <[
$check;
      $(m.Body)
    ]>);
      
    m.Body = newBody;
  }
  
  /** Enforces given boolean condition at the end of method invocation.

It checks at runtime, that given condition is true at the end
of each method invocation. The `otherwise' section allows to specify
what should happen when condition is false (for example throw some
exception).
Example: [Ensures (foo () != 4)]
foo (i : int) : int { ... }
or
foo (i : int) : int
ensures value > 0
{ ... }

after opening Nemerle.Assertions namespace
*/
[Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers,
Nemerle.MacroTargets.Method,
Inherited = true, AllowMultiple = true)]
  macro Ensures (_ : TypeBuilder, m : MethodBuilder, assertion, other = null)
  syntax ("ensures", assertion, Optional ("otherwise", other))
  {
    def check =
      if (other != null)
        <[ unless ($assertion) $other ]>
      else
        <[ assert($assertion, "The ``Ensures'' contract of method `" +
                   $(m.Name : string) + "' has been violated.") ]>;
    
    def newBody = Util.locate(m.Body.Location,
      if (m.ReturnType.Equals (FixedType.Void ()))
        <[
$(m.Body);
          $check;
        ]>
      else
        <[
def $("value" : usesite) = $(m.Body);
          $check;
          $("value" : usesite);
        ]>);
      
      m.Body = newBody;
  }

  /*
TODO for invariant:
- invariants are inherited from super class - all derived classes have
implicit call to own / overriden _N_invariant in public methods
- addition of expose to method can be disabled with attribute
- add expose (o upto T), which fires invariants for o in its superclasses
- only constants, fields, arrays, state independent and confined (depending
on owned mutable fields) methods can be mentioned in invariants
- add class invariants (for static fields)
- add support for checked exceptions and rethrowing them (in case of invariant
violation, they are rewhrown as inner exceptions)

Example: [Invariant (i > 0)] class A { mutable i : int; ... }
*/
[Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers,
Nemerle.MacroTargets.Class,
Inherited = true, AllowMultiple = true)]
  macro Invariant (ty : TypeBuilder, body)
  syntax ("invariant", body)
  {
    def existing =
      NList.Find (ty.LookupMember ("_N_invariant"), fun (x : IMember) {
        x.DeclaringType.Equals (ty) && x is IMethod
      });

    match (existing) {
      | None =>
        def methods = ty.GetMethods (BindingFlags.Public %|
                                     BindingFlags.Instance %|
                                     BindingFlags.DeclaredOnly);
        foreach (m :> MethodBuilder in methods)
        {
          def newBody = Util.locate(m.Body.Location,
            <[ InvariantExpose (this, $(m.Body)) ]>);
          m.Body = newBody;
        }
        ty.Define ( <[ decl:
          public mutable _N_invariant_affinity : int = -1;
        ]> );
        ty.Define ( <[ decl:
          public virtual _N_invariant () : void
          {
            assert($body, "The class invariant has been violated.")
          }
        ]> );
      | Some (m) =>
        def m = m :> MethodBuilder;
        def newBody = Util.locate(m.Body.Location, <[
$(m.Body);
          assert($body, "The class invariant has been violated.");
        ]>);
        m.Body = newBody;
    }
  }

  macro InvariantExpose (exposed, body)
  syntax ("expose", "(", exposed, ")", body)
  {
    def tbody = Nemerle.Macros.ImplicitCTX ().TypeExpr (body);
    def default = Macros.DefaultValueOfType (tbody.ty.Fix ());

    <[
def e = $exposed;
      lock (e) {
        def currentThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
        
        when (e._N_invariant_affinity != -1 && e._N_invariant_affinity != currentThreadId)
          throw System.Exception($"Invariant lock has already been taken from another thread $currentThreadId");
          
        e._N_invariant_affinity = currentThreadId
      }
      mutable need_to_check = false;

      mutable result = $default;
      try {
        result = $(tbody : typed);
        need_to_check = true;
        result
      }
      /* FIXME: we would need support for checked exceptions (at method's signature level)
catch {
| e is IChecked => need_to_check = true; throw;
| _ => throw;
}
*/
      finally
      {
        lock (e)
        {
          e._N_invariant_affinity = -1;
          when (need_to_check)
            e._N_invariant ()
        }
      }
    ]>
  }
}
Something went wrong with that request. Please try again.