예제 #1
0
        public IEnumerable<GrapeMethod> GetMethodsWithNameFromImportedPackagesInFile(GrapeCodeGeneratorConfiguration config, string methodName, string fileName, GrapeClass c)
        {
            PopulatePackageFileNames(config.Ast);
            List<string> importedPackageFiles = new List<string>();
            IEnumerable<GrapeEntity> importDeclarationsInFile = GetEntitiesOfTypeInFile(config.Ast, fileName, typeof(GrapeImportDeclaration));
            foreach (GrapeImportDeclaration importDeclaration in importDeclarationsInFile) {
                foreach (KeyValuePair<string, List<string>> pair in packageFileNames) {
                    if (pair.Key == importDeclaration.PackageName) {
                        foreach (string packageFileName in pair.Value) {
                            importedPackageFiles.Add(packageFileName);
                        }
                    }
                }
            }

            IList<string> segmentsInVariableName = GetSegmentsInQualifiedId(methodName, true);
            string actualFunctionName = segmentsInVariableName[segmentsInVariableName.Count - 1];
            string actualQualifiedId = "";
            for (int i = 0; i < segmentsInVariableName.Count - 1; i++) {
                actualQualifiedId += segmentsInVariableName[i] + ".";
            }

            actualQualifiedId = actualQualifiedId.Trim('.');
            if (actualQualifiedId == "this") {
                List<GrapeMethod> l = new List<GrapeMethod>();
                foreach (GrapeMethod m in GetEntitiesOfTypeInFile(config.Ast, fileName, typeof(GrapeMethod))) {
                    if (m.Name == actualFunctionName && m.GetLogicalParentOfEntityType<GrapeClass>() == c) {
                        l.Add(m);
                    }
                }

                return l;
            } else if (actualQualifiedId == "base") {
                List<GrapeMethod> l = new List<GrapeMethod>();
                foreach (GrapeMethod m in GetEntitiesOfTypeInFile(config.Ast, fileName, typeof(GrapeMethod))) {
                    if (m.Name == actualFunctionName && m.GetLogicalParentOfEntityType<GrapeClass>() == GetClassWithNameFromImportedPackagesInFile(config.Ast, typeCheckingUtils.GetTypeNameForTypeAccessExpression(config, c.Inherits), fileName)) {
                        l.Add(m);
                    }
                }

                return l;
            }

            IEnumerable<GrapeEntity> packagesInFile = GetEntitiesOfTypeInFile(config.Ast, fileName, typeof(GrapePackageDeclaration));
            foreach (GrapePackageDeclaration packageDeclaration in packagesInFile) {
                foreach (KeyValuePair<string, List<string>> pair in packageFileNames) {
                    if (pair.Key == packageDeclaration.PackageName) {
                        IEnumerable<string> otherPackagesInPackage = GetOtherPackagesInPackageName(packageDeclaration.PackageName);
                        foreach (string otherPackage in otherPackagesInPackage) {
                            foreach (KeyValuePair<string, List<string>> childPair in packageFileNames) {
                                if (childPair.Key == otherPackage) {
                                    importedPackageFiles.AddRange(childPair.Value);
                                }
                            }
                        }

                        importedPackageFiles.AddRange(pair.Value);
                    }
                }
            }

            List<GrapeMethod> list = new List<GrapeMethod>();
            foreach (string importedPackageFile in importedPackageFiles) {
                IEnumerable<GrapeEntity> methods = GetEntitiesOfTypeInFile(config.Ast, importedPackageFile, typeof(GrapeMethod));
                foreach (GrapeMethod m in methods) {
                    if (m.Name == actualFunctionName && (m.GetLogicalParentOfEntityType<GrapeClass>() == c || c.IsClassInInheritanceTree(config, m.GetLogicalParentOfEntityType<GrapeClass>()))) {
                        list.Add(m);
                    }
                }
            }

            IEnumerable<GrapeEntity> packages = GetEntitiesOfType(config.Ast, typeof(GrapePackageDeclaration));
            IEnumerable<GrapeEntity> allMethods = GetEntitiesOfType(config.Ast, typeof(GrapeMethod));
            foreach (GrapePackageDeclaration packageDeclaration in packages) {
                if (packageDeclaration.PackageName == actualQualifiedId) {
                    foreach (GrapeMethod m in allMethods) {
                        if (m.Name == actualFunctionName && (m.GetLogicalParentOfEntityType<GrapeClass>() == c || c.IsClassInInheritanceTree(config, m.GetLogicalParentOfEntityType<GrapeClass>()))) {
                            list.Add(m);
                        }
                    }
                }
            }

            if (list.Count > 1) {
                GrapeClass mostTopLevelClassInInheritanceTree = c;
                foreach (GrapeMethod method in list) {
                    GrapeClass functionClass = method.GetLogicalParentOfEntityType<GrapeClass>();
                    if (!mostTopLevelClassInInheritanceTree.IsClassInInheritanceTree(config, functionClass)) {
                        mostTopLevelClassInInheritanceTree = functionClass;
                    }
                }

                List<GrapeMethod> methodsToRemove = new List<GrapeMethod>();
                foreach (GrapeMethod method in list) {
                    if (method.GetLogicalParentOfEntityType<GrapeClass>() != mostTopLevelClassInInheritanceTree) {
                        methodsToRemove.Add(method);
                    }
                }

                methodsToRemove.ForEach(f => list.Remove(f));
            }

            return list;
        }
예제 #2
0
        private bool ValidateModifiers(GrapeClass c, out string errorMessage)
        {
            errorMessage = "";
            bool isStatic = c.Modifiers.Contains(GrapeModifier.GrapeModifierType.Static);
            bool isAbstract = c.Modifiers.Contains(GrapeModifier.GrapeModifierType.Abstract);
            bool isSealed = c.Modifiers.Contains(GrapeModifier.GrapeModifierType.Sealed);
            bool isOverride = c.Modifiers.Contains(GrapeModifier.GrapeModifierType.Override);
            if (c.Modifiers.HasInvalidAccessModifiers()) {
                errorMessage = "Invalid access modifiers found.";
                return false;
            }

            if (isStatic && isAbstract) {
                errorMessage = "A class cannot be declared static and abstract at the same time.";
                return false;
            }

            if (isSealed) {
                errorMessage = "A class cannot be declared sealed.";
                return false;
            }

            if (isOverride) {
                errorMessage = "A class cannot be declared override.";
                return false;
            }

            return true;
        }
        public bool IsTypeInClassInheritanceTree(GrapeCodeGeneratorConfiguration config, string typeName, GrapeClass c)
        {
            typeName = GetCorrectNativeTypeName(typeName);
            if (typeName == c.Name) {
                return true;
            }

            if (c == null) {
                return false;
            }

            if (c.Inherits != null) {
                string inheritsTypeName = c.Inherits.ToString();
                if (inheritsTypeName == typeName) {
                    return true;
                }

                return IsTypeInClassInheritanceTree(config, typeName, astUtils.GetClassWithNameFromImportedPackagesInFile(config.Ast, inheritsTypeName, c.FileName));
            }

            return false;
        }