Esempio n. 1
0
        public void SetTypeSystem(List <CodeCompilationUnit> compilationUnits)
        {
            Reset();
            //-----------------------
            this.compilationUnits = compilationUnits;
            //-----------------------
            //resolve cu's file path

            foreach (CodeCompilationUnit cu in compilationUnits)
            {
                //check absolute path for include file
                foreach (IncludeFileDirective includeDirective in cu._includeFiles)
                {
                    //remove " from begin and end of the original IncludeFile
                    if (includeDirective.SystemFolder)
                    {
                        continue;
                    }
                    //
                    string include_file = includeDirective.IncludeFile.Substring(1, includeDirective.IncludeFile.Length - 2);
                    includeDirective.ResolvedAbsoluteFilePath = RootFolder + "\\" + include_file;
                    //check
                    if (!System.IO.File.Exists(includeDirective.ResolvedAbsoluteFilePath))
                    {
                        //file not found
                        if (!(includeDirective.ResolvedAbsoluteFilePath.EndsWith("mac.h") ||
                              includeDirective.ResolvedAbsoluteFilePath.EndsWith("linux.h")))
                        {
                            throw new NotSupportedException();
                        }
                    }
                }
                //
                _compilationUnitDics.Add(cu.Filename, cu);
            }


            List <CodeMethodDeclaration> cppMethodList = new List <CodeMethodDeclaration>();

            //-----------------------
            //1. collect
            foreach (CodeCompilationUnit cu in compilationUnits)
            {
                //
                RegisterTypeDeclaration(cu.GlobalTypeDecl);
                //extract type from global typedecl
                foreach (CodeMemberDeclaration mb in cu.GlobalTypeDecl.GetMemberIter())
                {
                    switch (mb.MemberKind)
                    {
                    case CodeMemberKind.Method:
                    {
                        //check if this method has C++ explicit ower type
                        CodeMethodDeclaration metDecl = (CodeMethodDeclaration)mb;
                        if (metDecl.CppExplicitOwnerType != null)
                        {
                            //add this to typedecl later
                            cppMethodList.Add(metDecl);
                        }
                    }
                    break;

                    case CodeMemberKind.TypeDef:
                    {
                        CodeCTypeDef ctypeDef = (CodeCTypeDef)mb;
                        //
                        CTypeDefTypeSymbol ctypedefTypeSymbol = new CTypeDefTypeSymbol(ctypeDef.Name, ctypeDef.From);
                        ctypedefTypeSymbol.CreatedTypeCTypeDef = ctypeDef;
                        //---

                        TypeSymbol existing;
                        if (TryGetType(ctypeDef.Name, out existing))
                        {
                            throw new NotSupportedException();
                        }
                        RegisterType(ctypeDef.Name, ctypedefTypeSymbol);
                    }
                    break;

                    case CodeMemberKind.Type:
                    {
                        RegisterTypeDeclaration((CodeTypeDeclaration)mb);
                    }
                    break;
                    }
                }

                int typeCount = cu.TypeCount;
                for (int i = 0; i < typeCount; ++i)
                {
                    RegisterTypeDeclaration(cu.GetTypeDeclaration(i));
                }
            }
            //-----------------------
            //temp fix
            int methodCount = cppMethodList.Count;

            if (methodCount > 0)
            {
                //find owner and add the implementation
                for (int i = 0; i < methodCount; ++i)
                {
                    CodeMethodDeclaration metdecl = cppMethodList[i];
                    if (metdecl.LineComments != null)
                    {
                        //for cef, some line comment has transformation info
                    }
                }
            }
            //-----------------------
            ResolveBaseTypes();
            ResolveTypeMembers();
            //-----------------------

            AddMoreTypeInfo();
            //
            var cefTypeBridgeTxPlanner = new CefTypeBridgeTransformPlanner();

            cefTypeBridgeTxPlanner.AssignTypeBridgeInfo(this.typeSymbols);
            //
            this.Planner = cefTypeBridgeTxPlanner;

            //-----------------------
            //do class classification


            foreach (CodeTypeDeclaration t in typedeclDic.Values)
            {
                string name = t.Name;
                if (name.EndsWith("Callback"))
                {
                    _v_callBackClasses.Add(t);
                }
                else if (name.EndsWith("Handler"))
                {
                    _v_handlerClasses.Add(t);
                }
                else if (name.EndsWith("CToCpp"))
                {
                    //implementation
                    cToCppClasses.Add(t);
                }
                else if (name.EndsWith("CppToC"))
                {
                    //implementation
                    cppToCClasses.Add(t);
                }
                else
                {
                    switch (t.Kind)
                    {
                    default:
                    {
                        if (t.IsGlobalCompilationUnitType)
                        {
                            _fileModuleClasses.Add(t);
                        }
                        else if (t.IsTemplateTypeDefinition)
                        {
                            _templateClasses.Add(t);
                        }
                        else if (t.BaseIsVirtual)
                        {
                            _v_instanceClasses.Add(t);
                        }
                        else
                        {
                            if (t.BaseTypes != null && t.BaseTypes.Count > 0)
                            {
                                CodeTypeReference baseType = t.BaseTypes[0];
                                if (baseType.ResolvedType != null)
                                {
                                    switch (baseType.Name)
                                    {
                                    default:

                                        break;

                                    case "CefStructBase":
                                    case "CefBaseScoped":
                                    case "CefBaseRefCounted":
                                        _cefBaseTypes.Add(t);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                switch (t.Name)
                                {
                                default:
                                    break;

                                case "CefScopedSandboxInfo":
                                case "CefBaseRefCounted":
                                case "CefBaseScoped":
                                case "CefRefCount":
                                    break;
                                }
                            }
                        }
                    }
                    break;

                    case TypeKind.Enum:
                        if (!CefResolvingContext.IsAllLowerLetter(name))
                        {
                        }
                        _enumClasses.Add(t);
                        break;

                    case TypeKind.Struct:
                        if (!CefResolvingContext.IsAllLowerLetter(name))
                        {
                            if (name.EndsWith("Traits"))
                            {
                            }
                            else
                            {
                            }
                        }
                        _plainCStructs.Add(t);
                        break;
                    }
                }
            }
            //-----------------------
            //for analysis

            foreach (CodeTypeDeclaration t in typedeclDic.Values)
            {
                TypeSymbol resolvedType = t.ResolvedType;
                if (t.BaseTypes.Count == 0)
                {
                    //
                }
                else
                {
                    TypeSymbol        baseType = t.BaseTypes[0].ResolvedType;
                    TypeHierarchyNode found;
                    if (!hierarchy.TryGetValue(baseType, out found))
                    {
                        found = new TypeHierarchyNode(baseType);
                        hierarchy.Add(baseType, found);
                    }

                    if (found.Type != resolvedType)
                    {
                        found.AddTypeSymbol(resolvedType);
                    }
                }
            }
            //-----------------------
        }
Esempio n. 2
0
        void RegisterTypeDeclaration(CodeTypeDeclaration typeDecl)
        {
            //1. collect

            if (typeDecl.IsGlobalCompilationUnitType)
            {
                //this is global type
                if (typeDecl.MemberCount == 0)
                {
                    //skip this global type
                    return;
                }
            }

            if (!typeDecl.IsForwardDecl && typeDecl.Name != null)
            {
                CodeTypeDeclaration existingDecl;
                if (typedeclDic.TryGetValue(typeDecl.FullName, out existingDecl))
                {
                    //found
                    throw new Exception("duplicated key " + typeDecl.Name);
                }

                typedeclDic.Add(typeDecl.FullName, typeDecl);
                //-----------------------

                SimpleTypeSymbol typeSymbol = new SimpleTypeSymbol(typeDecl);
                typeDecl.ResolvedType = typeSymbol;
                //

                TypeSymbol existingTypeSymbol;
                if (TryGetType(typeSymbol.Name, out existingTypeSymbol))
                {
                    //have existing value
                    throw new NotSupportedException();
                }
                else
                {
                    RegisterType(typeSymbol.Name, typeSymbol);
                }
                //
                //and sub types
                if (!typeDecl.IsGlobalCompilationUnitType)
                {
                    foreach (CodeMemberDeclaration subType in typeDecl.GetSubTypeIter())
                    {
                        if (subType.MemberKind == CodeMemberKind.TypeDef)
                        {
                            CodeCTypeDef ctypeDef = (CodeCTypeDef)subType;
                            //

                            CTypeDefTypeSymbol ctypedefTypeSymbol = new CTypeDefTypeSymbol(ctypeDef.Name, ctypeDef.From);
                            ctypedefTypeSymbol.CreatedTypeCTypeDef = ctypeDef;
                            ctypedefTypeSymbol.ParentType          = typeSymbol;

                            //---
                            RegisterType(typeSymbol.Name + "." + ctypeDef.Name, ctypedefTypeSymbol);
                            List <TypeSymbol> nestedTypes = typeSymbol.NestedTypeSymbols;
                            if (nestedTypes == null)
                            {
                                typeSymbol.NestedTypeSymbols = nestedTypes = new List <TypeSymbol>();
                            }
                            nestedTypes.Add(ctypedefTypeSymbol);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        TypeSymbol ResolveType(string typename)
        {
            TypeSymbol foundSymbol = null;

            if (_currentResolvingType != null)
            {
                //1.
                if (_currentResolvingType.IsTemplateTypeDefinition)
                {
                    //
                    //check if this is the template type parameter
                    //if (typename == _currentResolvingType.TemplateNotation.templatePar.ReAssignToTypeName)
                    //{   //found
                    //    return new TemplateParameterTypeSymbol(_currentResolvingType.TemplateNotation.templatePar);
                    //}
                    CodeTemplateParameter foundTemplatePar = null;
                    if (_currentResolvingType.TemplateNotation.TryGetTemplateParByReAssignToName(typename, out foundTemplatePar))
                    {
                        //TODO: resolve template type parameter
                        return(new TemplateParameterTypeSymbol(foundTemplatePar));
                    }

                    if (_currentResolvingType.TemplateNotation.TryGetTemplateParByParameterName(typename, out foundTemplatePar))
                    {
                        //TODO: resolve template type parameter
                        return(new TemplateParameterTypeSymbol(foundTemplatePar));
                    }
                }


                //2.
                //search nest type
                //TODO: review here -> use field
                if (_currentResolvingType.HasSubType)
                {
                    List <CodeMemberDeclaration> tempResults = new List <CodeMemberDeclaration>();
                    int foundCount;
                    if ((foundCount = _currentResolvingType.FindSubType(typename, tempResults)) > 0)
                    {
                        for (int i = 0; i < foundCount; ++i)
                        {
                            CodeMemberDeclaration subtype = tempResults[i];
                            switch (subtype.MemberKind)
                            {
                            default: throw new NotSupportedException();

                            case CodeMemberKind.Type:
                                break;

                            case CodeMemberKind.TypeDef:
                            {
                                CodeCTypeDef ctypedef        = (CodeCTypeDef)subtype;
                                TypeSymbol   resolveFromType = ResolveType(ctypedef.From);
                                if (resolveFromType != null)
                                {
                                    //found
                                    return(resolveFromType);
                                }
                            }
                            break;
                            }
                        }
                    }
                }
                //3
                if (_currentResolvingType.BaseTypes != null)
                {
                    //check if we need to search in other scopes
                    int baseCount = _currentResolvingType.BaseTypes.Count;
                    //we get only 1 base count
                    if (baseCount > 0)
                    {
                        if ((foundSymbol = SearchFromFirstBase(_currentResolvingType.BaseTypes[0] as CodeTypeTemplateTypeReference, typename)) != null)
                        {
                            return(foundSymbol);
                        }
                    }
                }
            }



            //-------

            if (this._typeCollection.TryGetType(typename, out foundSymbol))
            {
                return(foundSymbol);
            }

            //this is convention
            if (typename.StartsWith("cef_") && IsAllLowerLetter(typename))
            {
                //assume this is base c/cpp type
                foundSymbol = new SimpleTypeSymbol(typename);
                this._typeCollection.RegisterType(
                    typename,
                    foundSymbol);
                return(foundSymbol);
            }

            //not found
            return(foundSymbol);
        }