Beispiel #1
0
 public bool SetVariableData(LocalVariable variable, EscapingMode escaping)
 {
     if (variable.Escaping == escaping)
     {
         return(false);
     }
     variable.Escaping = escaping;
     return(true);
 }
        public bool SetVariableData(LocalVariable variable, EscapingMode escaping)
        {
            EscapingMode result;

            if (LocalVarEscaping.TryGetValue(variable, out result))
            {
                if (result == escaping)
                {
                    return(false);
                }
            }

            LocalVarEscaping[variable] = escaping;
            return(true);
        }
 public void SetVariableData(LocalVariable variable, EscapingMode escaping)
 {
     LocalVarEscaping[variable] = escaping;
 }
Beispiel #4
0
        public static string EscapeIdentifier(string identifier, EscapingMode escapingMode = EscapingMode.MemberIdentifier)
        {
            if (escapingMode == EscapingMode.None)
            {
                return(identifier);
            }

            bool   isEscaped = false;
            string result    = identifier;

            var sb = EscapeStringBuilder.Value;

            sb.Clear();

            for (int i = 0, l = identifier.Length; i < l; i++)
            {
                var ch = identifier[i];

                switch (ch)
                {
                case '.':
                    if (escapingMode != EscapingMode.MemberIdentifier)
                    {
                        sb.Append(".");
                    }
                    else
                    {
                        sb.Append("_");
                        isEscaped = true;
                    }
                    break;

                case '/':
                    if (escapingMode == EscapingMode.MemberIdentifier)
                    {
                        sb.Append("_");
                        isEscaped = true;
                    }
                    else if (escapingMode == EscapingMode.TypeIdentifier)
                    {
                        sb.Append("_");
                        isEscaped = true;
                    }
                    else
                    {
                        sb.Append("/");
                    }
                    break;

                case '+':
                    if (escapingMode == EscapingMode.MemberIdentifier)
                    {
                        sb.Append("_");
                        isEscaped = true;
                    }
                    else if (escapingMode == EscapingMode.TypeIdentifier)
                    {
                        sb.Append("_");
                        isEscaped = true;
                    }
                    else
                    {
                        sb.Append("+");
                    }
                    break;

                case '`':
                    if (escapingMode != EscapingMode.String)
                    {
                        sb.Append("$b");
                    }
                    else
                    {
                        sb.Append("`");
                    }
                    isEscaped = true;
                    break;

                case '~':
                    sb.Append("$t");
                    isEscaped = true;
                    break;

                case ':':
                    sb.Append("$co");
                    isEscaped = true;
                    break;

                case '<':
                    sb.Append("$l");
                    isEscaped = true;
                    break;

                case '>':
                    sb.Append("$g");
                    isEscaped = true;
                    break;

                case '(':
                    sb.Append("$lp");
                    isEscaped = true;
                    break;

                case ')':
                    sb.Append("$rp");
                    isEscaped = true;
                    break;

                case '{':
                    sb.Append("$lc");
                    isEscaped = true;
                    break;

                case '}':
                    sb.Append("$rc");
                    isEscaped = true;
                    break;

                case '[':
                    sb.Append("$lb");
                    isEscaped = true;
                    break;

                case ']':
                    sb.Append("$rb");
                    isEscaped = true;
                    break;

                case '@':
                    sb.Append("$at");
                    isEscaped = true;
                    break;

                case '-':
                    sb.Append("$da");
                    isEscaped = true;
                    break;

                case '=':
                    sb.Append("$eq");
                    isEscaped = true;
                    break;

                case ' ':
                    sb.Append("$sp");
                    isEscaped = true;
                    break;

                case '?':
                    sb.Append("$qu");
                    isEscaped = true;
                    break;

                case '!':
                    sb.Append("$ex");
                    isEscaped = true;
                    break;

                case '*':
                    sb.Append("$as");
                    isEscaped = true;
                    break;

                case '&':
                    sb.Append("$am");
                    isEscaped = true;
                    break;

                case ',':
                    sb.Append("$cm");
                    isEscaped = true;
                    break;

                case '|':
                    sb.Append("$vb");
                    isEscaped = true;
                    break;

                case '\'':
                    sb.Append("$q");
                    isEscaped = true;
                    break;

                default:
                    if ((ch <= 32) || (ch >= 127))
                    {
                        sb.AppendFormat("${0:x}", (int)ch);
                        isEscaped = true;
                    }
                    else
                    {
                        sb.Append(ch);
                    }
                    break;
                }
            }

            if (isEscaped)
            {
                result = sb.ToString();
            }

            bool isReservedWord = ReservedWords.Contains(result);

            if (isReservedWord)
            {
                result = "$" + result;
            }

            return(result);
        }
Beispiel #5
0
        public static string ToCppName(this Type type, bool handleGenerics = true,
                                       EscapingMode isSmartPtr             = EscapingMode.Smart)
        {
            if (type == null)
            {
                return("void*");
            }
            if (type.IsArray)
            {
                var elementType  = type.GetElementType();
                var fullTypeName = elementType.ToCppName(handleGenerics);
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return(String.Format(StdSharedPtr + "< Array < {0} > >", fullTypeName));

                case EscapingMode.Pointer:
                    return(String.Format("Array < {0} > *", fullTypeName));

                case EscapingMode.Stack:
                    return(String.Format("Array < {0} > ", fullTypeName));
                }
            }
            if (type.IsClass || isSmartPtr != EscapingMode.Smart)
            {
                if (type.IsPrimitive || type.IsValueType)
                {
                    isSmartPtr = EscapingMode.Stack;
                }
                if (type.Name.EndsWith("*") || type.Name.EndsWith("&"))
                {
                    var elementType        = type.GetElementType();
                    var elementTypeCppName = elementType.ToCppName(handleGenerics);
                    return(String.Format("{0}* ", elementTypeCppName));
                }
                if (type.IsGenericFieldUsage())
                {
                    isSmartPtr = EscapingMode.Stack;
                }
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return(String.Format(StdSharedPtr + "<{0}>", type.ToCppMangling(handleGenerics)));

                case EscapingMode.Pointer:
                    return(String.Format("{0} *", type.ToCppMangling(handleGenerics)));

                case EscapingMode.Stack:
                    return(String.Format("{0} ", type.ToCppMangling()));
                }
            }
            if (!type.IsClass || isSmartPtr != EscapingMode.Smart)
            {
                return(type.IsSubclassOf(typeof(Enum))
                    ? "int"
                    : type.Name.ToCppMangling(type.Namespace));
            }
            if (type.IsByRef)
            {
                var elementType = type.GetElementType();
                return(String.Format("{0}*", elementType.ToCppMangling()));
            }
            return(String.Format(StdSharedPtr + "<{0}>", type.ToCppMangling()));
        }
Beispiel #6
0
        public static string ToDeclaredVariableType(this Type type, bool handleGenerics = true,
                                                    EscapingMode isSmartPtr             = EscapingMode.Smart)
        {
            if (type == null)
            {
                return("void*");
            }
            if (type.IsArray)
            {
                var elementType  = type.GetElementType();
                var fullTypeName = elementType.ToCppName(handleGenerics);
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return(String.Format(StdSharedPtr + "< Array < {0} > >", fullTypeName));

                case EscapingMode.Pointer:
                    return(String.Format("Array < {0} > *", fullTypeName));

                case EscapingMode.Stack:
                    return(String.Format("Array < {0} > ", fullTypeName));
                }
            }
            if (type.IsClass || isSmartPtr != EscapingMode.Smart)
            {
                if (type.IsPrimitive || type.IsValueType)
                {
                    isSmartPtr = EscapingMode.Stack;
                }
                if (type.Name.EndsWith("*") || type.Name.EndsWith("&"))
                {
                    var elementType        = type.GetElementType();
                    var elementTypeCppName = elementType.ToCppName(handleGenerics);
                    return(String.Format("{0}* ", elementTypeCppName));
                }
                var typeParameters = type.GetGenericArguments();
                if (typeParameters.Length != 0)
                {
                    var genericSpecializations = typeParameters.Select(
                        t => t.ToCppMangling()
                        ).ToList();
                    var genericSpecializationString = string.Join(", ", genericSpecializations);

                    switch (isSmartPtr)
                    {
                    case EscapingMode.Smart:
                        return(String.Format(StdSharedPtr + "<{0} <{1}> >", type.ToCppMangling(),
                                             genericSpecializationString));

                    case EscapingMode.Pointer:
                        return(String.Format("{0} <{1}>*", type.ToCppMangling(), genericSpecializationString));

                    case EscapingMode.Stack:
                        return(String.Format("{0} <{1}>", type.ToCppMangling(), genericSpecializationString));
                    }
                }
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return(String.Format(StdSharedPtr + "<{0}>", type.ToCppMangling(handleGenerics)));

                case EscapingMode.Pointer:
                    return(String.Format("{0} *", type.ToCppMangling()));

                case EscapingMode.Stack:
                    return(String.Format("{0} ", type.ToCppMangling()));
                }
            }
            if (!type.IsClass || isSmartPtr != EscapingMode.Smart)
            {
                return(type.IsSubclassOf(typeof(Enum))
                    ? "int"
                    : type.Name.ToCppMangling(type.Namespace));
            }
            if (type.IsByRef)
            {
                var elementType = type.GetElementType();
                return(String.Format("{0}*", elementType.ToCppMangling()));
            }
            return(String.Format(StdSharedPtr + "<{0}>", type.ToCppMangling()));
        }
 public void Identifier(string name, EscapingMode escapingMode = EscapingMode.MemberIdentifier)
 {
     WriteRaw(Util.EscapeIdentifier(
                  name, escapingMode
                  ));
 }
        public static string ToCppName(this Type type,
                                       EscapingMode isSmartPtr = EscapingMode.Smart, bool isPInvoke = false)
        {
            if (type == null)
            {
                return("System_Void*");
            }

            if (type == typeof(string) && isPInvoke)
            {
                return("System_Char*");
            }

            if (type.IsArray)
            {
                var elementType  = type.GetElementType();
                var fullTypeName = elementType.ToCppName();
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return($"{StdSharedPtr}< Array < {fullTypeName} > >");

                case EscapingMode.Pointer:
                    return($"Array < {fullTypeName} > *");

                case EscapingMode.Stack:
                    return($"Array < {fullTypeName} > ");
                }
            }
            if ((type.IsClass || type.IsInterface) || isSmartPtr != EscapingMode.Smart)
            {
                if (type.IsPrimitive || type.IsValueType)
                {
                    isSmartPtr = EscapingMode.Stack;
                }
                if (type.Name.EndsWith("*") || type.Name.EndsWith("&"))
                {
                    var elementType        = type.GetElementType();
                    var elementTypeCppName = elementType.ToCppName();
                    return($"{elementTypeCppName}* ");
                }
                if (type.IsGenericFieldUsage())
                {
                    isSmartPtr = EscapingMode.Stack;
                }
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return(string.Format(StdSharedPtr + "<{0}>", type.ToCppMangling()));

                case EscapingMode.Pointer:
                    return($"{type.ToCppMangling()}*");

                case EscapingMode.Stack:
                    return($"{type.ToCppMangling()}");
                }
            }
            if (!(type.IsClass || type.IsInterface) || isSmartPtr != EscapingMode.Smart)
            {
                return(type.IsSubclassOf(typeof(Enum))
                    ? "int"
                    : type.Name.ToCppMangling(type.Namespace));
            }
            if (type.IsByRef)
            {
                var elementType = type.GetElementType();
                return($"{elementType.ToCppMangling()}*");
            }

            return(string.Format(StdSharedPtr + "<{0}>", type.ToCppMangling()));
        }
        public static string ToDeclaredVariableType(this Type type,
                                                    EscapingMode isSmartPtr = EscapingMode.Smart)
        {
            if (type == null)
            {
                return("System_Void*");
            }
            if (type.IsArray)
            {
                var elementType  = type.GetElementType();
                var fullTypeName = elementType.ToCppName();
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return(string.Format(StdSharedPtr + "< Array < {0} > >", fullTypeName));

                case EscapingMode.Pointer:
                    //TODO: add the text to pointers to make possible
                    // auto-vectorisation in gcc
                    // ' __restrict__ __attribute__ ((aligned (16))) '
                    return($"Array < {fullTypeName} >  *");

                case EscapingMode.Stack:
                    return($"Array < {fullTypeName} > ");
                }
            }
            if ((type.IsClass || type.IsInterface) || isSmartPtr != EscapingMode.Smart)
            {
                if (type.IsPrimitive || type.IsValueType)
                {
                    isSmartPtr = EscapingMode.Stack;
                }
                if (type.Name.EndsWith("*", StringComparison.Ordinal) || type.Name.EndsWith("&", StringComparison.Ordinal))
                {
                    var elementType        = type.GetElementType();
                    var elementTypeCppName = elementType.ToCppName();
                    return($"{elementTypeCppName}* ");
                }

                /*
                 * var typeParameters = type.GetGenericArguments();
                 * if (typeParameters.Length != 0)
                 * {
                 *  var genericSpecializations = typeParameters.Select(
                 *      t => t.ToCppMangling()
                 *      ).ToList();
                 *  var genericSpecializationString = string.Join(", ", genericSpecializations);
                 *
                 *  switch (isSmartPtr)
                 *  {
                 *      case EscapingMode.Smart:
                 *          return String.Format(StdSharedPtr + "<{0} <{1}> >", type.ToCppMangling(),
                 *              genericSpecializationString);
                 *      case EscapingMode.Pointer:
                 *          return String.Format("{0} <{1}>*", type.ToCppMangling(), genericSpecializationString);
                 *      case EscapingMode.Stack:
                 *          return String.Format("{0} <{1}>", type.ToCppMangling(), genericSpecializationString);
                 *  }
                 * }
                 * */
                switch (isSmartPtr)
                {
                case EscapingMode.Smart:
                    return($"{StdSharedPtr}<{type.ToCppMangling()}>");

                case EscapingMode.Pointer:
                    return($"{type.ToCppMangling()} *");

                case EscapingMode.Stack:
                    return($"{type.ToCppMangling()} ");
                }
            }
            if (!(type.IsClass || type.IsInterface) || isSmartPtr != EscapingMode.Smart)
            {
                return(type.IsSubclassOf(typeof(Enum))
                    ? "int"
                    : type.Name.ToCppMangling(type.Namespace));
            }
            if (type.IsByRef)
            {
                var elementType = type.GetElementType();
                return($"{elementType.ToCppMangling()}*");
            }
            return($"{StdSharedPtr}<{type.ToCppMangling()}>");
        }