Beispiel #1
0
 private TypeMapping(TypeInfo aType)
 {
     //this._type = aType;
     this._classifier = aType.IsClass;   // ((this._type.IsClass || this._type.IsInterface) || this._type.IsEnum) || this._type.IsValueType;
     this._name = aType.Code;//TypeInfoHelper.GetTypeName(aType);
     this._bComplete = false;
     this._bDelegate = false;
     this._bModule = false;
     if (aType.IsClass)
     {
         ////foreach (object obj2 in this._type.GetCustomAttributes(false))
         ////{
         ////    if (obj2.GetType().FullName == "Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute")
         ////    {
         ////        this._bModule = true;
         ////        break;
         ////    }
         ////}
     }
     _tabType.Add(aType, this);
     if (_tabCheck.Contains(aType.Code))
     {
         if (LZ.Reverse.Info._bDebug)
         {
             LZ.Reverse.Info.Write(new string[] { "*** there are several classifiers \"{0}\"", this._type.FullName });
         }
     }
     else
     {
         _tabCheck.Add(aType.Code, this);
     }
 }
        public static string GetTypeNamespace(TypeInfo aType)
        {
            if (aType.Namespace == null)
            {
                return "";
            }

            return "";
            ////return (aType.Namespace + "." + aType.Code);
        }
Beispiel #3
0
        private void ProcessAType(ref TypeInfo ti, ref string sModifier, bool bIsValueType)
        {
            string word = NextTokenText();
            ti.Name = ti.Code = word;
            word = NextTokenText();
            string wordNext = GetTokenText(tokenCurrent.next);


            if (word == ";" || word == "*" || ";" == wordNext || wordNext == "*")
            {
                ti.IsClass = false;
                return;
            }
            ti.IsClass = true;

            ObjectInfo objTemp = new ObjectInfo(), paramObj = new ObjectInfo();
            List<ObjectInfo> paramList = new List<ObjectInfo>();
            bool isTypeBody = false, isMethord = false;
            Stack<int> stack = new Stack<int>();
            stack.Push(0);
            while (stack.Count > 0)
            {
                switch(word)
                {
                    case "operator":
                        ReadMethordBody(word, null);
                        objTemp.Clear();
                        break;
                    case "volatile":
                        objTemp.IsVolatile = true;
                        break;
                    case "static":
                        objTemp.IsStatic = true;
                        break;
                    case "virtual":
                        objTemp.IsVirtual = true;
                        objTemp.Virtual = word;
                        break;
                    case "WINAPI":
                    case "afx_msg":
                        break;
                    case "private":
                    case "protected":
                    case "public":
                        sModifier = word;
                        break;
                    case "union":
                        objTemp.Type = word;
                        ReadMethordBody(word, null);
                        break;
                    case ":":
                        if (!isTypeBody)
                        {
                            // 处理继承
                            ReadParentInfo(ti, word);
                        }
                        break;
                    case "(":
                        isMethord = true;
                        paramList.Clear();
                        paramObj.Clear();
                        break;
                    case ")":
                        if (isMethord && tokenCurrent.next != null && tokenCurrent.next.next != null && ("=" == GetTokenText(tokenCurrent.next)) && ("0" == GetTokenText(tokenCurrent.next.next)))
                        {
                            objTemp.IsAbstract = true;
                            ti.IsAbstract = true;
                            tokenCurrent = tokenCurrent.next.next;
                        }
                        if (isMethord && wordNext == ":")
                        {
                            // 构造函数后有参数初始化的
                            ReadMethordBody(word, objTemp.Implementation);

                            if (isTypeBody)
                            {
                                if (!string.IsNullOrEmpty(paramObj.Code))
                                {
                                    ObjectInfo param = new ObjectInfo();
                                    param.Store(paramObj);
                                    paramList.Add(param);
                                }
                                paramObj.Clear();

                                // 方法或属性定义结束
                                ti.AddMethord(objTemp, paramList);

                                objTemp.Clear();
                                isMethord = false;
                            }
                        }
                        break;
                    case "}":
                        stack.Pop();
                        break;
                    case "{":
                    case ";":
                    case ",":
                        if (isTypeBody && isMethord)    //  && (word == "," || word == ";")
                        {
                            if (!string.IsNullOrEmpty(paramObj.Code))
                            {
                                ObjectInfo param = new ObjectInfo();
                                param.Store(paramObj);
                                paramList.Add(param);
                            }
                            paramObj.Clear();
                        }

                        if (isTypeBody && word != ",") //  || word == ";"
                        {
                            // 方法或属性定义结束
                            if (isMethord)
                            {
                                // 跳过方法体
                                if (word == "{")
                                {
                                    ReadMethordBody(word, objTemp.Implementation);
                                }

                                ti.AddMethord(objTemp, paramList);
                            }
                            else
                            {
                                ti.AddAttribute(objTemp);
                            }

                            objTemp.Clear();
                            isMethord = false;

                            break;
                        }

                        // 类体定义,方法或属性
                        if (!isTypeBody && word == "{")
                        {
                            isTypeBody = true;
                        }
                        break;
                    case "DECLARE_DYNCREATE":
                    case "DECLARE_MESSAGE_MAP":
                        // 读取到行尾
                        word = ReadToLineEnd();
                        wordNext = GetTokenText(tokenCurrent);
                        continue;
                    default:
                        if (word == "~" && GetTokenText(tokenCurrent.next) == ti.Code)
                        {
                            // 析构函数
                            objTemp.Modifier = sModifier;
                            objTemp.Code = objTemp.Name = word + ti.Code;
                            word = NextTokenText();
                        }
                        else
                        {
                            if (isMethord)
                            {
                                if (word == "=")
                                {
                                    // 忽略参数的默认值
                                    wordNext = GetTokenText(tokenCurrent.next);
                                    Stack sTemp = new Stack();
                                    while ((wordNext != "," && wordNext != ")") || sTemp.Count > 0)
                                    {
                                        switch (wordNext)
                                        {
                                            case "(":
                                                sTemp.Push("(");
                                                break;
                                            case ")":
                                                if (sTemp.Count > 0) sTemp.Pop();
                                                break;
                                        }
                                        word = NextTokenText();
                                        paramObj.Default += word;
                                        if (tokenCurrent == null || tokenCurrent.next == null)
                                        {
                                            break;
                                        }
                                        wordNext = GetTokenText(tokenCurrent.next);
                                    }
                                }
                                else
                                {
                                    // 一般应该为参数
                                    ChangeObjectInfo(paramObj, word, sModifier);
                                }
                            }
                            else
                            {
                                // 可能为构造函数或属性定义【指针】
                                ChangeObjectInfo(objTemp, word, sModifier);
                            }
                        }

                        break;
                }

                if (tokenCurrent == null ||tokenCurrent.next == null)
                {
                    break;
                }
                word = NextTokenText();
                wordNext = GetTokenText(tokenCurrent.next);
            }
        }
Beispiel #4
0
        private void ReadParentInfo(TypeInfo ti, string currentWord)
        {
            string word = NextTokenText();
            while (tokenCurrent != null && "{" != GetTokenText(tokenCurrent.next))
            {
                switch (word)
                {
                    case "private":
                    case "protected":
                    case "public":
                        word = NextTokenText();
                        break;
                    default:
                        break;
                }

                TypeInfo obj = new TypeInfo();
                obj.Code = word;
                obj.Name = word;
                ti.Parents.Add(obj);

                word = GetTokenText(tokenCurrent.next);
                if (word == ",")
                {
                    word = NextTokenText();
                    word = NextTokenText();
                }
            }
        }
Beispiel #5
0
        private void AddClassifier(TypeInfo aType, bool bComplete)
        {
            bool bCreate = true;
            TypeMapping mapping = TypeMapping.Retrieve(aType, ref bCreate);
            if (bCreate || bComplete)
            {
                PdOOM.BasePackage package = null;
                Classifier classifier;
                if (bCreate)
                {
                    bool flag2;
                    PdOOM.NamedObject container = this.GetContainer(aType, out flag2);
                    if (flag2)
                    {
                        package = (PdOOM.BasePackage)container;
                    }
                    else
                    {
                        package = NamespaceMapping.RetrieveFromChildName("").Package;
                    }
                    int kind = 0x18112063;  // aType.IsInterface ? 0x18112064 : 0x18112063;
                    classifier = (Classifier)container.CreateObject(kind, "", -1, true);
                    mapping.Classifier = classifier;
                    try
                    {
                        classifier.Name = classifier.Code = aType.Code; // TypeInfoHelper.GetTypeName(aType);
                    }
                    catch (COMException)
                    {
                        if (LZ.Reverse.Info._bDebug)
                        {
                            LZ.Reverse.Info.Write(new string[] { "*** exception while naming the type \"{0}\"", /*TypeInfoHelper.GetTypeFullName(aType)*/aType.Code });
                        }
                    }
                    classifier.Visibility = GetTypeVisibility(aType.Modifier);

                    if (!bComplete)
                    {
                        return;
                    }
                }
                else
                {
                    classifier = mapping.Classifier;
                    ////package = mapping.Classifier.Package;
                    package = NamespaceMapping.RetrieveFromChildName("").Package;
                }
                mapping.Complete = true;
                classifier.Comment = aType.Comment;


                if (aType.IsClass && aType.IsAbstract)
                {
                    classifier.Abstract = true;
                }
                if (aType.IsValueType)
                {
                    classifier.Stereotype = "structure";
                }
                if (aType.IsGenericType)
                {
                    classifier.Stereotype = "template";
                }
                // 处理继承或实现
                foreach(TypeInfo ti in aType.Parents)
                {
                    ti.IsClass = true;
                    this.CreateGeneralization(classifier, ti, package);
                }
                InfluenceTable aInfTable = new InfluenceTable();

                foreach (AttributeInfo ai in aType.Attributes)
                {
                    if (package == null)//(!aAttrTable.Contains(info5))
                    {
                        ai.ToString();
                    }
                    else
                    {
                        this.CreateAttribute(aType, classifier, ai, package);
                    }
                }

                foreach (MethordInfo mi in aType.Methords)
                {
                    //if (!aAttrTable.Contains(info5))
                    {
                        this.CreateMethod(aType, classifier, mi, package, null, aInfTable);
                    }
                }
            }
        }
Beispiel #6
0
 private PdOOM.NamedObject GetContainer(TypeInfo aType, out bool bPackage)
 {
     //////Type declaringType = aType.Code;    //aType.DeclaringType;
     //////if (declaringType != null)
     //////{
     ////    bool bCreate = false;
     ////    //this.ProcessType(aType, false);
     ////    TypeMapping mapping = TypeMapping.Retrieve(aType, ref bCreate);
     ////    bPackage = false;
     ////    return mapping.Classifier;
     //////}
     NamespaceMapping mapping2 = NamespaceMapping.RetrieveFromChildName(TypeInfoHelper.GetTypeNamespace(aType));
     bPackage = true;
     return mapping2.Package;
 }
Beispiel #7
0
        private void CreateMethod(TypeInfo aType, Classifier aCls, MethordInfo aMethod, PdOOM.BasePackage aPckg, Operation anOper, InfluenceTable anInfTable)
        {
            Operation operation;
            if (anOper != null)
            {
                operation = anOper;
            }
            else
            {
                operation = (Operation)aCls.CreateObject(0x18112066, "", -1, true);
            }
            operation.Static = aMethod.IsStatic;
            operation.Visibility = GetTypeVisibility(aMethod.Modifier);
            operation.ReturnType = aMethod.Type;
            //operation.Final = aMethod.IsFinal;
            if (aMethod.IsGenericMethod)
            {
                operation.Generic = true;
                foreach (TypeParameter parameter in operation.TypeParameters)
                {
                    parameter.delete();
                }
                ////foreach (Type type in aMethod.GetGenericArguments())
                ////{
                ////    TypeParameter parameter2 = (TypeParameter)operation.CreateObject(0x1811207d, "", -1, true);
                ////    this.ProcessType(type, false);
                ////    TypeMapping mapping = TypeMapping.Retrieve(type);
                ////    parameter2.Name = parameter2.Code = mapping.Name;
                ////    operation.TypeParameters.Add(parameter2);
                ////}
            }
            if (aMethod.IsVirtual)
            {
                operation.SetExtendedAttribute("C++.virtual", "true");
            }
            if (aMethod.IsAbstract)
            {
                operation.Abstract = true;
            }
            try
            {
                operation.Code = aMethod.Code;
                operation.Name = aMethod.Name;
            }
            catch (COMException)
            {
                if (LZ.Reverse.Info._bDebug)
                {
                    LZ.Reverse.Info.Write(new string[] { "*** exception while naming the operation \"{1}\" on the type \"{0}\"", aType.Code, aMethod.Name });
                }
            }

            new CustomHandlerMethod(aMethod, operation).Convert();
            // 添加参数
            foreach (ObjectInfo type in aMethod.Parameters)
            {
                AddParameter(operation, type, aPckg);
            }

            // 防止有重载方法前面改名失败
            if (aMethod.Parameters.Count > 0 && operation.Code != aMethod.Code)
            {
                try
                {
                    operation.Code = aMethod.Code;
                    operation.Name = aMethod.Name;
                }
                catch (COMException)
                {
                    if (LZ.Reverse.Info._bDebug)
                    {
                        LZ.Reverse.Info.Write(new string[] { "*** exception while naming the operation \"{1}\" on the type \"{0}\"", aType.Code, aMethod.Name });
                    }
                }
            }
        }
Beispiel #8
0
        private void CreateGeneralization(PdOOM.NamedObject obj, TypeInfo aParentType, PdOOM.BasePackage aPckg)
        {
            this.AddClassifier(aParentType, false);
            ////if (aParentType.IsGenericType && !aParentType.IsGenericTypeDefinition)
            ////{
            ////    aParentType = aParentType.GetGenericTypeDefinition();
            ////}
            bool bCreate = true;
            TypeMapping aType = TypeMapping.Retrieve(aParentType, ref bCreate);
            PdOOM.BaseObject objParent = ProperRef.Link(NamespaceMapping.Retrieve(aPckg), aType);
            Generalization generalization = (Generalization)aPckg.CreateObject(0x18112067, "", -1, true);
            generalization.ParentObject = objParent;
            generalization.ChildObject = obj;

            try
            {
                generalization.Name = generalization.Code = string.Format("gen_{0}_2_{1}", obj.Code, aParentType.Code); // TypeInfoHelper.GetTypeName(aType);
            }
            catch (COMException)
            {
                if (LZ.Reverse.Info._bDebug)
                {
                    LZ.Reverse.Info.Write(new string[] { "*** exception while naming the Generalization \"{0}\"", generalization.Name });
                }
            }

        }
Beispiel #9
0
        private void CreateAttribute(TypeInfo ti, Classifier aCls, AttributeInfo aField, PdOOM.BasePackage aPckg)
        {
            CustomHandlerField field = new CustomHandlerField(aField, null);
            if (!field.ThroughProperty)
            {
                PdOOM.Attribute attribute = (PdOOM.Attribute)aCls.CreateObject(0x18112065, "", -1, true);
                if (aField.IsStatic)
                {
                    attribute.Static = true;
                }
                try
                {
                    attribute.Name = attribute.Code = aField.Name;
                }
                catch (COMException)
                {
                    if (LZ.Reverse.Info._bDebug)
                    {
                        LZ.Reverse.Info.Write(new string[] { "*** exception while naming the attribute \"{1}\" on the type \"{0}\"", aField.Code, aField.Name });
                    }
                }
                attribute.Visibility = GetTypeVisibility(aField.Modifier);
                //attribute.Persistent = !aField.IsNotSerialized;
                attribute.Volatile = aField.IsVolatile;
                if (aField.IsPointer)
                {
                    attribute.Multiplicity = "*";
                }
                field.PdObj = attribute;
                field.Convert();

                TypeInfo tiField = new TypeInfo();
                ////System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[a-zA-Z_][a-zA-Z_0-9]*");
                ////System.Text.RegularExpressions.Match m = reg.Match(aField.Type.Replace("unsigned",""));
                tiField.Code = tiField.Name = aField.Type;
                tiField.IsClass = true;
                tiField.Comment = aCls.Comment;
                this.AddClassifier(tiField, false);
                bool bCreate = true;
                TypeMapping mapping = TypeMapping.Retrieve(tiField, ref bCreate);
                if (mapping.HasClassifier())
                {
                    PdOOM.NamedObject objClassB = (PdOOM.NamedObject)ProperRef.Link(NamespaceMapping.Retrieve(aPckg), mapping);
                    PdOOM.NamedObject objClassA = (PdOOM.NamedObject)attribute.Parent;
                    attribute.UseQualifiedDataType = false;
                    attribute.DataTypeObject = objClassA;

                    Association ass = (Association)aPckg.CreateObject((int)PdOOM_Classes.cls_Association, "", -1, true);
                    ass.ClassA = objClassA;
                    ass.ClassB = objClassB;
                    try
                    {
                        ass.Name = ass.Code = string.Format("ass_{0}_2_{1}_{2}", objClassA.Code, objClassB.Code, attribute.Code);
                        if (tiField.Code == aField.FullType)
                        {
                            ass.RoleBMultiplicity = "1..1";
                            ass.RoleAIndicator = aField.IsPointer ? "A" : "C";
                        }
                        ass.RoleAMultiplicity = "";
                        ass.RoleBName = aField.Name;
                        ass.RoleANavigability = false;
                        ass.RoleBNavigability = false;
                    }
                    catch (COMException)
                    {
                        if (LZ.Reverse.Info._bDebug)
                        {
                            LZ.Reverse.Info.Write(new string[] { "*** exception while naming the attribute \"{1}\" on the type \"{0}\"", aField.Code, aField.Name });
                        }
                    }

                }
                ////else
                {
                    attribute.DataType = aField.FullType;   // mapping.Name;
                }
            }
        }
Beispiel #10
0
        private void AddParameter(Operation aMethod, ObjectInfo aParam, PdOOM.BasePackage aPckg)
        {
            Parameter aPrm = (Parameter)aMethod.CreateObject(0x1811206d, "", -1, true);
            string str = "I";    // aParam.IsOut ? "O" : "I";
            ////Type parameterType = aParam.Type;
            ////if (parameterType.IsGenericType && !parameterType.IsGenericTypeDefinition)
            ////{
            ////    parameterType = parameterType.GetGenericTypeDefinition();
            ////}
            if (aParam.IsConst)
            {
                aPrm.SetExtendedAttribute("C++.const", "true");
            }
            if (aParam.IsByRef)
            {
                aPrm.SetExtendedAttribute("C++.ref", "true");
            }
            if (!aParam.IsConst && (aParam.IsByRef || aParam.IsPointer))
            {
                str = "IO";
            }
            if (aParam.IsArray)
            {
                aPrm.Array = true;
            }
            aPrm.SetExtendedAttribute("C++.init", aParam.Default);

            TypeInfo tiField = new TypeInfo();
            tiField.Code = tiField.Name = aParam.Type;
            tiField.IsClass = true;
            tiField.Comment = ((PdOOM.NamedObject)aMethod.Parent).Comment;
            this.AddClassifier(tiField, false);
            bool bCreate = true;
            TypeMapping aType = TypeMapping.Retrieve(tiField, ref bCreate);
            if (aType.HasClassifier())
            {
                PdOOM.BaseObject obj2 = ProperRef.Link(NamespaceMapping.Retrieve(aPckg), aType);
                aPrm.UseQualifiedDataType = false;
                aPrm.DataTypeObject = obj2;
            }
            aPrm.ParameterType = str;
            aPrm.DataType = aParam.FullType;
            aPrm.Name = aPrm.Code = aParam.Name;
            CustomHandlerParam param = new CustomHandlerParam(aParam, aPrm);
            param.Convert();
            if (param.Params)
            {
                aPrm.SetExtendedAttribute("C++.params", "true");
            }
        }
Beispiel #11
0
        public void ProcessAFile(string aName, Encoding encode)
        {

            Debug.WriteLine(string.Format("reverse file: {0}", aName));

            TextReader reader = new StreamReader(aName, encode);
            _lexer = new LZ.Lexer.CPP.Lexer(reader);

            LZ.Lexer.Token tokenFirst = null, token = null;
            int i = 0;
            do
            {
                i++;
                token = _lexer.NextToken();
                if (token.kind == 0) break;
                if (tokenFirst == null)
                {
                    tokenFirst = token;
                }

                //Debug.WriteLine(string.Format("{0}. {1}", i, GetTokenText(token)));
            }
            while (token.kind != 0);

            tokenCurrent = null;
            tokenCurrent = tokenFirst;
            string sModifier = "private";  // 0=private;1=protected;2=public;
            bool isInTypeDef = false, isParameter = false, isGenericType = false;
            while (tokenCurrent != null && tokenCurrent.next != null)
            {
                string word = tokenCurrent.val;
                TypeInfo ti = null;

                switch (word)
                {
                    case "template":
                        isGenericType = true;
                        ReadTemplate();
                        break;
                    case "class":
                        sModifier = "private";
                        ti = new TypeInfo();
                        ti.Comment = aName;
                        ti.IsGenericType = isGenericType;
                        ProcessAType(ref ti, ref sModifier, false);
                        isGenericType = false;
                        ////if (ti.IsClass) AddClassifier(ti, true);
                        if (!this._work.Contains(ti))
                        {
                            this._work.Push(ti);
                        }
                        break;
                    case "struct":
                        // 跳过在类型定义体中的struct关键字
                        if (isParameter) break;

                        ti = new TypeInfo();
                        ti.Comment = aName;
                        sModifier = "public";
                        ti.IsValueType = true;
                        ti.IsGenericType = isGenericType;
                        ProcessAType(ref ti, ref sModifier, true);
                        isGenericType = false;
                        if (ti.IsClass && !this._work.Contains(ti))
                        {
                            this._work.Push(ti);
                        }
                        ////if (ti.IsClass) AddClassifier(ti, true);
                        break;
                    case "typedef":
                        isInTypeDef = true;
                        break;
                    case ";":
                        isInTypeDef = false;
                        break;
                    case "(":
                        isParameter = true;
                        break;
                    case ")":
                        isParameter = false;
                        break;
                    default:
                        break;
                }

                if (tokenCurrent == null) break;
                tokenCurrent = tokenCurrent.next;
            }

            // 关闭文件
            reader.Close();
            _lexer = null;
        }
Beispiel #12
0
 public static TypeMapping Retrieve(TypeInfo aType, ref bool bCreate)
 {
     if (aType.IsGenericType && !aType.IsGenericTypeDefinition)
     {
         //aType = aType.GetGenericTypeDefinition();
     }
     ////if (aType.IsGenericParameter)
     ////{
     ////    return DefineExtraType(aType, aType.Name);
     ////}
     TypeMapping mapping = (TypeMapping)_tabType[aType];
     if (mapping != null)
     {
         bCreate = false;
         return mapping;
     }
     if (!bCreate)
     {
         bCreate = false;
         return null;
     }
     mapping = (TypeMapping)_tabCheck[aType.Code];
     if (mapping != null)
     {
         _tabType[aType] = mapping;
         bCreate = false;
         return mapping;
     }
     mapping = new TypeMapping(aType);
     mapping._classifier = aType.IsClass;
     bCreate = true;
     return mapping;
 }
Beispiel #13
0
 public static TypeMapping DefineExtraType(string aName)
 {
     TypeInfo aType = new TypeInfo();
     aType.Code = aType.Name = aName;
     aType.Modifier = "public";
     TypeMapping mapping = (TypeMapping)_tabType[aType];
     if (mapping == null)
     {
         mapping = new TypeMapping(aType);
         if (aName != "")
         {
             mapping._name = aName;
         }
         mapping.Complete = true;
     }
     return mapping;
 }