/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 */
package org.thingml.xtext.helpers;

import org.thingml.xtext.constraints.Types;
import org.thingml.xtext.thingML.ObjectType;
import org.thingml.xtext.thingML.Type;

/**
 * Created by ffl on 10.05.2016.
 */
public class TyperHelper {


    public static Type getBroadType(Type self) {
        if (AnnotatedElementHelper.hasAnnotation(self, "type_checker")) {
            final String ty = AnnotatedElementHelper.annotation(self, "type_checker").get(0);
            if (ty.equals("Integer"))
                return Types.INTEGER_TYPE;
            else if (ty.equals("Real"))
                return Types.REAL_TYPE;
            else if (ty.equals("Boolean"))
                return Types.BOOLEAN_TYPE;
            else if (ty.equals("Character"))
                return Types.CHARACTER_TYPE;
            else if (ty.equals("String"))
                return Types.STRING_TYPE;
            else if (ty.equals("Object"))
                return Types.OBJECT_TYPE;
            else if (ty.equals("Error"))
                return Types.ERROR_TYPE;
            else if (ty.equals("Void"))
                return Types.VOID_TYPE;
            else
                return Types.ANY_TYPE;
        } else if (self instanceof ObjectType) {
            return Types.OBJECT_TYPE;
        }
        return Types.ANY_TYPE;
    }

    public static boolean isA(Type self, Type t) {
        if (getBroadType(self) == Types.ANY_TYPE && getBroadType(t) != Types.ERROR_TYPE)//Any is anything
            return true;
        if (getBroadType(t) == Types.ANY_TYPE && getBroadType(self) != Types.ERROR_TYPE)//anything is an Any
            return true;
        if (getBroadType(self) == getBroadType(t))
            return true;
        if (getBroadType(self) == Types.INTEGER_TYPE && getBroadType(t) == Types.REAL_TYPE) //an Integer is a Real
            return true;
        if (getBroadType(self) == Types.STRING_TYPE && getBroadType(t) == Types.OBJECT_TYPE)//a String is an Object
            return true;
        return false;
    }
}

@@ -2032,32 +2032,32 @@
 pe t) %7B%0A    
-    if (
+%09self = 
 getBroadType
@@ -2066,186 +2066,261 @@
 elf)
- == Types.ANY_TYPE && getBroadType(t) != Types.ERROR_TYPE)//Any is anything%0A            return true;%0A        if (getBroadType(t) == Types.ANY_TYPE && getBroadType
+;%0A    %09t = getBroadType(t);%0A        if (self == t) // T is a T%0A            return true;%0A    %09if (t == Types.OBJECT_TYPE) //Only String and Object are Object%0A    %09%09return self == Types.OBJECT_TYPE %7C%7C self == Types.STRING_TYPE; %0A        if 
 (self
-) !
+ =
 = Types.
 ERRO
@@ -2319,21 +2319,19 @@
 pes.
-ERROR
+ANY
 _TYPE)//
 anyt
@@ -2326,16 +2326,23 @@
 _TYPE)//
+Any is 
 anything
@@ -2346,159 +2346,179 @@
 ing 
-is an Any%0A            return true;%0A        if (getBroadType(self) == getBroadType(t))%0A            return true;%0A        if (getBroadType(
+(except Object, see above)%0A            return t != Types.ERROR_TYPE;%0A        if (t == Types.ANY_TYPE)//anything (except Object) is an Any%0A            return 
 self
-) =
+ !
 = Types.
 INTE
@@ -2509,31 +2509,30 @@
 lf != Types.
-INTEGER
+OBJECT
 _TYPE && get
@@ -2532,90 +2532,32 @@
  && 
-getBroadType(t) == Types.REAL_TYPE) //an Integer is a Real%0A            return true
+self != Types.ERROR_TYPE
 ;%0A  
@@ -2569,27 +2569,13 @@
  if 
-(getBroadType
 (self
-)
  == 
@@ -2576,30 +2576,31 @@
 lf == Types.
-STRING
+INTEGER
 _TYPE && get
@@ -2596,31 +2596,17 @@
 TYPE && 
-getBroadType(t)
+t
  == Type
@@ -2611,43 +2611,41 @@
 pes.
-OBJECT
+REAL
 _TYPE)
+ 
 //a
- String is an Object
+n Integer is a Real
 %0A   
@@ -2661,24 +2661,32 @@
 return true;
+        
 %0A        ret

fix typechecker not to consider Object as Any (and vice versa)