Exemple #1
0
 protected static void AddInitializationTo(ICollection<Statement> statements, Expression source, Expression target, TypeExpression targetType, BlockStatement containingBlock)
 {
     VccInitializer initializer = source as VccInitializer;
       if (initializer != null) {
     VccArrayTypeExpression arrayType = initializer.arrayTypeExpression ?? targetType as VccArrayTypeExpression;
     if (arrayType != null) {
       initializer.AddInitializingElementAssignmentsTo(statements, target, arrayType);
     } else if (initializer.IsOfStructuredType) {
       VccStructuredTypeDeclaration structType = initializer.GetStructuredTypeDecl();
       if (structType != null) initializer.AddInitializingFieldAssignmentsTo(statements, target, structType);
     }
       } else {
     // It is not an initializer
     // If the expression is a string and the target is a char array, in which case we treat it as an array initializer.
     VccByteStringLiteral stringLiteral = source as VccByteStringLiteral;
     VccArrayTypeExpression arrayType = targetType as VccArrayTypeExpression;
     if (stringLiteral != null && arrayType != null) {
       string val = stringLiteral.Value as string;
       if (val != null) {
     if (arrayType.Size == null) {
       CompileTimeConstant ctc = new CompileTimeConstant(val.Length + 1, stringLiteral.SourceLocation);
       ctc.SetContainingExpression(stringLiteral);
       arrayType.ResetSizeWhenProvidedByInitializer(ctc);
     }
     int size = arrayType.SizeAsInt32;
     VccInitializer newInitializer = VccInitializer.fromStringWithPatchedZeros(val, size, stringLiteral);
     // No need to assign the array type expression field, because we know the element type is char.
     if (newInitializer != null) {
       newInitializer.AddInitializingElementAssignmentsTo(statements, target, arrayType);
     }
       }
     } else {
       // If the target is a union, we will try to treat the constant as an initializer.
       CompileTimeConstant ctc = source as CompileTimeConstant;
       VccUnionDeclaration unionType = MiniResolve(containingBlock.ContainingNamespaceDeclaration, targetType as VccNamedTypeExpression) as VccUnionDeclaration;
       if (ctc != null && unionType != null) {
     List<Expression> exprs = new List<Expression> {ctc};
     VccInitializer newInitializer = new VccInitializer(exprs, false, source.SourceLocation);
     newInitializer.SetContainingBlock(containingBlock);
     newInitializer.AddInitializingFieldAssignmentsTo(statements, target, unionType);
       } else {
     // otherwise, generate an assignment.
     ExpressionStatement elementAssignment = new ExpressionStatement(new Assignment(new TargetExpression(target), source, source.SourceLocation));
     elementAssignment.SetContainingBlock(containingBlock);
     statements.Add(elementAssignment);
       }
     }
       }
 }