本文整理汇总了C#中TypeNodeList.Add方法的典型用法代码示例。如果您正苦于以下问题:C# TypeNodeList.Add方法的具体用法?C# TypeNodeList.Add怎么用?C# TypeNodeList.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeNodeList
的用法示例。
在下文中一共展示了TypeNodeList.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddExtensionMethod
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="type">the current type being extended</param>
/// <param name="extensionMethodTemplate">A reference to the extension method. For generic methods, this is a reference to the
/// non-specialized method, e.g. System.Linq.Enumerable.Select``2.
/// </param>
/// <param name="specialization">When the current type implements or inherits from a specialization of a generic type,
/// this parameter has a TypeNode for the type used as apecialization of the generic type's first template param.
/// </param>
private void AddExtensionMethod(XmlWriter writer, TypeNode type, Method extensionMethodTemplate, TypeNode specialization)
{
// If this is a specialization of a generic method, construct a Method object that describes the specialization
Method extensionMethodTemplate2 = extensionMethodTemplate;
if (extensionMethodTemplate2.IsGeneric && (specialization != null))
{
// the specialization type is the first of the method's template arguments
TypeNodeList templateArgs = new TypeNodeList();
templateArgs.Add(specialization);
// add any additional template arguments
for (int i = 1; i < extensionMethodTemplate.TemplateParameters.Count; i++)
templateArgs.Add(extensionMethodTemplate.TemplateParameters[i]);
extensionMethodTemplate2 = extensionMethodTemplate.GetTemplateInstance(type, templateArgs);
}
TypeNode extensionMethodTemplateReturnType = extensionMethodTemplate2.ReturnType;
ParameterList extensionMethodTemplateParameters = extensionMethodTemplate2.Parameters;
ParameterList extensionMethodParameters = new ParameterList();
for (int i = 1; i < extensionMethodTemplateParameters.Count; i++)
{
Parameter extensionMethodParameter = extensionMethodTemplateParameters[i];
extensionMethodParameters.Add(extensionMethodParameter);
}
Method extensionMethod = new Method(extensionMethodTemplate.DeclaringType, new AttributeList(), extensionMethodTemplate.Name, extensionMethodParameters, extensionMethodTemplate.ReturnType, null);
extensionMethod.Flags = extensionMethodTemplate.Flags & ~MethodFlags.Static;
// for generic methods, set the template args and params so the template data is included in the id and the method data
if (extensionMethodTemplate2.IsGeneric)
{
extensionMethod.IsGeneric = true;
if (specialization != null)
{
// set the template args for the specialized generic method
extensionMethod.TemplateArguments = extensionMethodTemplate2.TemplateArguments;
}
else
{
// set the generic template params for the non-specialized generic method
extensionMethod.TemplateParameters = extensionMethodTemplate2.TemplateParameters;
}
}
// Get the id
string extensionMethodTemplateId = reflector.ApiNamer.GetMemberName(extensionMethodTemplate);
// write the element node
writer.WriteStartElement("element");
writer.WriteAttributeString("api", extensionMethodTemplateId);
writer.WriteAttributeString("source", "extension");
isExtensionMethod = true;
reflector.WriteMember(extensionMethod);
isExtensionMethod = false;
writer.WriteEndElement();
}
示例2: GetTypeList
internal static TypeNodeList GetTypeList(string typeList, IDebugContext context){
TypeNodeList list = new TypeNodeList();
int startIndex = typeList.LastIndexOf(".")+1;
int endIndex;
IDebugType typ;
while((endIndex = typeList.IndexOf("Or", startIndex)) > 0){
typ = context.GetType(typeList.Substring(startIndex, endIndex - startIndex));
if (typ != null) list.Add(typ.CompilerType);
startIndex = endIndex+2;
}
typ = context.GetType(typeList.Substring(startIndex));
if (typ != null) list.Add(typ.CompilerType);
return list;
}
示例3: VisitTypeNodeList
public virtual Differences VisitTypeNodeList(TypeNodeList list1, TypeNodeList list2,
out TypeNodeList changes, out TypeNodeList deletions, out TypeNodeList insertions){
changes = list1 == null ? null : list1.Clone();
deletions = list1 == null ? null : list1.Clone();
insertions = list1 == null ? new TypeNodeList() : list1.Clone();
//^ assert insertions != null;
Differences differences = new Differences();
//Compare definitions that have matching key attributes
TrivialHashtable matchingPosFor = new TrivialHashtable();
TrivialHashtable matchedNodes = new TrivialHashtable();
for (int j = 0, n = list2 == null ? 0 : list2.Count; j < n; j++){
//^ assert list2 != null;
TypeNode nd2 = list2[j];
if (nd2 == null || nd2.Name == null) continue;
string fullName = nd2.FullName;
if (fullName == null) continue;
matchingPosFor[Identifier.For(fullName).UniqueIdKey] = j;
insertions.Add(null);
}
for (int i = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){
//^ assert list1 != null && changes != null && deletions != null;
TypeNode nd1 = list1[i];
if (nd1 == null || nd1.Name == null) continue;
string fullName = nd1.FullName;
if (fullName == null) continue;
object pos = matchingPosFor[Identifier.For(fullName).UniqueIdKey];
if (!(pos is int)) continue;
//^ assert pos != null;
//^ assume list2 != null; //since there was entry int matchingPosFor
int j = (int)pos;
TypeNode nd2 = list2[j];
//^ assume nd2 != null;
//nd1 and nd2 have the same key attributes and are therefore treated as the same entity
matchedNodes[nd1.UniqueKey] = nd1;
matchedNodes[nd2.UniqueKey] = nd2;
//nd1 and nd2 may still be different, though, so find out how different
Differences diff = this.VisitTypeNode(nd1, nd2);
if (diff == null){Debug.Assert(false); continue;}
if (diff.NumberOfDifferences != 0){
changes[i] = diff.Changes as TypeNode;
deletions[i] = diff.Deletions as TypeNode;
insertions[i] = diff.Insertions as TypeNode;
insertions[n+j] = nd1; //Records the position of nd2 in list2 in case the change involved a permutation
//Debug.Assert(diff.Changes == changes[i] && diff.Deletions == deletions[i] && diff.Insertions == insertions[i]);
differences.NumberOfDifferences += diff.NumberOfDifferences;
differences.NumberOfSimilarities += diff.NumberOfSimilarities;
if (nd1.DeclaringModule == this.OriginalModule ||
(nd1.DeclaringType != null && nd1.DeclaringType.DeclaringModule == this.OriginalModule)){
if (this.MembersThatHaveChanged == null) this.MembersThatHaveChanged = new MemberList();
this.MembersThatHaveChanged.Add(nd1);
}
continue;
}
changes[i] = null;
deletions[i] = null;
insertions[i] = null;
insertions[n+j] = nd1; //Records the position of nd2 in list2 in case the change involved a permutation
}
//Find deletions
for (int i = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){
//^ assert list1 != null && changes != null && deletions != null;
TypeNode nd1 = list1[i];
if (nd1 == null) continue;
if (matchedNodes[nd1.UniqueKey] != null) continue;
changes[i] = null;
deletions[i] = nd1;
insertions[i] = null;
differences.NumberOfDifferences += 1;
if (nd1.DeclaringModule == this.OriginalModule ||
(nd1.DeclaringType != null && nd1.DeclaringType.DeclaringModule == this.OriginalModule)){
if (this.MembersThatHaveChanged == null) this.MembersThatHaveChanged = new MemberList();
this.MembersThatHaveChanged.Add(nd1);
}
}
//Find insertions
for (int j = 0, n = list1 == null ? 0 : list1.Count, m = list2 == null ? 0 : list2.Count; j < m; j++){
//^ assert list2 != null;
TypeNode nd2 = list2[j];
if (nd2 == null) continue;
if (matchedNodes[nd2.UniqueKey] != null) continue;
insertions[n+j] = nd2; //Records nd2 as an insertion into list1, along with its position in list2
differences.NumberOfDifferences += 1; //REVIEW: put the size of the tree here?
}
if (differences.NumberOfDifferences == 0){
changes = null;
deletions = null;
insertions = null;
}
return differences;
}
示例4: FindNonStandardTypeParametersToBeDuplicated
/// <summary>
/// If source type is insided target type, only grab the type parameters up to the target type. Otherwise, grab consolidated.
/// </summary>
private static TypeNodeList FindNonStandardTypeParametersToBeDuplicated(FindClosurePartsToDuplicate fmd, TypeNode sourceType, TypeNode targetType)
{
Debug.Assert(TypeNode.IsCompleteTemplate(sourceType));
Debug.Assert(TypeNode.IsCompleteTemplate(targetType));
TypeNodeList result = null;
if (sourceType.DeclaringType != null)
{
if (fmd.MembersToDuplicate.Contains(sourceType.DeclaringType))
{
Debug.Assert(false);
return null;
}
if (sourceType.DeclaringType == targetType) return null;
if (IsInsideOf(sourceType, targetType))
{
// difficult case. Grab consolidated type parameters, except the ones up from the target type
var sourceConsolidated = sourceType.ConsolidatedTemplateParameters;
if (sourceConsolidated == null || sourceConsolidated.Count == 0) return null;
var targetConsolidated = targetType.ConsolidatedTemplateParameters;
if (targetConsolidated == null || targetConsolidated.Count == 0) return sourceConsolidated;
if (sourceConsolidated.Count == targetConsolidated.Count) return null; // no extra type parameters
result = new TypeNodeList(sourceConsolidated.Count - targetConsolidated.Count);
for (int i = 0; i < sourceConsolidated.Count; i++)
{
if (i < targetConsolidated.Count) continue;
result.Add(sourceConsolidated[i]);
return result;
}
}
else
{
// For Roslyn-based closures we need to combine all the types from source and target types together.
if (sourceType.IsRoslynBasedStaticClosure())
{
var sourceConsolidated = sourceType.ConsolidatedTemplateParameters;
if (sourceConsolidated == null || sourceConsolidated.Count == 0) return null;
var targetConsolidated = targetType.ConsolidatedTemplateParameters;
if (targetConsolidated == null || targetConsolidated.Count == 0) return sourceConsolidated;
if (sourceConsolidated.Count == targetConsolidated.Count)
return null; // no extra type parameters
result = new TypeNodeList(sourceConsolidated.Count + targetConsolidated.Count);
for (int i = 0; i < targetConsolidated.Count; i++)
{
result.Add(targetConsolidated[i]);
}
for (int i = 0; i < sourceConsolidated.Count; i++)
{
result.Add(sourceConsolidated[i]);
}
return result;
}
result = sourceType.ConsolidatedTemplateParameters;
}
}
return result;
}
示例5: BuildMappingFromClosureToOriginal
private static MapClosureExpressionToOriginalExpression BuildMappingFromClosureToOriginal(TypeNode ClosureClass,
Method MoveNextMethod, Method OriginalMethod)
{
Contract.Ensures(Contract.Result<MapClosureExpressionToOriginalExpression>() != null);
Dictionary<string, Parameter> closureFieldsMapping = GetClosureFieldsMapping(ClosureClass, OriginalMethod);
TypeNodeList TPListSource = ClosureClass.ConsolidatedTemplateParameters;
if (TPListSource == null) TPListSource = new TypeNodeList();
TypeNodeList TPListTarget = new TypeNodeList();
if (OriginalMethod.DeclaringType != null &&
OriginalMethod.DeclaringType.ConsolidatedTemplateParameters != null)
{
foreach (TypeNode tn in OriginalMethod.DeclaringType.ConsolidatedTemplateParameters)
TPListTarget.Add(tn);
}
if (OriginalMethod.TemplateParameters != null)
{
foreach (TypeNode tn in OriginalMethod.TemplateParameters)
{
TPListTarget.Add(tn);
}
}
Debug.Assert((TPListSource == null && TPListTarget == null) || TPListSource.Count == TPListTarget.Count);
return new MapClosureExpressionToOriginalExpression(
ClosureClass, closureFieldsMapping, TPListSource,
TPListTarget, OriginalMethod);
}
示例6: VisitMethod
//.........这里部分代码省略.........
this.HandleError(method, Error.RequiresNotAllowedInInterfaceImplementation, this.GetMethodSignature(ifaceMethod));
break;
}
cumulativeContract.CopyFrom(ifaceMethod.Contract);
somethingWasCopied = true;
ifaceContractWasCopied = true;
}
}
for (int i = 0, n = method.ImplicitlyImplementedInterfaceMethods == null ? 0 : method.ImplicitlyImplementedInterfaceMethods.Count; i < n; i++) {
Method ifaceMethod = method.ImplicitlyImplementedInterfaceMethods[i];
if (ifaceMethod == null) continue;
if (ifaceMethod.Contract != null) {
if (ifaceContractWasCopied) {
this.HandleError(method, Error.RequiresNotAllowedInInterfaceImplementation, this.GetMethodSignature(ifaceMethod));
break;
}
cumulativeContract.CopyFrom(ifaceMethod.Contract);
somethingWasCopied = true;
ifaceContractWasCopied = true;
}
}
if (method.Contract == null && somethingWasCopied) { // otherwise it was already copied into the method's contract
method.Contract = cumulativeContract;
}
}
#endregion
// For checked exceptions, the actual exceptions thrown must be a subset of the allowed exceptions
TypeNodeList aes = new TypeNodeList();
if (method.Contract != null && method.Contract.Ensures != null) {
for (int i = 0, n = method.Contract.Ensures.Count; i < n; i++) {
EnsuresExceptional ee = method.Contract.Ensures[i] as EnsuresExceptional;
if (ee == null || ee.Inherited) continue;
aes.Add(ee.Type);
}
}
TypeNodeList saveAllowedExceptions = this.allowedExceptions;
this.allowedExceptions = aes;
// don't check method body of proxy methods.
Method result = (method is ProxyMethod) ? method : base.VisitMethod(method);
this.allowedExceptions = saveAllowedExceptions;
if (this.yieldNode != null && TypeNode.StripModifiers(method.ReturnType) is Interface) {
StatementList statements = new StatementList(1);
TypeNode elementType = SystemTypes.Object;
Interface stype = (Interface)TypeNode.StripModifiers(method.ReturnType);
if (stype.TemplateArguments != null && stype.TemplateArguments.Count == 1) elementType = stype.TemplateArguments[0];
Class state = scope.ClosureClass;
elementType = scope.FixTypeReference(elementType);
state.Flags |= TypeFlags.Abstract; //So that no complaints are given about missing methods added by Normalizer
state.Interfaces = new InterfaceList(5);
state.Interfaces.Add(SystemTypes.IEnumerable);
state.Interfaces.Add((Interface)SystemTypes.GenericIEnumerator.GetTemplateInstance(this.currentType, elementType));
state.Interfaces.Add(SystemTypes.IEnumerator);
state.Interfaces.Add(SystemTypes.IDisposable);
state.Interfaces.Add((Interface)SystemTypes.GenericIEnumerable.GetTemplateInstance(this.currentType, elementType));
//Add these methods so that Normalizer can find them even when reference to iterator is forward
Method moveNext = new Method(state, null, StandardIds.MoveNext, null, SystemTypes.Boolean, null);
moveNext.CallingConvention = CallingConventionFlags.HasThis;
moveNext.Flags = MethodFlags.Public|MethodFlags.Virtual;
moveNext.Body = new Block(new StatementList());
state.Members.Add(moveNext);
Method getCurrent = new Method(state, null, StandardIds.getCurrent, null, elementType, null);
getCurrent.CallingConvention = CallingConventionFlags.HasThis;
getCurrent.Flags = MethodFlags.Public|MethodFlags.Virtual|MethodFlags.SpecialName;
示例7: ResolveOverload
public virtual Member ResolveOverload(MemberList members, ExpressionList arguments, bool doNotDiscardOverloadsWithMoreParameters){
if (members == null || members.Count == 0) return null;
TypeNode declaringType = members[0].DeclaringType;
Identifier id = members[0].Name;
int n = arguments == null ? 0 : arguments.Count;
TypeNode[] argTypes = new TypeNode[n];
for (int i = 0; i < n; i++){
Expression arg = arguments[i];
if (arg == null){
argTypes[i] = SystemTypes.Object;
}else{
Literal lit = arg as Literal;
if (lit != null && lit.Type != null && lit.Type.IsPrimitiveInteger && !lit.TypeWasExplicitlySpecifiedInSource)
argTypes[i] = this.typeSystem.SmallestIntegerType(lit.Value);
else
argTypes[i] = TypeNode.StripModifiers(arg.Type);
}
}
TypeNodeList bestParamTypes = new TypeNodeList(n);
for (int i = 0; i < n; i++) bestParamTypes.Add(TypeSystem.DoesNotMatchAnyType);
TypeNode bestElementType = null;
Member bestMember = this.GetBestMatch(members, arguments, argTypes, bestParamTypes, null, ref bestElementType, doNotDiscardOverloadsWithMoreParameters);
if (bestMember == Resolver.MethodNotFound) return null;
if (!(members[0] is InstanceInitializer || members[0] is StaticInitializer)){
while (declaringType != SystemTypes.Object && (declaringType = declaringType.BaseType) != null){
MemberList baseMembers = this.GetTypeView(declaringType).GetMembersNamed(id);
if (baseMembers.Count == 0) continue;
bestMember = this.GetBestMatch(baseMembers, arguments, argTypes, bestParamTypes, bestMember, ref bestElementType);
}
}
if (bestMember == null){
//Search again, but consider inaccessible members. Otherwise the error message does not distinguish between no member no accessible member.
this.considerInaccessibleMethods = true;
declaringType = members[0].DeclaringType;
for (int i = 0; i < n; i++) bestParamTypes[i] = TypeSystem.DoesNotMatchAnyType;
bestElementType = null;
bestMember = this.GetBestMatch(members, arguments, argTypes, bestParamTypes, null, ref bestElementType);
if (bestMember == Resolver.MethodNotFound) return null;
if (!(members[0] is InstanceInitializer || members[0] is StaticInitializer)){
while ((declaringType = declaringType.BaseType) != null){
MemberList baseMembers = this.GetTypeView(declaringType).GetMembersNamed(id);
if (baseMembers.Count == 0) continue;
Member bestMember2 = this.GetBestMatch(baseMembers, arguments, argTypes, bestParamTypes, bestMember, ref bestElementType);
if (bestMember2 != null) bestMember = bestMember2;
}
}
this.considerInaccessibleMethods = false;
}
return bestMember;
}
示例8: InferMethodTemplateArgumentsAndReturnTemplateInstance
public virtual Method InferMethodTemplateArgumentsAndReturnTemplateInstance(Method method, ParameterList delegateParameters){
if (method == null || method.Parameters == null || method.Parameters.Count == 0 ||
method.TemplateParameters == null || method.TemplateParameters.Count == 0){Debug.Assert(false); return method;}
if (delegateParameters == null) return method;
int numParams = delegateParameters.Count;
if (numParams == 0 || numParams != method.Parameters.Count){Debug.Assert(false); return method;}
TrivialHashtable inferredTypeFor = new TrivialHashtable();
for (int i = 0; i < numParams; i++){
Parameter dpar = delegateParameters[i]; if (dpar == null || dpar.Type == null) continue;
Parameter mpar = method.Parameters[i]; if (mpar == null) continue;
if (!this.InferMethodTemplateArguments(dpar.Type, null, mpar.Type, inferredTypeFor)) return method;
}
int numTypeArgs = method.TemplateParameters.Count;
TypeNodeList typeArguments = new TypeNodeList(numTypeArgs);
for (int i = 0; i < numTypeArgs; i++){
TypeNode templPar = method.TemplateParameters[i];
if (templPar == null) return method;
TypeNode templArg = inferredTypeFor[templPar.UniqueKey] as TypeNode;
if (templArg == null) return method;
typeArguments.Add(templArg);
}
return method.GetTemplateInstance(this.currentType, typeArguments);
}
示例9: VisitConstruct
/// <summary>
/// If there is an anonymous delegate within a postcondition, then there
/// will be a call to a delegate constructor.
/// That call looks like "d..ctor(o,m)" where d is the type of the delegate.
/// There are two cases depending on whether the anonymous delegate captured
/// anything. In both cases, m is the method implementing the anonymous delegate.
/// (1) It does capture something. Then o is the instance of the closure class
/// implementing the delegate, and m is an instance method in the closure
/// class.
/// (2) It does *not* capture anything. Then o is the literal for null and
/// m is a static method that was added directly to the class.
///
/// This method will cause the method (i.e., m) to be visited to collect any
/// Result<T>() expressions that occur in it.
/// </summary>
/// <param name="cons">The AST representing the call to the constructor
/// of the delegate type.</param>
/// <returns>Whatever the base visitor returns</returns>
public override Expression VisitConstruct(Construct cons) {
if (cons.Type is DelegateNode) {
UnaryExpression ue = cons.Operands[1] as UnaryExpression;
if (ue == null) goto JustVisit;
MemberBinding mb = ue.Operand as MemberBinding;
if (mb == null) goto JustVisit;
Method m = mb.BoundMember as Method;
if (!HelperMethods.IsCompilerGenerated(m)) goto JustVisit;
Contract.Assume(m != null);
m = Definition(m);
this.delegateNestingLevel++;
TypeNode savedClosureClass = this.currentClosureClassInstance;
Method savedClosureMethod = this.currentClosureMethod;
Expression savedCurrentAccessToTopLevelClosure = this.currentAccessToTopLevelClosure;
try
{
this.currentClosureMethod = m;
if (m.IsStatic)
{
this.currentClosureClassInstance = null; // no closure object
}
else
{
this.currentClosureClassInstance = cons.Operands[0].Type;
if (savedClosureClass == null)
{
// Then this is the top-level closure class.
this.topLevelClosureClassInstance = this.currentClosureClassInstance;
this.topLevelClosureClassDefinition = Definition(this.topLevelClosureClassInstance);
this.currentAccessToTopLevelClosure = new This(this.topLevelClosureClassDefinition);
this.properlyInstantiatedFieldType = this.originalLocalForResult.Type;
if (this.topLevelMethodFormals != null)
{
Contract.Assume(this.topLevelClosureClassDefinition.IsGeneric);
Contract.Assume(topLevelClosureClassDefinition.TemplateParameters.Count >= this.topLevelMethodFormals.Count);
// replace method type parameters in result properly with last n corresponding type parameters of closure class
TypeNodeList closureFormals = topLevelClosureClassDefinition.TemplateParameters;
if (closureFormals.Count > this.topLevelMethodFormals.Count)
{
int offset = closureFormals.Count - this.topLevelMethodFormals.Count;
closureFormals = new TypeNodeList(this.topLevelMethodFormals.Count);
for (int i = 0; i < this.topLevelMethodFormals.Count; i++)
{
closureFormals.Add(topLevelClosureClassDefinition.TemplateParameters[i + offset]);
}
}
Duplicator dup = new Duplicator(this.declaringType.DeclaringModule, this.declaringType);
Specializer spec = new Specializer(this.declaringType.DeclaringModule, topLevelMethodFormals, closureFormals);
var type = dup.VisitTypeReference(this.originalLocalForResult.Type);
type = spec.VisitTypeReference(type);
this.properlyInstantiatedFieldType = type;
}
}
else
{
while (currentClosureClassInstance.Template != null) currentClosureClassInstance = currentClosureClassInstance.Template;
// Find the field in this.closureClass that the C# compiler generated
// to point to the top-level closure
foreach (Member mem in this.currentClosureClassInstance.Members)
{
Field f = mem as Field;
if (f == null) continue;
if (f.Type == this.topLevelClosureClassDefinition)
{
var consolidatedTemplateParams = this.currentClosureClassInstance.ConsolidatedTemplateParameters;
TypeNode thisType;
if (consolidatedTemplateParams != null && consolidatedTemplateParams.Count > 0) {
thisType = this.currentClosureClassInstance.GetGenericTemplateInstance(this.assemblyBeingRewritten, consolidatedTemplateParams);
}
else {
thisType = this.currentClosureClassInstance;
}
this.currentAccessToTopLevelClosure = new MemberBinding(new This(thisType), f);
break;
//.........这里部分代码省略.........
示例10: UnifiedType
/// <summary>
/// Computes an upper bound in the type hierarchy for the set of argument types.
/// This upper bound is a type that all types in the list are assignable to.
/// If the types are all classes, then *the* least-upper-bound in the class
/// hierarchy is returned.
/// If the types contain at least one interface, then *a* deepest upper-bound
/// is found from the intersection of the upward closure of each type.
/// Note that if one of the types is System.Object, then that is immediately
/// returned as the unified type without further examination of the list.
/// </summary>
/// <param name="ts">A list containing the set of types from which to compute the unified type.</param>
/// <returns>The type corresponding to the least-upper-bound.</returns>
public virtual TypeNode UnifiedType(TypeNodeList ts, TypeViewer typeViewer){
if (ts == null || ts.Count == 0) return null;
TypeNode unifiedType = SystemTypes.Object; // default unified type
bool atLeastOneInterface = false;
#region If at least one of the types is System.Object, then that is the unified type
for (int i = 0, n = ts.Count; i < n; i++){
TypeNode t = this.Unwrap(ts[i]);
if (t == SystemTypes.Object){
return SystemTypes.Object;
}
}
#endregion If at least one of the types is System.Object, then that is the unified type
// assert forall{TypeNode t in ts; t != SystemTypes.Object};
#region See if any of the types are interfaces
for (int i = 0, n = ts.Count; i < n; i++){
TypeNode t = this.Unwrap(ts[i]);
if (t.NodeType == NodeType.Interface){
atLeastOneInterface = true;
break;
}
}
#endregion See if any of the types are interfaces
#region Find the LUB in the class hierarchy (if there are no interfaces)
if (!atLeastOneInterface){
TrivialHashtable h = new TrivialHashtable(ts.Count);
// Create the list [s, .., t] for each element t of ts where for each item
// in the list, t_i, t_i = t_{i+1}.BaseType. (s.BaseType == SystemTypes.Object)
// Store the list in a hashtable keyed by t.
// Do this only for classes. Handle interfaces in a different way because of
// multiple inheritance.
for (int i = 0, n = ts.Count; i < n; i++){
TypeNodeList tl = new TypeNodeList();
TypeNode t = this.Unwrap(ts[i]);
tl.Add(t);
TypeNode t2 = t.BaseType;
while (t2 != null && t2 != SystemTypes.Object){ // avoid including System.Object in the list for classes
tl.Insert(t2,0);
t2 = this.Unwrap(t2.BaseType);
}
h[ts[i].UniqueKey] = tl;
}
bool stop = false;
int depth = 0;
while (!stop){
TypeNode putativeUnifiedType = null;
int i = 0;
int n = ts.Count;
putativeUnifiedType = ((TypeNodeList) h[ts[0].UniqueKey])[depth];
while (i < n){
TypeNode t = ts[i];
TypeNodeList subTypes = (TypeNodeList) h[t.UniqueKey];
if (subTypes.Count <= depth || subTypes[depth] != putativeUnifiedType){
// either reached the top of the hierarchy for t_i or it is on a different branch
// than the current one.
stop = true;
break;
}
i++;
}
if (i == n){ // made it all the way through: all types are subtypes of the current one
unifiedType = putativeUnifiedType;
}
depth++;
}
}
#endregion Find the LUB in the class hierarchy (if there are no interfaces)
#region Find *a* LUB in the interface hierarchy (if there is at least one interface or current LUB is object)
if (unifiedType == SystemTypes.Object || atLeastOneInterface){
TrivialHashtable interfaces = new TrivialHashtable();
for (int i = 0, n = ts.Count; i < n; i++){
InterfaceList il = new InterfaceList();
interfaces[ts[i].UniqueKey] = il;
this.SupportedInterfaces(ts[i],il,typeViewer); // side-effect: il gets added to
}
// interfaces[ts[i]] is the upward closure of all of the interfaces supported by ts[i]
// compute the intersection of all of the upward closures
// might as well start with the first type in the list ts
InterfaceList intersection = new InterfaceList();
InterfaceList firstIfaceList = (InterfaceList)interfaces[ts[0].UniqueKey];
for (int i = 0, n = firstIfaceList.Count; i < n; i++){
Interface iface = firstIfaceList[i];
bool found = false;
int j = 1; // start at second type in the list ts
while (j < ts.Count){
InterfaceList cur = (InterfaceList)interfaces[ts[j].UniqueKey];
found = false;
for (int k = 0, p = cur.Count; k < p; k++){
//.........这里部分代码省略.........
示例11: ParseTypeArguments
private TypeNodeList ParseTypeArguments(bool returnNullIfError, bool Typeof, TokenSet followers, out int endCol, out int arity){
Debug.Assert(this.currentToken == Token.LessThan);
if (this.sink != null) this.sink.StartTemplateParameters(this.scanner.CurrentSourceContext);
arity = 1;
this.GetNextToken();
if (Typeof && this.currentToken == Token.GreaterThan){
endCol = this.scanner.endPos;
this.GetNextToken();
if (this.sink != null) this.sink.EndTemplateParameters(this.scanner.CurrentSourceContext);
return null;
}
SourceContext commaContext = this.scanner.CurrentSourceContext;
TypeNodeList arguments = new TypeNodeList();
for(;;){
if (arity > 0 && Typeof && this.currentToken == Token.Comma){
arity++;
this.GetNextToken();
continue;
}
if (arity > 1){
if (Typeof && this.currentToken == Token.GreaterThan) break;
this.HandleError(commaContext, Error.TypeExpected);
}
arity = 0;
SourceContext sctx = this.scanner.CurrentSourceContext;
TypeNode t = this.ParseTypeExpression(null, followers|Token.Comma|Token.GreaterThan|Token.RightShift, true);
if (this.sink != null && t != null) {
sctx.EndPos = this.scanner.endPos;
this.sink.NextTemplateParameter(sctx);
}
endCol = this.scanner.endPos;
if (returnNullIfError && t == null) {
if (this.sink != null) this.sink.EndTemplateParameters(this.scanner.CurrentSourceContext);
return null;
}
arguments.Add(t);
if (this.currentToken != Token.Comma) break;
this.GetNextToken();
}
if (this.sink != null) this.sink.EndTemplateParameters(this.scanner.CurrentSourceContext);
endCol = this.scanner.endPos;
if (returnNullIfError && this.currentToken != Token.GreaterThan && this.currentToken != Token.RightShift && this.currentToken != Token.EndOfFile)
return null;
if (this.currentToken == Token.RightShift)
this.currentToken = Token.GreaterThan;
else if (this.currentToken == Token.GreaterThan)
this.Skip(Token.GreaterThan);
return arguments;
}
示例12: ParseInterfaceList
private InterfaceList ParseInterfaceList(TokenSet followers, bool expectLeftBrace){
InterfaceList ilist = new InterfaceList();
TokenSet followersOrComma = followers|Token.Comma;
for(;;){
Expression id = this.scanner.GetIdentifier();
switch(this.currentToken){
case Token.Bool:
case Token.Decimal:
case Token.Sbyte:
case Token.Byte:
case Token.Short:
case Token.Ushort:
case Token.Int:
case Token.Uint:
case Token.Long:
case Token.Ulong:
case Token.Char:
case Token.Float:
case Token.Double:
case Token.Object:
case Token.String:
case Token.Void:
TypeExpression texpr = this.TypeExpressionFor(this.currentToken);
this.GetNextToken();
ilist.Add(new InterfaceExpression(texpr.Expression, texpr.SourceContext));
goto lookForComma;
default:
bool idOK = Parser.IdentifierOrNonReservedKeyword[this.currentToken];
if (idOK){
this.GetNextToken();
if (this.currentToken == Token.DoubleColon){
this.GetNextToken();
Identifier id2 = this.scanner.GetIdentifier();
id2.Prefix = (Identifier)id;
id2.SourceContext.StartPos = id.SourceContext.StartPos;
this.SkipIdentifierOrNonReservedKeyword();
id = id2;
}
if (this.currentToken == Token.Dot)
id = this.ParseQualifiedIdentifier(id, followersOrComma|Token.LessThan);
}else{
int col = this.scanner.endPos;
this.SkipIdentifierOrNonReservedKeyword(Error.TypeExpected);
if (col == this.scanner.endPos && this.currentToken != Token.EndOfFile){
//Did not consume a token, but just gave an error
if (!followersOrComma[this.currentToken]) this.GetNextToken();
if (followers[this.currentToken]) return ilist;
if (this.currentToken != Token.Comma){
if (Parser.IdentifierOrNonReservedKeyword[this.currentToken]) continue;
break;
}
this.GetNextToken();
continue;
}
if (this.currentToken == Token.Dot)
id = this.ParseQualifiedIdentifier(id, followersOrComma|Token.LessThan);
if (!idOK) goto lookForComma;
}
break;
}
//I really want an Identifier here for StartName
if (this.sink != null) {
Identifier name = id as Identifier;
if (id is QualifiedIdentifier) {
name = ((QualifiedIdentifier)id).Identifier;
}
if (name != null) {
this.sink.StartName(name);
}
}
InterfaceExpression ifaceExpr = new InterfaceExpression(id, id.SourceContext);
if (this.currentToken == Token.LessThan){
yetAnotherTypeArgumentList:
this.GetNextToken();
TypeNodeList arguments = new TypeNodeList();
for(;;){
TypeNode t = this.ParseTypeExpression(null, followers|Token.Comma|Token.GreaterThan);
arguments.Add(t);
if (this.currentToken != Token.Comma) break;
this.GetNextToken();
}
ifaceExpr.TemplateArguments = arguments;
ifaceExpr.TemplateArgumentExpressions = arguments.Clone();
ifaceExpr.SourceContext.EndPos = this.scanner.endPos;
this.Skip(Token.GreaterThan);
if (this.currentToken == Token.Dot) {
TemplateInstance tempInst = new TemplateInstance(ifaceExpr.Expression, ifaceExpr.TemplateArguments);
tempInst.TypeArgumentExpressions = ifaceExpr.TemplateArguments == null ? null : ifaceExpr.TemplateArguments.Clone();
tempInst.SourceContext = ifaceExpr.SourceContext;
ifaceExpr.Expression = this.ParseQualifiedIdentifier(tempInst, followersOrComma|Token.LessThan);
ifaceExpr.TemplateArguments = null;
ifaceExpr.TemplateArgumentExpressions = null;
if (ifaceExpr.Expression != null) ifaceExpr.SourceContext = ifaceExpr.Expression.SourceContext;
if (this.currentToken == Token.LessThan) goto yetAnotherTypeArgumentList;
}
}
ilist.Add(ifaceExpr);
lookForComma:
if (Parser.TypeOperator[this.currentToken] && !(expectLeftBrace && this.currentToken == Token.LeftBrace)){
this.HandleError(Error.BadBaseType);
//.........这里部分代码省略.........
示例13: ParseTypeParameters
private void ParseTypeParameters(Member parent, TokenSet followers, TypeNodeList parameters){
TypeNode declaringType = parent as TypeNode;
if (declaringType == null) declaringType = parent.DeclaringType;
Debug.Assert(this.currentToken == Token.LessThan);
this.GetNextToken();
for(int i = 0; ; i++){
AttributeList attributes = null;
if (this.currentToken == Token.LeftBracket)
attributes = this.ParseAttributes(null, followers|Token.Identifier|Token.Comma|Token.GreaterThan|Token.RightShift);
if (this.currentToken != Token.Identifier) {
this.HandleError(Error.ExpectedIdentifier);
break;
}
Identifier id = this.scanner.GetIdentifier();
TypeParameter param = parent is Method ? new MethodTypeParameter() : new TypeParameter();
param.Attributes = attributes;
param.DeclaringMember = parent;
param.ParameterListIndex = i;
param.Name = id;
param.DeclaringModule = declaringType.DeclaringModule;
if (!this.useGenerics){
param.DeclaringType = declaringType;
if (!(parent is Method)) declaringType.Members.Add(param);
}
parameters.Add(param);
param.SourceContext = this.scanner.CurrentSourceContext;
this.GetNextToken();
if (this.currentToken == Token.Dot){
QualifiedIdentifier qualid = (QualifiedIdentifier)this.ParseQualifiedIdentifier(id, followers|Token.Colon|Token.Comma|Token.GreaterThan);
param.Namespace = Identifier.For(qualid.Qualifier.ToString());
param.Name = qualid.Identifier;
param.SourceContext = qualid.SourceContext;
}
param.Interfaces = new InterfaceList();
if (this.currentToken != Token.Comma) break;
this.GetNextToken();
}
this.Skip(Token.GreaterThan);
this.SkipTo(followers);
}
示例14: PopulateTypeList
public void PopulateTypeList(TypeNodeList types, TypeNode t){
if (types == null || t == null){Debug.Assert(false); return;}
types.Add(t);
MemberList members = t.Members;
for (int i = 0, n = members == null ? 0 : members.Count; i < n; i++){
t = members[i] as TypeNode;
if (t == null) continue;
this.PopulateTypeList(types, t);
}
}
示例15: ParseFunctionPointer
private FunctionPointer/*!*/ ParseFunctionPointer(MemoryCursor/*!*/ sigReader)
{
CallingConventionFlags convention = (CallingConventionFlags)sigReader.ReadByte();
int n = sigReader.ReadCompressedInt();
TypeNode returnType = this.ParseTypeSignature(sigReader);
if(returnType == null)
returnType = CoreSystemTypes.Object;
TypeNodeList parameterTypes = new TypeNodeList();
int m = n;
for(int i = 0; i < n; i++)
{
TypeNode t = this.ParseTypeSignature(sigReader);
if (t == null)
m = i--;
else
parameterTypes.Add(t);
}
FunctionPointer fp = FunctionPointer.For(parameterTypes, returnType);
fp.CallingConvention = convention;
fp.VarArgStart = m;
return fp;
}