| OLD | NEW |
| (Empty) |
| 1 #region Copyright notice and license | |
| 2 // Protocol Buffers - Google's data interchange format | |
| 3 // Copyright 2008 Google Inc. All rights reserved. | |
| 4 // https://developers.google.com/protocol-buffers/ | |
| 5 // | |
| 6 // Redistribution and use in source and binary forms, with or without | |
| 7 // modification, are permitted provided that the following conditions are | |
| 8 // met: | |
| 9 // | |
| 10 // * Redistributions of source code must retain the above copyright | |
| 11 // notice, this list of conditions and the following disclaimer. | |
| 12 // * Redistributions in binary form must reproduce the above | |
| 13 // copyright notice, this list of conditions and the following disclaimer | |
| 14 // in the documentation and/or other materials provided with the | |
| 15 // distribution. | |
| 16 // * Neither the name of Google Inc. nor the names of its | |
| 17 // contributors may be used to endorse or promote products derived from | |
| 18 // this software without specific prior written permission. | |
| 19 // | |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 31 #endregion | |
| 32 | |
| 33 using System; | |
| 34 using System.Collections.Generic; | |
| 35 using System.Text; | |
| 36 using System.Text.RegularExpressions; | |
| 37 | |
| 38 namespace Google.Protobuf.Reflection | |
| 39 { | |
| 40 /// <summary> | |
| 41 /// Contains lookup tables containing all the descriptors defined in a parti
cular file. | |
| 42 /// </summary> | |
| 43 internal sealed class DescriptorPool | |
| 44 { | |
| 45 private readonly IDictionary<string, IDescriptor> descriptorsByName = | |
| 46 new Dictionary<string, IDescriptor>(); | |
| 47 | |
| 48 private readonly IDictionary<DescriptorIntPair, FieldDescriptor> fieldsB
yNumber = | |
| 49 new Dictionary<DescriptorIntPair, FieldDescriptor>(); | |
| 50 | |
| 51 private readonly IDictionary<DescriptorIntPair, EnumValueDescriptor> enu
mValuesByNumber = | |
| 52 new Dictionary<DescriptorIntPair, EnumValueDescriptor>(); | |
| 53 | |
| 54 private readonly HashSet<FileDescriptor> dependencies; | |
| 55 | |
| 56 internal DescriptorPool(FileDescriptor[] dependencyFiles) | |
| 57 { | |
| 58 dependencies = new HashSet<FileDescriptor>(); | |
| 59 for (int i = 0; i < dependencyFiles.Length; i++) | |
| 60 { | |
| 61 dependencies.Add(dependencyFiles[i]); | |
| 62 ImportPublicDependencies(dependencyFiles[i]); | |
| 63 } | |
| 64 | |
| 65 foreach (FileDescriptor dependency in dependencyFiles) | |
| 66 { | |
| 67 AddPackage(dependency.Package, dependency); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 private void ImportPublicDependencies(FileDescriptor file) | |
| 72 { | |
| 73 foreach (FileDescriptor dependency in file.PublicDependencies) | |
| 74 { | |
| 75 if (dependencies.Add(dependency)) | |
| 76 { | |
| 77 ImportPublicDependencies(dependency); | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 /// <summary> | |
| 83 /// Finds a symbol of the given name within the pool. | |
| 84 /// </summary> | |
| 85 /// <typeparam name="T">The type of symbol to look for</typeparam> | |
| 86 /// <param name="fullName">Fully-qualified name to look up</param> | |
| 87 /// <returns>The symbol with the given name and type, | |
| 88 /// or null if the symbol doesn't exist or has the wrong type</returns> | |
| 89 internal T FindSymbol<T>(string fullName) where T : class | |
| 90 { | |
| 91 IDescriptor result; | |
| 92 descriptorsByName.TryGetValue(fullName, out result); | |
| 93 T descriptor = result as T; | |
| 94 if (descriptor != null) | |
| 95 { | |
| 96 return descriptor; | |
| 97 } | |
| 98 | |
| 99 foreach (FileDescriptor dependency in dependencies) | |
| 100 { | |
| 101 dependency.DescriptorPool.descriptorsByName.TryGetValue(fullName
, out result); | |
| 102 descriptor = result as T; | |
| 103 if (descriptor != null) | |
| 104 { | |
| 105 return descriptor; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 return null; | |
| 110 } | |
| 111 | |
| 112 /// <summary> | |
| 113 /// Adds a package to the symbol tables. If a package by the same name | |
| 114 /// already exists, that is fine, but if some other kind of symbol | |
| 115 /// exists under the same name, an exception is thrown. If the package | |
| 116 /// has multiple components, this also adds the parent package(s). | |
| 117 /// </summary> | |
| 118 internal void AddPackage(string fullName, FileDescriptor file) | |
| 119 { | |
| 120 int dotpos = fullName.LastIndexOf('.'); | |
| 121 String name; | |
| 122 if (dotpos != -1) | |
| 123 { | |
| 124 AddPackage(fullName.Substring(0, dotpos), file); | |
| 125 name = fullName.Substring(dotpos + 1); | |
| 126 } | |
| 127 else | |
| 128 { | |
| 129 name = fullName; | |
| 130 } | |
| 131 | |
| 132 IDescriptor old; | |
| 133 if (descriptorsByName.TryGetValue(fullName, out old)) | |
| 134 { | |
| 135 if (!(old is PackageDescriptor)) | |
| 136 { | |
| 137 throw new DescriptorValidationException(file, | |
| 138 "\"" + name + | |
| 139 "\" is already defin
ed (as something other than a " + | |
| 140 "package) in file \"
" + old.File.Name + "\"."); | |
| 141 } | |
| 142 } | |
| 143 descriptorsByName[fullName] = new PackageDescriptor(name, fullName,
file); | |
| 144 } | |
| 145 | |
| 146 /// <summary> | |
| 147 /// Adds a symbol to the symbol table. | |
| 148 /// </summary> | |
| 149 /// <exception cref="DescriptorValidationException">The symbol already e
xisted | |
| 150 /// in the symbol table.</exception> | |
| 151 internal void AddSymbol(IDescriptor descriptor) | |
| 152 { | |
| 153 ValidateSymbolName(descriptor); | |
| 154 String fullName = descriptor.FullName; | |
| 155 | |
| 156 IDescriptor old; | |
| 157 if (descriptorsByName.TryGetValue(fullName, out old)) | |
| 158 { | |
| 159 int dotPos = fullName.LastIndexOf('.'); | |
| 160 string message; | |
| 161 if (descriptor.File == old.File) | |
| 162 { | |
| 163 if (dotPos == -1) | |
| 164 { | |
| 165 message = "\"" + fullName + "\" is already defined."; | |
| 166 } | |
| 167 else | |
| 168 { | |
| 169 message = "\"" + fullName.Substring(dotPos + 1) + "\" is
already defined in \"" + | |
| 170 fullName.Substring(0, dotPos) + "\"."; | |
| 171 } | |
| 172 } | |
| 173 else | |
| 174 { | |
| 175 message = "\"" + fullName + "\" is already defined in file \
"" + old.File.Name + "\"."; | |
| 176 } | |
| 177 throw new DescriptorValidationException(descriptor, message); | |
| 178 } | |
| 179 descriptorsByName[fullName] = descriptor; | |
| 180 } | |
| 181 | |
| 182 private static readonly Regex ValidationRegex = new Regex("^[_A-Za-z][_A
-Za-z0-9]*$", | |
| 183 FrameworkPorta
bility.CompiledRegexWhereAvailable); | |
| 184 | |
| 185 /// <summary> | |
| 186 /// Verifies that the descriptor's name is valid (i.e. it contains | |
| 187 /// only letters, digits and underscores, and does not start with a digi
t). | |
| 188 /// </summary> | |
| 189 /// <param name="descriptor"></param> | |
| 190 private static void ValidateSymbolName(IDescriptor descriptor) | |
| 191 { | |
| 192 if (descriptor.Name == "") | |
| 193 { | |
| 194 throw new DescriptorValidationException(descriptor, "Missing nam
e."); | |
| 195 } | |
| 196 if (!ValidationRegex.IsMatch(descriptor.Name)) | |
| 197 { | |
| 198 throw new DescriptorValidationException(descriptor, | |
| 199 "\"" + descriptor.Name +
"\" is not a valid identifier."); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 /// <summary> | |
| 204 /// Returns the field with the given number in the given descriptor, | |
| 205 /// or null if it can't be found. | |
| 206 /// </summary> | |
| 207 internal FieldDescriptor FindFieldByNumber(MessageDescriptor messageDesc
riptor, int number) | |
| 208 { | |
| 209 FieldDescriptor ret; | |
| 210 fieldsByNumber.TryGetValue(new DescriptorIntPair(messageDescriptor,
number), out ret); | |
| 211 return ret; | |
| 212 } | |
| 213 | |
| 214 internal EnumValueDescriptor FindEnumValueByNumber(EnumDescriptor enumDe
scriptor, int number) | |
| 215 { | |
| 216 EnumValueDescriptor ret; | |
| 217 enumValuesByNumber.TryGetValue(new DescriptorIntPair(enumDescriptor,
number), out ret); | |
| 218 return ret; | |
| 219 } | |
| 220 | |
| 221 /// <summary> | |
| 222 /// Adds a field to the fieldsByNumber table. | |
| 223 /// </summary> | |
| 224 /// <exception cref="DescriptorValidationException">A field with the sam
e | |
| 225 /// containing type and number already exists.</exception> | |
| 226 internal void AddFieldByNumber(FieldDescriptor field) | |
| 227 { | |
| 228 DescriptorIntPair key = new DescriptorIntPair(field.ContainingType,
field.FieldNumber); | |
| 229 FieldDescriptor old; | |
| 230 if (fieldsByNumber.TryGetValue(key, out old)) | |
| 231 { | |
| 232 throw new DescriptorValidationException(field, "Field number " +
field.FieldNumber + | |
| 233 "has already been
used in \"" + | |
| 234 field.ContainingT
ype.FullName + | |
| 235 "\" by field \""
+ old.Name + "\"."); | |
| 236 } | |
| 237 fieldsByNumber[key] = field; | |
| 238 } | |
| 239 | |
| 240 /// <summary> | |
| 241 /// Adds an enum value to the enumValuesByNumber table. If an enum value | |
| 242 /// with the same type and number already exists, this method does nothi
ng. | |
| 243 /// (This is allowed; the first value defined with the number takes prec
edence.) | |
| 244 /// </summary> | |
| 245 internal void AddEnumValueByNumber(EnumValueDescriptor enumValue) | |
| 246 { | |
| 247 DescriptorIntPair key = new DescriptorIntPair(enumValue.EnumDescript
or, enumValue.Number); | |
| 248 if (!enumValuesByNumber.ContainsKey(key)) | |
| 249 { | |
| 250 enumValuesByNumber[key] = enumValue; | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 /// <summary> | |
| 255 /// Looks up a descriptor by name, relative to some other descriptor. | |
| 256 /// The name may be fully-qualified (with a leading '.'), partially-qual
ified, | |
| 257 /// or unqualified. C++-like name lookup semantics are used to search fo
r the | |
| 258 /// matching descriptor. | |
| 259 /// </summary> | |
| 260 /// <remarks> | |
| 261 /// This isn't heavily optimized, but it's only used during cross linkin
g anyway. | |
| 262 /// If it starts being used more widely, we should look at performance m
ore carefully. | |
| 263 /// </remarks> | |
| 264 internal IDescriptor LookupSymbol(string name, IDescriptor relativeTo) | |
| 265 { | |
| 266 IDescriptor result; | |
| 267 if (name.StartsWith(".")) | |
| 268 { | |
| 269 // Fully-qualified name. | |
| 270 result = FindSymbol<IDescriptor>(name.Substring(1)); | |
| 271 } | |
| 272 else | |
| 273 { | |
| 274 // If "name" is a compound identifier, we want to search for the | |
| 275 // first component of it, then search within it for the rest. | |
| 276 int firstPartLength = name.IndexOf('.'); | |
| 277 string firstPart = firstPartLength == -1 ? name : name.Substring
(0, firstPartLength); | |
| 278 | |
| 279 // We will search each parent scope of "relativeTo" looking for
the | |
| 280 // symbol. | |
| 281 StringBuilder scopeToTry = new StringBuilder(relativeTo.FullName
); | |
| 282 | |
| 283 while (true) | |
| 284 { | |
| 285 // Chop off the last component of the scope. | |
| 286 | |
| 287 int dotpos = scopeToTry.ToString().LastIndexOf("."); | |
| 288 if (dotpos == -1) | |
| 289 { | |
| 290 result = FindSymbol<IDescriptor>(name); | |
| 291 break; | |
| 292 } | |
| 293 else | |
| 294 { | |
| 295 scopeToTry.Length = dotpos + 1; | |
| 296 | |
| 297 // Append firstPart and try to find. | |
| 298 scopeToTry.Append(firstPart); | |
| 299 result = FindSymbol<IDescriptor>(scopeToTry.ToString()); | |
| 300 | |
| 301 if (result != null) | |
| 302 { | |
| 303 if (firstPartLength != -1) | |
| 304 { | |
| 305 // We only found the first part of the symbol.
Now look for | |
| 306 // the whole thing. If this fails, we *don't* w
ant to keep | |
| 307 // searching parent scopes. | |
| 308 scopeToTry.Length = dotpos + 1; | |
| 309 scopeToTry.Append(name); | |
| 310 result = FindSymbol<IDescriptor>(scopeToTry.ToSt
ring()); | |
| 311 } | |
| 312 break; | |
| 313 } | |
| 314 | |
| 315 // Not found. Remove the name so we can try again. | |
| 316 scopeToTry.Length = dotpos; | |
| 317 } | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 if (result == null) | |
| 322 { | |
| 323 throw new DescriptorValidationException(relativeTo, "\"" + name
+ "\" is not defined."); | |
| 324 } | |
| 325 else | |
| 326 { | |
| 327 return result; | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 /// <summary> | |
| 332 /// Struct used to hold the keys for the fieldByNumber table. | |
| 333 /// </summary> | |
| 334 private struct DescriptorIntPair : IEquatable<DescriptorIntPair> | |
| 335 { | |
| 336 private readonly int number; | |
| 337 private readonly IDescriptor descriptor; | |
| 338 | |
| 339 internal DescriptorIntPair(IDescriptor descriptor, int number) | |
| 340 { | |
| 341 this.number = number; | |
| 342 this.descriptor = descriptor; | |
| 343 } | |
| 344 | |
| 345 public bool Equals(DescriptorIntPair other) | |
| 346 { | |
| 347 return descriptor == other.descriptor | |
| 348 && number == other.number; | |
| 349 } | |
| 350 | |
| 351 public override bool Equals(object obj) | |
| 352 { | |
| 353 if (obj is DescriptorIntPair) | |
| 354 { | |
| 355 return Equals((DescriptorIntPair) obj); | |
| 356 } | |
| 357 return false; | |
| 358 } | |
| 359 | |
| 360 public override int GetHashCode() | |
| 361 { | |
| 362 return descriptor.GetHashCode()*((1 << 16) - 1) + number; | |
| 363 } | |
| 364 } | |
| 365 } | |
| 366 } | |
| OLD | NEW |