From f965caa221ef6da0e2ab2263f11fd79c97f06842 Mon Sep 17 00:00:00 2001 From: triton Date: Wed, 30 Jan 2013 15:41:45 +0000 Subject: [PATCH] Added a pass to check for duplicated declaration names. --- .../Passes/DuplicatedNamesCheckerPass.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Generator/Passes/DuplicatedNamesCheckerPass.cs diff --git a/src/Generator/Passes/DuplicatedNamesCheckerPass.cs b/src/Generator/Passes/DuplicatedNamesCheckerPass.cs new file mode 100644 index 00000000..0308d8eb --- /dev/null +++ b/src/Generator/Passes/DuplicatedNamesCheckerPass.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; + +namespace Cxxi.Passes +{ + public class DuplicatedNamesCheckerPass : TranslationUnitPass + { + private readonly IDictionary names; + + public DuplicatedNamesCheckerPass() + { + names = new Dictionary(); + } + + public override bool ProcessClass(Class @class) + { + if (@class.Ignore) return false; + + names.Clear(); + + foreach (var baseClass in @class.Bases) + ProcessClass(baseClass.Class); + + CheckDuplicates(@class.Fields); + CheckDuplicates(@class.Methods); + CheckDuplicates(@class.Properties); + + return true; + } + + void CheckDuplicates(IEnumerable decls) + { + foreach (var decl in decls) + { + if (decl.Ignore) continue; + CheckDuplicate(decl); + } + } + + void CheckDuplicate(Declaration decl) + { + Declaration duplicate; + + // If the name is not yet on the map, then add it. + if (!names.TryGetValue(decl.Name, out duplicate)) + { + names[decl.Name] = decl; + return; + } + + // Else we found a duplicate name and need to change it. + Console.WriteLine("Found a duplicate named declaration: {0}", + decl.Name); + } + } + + public static class CheckDuplicateNamesExtensions + { + public static void CheckDuplicateNames(this PassBuilder builder) + { + var pass = new DuplicatedNamesCheckerPass(); + builder.AddPass(pass); + } + } +}