private static bool TryCreate( ImmutableContextBase context, ModuleLiteral env, out QualifierValue qualifierValue, LineInfo lineInfo, ObjectLiteral objectLiteral) { Contract.Requires(context != null); Contract.Requires(env != null); Contract.Requires(objectLiteral != null); qualifierValue = null; if (objectLiteral.Count == 0) { qualifierValue = CreateEmpty(context.FrontEndContext.QualifierTable); return(true); } Tuple <StringId, StringId>[] qualifierKvps = new Tuple <StringId, StringId> [objectLiteral.Count]; int i = 0; foreach (var kvp in objectLiteral.Members) { if (!(kvp.Value.Value is string value)) { var location = lineInfo.AsUniversalLocation(env, context); var error = new ErrorContext(name: kvp.Key, objectCtx: objectLiteral).ToErrorString(context); context.Logger.ReportQualifierValueMustEvaluateToString(context.LoggingContext, location.AsLoggingLocation(), error, context.GetStackTraceAsErrorMessage(location)); return(false); } qualifierKvps[i] = Tuple.Create(kvp.Key, context.FrontEndContext.StringTable.AddString(value)); ++i; } QualifierId qualifierId = context.FrontEndContext.QualifierTable.CreateQualifier(qualifierKvps); qualifierValue = new QualifierValue(objectLiteral, qualifierId); return(true); }
/// <summary> /// Tries create a qualifier value given an object literal. /// </summary> public static bool TryCreate( ImmutableContextBase context, ModuleLiteral env, object potentialLiteral, out QualifierValue qualifierValue, LineInfo lineInfo = default(LineInfo)) { Contract.Requires(context != null); Contract.Requires(env != null); Contract.Requires(potentialLiteral != null); qualifierValue = null; if (potentialLiteral is ObjectLiteral0) { qualifierValue = CreateEmpty(context.FrontEndContext.QualifierTable); return(true); } if (potentialLiteral is ObjectLiteralSlim || potentialLiteral is ObjectLiteralN) { return(TryCreate(context, env, out qualifierValue, lineInfo, (ObjectLiteral)potentialLiteral)); } var location = lineInfo.AsUniversalLocation(env, context); context.Logger.ReportQualifierMustEvaluateToObjectLiteral(context.LoggingContext, location.AsLoggingLocation(), context.GetStackTraceAsErrorMessage(location)); return(false); }