summaryrefslogtreecommitdiff
path: root/lib/Sema/SemaArrayExpr.cpp
blob: 89b8cdf7131dd737d365ca64c18606abc885af58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//===- SemaArrayExpr.cpp - Array Expressions AST Builder and Sema  -------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "flang/Sema/Sema.h"
#include "flang/Sema/DeclSpec.h"
#include "flang/Sema/SemaDiagnostic.h"
#include "flang/AST/ASTContext.h"
#include "flang/AST/Decl.h"
#include "flang/AST/Expr.h"
#include "flang/Basic/Diagnostic.h"
#include "llvm/Support/raw_ostream.h"

namespace flang {

/// DimensionConstructor - constructs a specification
/// for a dimension of a one dimensional array which is created
/// by joining multiple scalar expressions or arrays.
class DimensionConstructor {
  ASTContext &Context;
  uint64_t ResultingSize;
  bool IsConstSizeOnly;
public:
  DimensionConstructor(ASTContext &C)
    : ResultingSize(0),IsConstSizeOnly(true),
      Context(C) {}

  void JoinWith(const ArrayType *T);
  void JoinWith(const Expr *E);

  ArraySpec *CreateDimension();
};

void DimensionConstructor::JoinWith(const ArrayType *T) {
  uint64_t Size;
  if(T->EvaluateSize(Size, Context)) {
    ResultingSize+=Size;
    return;
  }
  IsConstSizeOnly = false;
}

void DimensionConstructor::JoinWith(const Expr *E) {
  if(!IsConstSizeOnly) return;
  if(E->getType()->isArrayType())
    return JoinWith(E->getType()->asArrayType());
  ResultingSize++;
}

ArraySpec *DimensionConstructor::CreateDimension() {
  if(IsConstSizeOnly) {
    return ExplicitShapeSpec::Create(Context,
             IntegerConstantExpr::Create(Context, ResultingSize));
  }
  return DeferredShapeSpec::Create(Context);
}

// FIXME: Items can be implied do.
bool Sema::CheckArrayConstructorItems(ArrayRef<Expr*> Items,
                                      QualType &ResultingArrayType) {
  bool Result = true;
  size_t I;
  QualType ElementType;
  DimensionConstructor SpecConstructor(Context);

  // Set the first valid type to be the element type
  for(I = 0; I < Items.size(); ++I) {
    ElementType = Items[I]->getType();
    if(ElementType->isArrayType()) {
      CheckArrayExpr(Items[I]);
      ElementType = ElementType->asArrayType()->getElementType();
    }
    SpecConstructor.JoinWith(Items[I]);
    if(CheckTypeScalarOrCharacter(Items[I], ElementType, true)) {
      ++I;
      break;
    }
    Result = false;
  }

  // Constraint: Each ac-value expression in the array-constructor
  // shall have the same type and kind type parameter.
  // FIXME: Constraint: Each character value same length.
  for(; I < Items.size(); ++I) {
    auto T = Items[I]->getType();
    if(T->isArrayType()) {
      CheckArrayExpr(Items[I]);
      T = T->asArrayType()->getElementType();
    }
    SpecConstructor.JoinWith(Items[I]);
    if(!CheckTypesOfSameKind(ElementType, T, Items[I]))
      Result = false;
  }

  ResultingArrayType = Context.getArrayType(ElementType, SpecConstructor.CreateDimension());
  return Result;
}

ExprResult Sema::ActOnArrayConstructorExpr(ASTContext &C, SourceLocation Loc,
                                           SourceLocation RParenLoc, ArrayRef<Expr*> Elements) {
  QualType ReturnType;
  CheckArrayConstructorItems(Elements, ReturnType);
  return ArrayConstructorExpr::Create(C, Loc, Elements, ReturnType);
}

} // namespace flang