Infer fixed array types from string literals.
authorDavid Anderson <dvander@alliedmods.net>
Sat Dec 29 00:12:27 2012 -0800 (2012-12-29)
changeset 24982b1f8bc93a9
parent 248 372adeda3b22
child 250 0998a43cc155
Infer fixed array types from string literals.
src/compiler/NameBinding.cpp
tests/basic/dependent-strings.sp
     1.1 --- a/src/compiler/NameBinding.cpp	Fri Dec 28 22:47:05 2012 -0800
     1.2 +++ b/src/compiler/NameBinding.cpp	Sat Dec 29 00:12:27 2012 -0800
     1.3 @@ -572,15 +572,16 @@
     1.4  
     1.5              // Do some extra inference based on the initializer, if present.
     1.6              Expression *init = node->initialization();
     1.7 -            if (init &&
     1.8 -                init->isArrayLiteral() &&
     1.9 -                init->toArrayLiteral()->isFixed())
    1.10 -            {
    1.11 -                Local<Type> contained(zone_, type->isArray()
    1.12 -                                             ? ArrayType::cast(type)->contained()
    1.13 -                                             : type);
    1.14 -                if (!inferFixedArrayDimensions(init->toArrayLiteral(), contained, dims, levels))
    1.15 -                    return;
    1.16 +            if (init) {
    1.17 +                if (init->isArrayLiteral() && init->toArrayLiteral()->isFixed()) {
    1.18 +                    Local<Type> contained(zone_, type->isArray()
    1.19 +                                                 ? ArrayType::cast(type)->contained()
    1.20 +                                                 : type);
    1.21 +                    if (!inferFixedArrayDimensions(init->toArrayLiteral(), contained, dims, levels))
    1.22 +                        return;
    1.23 +                } else if (init->isStringLiteral()) {
    1.24 +                    dims[levels - 1] = init->toStringLiteral()->string()->length() + 1;
    1.25 +                }
    1.26              }
    1.27  
    1.28              // If we got extra dimensions, we need to build a new type.
     2.1 --- a/tests/basic/dependent-strings.sp	Fri Dec 28 22:47:05 2012 -0800
     2.2 +++ b/tests/basic/dependent-strings.sp	Sat Dec 29 00:12:27 2012 -0800
     2.3 @@ -8,7 +8,10 @@
     2.4  public main()
     2.5  {
     2.6      new char:fixed[100] = "crab\n";
     2.7 -    new char:dynamo[] = "cake\n";
     2.8 +    new char:dynamo[];
     2.9 +
    2.10 +    dynamo = "cake\n";
    2.11 +
    2.12      f(fixed[1]);
    2.13      f(dynamo[1]);
    2.14      PrintString(fixed[1]);