1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
|
//
// MetaDataProvider.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2011 Alexander Chebaturkin
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.AST.Visitors;
using Mono.CodeContracts.Static.ContractExtraction;
using Mono.CodeContracts.Static.DataStructures;
namespace Mono.CodeContracts.Static.Providers {
class MetaDataProvider : IMetaDataProvider {
public static readonly MetaDataProvider Instance = new MetaDataProvider ();
#region IMetaDataProvider Members
public Result AccessMethodBody<Data, Result> (Method method, IMethodCodeConsumer<Data, Result> consumer, Data data)
{
return consumer.Accept (CodeProviderImpl.Instance, CodeProviderImpl.Instance.Entry (method), method, data);
}
public bool IsReferenceType (TypeNode type)
{
return (!type.IsValueType);
}
public Method DeclaringMethod (Parameter parameter)
{
return parameter.DeclaringMethod;
}
public int ParameterIndex (Parameter parameter)
{
return parameter.Index;
}
public bool IsVoidMethod (Method method)
{
return IsVoid (method.ReturnType);
}
public bool TryLoadAssembly (string filename, out AssemblyNode assembly, out string reasonOfFailure)
{
assembly = AssemblyNode.ReadAssembly (filename);
if (!TryLoadContractNodes (ref assembly)) {
reasonOfFailure = "Couldn't find CodeContracts assembly in referencing assemblies";
return false;
}
reasonOfFailure = null;
return true;
}
public TypeNode ReturnType (Method method)
{
return method.ReturnType;
}
public IIndexable<Parameter> Parameters (Method method)
{
return new Indexable<Parameter> (method.Parameters);
}
public Parameter This (Method method)
{
return method.ThisParameter;
}
public string Name (Method method)
{
return method.Name;
}
public string Name (Field field)
{
return field.Name;
}
public string Name (TypeNode type)
{
return type.Name;
}
public TypeNode FieldType (Field field)
{
return field.FieldType;
}
public string FullName (Method method)
{
return method.FullName;
}
public string FullName (TypeNode type)
{
return type.FullName;
}
public TypeNode DeclaringType (Field field)
{
return field.DeclaringType;
}
public bool IsMain (Method method)
{
MethodDefinition entryPoint = method.Definition.Module.EntryPoint;
return entryPoint != null && entryPoint.Equals (method);
}
public bool IsStatic (Method method)
{
return method.IsStatic;
}
public bool IsStatic (Field field)
{
return field.IsStatic;
}
public bool IsPrivate (Method method)
{
return method.IsPrivate;
}
public bool IsProtected (Method method)
{
return method.IsProtected;
}
public bool IsPublic (Method method)
{
return method.IsPublic;
}
public bool IsVirtual (Method method)
{
return method.IsVirtual;
}
public bool IsNewSlot (Method method)
{
return method.IsNewSlot;
}
public bool IsOverride (Method method)
{
return !method.IsNewSlot && method.HasOverrides;
}
public bool IsFinal (Method method)
{
return method.IsFinal;
}
public bool IsConstructor (Method method)
{
return method.IsConstructor;
}
public bool IsAbstract (Method method)
{
return method.IsAbstract;
}
public bool IsPropertySetter (Method method, out Property property)
{
//todo: implement this
property = null;
return false;
}
public bool IsPropertyGetter (Method method, out Property property)
{
//todo: implement this
property = null;
return false;
}
public TypeNode DeclaringType (Method method)
{
return method.DeclaringType;
}
public bool HasBody (Method method)
{
return method.HasBody;
}
public bool DerivesFrom (TypeNode sub, TypeNode type)
{
return sub.IsAssignableTo (type);
}
public bool Equal (TypeNode type, TypeNode otherType)
{
return type == otherType;
}
public bool TryGetImplementingMethod (TypeNode type, Method calledMethod, out Method implementingMethod)
{
//todo: implement this
implementingMethod = null;
return false;
}
public Method Unspecialized (Method method)
{
if (method.HasGenericParameters)
throw new NotImplementedException ();
return method;
}
public IEnumerable<Method> OverridenAndImplementedMethods (Method method)
{
//todo: implement this
yield break;
}
public TypeNode ManagedPointer (TypeNode type)
{
return type.GetReferenceType ();
}
public bool TryGetRootMethod (Method method, out Method rootMethod)
{
//todo: implement this
rootMethod = method;
return true;
}
public IEnumerable<Method> ImplementedMethods (Method method)
{
yield break;
}
public bool IsAutoPropertyMember (Method method)
{
//todo: implement this
return false;
}
public bool IsFinalizer (Method method)
{
return "Finalize" == method.Name;
}
public bool IsDispose (Method method)
{
if (method.Name != "Dispose" && method.Name != "System.IDisposable.Dispose")
return false;
if (method.Parameters == null || method.Parameters.Count == 0)
return true;
if (method.Parameters.Count == 1)
return Equal(method.Parameters [0].Type, CoreSystemTypes.Instance.TypeBoolean);
return false;
}
#endregion
#region Implementation of IMetaDataProvider<Local,Parameter,Method,FieldReference,PropertyReference,EventReference,TypeNode,Attribute,AssemblyNode>
public TypeNode System_Single
{
get { return CoreSystemTypes.Instance.TypeSingle; }
}
public TypeNode System_Double
{
get { return CoreSystemTypes.Instance.TypeDouble; }
}
public TypeNode System_Type
{
get { return CoreSystemTypes.Instance.TypeSystemType; }
}
public TypeNode System_Char
{
get { return CoreSystemTypes.Instance.TypeChar; }
}
public TypeNode System_Int32
{
get { return CoreSystemTypes.Instance.TypeInt32; }
}
public TypeNode System_String
{
get { return CoreSystemTypes.Instance.TypeString; }
}
public TypeNode System_Boolean
{
get { return CoreSystemTypes.Instance.TypeBoolean; }
}
public TypeNode System_IntPtr
{
get { return CoreSystemTypes.Instance.TypeIntPtr; }
}
public TypeNode System_UIntPtr
{
get { return CoreSystemTypes.Instance.TypeUIntPtr; }
}
public TypeNode System_Void
{
get { return CoreSystemTypes.Instance.TypeVoid; }
}
public TypeNode System_Array
{
get { return CoreSystemTypes.Instance.TypeArray; }
}
public TypeNode System_Object
{
get { return CoreSystemTypes.Instance.TypeObject; }
}
public TypeNode System_Int8
{
get { return CoreSystemTypes.Instance.TypeSByte; }
}
public TypeNode System_Int64
{
get { return CoreSystemTypes.Instance.TypeInt64; }
}
public TypeNode System_Int16
{
get { return CoreSystemTypes.Instance.TypeInt16; }
}
public TypeNode System_UInt8
{
get { return CoreSystemTypes.Instance.TypeByte; }
}
public TypeNode System_UInt64
{
get { return CoreSystemTypes.Instance.TypeUInt64; }
}
public TypeNode System_UInt32
{
get { return CoreSystemTypes.Instance.TypeUInt32; }
}
public TypeNode System_UInt16
{
get { return CoreSystemTypes.Instance.TypeUInt16; }
}
public IEnumerable<Method> Methods (AssemblyNode assembly)
{
return assembly.Modules.SelectMany (a => a.Types).SelectMany (t => t.Methods);
}
public string Name (Parameter parameter)
{
return parameter.Name;
}
public TypeNode ParameterType (Parameter parameter)
{
return parameter.Type;
}
public TypeNode LocalType (Local local)
{
return local.Type;
}
public bool IsStruct (TypeNode type)
{
return type.IsValueType;
}
public Field Unspecialized (Field field)
{
return field;
}
public bool IsManagedPointer (TypeNode value)
{
return value is Reference;
}
public bool IsArray (TypeNode type)
{
return type.IsArray;
}
public IEnumerable<TypeNode> Interfaces (TypeNode type)
{
return type.Interfaces;
}
public bool TryGetSystemType (string fullName, out TypeNode type)
{
int len = fullName.LastIndexOf (".");
string ns = "";
string className = fullName;
if (len > 0) {
ns = fullName.Substring (0, len);
className = fullName.Substring (len + 1);
}
type = CoreSystemTypes.Instance.SystemAssembly.GetType (ns, className);
return type != null;
}
public bool IsInterface (TypeNode type)
{
return type.IsInterface;
}
public IEnumerable<Property> Properties (TypeNode type)
{
return type.Properties;
}
public bool IsStatic (Property property)
{
return property.IsStatic;
}
public bool IsAsVisibleAs (Field value, Method method)
{
return HelperMethods.IsReferenceAsVisibleAs (value, method);
}
public bool IsAsVisibleAs (Method value, Method method)
{
return HelperMethods.IsReferenceAsVisibleAs (value, method);
}
public bool IsVisibleFrom (Field field, TypeNode declaringType)
{
return field.IsVisibleFrom (declaringType);
}
public bool IsVisibleFrom (Method value, TypeNode declaringType)
{
return value.IsVisibleFrom (declaringType);
}
public bool Equal (Method thisMethod, Method thatMethod)
{
return thisMethod == thatMethod;
}
public IIndexable<Local> Locals (Method method)
{
return new Indexable<Local> (method.Locals);
}
public TypeNode ElementType (TypeNode type)
{
var reference = type as Reference;
if (reference != null)
return reference.ElementType;
//todo: array
throw new NotImplementedException ();
}
public TypeNode Unspecialized (TypeNode type)
{
return type;
}
public bool IsProtected (Field field)
{
return field.IsFamily || field.IsFamilyAndAssembly || field.IsFamilyOrAssembly;
}
public bool IsPublic (Field field)
{
return field.IsPublic;
}
public int ParameterStackIndex (Parameter parameter)
{
Method declaringMethod = parameter.DeclaringMethod;
return declaringMethod.Parameters.Count + (declaringMethod.IsStatic ? 0 : 1) - parameter.Index - 1;
}
public bool HasGetter (Property property, out Method method)
{
if (property.HasGetter) {
method = property.Getter;
return true;
}
method = null;
return false;
}
public bool HasSetter (Property property, out Method method)
{
if (property.HasSetter) {
method = property.Setter;
return true;
}
method = null;
return false;
}
public bool IsReadonly (Field value)
{
return value.IsReadonly;
}
public bool HasValueRepresentation (TypeNode type)
{
return !IsStruct (type) || IsPrimitive (type) || IsEnum (type);
}
public bool IsVoid (TypeNode type)
{
return type.Equals (System_Void);
}
public void IsSpecialized (Method calledMethod, ref IImmutableMap<TypeNode, TypeNode> specialization)
{
throw new NotImplementedException ();
}
public IEnumerable<Field> Fields (TypeNode type)
{
return type.Fields;
}
public bool IsOut (Parameter p)
{
return p.IsOut;
}
public bool IsPrimitive (TypeNode type)
{
return type.IsPrimitive;
}
public bool IsClass (TypeNode type)
{
return type.IsClass;
}
public bool IsVisibleFrom (TypeNode type, TypeNode fromType)
{
return type.IsVisibleFrom (fromType);
}
public bool IsCompilerGenerated (Method method)
{
return method.IsCompilerGenerated;
}
public bool IsSpecialized (Method method)
{
return false;
}
public bool IsFormalTypeParameter (TypeNode type)
{
return false;
}
public bool IsMethodFormalTypeParameter (TypeNode type)
{
return false;
}
public TypeNode ArrayType (TypeNode type, int rank)
{
return type.GetArrayType (rank);
}
public bool IsCompilerGenerated (Field field)
{
return field.IsCompilerGenerated;
}
public int TypeSize (TypeNode type)
{
int classSize = type.ClassSize;
if (classSize > 0)
return classSize;
int size = TypeSizeHelper (type);
type.ClassSize = size;
return size;
}
public string Name (Local local)
{
return local.Name;
}
private int TypeSizeHelper (TypeNode type)
{
if (IsManagedPointer (type))
return 4;
if (type == System_Boolean)
return 1;
if (type == System_Char)
return 2;
if (type == System_Int8)
return 1;
if (type == System_Int16)
return 2;
if (type == System_Int32)
return 4;
if (type == System_Int64)
return 8;
if (type == System_IntPtr || type == System_Object || type == System_String || type == System_Type)
return 4;
if (type == System_UInt8)
return 1;
if (type == System_UInt16)
return 2;
if (type == System_UInt32)
return 4;
if (type == System_UInt64)
return 8;
if (type == System_UIntPtr || type == System_Single)
return 4;
if (type == System_Double)
return 8;
return -1;
}
private bool IsEnum (TypeNode type)
{
return type.IsEnum;
}
#endregion
private bool TryLoadContractNodes (ref AssemblyNode assembly)
{
ContractNodes nodes = null;
foreach (Module module in assembly.Modules) {
IAssemblyResolver assemblyResolver = module.Definition.AssemblyResolver;
foreach (AssemblyNameReference reference in module.Definition.AssemblyReferences) {
AssemblyDefinition def = assemblyResolver.Resolve (reference);
nodes = ContractNodes.GetContractNodes (new AssemblyNode (def), (s) => { });
if (nodes != null)
break;
}
}
if (nodes == null)
return false;
var extractor = new ContractExtractor (nodes, assembly, true);
assembly = (AssemblyNode) extractor.Visit (assembly);
return true;
}
}
}
|