示例#1
0
        public override constant_node get_constant_copy(location loc)
        {
            array_const ac = new array_const(this._element_values, loc);

            ac.type = this.type;
            return(ac);
        }
 public override void visit(SyntaxTree.array_const _array_const)
 {
     if (_array_const.elements != null)
     {
         List<constant_node> elements = new List<constant_node>();
         List<expression_node> exprs = new List<expression_node>();
         foreach (SyntaxTree.expression expr in _array_const.elements.expressions)
         {
         	if (is_typed_const_def)
         		elements.Add(convert_strong_to_constant_node(expr));
         	else
         		exprs.Add(convert_strong(expr));
         }
         if (is_typed_const_def)
         {
         	array_const arrc = new array_const(elements, get_location(_array_const));
         	arrc.SetType(new ArrayConstType(elements[0].type, elements.Count, arrc.location));
         	return_value(arrc);
         }
         else
         {
         	array_initializer arrc = new array_initializer(exprs, get_location(_array_const));
         	arrc.type = new ArrayConstType(exprs[0].type, exprs.Count, arrc.location);
         	return_value(arrc);
         }
     }
     else
     {
         //это пустой инициализатор записи
         record_constant rc = new record_constant(new List<PascalABCCompiler.SyntaxTree.record_const_definition>(), get_location(_array_const));
         rc.SetType(new RecordConstType(get_location(_array_const)));
         return_value(rc);
     }
 }
 private array_const ConvertArrayConst(type_node tn, array_const constant)
 {
     type_node element_type = null;
     if (IsBoundedArray(tn))
     {
         bounded_array_interface bai = tn.get_internal_interface(internal_interface_kind.bounded_array_interface) as bounded_array_interface;
         element_type = bai.element_type;
         ordinal_type_interface oti_indexer = bai.ordinal_type_interface;
         int arr_length = oti_indexer.ordinal_type_to_int(oti_indexer.upper_value) - oti_indexer.ordinal_type_to_int(oti_indexer.lower_value) + 1;
         if (arr_length != constant.element_values.Count)
             AddError(constant.location, "ARRAY_CONST_{0}_ELEMENTS_EXPECTED", arr_length);
     }
     else
         if (IsUnsizedArray(tn))
         {
             array_internal_interface aii = tn.get_internal_interface(internal_interface_kind.unsized_array_interface) as array_internal_interface;
             element_type = aii.element_type;
             if (aii.rank > 1)
             {
             	array_const cnst = ConvertNDimArrayConst(tn,1,element_type,constant);
             	cnst.SetType(tn);
             	return cnst;
             }
         }
         else
             throw new CompilerInternalError("Unexpected array type");
     for (int i = 0; i < constant.element_values.Count; i++)
         constant.element_values[i] = convert_strong_to_constant_node(constant.element_values[i], element_type);
     constant.SetType(tn);
     return constant;
 }
 private array_const ConvertNDimArrayConst(type_node tn,  int cur_rank, type_node element_type, array_const constant)
 {
 	array_internal_interface aii = tn.get_internal_interface(internal_interface_kind.unsized_array_interface) as array_internal_interface;
 	int rank = aii.rank;
 	for (int i=0; i<constant.element_values.Count; i++)
 	{
 		expression_node e = constant.element_values[i];
 		if (e is array_const)
 		{
 			if (cur_rank>=rank)
 				constant.element_values[i] = ConvertArrayConst(tn.element_type,e as array_const);
 				//AddError(new CanNotConvertTypes(e,e.type,tn.element_type,e.location));
 			else
 			constant.element_values[i] = ConvertNDimArrayConst(tn,cur_rank+1,element_type,e as array_const);
 		}
 		else if (e is record_constant)
     	{
     		if (element_type is common_type_node)
     			constant.element_values[i] = ConvertRecordConst(element_type as common_type_node, e as record_constant);
     		else AddError(new NotSupportedError(constant.element_values[i].location));
     	}
 		else
     	{
 			if (cur_rank != rank)
                 AddError(constant.location, "RANK_MISMATCH_IN_INITILIALIZER");
 			constant.element_values[i] = convert_strong_to_constant_node(constant.element_values[i], element_type);
     	}
 	}
 	constant.SetType(tn);
 	return constant;
 }