summaryrefslogtreecommitdiff
path: root/lib/Sema/SemaChecking.cpp
blob: f7f783edad0ad2bf145357b76ccb7550cf27fc9c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
//
//                     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/AST/ExprVisitor.h"
#include "flang/Basic/Diagnostic.h"
#include "llvm/Support/raw_ostream.h"

namespace flang {

int64_t Sema::EvalAndCheckIntExpr(const Expr *E,
                                  int64_t ErrorValue) {
  auto Type = E->getType();
  if(Type.isNull() || !Type->isIntegerType())
    goto error;
  int64_t Result;
  if(!E->EvaluateAsInt(Result, Context))
    goto error;

  return Result;
error:
  Diags.Report(E->getLocation(), diag::err_expected_integer_constant_expr)
    << E->getSourceRange();
  return ErrorValue;
}

int64_t Sema::CheckIntGT0(const Expr *E, int64_t EvalResult,
                          int64_t ErrorValue) {
  if(EvalResult < 1) {
    Diags.Report(E->getLocation(), diag::err_expected_integer_gt_0)
      << E->getSourceRange();
    return ErrorValue;
  }
  return EvalResult;
}

BuiltinType::TypeKind Sema::EvalAndCheckTypeKind(QualType T,
                                                 const Expr *E) {
  auto Result = EvalAndCheckIntExpr(E, 4);
  switch(cast<BuiltinType>(T.getTypePtr())->getTypeSpec()) {
  case BuiltinType::Integer:
  case BuiltinType::Logical: {
    switch(Result) {
    case 1: return BuiltinType::Int1;
    case 2: return BuiltinType::Int2;
    case 4: return BuiltinType::Int4;
    case 8: return BuiltinType::Int8;
    }
    break;
  }
  case BuiltinType::Real:
  case BuiltinType::Complex: {
    switch(Result) {
    case 4: return BuiltinType::Real4;
    case 8: return BuiltinType::Real8;
    }
    break;
  }
  default:
    llvm_unreachable("invalid type kind");
  }
  Diags.Report(E->getLocation(), diag::err_invalid_kind_selector)
    << int(Result) << T << E->getSourceRange();
  return BuiltinType::NoKind;
}

QualType Sema::ApplyTypeKind(QualType T, const Expr *E) {
  auto Kind = EvalAndCheckTypeKind(T, E);
  return Kind != BuiltinType::NoKind?
        QualType(Context.getBuiltinType(T->asBuiltinType()->getTypeSpec(),
                   Kind, true), 0) : T;
}

bool Sema::CheckConstantExpression(const Expr *E) {
  if(!E->isEvaluatable(Context)) {
    Diags.Report(E->getLocation(),
                 diag::err_expected_constant_expr)
      << E->getSourceRange();
    return false;
  }
  return true;
}

bool Sema::CheckIntegerOrRealConstantExpression(const Expr *E) {
  auto T = E->getType();
  if(!(T->isIntegerType() || T->isRealType()) ||
     !E->isEvaluatable(Context)) {
    Diags.Report(E->getLocation(),
                 diag::err_expected_integer_or_real_constant_expr)
      << E->getSourceRange();
    return false;
  }
  return false;
}

class ArgumentDependentExprChecker : public ExprVisitor<ArgumentDependentExprChecker> {
public:
  ASTContext &Context;
  Sema &Sem;
  DiagnosticsEngine &Diags;
  bool HasArgumentTypeErrors;
  bool CheckEvaluatable;
  bool ArgumentDependent;
  bool Evaluatable;

  ArgumentDependentExprChecker(ASTContext &C, Sema &S,
                               DiagnosticsEngine &Diag)
    : Context(C), Sem(S), Diags(Diag),
      HasArgumentTypeErrors(false),
      CheckEvaluatable(false), ArgumentDependent(false), Evaluatable(true) {}

  void VisitVarExpr(VarExpr *E) {
    auto VD = E->getVarDecl();
    if(VD->isArgument()) {
      ArgumentDependent = true;
      if(!CheckEvaluatable) {
        if(VD->getType().isNull()) {
          if(!Sem.ApplyImplicitRulesToArgument(const_cast<VarDecl*>(VD),
                                               E->getSourceRange())) {
            HasArgumentTypeErrors = true;
            return;
          }
          E->setType(VD->getType());
        }
        if(!VD->getType()->isIntegerType()) {
          Diags.Report(E->getLocation(), diag::err_array_explicit_shape_requires_int_arg)
            << VD->getType() << E->getSourceRange();
          HasArgumentTypeErrors = true;
        }
      }
    } else
      VisitExpr(E);
  }

  void VisitBinaryExpr(BinaryExpr *E) {
    Visit(E->getLHS());
    Visit(E->getRHS());
  }
  void VisitUnaryExpr(UnaryExpr *E) {
    Visit(E->getExpression());
  }
  void VisitImplicitCastExpr(ImplicitCastExpr *E) {
    Visit(E->getExpression());
  }
  void VisitExpr(Expr *E) {
    if(!E->isEvaluatable(Context)) {
      Evaluatable = false;
      if(CheckEvaluatable) {
        Diags.Report(E->getLocation(),
                     diag::err_expected_constant_expr)
          << E->getSourceRange();
      }
    }
  }
};

bool Sema::CheckArgumentDependentEvaluatableIntegerExpression(Expr *E) {
  ArgumentDependentExprChecker EV(Context, *this, Diags);
  EV.Visit(E);
  if(EV.ArgumentDependent) {
    /// Report unevaluatable errors.
    if(!EV.Evaluatable) {
      EV.CheckEvaluatable = true;
      EV.Visit(E);
    } else if(!EV.HasArgumentTypeErrors)
      CheckIntegerExpression(E);
    return true;
  }
  return false;
}

bool Sema::StatementRequiresConstantExpression(SourceLocation Loc, const Expr *E) {
  if(!E->isEvaluatable(Context)) {
    Diags.Report(Loc,
                 diag::err_stmt_requires_consant_expr)
      << E->getSourceRange();
    return false;
  }
  return true;
}

bool Sema::CheckIntegerExpression(const Expr *E) {
  if(E->getType()->isIntegerType())
    return true;
  Diags.Report(E->getLocation(),
               diag::err_typecheck_expected_integer_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::StmtRequiresIntegerExpression(SourceLocation Loc, const Expr *E) {
  if(E->getType()->isIntegerType())
    return true;
  Diags.Report(Loc, diag::err_typecheck_stmt_requires_int_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::CheckScalarNumericExpression(const Expr *E) {
  if(E->getType()->isIntegerType() ||
     E->getType()->isRealType())
    return true;
  Diags.Report(E->getLocation(),
               diag::err_expected_scalar_numeric_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::StmtRequiresIntegerVar(SourceLocation Loc, const VarExpr *E) {
  if(E->getType()->isIntegerType())
    return true;
  Diags.Report(Loc,
               diag::err_typecheck_stmt_requires_int_var)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::StmtRequiresScalarNumericVar(SourceLocation Loc, const VarExpr *E, unsigned DiagId) {
  if(E->getType()->isIntegerType() ||
     E->getType()->isRealType())
    return true;
  Diags.Report(Loc, DiagId)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::CheckLogicalExpression(const Expr *E) {
  if(E->getType()->isLogicalType())
    return true;
  Diags.Report(E->getLocation(),
               diag::err_typecheck_expected_logical_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::StmtRequiresLogicalExpression(SourceLocation Loc, const Expr *E) {
  if(E->getType()->isLogicalType())
    return true;
  Diags.Report(Loc, diag::err_typecheck_stmt_requires_logical_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::StmtRequiresLogicalArrayExpression(SourceLocation Loc, const Expr *E) {
  auto T = E->getType();
  if(T->isArrayType()) {
    T = T->asArrayType()->getElementType();
    if(T->isLogicalType())
      return true;
  }
  Diags.Report(Loc, diag::err_typecheck_stmt_requires_logical_array_expr)
    << T << E->getSourceRange();
  return false;
}

bool Sema::StmtRequiresIntegerOrLogicalOrCharacterExpression(SourceLocation Loc, const Expr *E) {
  auto Type = E->getType();
  if(Type->isIntegerType() || Type->isLogicalType() || Type->isCharacterType())
    return true;
  Diags.Report(Loc, diag::err_typecheck_stmt_requires_int_logical_char_expr)
    << Type << E->getSourceRange();
  return false;
}

bool Sema::CheckCharacterExpression(const Expr *E) {
  if(E->getType()->isCharacterType())
    return true;
  Diags.Report(E->getLocation(),
               diag::err_typecheck_expected_char_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

bool Sema::AreTypesOfSameKind(QualType A, QualType B) const {
  if(A->isCharacterType())
    return B->isCharacterType();
  if(auto ABTy = dyn_cast<BuiltinType>(A.getTypePtr())) {
    auto BBTy = dyn_cast<BuiltinType>(B.getTypePtr());
    if(!BBTy) return false;    
    auto Spec = ABTy->getTypeSpec();
    if(Spec != BBTy->getTypeSpec()) return false;
    return ABTy->getBuiltinTypeKind() == BBTy->getBuiltinTypeKind();
  }
  return false;
}

bool Sema::CheckTypesOfSameKind(QualType A, QualType B,
                                const Expr *E) const {
  if(AreTypesOfSameKind(A,B)) return true;
  Diags.Report(E->getLocation(),
               diag::err_typecheck_expected_expr_of_type)
    << A << B << E->getSourceRange();
  return false;
}

bool Sema::CheckTypeScalarOrCharacter(const Expr *E, QualType T, bool IsConstant) {
  if(T->isBuiltinType() || T->isCharacterType()) return true;
  Diags.Report(E->getLocation(), IsConstant?
                 diag::err_expected_scalar_or_character_constant_expr :
                 diag::err_expected_scalar_or_character_expr)
    << E->getSourceRange();
  return false;
}

Expr *Sema::TypecheckExprIntegerOrLogicalOrSameCharacter(Expr *E,
                                                         QualType ExpectedType) {
  auto GivenType = E->getType();
  if(ExpectedType->isIntegerType()) {
    if(CheckIntegerExpression(E)) {
      if(!AreTypesOfSameKind(ExpectedType, GivenType))
        return ImplicitCastExpr::Create(Context, E->getLocation(), ExpectedType, E);
    }
  }
  else if(ExpectedType->isLogicalType()) {
    if(CheckLogicalExpression(E)) {
      if(!AreTypesOfSameKind(ExpectedType, GivenType))
        return ImplicitCastExpr::Create(Context, E->getLocation(), ExpectedType, E);
    }
  } else {
    assert(ExpectedType->isCharacterType());
    if(!GivenType->isCharacterType() || !AreTypesOfSameKind(ExpectedType, GivenType)) {
      Diags.Report(E->getLocation(),
                   diag::err_typecheck_expected_char_expr)
        << E->getType() << E->getSourceRange();
    }
  }
  return E;
}

bool Sema::IsDefaultBuiltinOrDoublePrecisionType(QualType T) {
  if(T->isCharacterType())
    return true;
  if(!T->isBuiltinType())
    return false;
  auto BTy = T->asBuiltinType();
  if(BTy->isDoublePrecisionKindSpecified() ||
     BTy->isByteKindSpecified() ||
     !BTy->isKindExplicitlySpecified())
    return true;
  return false;
}

bool Sema::CheckDefaultBuiltinOrDoublePrecisionExpression(const Expr *E) {
  if(IsDefaultBuiltinOrDoublePrecisionType(E->getType()))
    return true;
  Diags.Report(E->getLocation(),
               diag::err_typecheck_expected_default_kind_expr)
    << E->getType() << E->getSourceRange();
  return false;
}

void Sema::CheckExpressionListSameTypeKind(ArrayRef<Expr*> Expressions, bool AllowArrays) {
  assert(!Expressions.empty());
  auto T = Expressions.front()->getType();
  for(size_t I = 0; I < Expressions.size(); ++I) {
    auto E = Expressions[I];
    if(!AreTypesOfSameKind(T, E->getType())) {
      Diags.Report(E->getLocation(), 0) // FIXME
        << E->getSourceRange();
    }
  }
}

static QualType ScalarizeType(const QualType &T, bool AllowArrays) {
  if(AllowArrays)
    return T.getSelfOrArrayElementType();
  return T;
}

static const BuiltinType *getBuiltinType(const Expr *E, bool AllowArrays = false) {
  return dyn_cast<BuiltinType>(ScalarizeType(E->getType(), AllowArrays).getTypePtr());
}

static const BuiltinType *getBuiltinType(QualType T) {
  return dyn_cast<BuiltinType>(T.getTypePtr());
}

bool Sema::DiagnoseIncompatiblePassing(const Expr *E, QualType T,
                                       bool AllowArrays,
                                       StringRef ArgName) {
  QualType ET = AllowArrays? E->getType().getSelfOrArrayElementType() :
                             E->getType();
  if(ArgName.empty())
    Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible)
      << ET << T << E->getSourceRange();
  else
    Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible_named_arg)
      << ET << ArgName << T << E->getSourceRange();
  return true;
}

bool Sema::DiagnoseIncompatiblePassing(const Expr *E, StringRef T,
                                       bool AllowArrays,
                                       StringRef ArgName) {
  QualType ET = AllowArrays? E->getType().getSelfOrArrayElementType() :
                             E->getType();
  if(ArgName.empty())
    Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible)
      << ET << T << E->getSourceRange();
  else
    Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible_named_arg)
      << ET << ArgName << T << E->getSourceRange();
  return true;
}

bool Sema::CheckArgumentsTypeCompability(const Expr *E1, const Expr *E2,
                                         StringRef ArgName1, StringRef ArgName2,
                                         bool AllowArrays) {
  // assume builtin type
  auto T1 = ScalarizeType(E1->getType(), AllowArrays);
  auto T2 = ScalarizeType(E2->getType(), AllowArrays);

  if(T1->isCharacterType()) {
    if(T2->isCharacterType())
      return false;
  } else {
    auto Type1 = getBuiltinType(E1, AllowArrays);
    auto Type2 = getBuiltinType(E2, AllowArrays);
    if(!(!Type2 || Type1->getTypeSpec() != Type2->getTypeSpec() ||
       Type1->getBuiltinTypeKind() != Type2->getBuiltinTypeKind()))
      return false;
  }

  Diags.Report(E2->getLocation(), diag::err_typecheck_arg_conflict_type)
   << ArgName1 << ArgName2
   << T1 << T2
   << E1->getSourceRange() << E2->getSourceRange();
  return true;
}

bool Sema::CheckBuiltinTypeArgument(const Expr *E, bool AllowArrays) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type)
    return DiagnoseIncompatiblePassing(E, "'intrinsic type'", AllowArrays);
  return false;
}

bool Sema::CheckIntegerArgument(const Expr *E, bool AllowArrays, StringRef ArgName) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type || !Type->isIntegerType())
    return DiagnoseIncompatiblePassing(E, Context.IntegerTy, AllowArrays, ArgName);
  return false;
}

bool Sema::CheckRealArgument(const Expr *E, bool AllowArrays) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type || !Type->isRealType())
    return DiagnoseIncompatiblePassing(E, Context.RealTy, AllowArrays);
  return false;
}

bool Sema::CheckComplexArgument(const Expr *E, bool AllowArrays) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type || !Type->isComplexType())
    return DiagnoseIncompatiblePassing(E, Context.ComplexTy, AllowArrays);
  return false;
}

bool Sema::CheckStrictlyRealArgument(const Expr *E, bool AllowArrays) {
  auto T = ScalarizeType(E->getType(), AllowArrays);
  if(!T->isRealType() || IsTypeDoublePrecisionReal(T))
    return DiagnoseIncompatiblePassing(E, Context.RealTy, AllowArrays);
  return false;
}

bool Sema::CheckStrictlyRealArrayArgument(const Expr *E, StringRef ArgName) {
  auto T = E->getType()->asArrayType();
  if(T) {
    auto ET = T->getElementType();
    if(ET->isRealType() && !IsTypeDoublePrecisionReal(ET))
      return false;
  }
  return DiagnoseIncompatiblePassing(E, "'real array'", false, ArgName);
}

bool Sema::CheckDoublePrecisionRealArgument(const Expr *E, bool AllowArrays) {
  if(!IsTypeDoublePrecisionReal(ScalarizeType(E->getType(), AllowArrays)))
    return DiagnoseIncompatiblePassing(E, Context.DoublePrecisionTy, AllowArrays);
  return false;
}

bool Sema::CheckDoubleComplexArgument(const Expr *E, bool AllowArrays) {
  if(!IsTypeDoublePrecisionComplex(ScalarizeType(E->getType(), AllowArrays)))
    return DiagnoseIncompatiblePassing(E, Context.DoubleComplexTy, AllowArrays);
  return false;
}

bool Sema::CheckCharacterArgument(const Expr *E) {
  if(!E->getType()->isCharacterType())
    return DiagnoseIncompatiblePassing(E, Context.CharacterTy, false);
  return false;
}

bool Sema::CheckIntegerOrRealArgument(const Expr *E, bool AllowArrays) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type || !Type->isIntegerOrRealType())
    return DiagnoseIncompatiblePassing(E, "'integer' or 'real'", AllowArrays);
  return false;
}

bool Sema::CheckIntegerOrRealArrayArgument(const Expr *E, StringRef ArgName) {
  auto T = E->getType()->asArrayType();
  if(T) {
    auto Element = getBuiltinType(T->getElementType());
    if(Element && Element->isIntegerOrRealType())
      return false;
  }

  Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible_named_arg)
    << E->getType() << ArgName << "'integer array' or 'real array'"
    << E->getSourceRange();
  return true;
}

bool Sema::CheckIntegerOrRealOrComplexArgument(const Expr *E, bool AllowArrays) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type || !Type->isIntegerOrRealOrComplexType())
    return DiagnoseIncompatiblePassing(E, "'integer' or 'real' or 'complex'", AllowArrays);
  return false;
}

bool Sema::CheckRealOrComplexArgument(const Expr *E, bool AllowArrays) {
  auto Type = getBuiltinType(E, AllowArrays);
  if(!Type || !Type->isRealOrComplexType())
    return DiagnoseIncompatiblePassing(E, "'real' or 'complex'", AllowArrays);
  return false;
}

bool Sema::IsLogicalArray(const Expr *E) {
  auto T = E->getType()->asArrayType();
  if(T) {
    auto Element = getBuiltinType(T->getElementType());
    if(Element && Element->isLogicalType())
      return true;
  }
  return false;
}

bool Sema::CheckLogicalArrayArgument(const Expr *E, StringRef ArgName) {
  if(IsLogicalArray(E)) return false;

  Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible_named_arg)
    << E->getType() << ArgName << "'logical array'"
    << E->getSourceRange();
  return true;
}

bool Sema::CheckIntegerArgumentOrLogicalArrayArgument(const Expr *E, StringRef ArgName1,
                                                      StringRef ArgName2) {
  if(E->getType()->isIntegerType() || IsLogicalArray(E))
    return false;

  Diags.Report(E->getLocation(), diag::err_typecheck_passing_incompatible_named_args)
    << E->getType() << ArgName1 << "'integer'"
    << ArgName2 << "'logical array'"
    << E->getSourceRange();
  return true;
}

bool Sema::CheckArrayNoImpliedDimension(const ArrayType *T,
                                        SourceRange Range) {
  if(isa<ImpliedShapeSpec>(T->getDimensions().back())) {
    Diags.Report(Range.Start, diag::err_typecheck_use_of_implied_shape_array)
      << Range;
    return false;
  }
  return true;
}

bool Sema::CheckArrayExpr(const Expr *E) {
  return CheckArrayNoImpliedDimension(E->getType()->asArrayType(), E->getSourceRange());
}

// FIXME: ARR(:) = ARR(:)

template<typename T>
static bool CheckArrayCompability(Sema &S, T& Handler,
                                  const ArrayType *LHS,
                                  const ArrayType *RHS,
                                  SourceLocation Loc,
                                  SourceRange LHSRange,
                                  SourceRange RHSRange) {
  if(LHS->getDimensionCount() !=
     RHS->getDimensionCount()) {
    Handler.DimensionCountMismatch(Loc, LHS->getDimensionCount(),
                                   RHS->getDimensionCount(),
                                   LHSRange, RHSRange);
    return false;
  }

  bool Valid = S.CheckArrayNoImpliedDimension(LHS, LHSRange);
  if(!S.CheckArrayNoImpliedDimension(RHS, RHSRange))
    Valid = false;
  if(!Valid)
    return false;

  for(size_t I = 0, Count = LHS->getDimensionCount(); I < Count; ++I) {
    auto LHSDim = LHS->getDimensions()[I];
    auto RHSDim = RHS->getDimensions()[I];
    EvaluatedArraySpec LHSSpec, RHSSpec;
    if(LHSDim->Evaluate(LHSSpec, S.getContext())) {
      if(RHSDim->Evaluate(RHSSpec, S.getContext())) {
        auto LHSSize = LHSSpec.Size;
        auto RHSSize = RHSSpec.Size;
        if(LHSSize != RHSSize) {
          Handler.DimensionSizeMismatch(Loc, LHSSize, I+1, RHSSize,
                                        LHSRange, RHSRange);
          return false;
        }
      }
    }
  }

  return true;
}

bool Sema::CheckArrayDimensionsCompability(const ArrayType *LHS,
                                           const ArrayType *RHS,
                                           SourceLocation Loc,
                                           SourceRange LHSRange,
                                           SourceRange RHSRange) {
  struct Handler {
    DiagnosticsEngine &Diags;

    Handler(DiagnosticsEngine &D) : Diags(D) {}

    void DimensionCountMismatch(SourceLocation Loc, int LHSDims,
                                int RHSDims, SourceRange LHSRange,
                                SourceRange RHSRange) {
      Diags.Report(Loc, diag::err_typecheck_expected_array_of_dim_count)
        << LHSDims << RHSDims
        << LHSRange << RHSRange;
    }

    void DimensionSizeMismatch(SourceLocation Loc, int64_t LHSSize,
                               int Dim, int64_t RHSSize, SourceRange LHSRange,
                               SourceRange RHSRange) {
      Diags.Report(Loc, diag::err_typecheck_array_dim_shape_mismatch)
       << Dim << int(LHSSize) << int(RHSSize)
       << LHSRange << RHSRange;
    }
  };

  Handler Reporter(Diags);
  return CheckArrayCompability(*this, Reporter, LHS, RHS, Loc, LHSRange, RHSRange);
}

bool Sema::CheckArrayArgumentDimensionCompability(const Expr *E, const ArrayType *AT,
                                                  StringRef ArgName) {
  auto AT1 = E->getType()->asArrayType();

  struct Handler {
    DiagnosticsEngine &Diags;
    StringRef ArgName;

    Handler(DiagnosticsEngine &D,
            StringRef Name1)
      : Diags(D), ArgName(Name1) {}

    void DimensionCountMismatch(SourceLocation Loc, int LHSDims,
                                int RHSDims, SourceRange LHSRange,
                                SourceRange RHSRange) {
      Diags.Report(Loc, diag::err_typecheck_arg_conflict_array_dim_count)
        << LHSDims << RHSDims
        << ArgName << LHSRange;
    }

    void DimensionSizeMismatch(SourceLocation Loc, int64_t LHSSize,
                               int Dim, int64_t RHSSize, SourceRange LHSRange,
                               SourceRange RHSRange) {
      Diags.Report(Loc, diag::err_typecheck_arg_conflict_array_dim_size)
       << Dim << int(LHSSize)  << int(RHSSize)
       << ArgName << LHSRange;
    }
  };

  Handler Reporter(Diags, ArgName);
  return CheckArrayCompability(*this, Reporter, AT1, AT, E->getLocation(),
                               E->getSourceRange(), SourceRange());
}

bool Sema::CheckArrayArgumentsDimensionCompability(const Expr *E1, const Expr *E2,
                                                   StringRef ArgName1, StringRef ArgName2) {
  auto AT1 = E1->getType()->asArrayType();
  auto AT2 = E2->getType()->asArrayType();
  if(!AT1 || !AT2) return true;

  struct Handler {
    DiagnosticsEngine &Diags;
    StringRef A1, A2;

    Handler(DiagnosticsEngine &D,
            StringRef Name1, StringRef Name2)
      : Diags(D), A1(Name1), A2(Name2) {}

    void DimensionCountMismatch(SourceLocation Loc, int LHSDims,
                                int RHSDims, SourceRange LHSRange,
                                SourceRange RHSRange) {
      Diags.Report(Loc, diag::err_typecheck_args_conflict_array_dim_count)
        << LHSDims << RHSDims
        << A1 << A2
        << LHSRange << RHSRange;
    }

    void DimensionSizeMismatch(SourceLocation Loc, int64_t LHSSize,
                               int Dim, int64_t RHSSize, SourceRange LHSRange,
                               SourceRange RHSRange) {
      Diags.Report(Loc, diag::err_typecheck_args_conflict_array_dim_size)
       << Dim << int(LHSSize)  << int(RHSSize)
       << A1 << A2
       << LHSRange << RHSRange;
    }
  };

  Handler Reporter(Diags, ArgName1, ArgName2);
  return CheckArrayCompability(*this, Reporter, AT1, AT2, E2->getLocation(),
                               E1->getSourceRange(), E2->getSourceRange());
}

bool Sema::CheckVarIsAssignable(const VarExpr *E) {
  for(auto I : CurLoopVars) {
    if(I->getVarDecl() == E->getVarDecl()) {
      Diags.Report(E->getLocation(), diag::err_var_not_assignable)
        << E->getVarDecl()->getIdentifier() << E->getSourceRange();
      Diags.Report(I->getLocation(), diag::note_var_prev_do_use)
        << I->getSourceRange();
      return false;
    }
  }
  return true;
}

bool Sema::CheckRecursiveFunction(SourceLocation Loc) {
  auto Function = cast<FunctionDecl>(CurContext);
  if(!Function->isRecursive()) {
    Diags.Report(Loc, diag::err_call_non_recursive)
      << (Function->isSubroutine()? 1: 0)
      << Function->getIdentifier()
      << getTokenRange(Loc);
    return false;
  }
  return true;
}

} // end namespace flang