summaryrefslogtreecommitdiff
path: root/lib/Sema/SemaIntrinsic.cpp
blob: c65a79a9c4e8b7ff7d189fff30a0115c28274ab8 (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
//===--- SemaIntrinsic.cpp - Intrinsic call 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/Basic/Diagnostic.h"
#include "llvm/Support/raw_ostream.h"

namespace flang {

using namespace intrinsic;

bool Sema::CheckIntrinsicCallArgumentCount(intrinsic::FunctionKind Function,
                                           ArrayRef<Expr*> Args,
                                           SourceLocation Loc) {
  unsigned ArgCountDiag = 0;
  int ExpectedCount = 0;
  const char *ExpectedString = nullptr;

  switch(getFunctionArgumentCount(Function)) {
  case ArgumentCount1:
    ExpectedCount = 1;
    if(Args.size() < 1)
      ArgCountDiag = diag::err_typecheck_call_too_few_args;
    else if(Args.size() > 1)
      ArgCountDiag = diag::err_typecheck_call_too_many_args;
    break;
  case ArgumentCount2:
    ExpectedCount = 2;
    if(Args.size() < 2)
      ArgCountDiag = diag::err_typecheck_call_too_few_args;
    else if(Args.size() > 2)
      ArgCountDiag = diag::err_typecheck_call_too_many_args;
    break;
  case ArgumentCount3:
    ExpectedCount = 3;
    if(Args.size() < 3)
      ArgCountDiag = diag::err_typecheck_call_too_few_args;
    else if(Args.size() > 3)
      ArgCountDiag = diag::err_typecheck_call_too_many_args;
    break;
  case ArgumentCount1or2:
    ExpectedString = "1 or 2";
    if(Args.size() < 1)
      ArgCountDiag = diag::err_typecheck_call_too_few_args;
    else if(Args.size() > 2)
      ArgCountDiag = diag::err_typecheck_call_too_many_args;
    break;
  case ArgumentCount2orMore:
    ExpectedCount = 2;
    if(Args.size() < 2)
      ArgCountDiag = diag::err_typecheck_call_too_few_args_at_least;
    break;
  default:
    llvm_unreachable("invalid arg count");
  }
  if(ArgCountDiag) {
    auto Reporter = Diags.Report(Loc, ArgCountDiag)
                      << /*intrinsic function=*/ 0;
    if(ExpectedString)
      Reporter << ExpectedString;
    else
      Reporter << ExpectedCount;
    Reporter << unsigned(Args.size());
    return true;
  }
  return false;
}

static QualType ApplyKind(Sema &S, QualType T, const Expr *E) {

}

// FIXME: add support for kind parameter
bool Sema::CheckIntrinsicConversionFunc(intrinsic::FunctionKind Function,
                                        ArrayRef<Expr*> Args,
                                        QualType &ReturnType) {
  const Expr *Item = Args[0];
  const Expr *Kind = nullptr;

  if(Function == INT || Function == REAL) {
    if(Args.size() >= 2)
      Kind = Args[1];
  } else if(Function == CMPLX) {
    if(Args.size() >= 2) {
      if(Item->getType().getSelfOrArrayElementType()->isComplexType())
        Kind = Args[1];
      else if(Args.size() == 3) {
        Kind = Args[2];
      }
    }
  }
  if(Kind) {
    if(CheckIntegerArgument(Kind, false, "kind"))
      Kind = nullptr;
  }

  switch(Function) {
  case INT: case IFIX: case IDINT:
    if(Function == IFIX) CheckStrictlyRealArgument(Item, true);
    else if(Function == IDINT) CheckDoublePrecisionRealArgument(Item, true);
    else CheckIntegerOrRealOrComplexArgument(Item, true);
    ReturnType = GetUnaryReturnType(Item, Kind? ApplyTypeKind(Context.IntegerTy, Kind) :
                                                Context.IntegerTy);
    break;

  case REAL: case FLOAT: case SNGL: {
    if(Function == FLOAT) CheckIntegerArgument(Item , true);
    else if(Function == SNGL) CheckDoublePrecisionRealArgument(Item , true);
    else CheckIntegerOrRealOrComplexArgument(Item , true);

    auto ItemType = Item->getType().getSelfOrArrayElementType();
    if(!Kind && ItemType->isComplexType()) {
      ReturnType = GetUnaryReturnType(Item, Context.getComplexTypeElementType(ItemType));
      break;
    }
    ReturnType = GetUnaryReturnType(Item, Kind? ApplyTypeKind(Context.RealTy, Kind) :
                                                Context.RealTy);
    break;
  }
  case DBLE:
    CheckIntegerOrRealOrComplexArgument(Item , true);
    ReturnType = GetUnaryReturnType(Item , Context.DoublePrecisionTy);
    break;

  case CMPLX:
  case DCMPLX:
    CheckIntegerOrRealOrComplexArgument(Item , true);
    if(Args.size() > 1) {
      if(!Item->getType().getSelfOrArrayElementType()->isComplexType()) {
        CheckIntegerOrRealArgument(Args[1], true);
        CheckArrayArgumentsDimensionCompability(Item, Args[1], "x", "y");
      }
    }
    ReturnType = GetUnaryReturnType(Item, Kind? ApplyTypeKind(Context.ComplexTy, Kind) :
                                     (Function == CMPLX? Context.ComplexTy :
                                                         Context.DoubleComplexTy));
    break;

    // FIXME: array and kind support
  case ICHAR:
    CheckCharacterArgument(Item);
    ReturnType = Context.IntegerTy;
    break;

  case CHAR:
    CheckIntegerArgument(Item);
    ReturnType = Context.CharacterTy;
    break;

  default:
    break;
  }
  return false;
}

bool Sema::CheckIntrinsicTruncationFunc(intrinsic::FunctionKind Function,
                                        ArrayRef<Expr*> Args,
                                        QualType &ReturnType) {
  auto Arg = Args[0];
  auto GenericFunction = getGenericFunctionKind(Function);

  if(GenericFunction != Function)
    CheckDoublePrecisionRealArgument(Arg, true);
  else CheckRealArgument(Arg, true);

  switch(GenericFunction) {
  case AINT:
  case ANINT:
    ReturnType = Arg->getType();
    break;
  case NINT:
  case CEILING:
  case FLOOR:
    ReturnType = GetUnaryReturnType(Arg, Context.IntegerTy);
    break;
  default:
    break;
  }
  return false;
}

bool Sema::CheckIntrinsicComplexFunc(intrinsic::FunctionKind Function,
                                     ArrayRef<Expr*> Args,
                                     QualType &ReturnType) {
  auto Arg = Args[0];
  auto GenericFunction = getGenericFunctionKind(Function);

  if(GenericFunction != Function) {
    if(CheckDoubleComplexArgument(Arg, true)) return true;
  } else {
    if(CheckComplexArgument(Arg, true)) return true;
  }

  switch(GenericFunction) {
  case AIMAG:
    ReturnType = GetUnaryReturnType(Arg,
                   Context.getComplexTypeElementType(Arg->getType().getSelfOrArrayElementType()));
    break;
  case CONJG:
    ReturnType = Arg->getType();
    break;
  default:
    break;
  }
  return false;
}

static QualType TypeWithKind(ASTContext &C, QualType T, QualType TKind) {
  return C.getQualTypeOtherKind(T, TKind);
}

bool Sema::CheckIntrinsicMathsFunc(intrinsic::FunctionKind Function,
                                   ArrayRef<Expr*> Args,
                                   QualType &ReturnType) {
  auto FirstArg = Args[0];
  auto SecondArg = Args.size() > 1? Args[1] : nullptr;
  auto GenericFunction = getGenericFunctionKind(Function);

  switch(GenericFunction) {
  case ABS:
    if(GenericFunction != Function) {
      switch(Function) {
      case IABS: CheckIntegerArgument(FirstArg, true); break;
      case DABS: CheckDoublePrecisionRealArgument(FirstArg, true); break;
      case CABS: CheckComplexArgument(FirstArg, true); break;
      case CDABS:
        CheckDoubleComplexArgument(FirstArg, true);
        ReturnType = GetUnaryReturnType(FirstArg, Context.DoublePrecisionTy);
        return false;
      }
    }
    else CheckIntegerOrRealOrComplexArgument(FirstArg, true);
    if(FirstArg->getType()->isComplexType()) {
      ReturnType = GetUnaryReturnType(FirstArg, TypeWithKind(Context, Context.RealTy,
                                      FirstArg->getType()));
    } else
      ReturnType = FirstArg->getType();
    break;

  // 2 integer/real/complex
  case MOD:
  case SIGN:
  case DIM:
  case ATAN2:
    if(GenericFunction != Function) {
      switch(Function) {
      case ISIGN: case IDIM:
        CheckIntegerArgument(FirstArg, true);
        break;
      case AMOD:
        CheckRealArgument(FirstArg, true);
        break;
      case DMOD: case DSIGN: case DDIM:
      case DATAN2:
        CheckDoublePrecisionRealArgument(FirstArg, true);
        break;
      }
    }
    else {
      if(GenericFunction == ATAN2)
        CheckRealArgument(FirstArg, true);
     else
        CheckIntegerOrRealArgument(FirstArg, true);
    }
    CheckArgumentsTypeCompability(FirstArg, SecondArg, "x", "y", true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "x", "y");
    ReturnType = FirstArg->getType(); // FIXME: Binary type
    break;

  case DPROD:
    CheckStrictlyRealArgument(FirstArg, true);
    CheckStrictlyRealArgument(SecondArg, true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "x", "y");
    ReturnType = GetBinaryReturnType(FirstArg, SecondArg, Context.DoublePrecisionTy);
    break;

  case MAX:
  case MIN:
    if(GenericFunction != Function) {
      //FIXME
      bool Failed = false;
      for(size_t I = 0; I < Args.size(); ++I) {
        switch(Function) {
        case MAX0: case MIN0: case AMIN0:
          if(CheckIntegerArgument(Args[I]))
            Failed = true;
          break;
        case AMAX1: case AMIN1: case MIN1:
          if(CheckStrictlyRealArgument(Args[I]))
            Failed = true;
          break;
        case DMAX1: case DMIN1:
          if(CheckDoublePrecisionRealArgument(Args[I]))
            Failed = true;
          break;
        }
      }
      if(!Failed)
        CheckExpressionListSameTypeKind(Args);
      //FIXME AMIN0 returns -> Real
      //FIXME MIN1  returns -> Integer ???
    } else {
      bool Failed = false;
      for(size_t I = 0; I < Args.size(); ++I) {
        if(CheckIntegerOrRealArgument(Args[I]))
          Failed = true;
      }
      if(!Failed)
        CheckExpressionListSameTypeKind(Args);
    }
    ReturnType = FirstArg->getType();
    break;

  // 1 real/double/complex
  case SQRT:
  case EXP:
  case LOG:
  case SIN:
  case COS:
  case TAN:
    if(GenericFunction != Function) {
      switch(Function) {
      case ALOG:
        if(CheckStrictlyRealArgument(FirstArg, true))
          return true;
        break;
      case DSQRT: case DEXP: case DLOG:
      case DSIN: case DCOS: case DTAN:
        if(CheckDoublePrecisionRealArgument(FirstArg, true))
          return true;
        break;
      case CSQRT: case CEXP: case CLOG:
      case CSIN:  case CCOS: case CTAN:
        if(CheckComplexArgument(FirstArg, true))
          return true;
        break;
      }
    }
    else if(CheckRealOrComplexArgument(FirstArg, true))
      return true;
    ReturnType = FirstArg->getType();
    break;

  // 1 real/double
  case LOG10:
  case ASIN:
  case ACOS:
  case ATAN:
  case SINH:
  case COSH:
  case TANH:
    if(GenericFunction != Function) {
      switch(Function) {
      case ALOG10:
        if(CheckStrictlyRealArgument(FirstArg, true))
          return true;
        break;
      default:
        if(CheckDoublePrecisionRealArgument(FirstArg, true))
          return true;
      }
    }
    else if(CheckRealArgument(FirstArg, true))
      return true;
    ReturnType = FirstArg->getType();
    break;

  }
  return false;
}

bool Sema::CheckIntrinsicCharacterFunc(intrinsic::FunctionKind Function,
                                       ArrayRef<Expr*> Args,
                                       QualType &ReturnType) {
  auto FirstArg = Args[0];
  auto SecondArg = Args.size() > 1? Args[1] : nullptr;

  CheckCharacterArgument(FirstArg);
  if(SecondArg) {
    if(!CheckCharacterArgument(SecondArg))
      CheckExpressionListSameTypeKind(Args);
  }

  switch(Function) {
  case LEN:
  case LEN_TRIM:
  case INDEX:
    ReturnType = Context.IntegerTy;
    break;

  case LGE: case LGT: case LLE: case LLT:
    ReturnType = Context.LogicalTy;
    break;
  }
  return false;
}

bool Sema::CheckIntrinsicArrayFunc(intrinsic::FunctionKind Function,
                                   ArrayRef<Expr*> Args,
                                   QualType &ReturnType) {
  auto FirstArg = Args[0];
  auto SecondArg = Args.size() > 1? Args[1] : nullptr;
  auto ThirdArg = Args.size() > 2? Args[2] : nullptr;
  size_t ArrayDimCount = 0;

  switch(Function) {
  case MAXLOC:
  case MINLOC:
    // FIXME: Optional DIM (when array has more than one dim)
    // FIXME: Third argument mask

    if(!CheckIntegerOrRealArrayArgument(FirstArg, "array")) {
      ArrayDimCount = FirstArg->getType()->asArrayType()->getDimensionCount();
      ReturnType = GetSingleDimArrayType(Context.IntegerTy, ArrayDimCount);
    } else ReturnType = Context.IntegerTy;
    if(SecondArg) {

      if(SecondArg->getType()->isIntegerType()) {
        if(ArrayDimCount == 1) {
          ReturnType = Context.IntegerTy;
        } else {
          // Result: array with a dimension
          assert(false && "FIXME");
        }
      }

      else if(IsLogicalArray(SecondArg))
        CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg,
                                                "array", "mask");
      else {
        if(ArrayDimCount == 1) {
          ReturnType = Context.IntegerTy;
        }
        CheckIntegerArgumentOrLogicalArrayArgument(SecondArg, "dim", "mask");
      }
    }

    break;
  }

  return false;
}

bool Sema::CheckIntrinsicNumericInquiryFunc(intrinsic::FunctionKind Function,
                                            ArrayRef<Expr*> Args,
                                            QualType &ReturnType) {
  auto Arg = Args[0];
  ReturnType = Context.IntegerTy;

  switch(Function) {
  case RADIX:
  case DIGITS:
    CheckIntegerOrRealArgument(Arg);
    break;
  case MINEXPONENT:
  case MAXEXPONENT:
    CheckRealArgument(Arg);
    break;
  case PRECISION:
    CheckRealOrComplexArgument(Arg);
    break;
  case RANGE:
    CheckIntegerOrRealOrComplexArgument(Arg);
    break;
  case intrinsic::HUGE:
  case TINY:
    if(!CheckIntegerOrRealArgument(Arg))
      ReturnType = Arg->getType();
    break;
  case EPSILON:
    if(!CheckRealArgument(Arg))
      ReturnType = Arg->getType();
    break;
  }

  return false;
}

bool Sema::CheckIntrinsicSystemFunc(intrinsic::FunctionKind Function,
                                    ArrayRef<Expr*> Args,
                                    QualType &ReturnType) {
  auto FirstArg = Args[0];

  switch(Function) {
  case ETIME:
    if(!CheckStrictlyRealArrayArgument(FirstArg, "tarray"))
      CheckArrayArgumentDimensionCompability(FirstArg,
                                             GetSingleDimArrayType(Context.RealTy, 2)->asArrayType(),
                                             "tarray");
    ReturnType = Context.RealTy;
    break;
  }

  return false;
}

bool Sema::CheckIntrinsicInquiryFunc(intrinsic::FunctionKind Function,
                                     ArrayRef<Expr*> Args,
                                     QualType &ReturnType) {
  switch(Function) {
  case KIND:
    CheckBuiltinTypeArgument(Args[0], true);
    ReturnType = Context.IntegerTy;
    break;
  case SELECTED_INT_KIND:
    CheckIntegerArgument(Args[0]);
    ReturnType = Context.IntegerTy;
    break;
  case SELECTED_REAL_KIND:
    CheckIntegerArgument(Args[0]);
    if(Args.size() > 1)
      CheckIntegerArgument(Args[1]);
    ReturnType = Context.IntegerTy;
    break;
  case BIT_SIZE:
    if(CheckIntegerArgument(Args[0], true))
      ReturnType = Context.IntegerTy;
    else {
      auto Kind = Args[0]->getType().getSelfOrArrayElementType();
      ReturnType = Context.getQualTypeOtherKind(Context.IntegerTy, Kind);
    }
    break;
  }
  return false;
}

// FIXME: apply constant arguments constraints (e.g. 0 .. BIT_SIZE range)
bool Sema::CheckIntrinsicBitFunc(intrinsic::FunctionKind Function,
                                 ArrayRef<Expr*> Args,
                                 QualType &ReturnType) {
  if(Function == NOT) {
    CheckIntegerArgument(Args[0], true);
    ReturnType = Args[0]->getType();
    return false;
  }

  auto FirstArg = Args[0];
  auto SecondArg = Args[1];
  if(CheckIntegerArgument(FirstArg,true)) {
    ReturnType = Context.IntegerTy;
    return true;
  }

  switch(Function) {
  case BTEST:
    CheckIntegerArgument(SecondArg,true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "i", "pos");
    ReturnType = GetBinaryReturnType(FirstArg, SecondArg, Context.LogicalTy);
    break;
  case IBCLR:
  case IBSET:
    CheckIntegerArgument(SecondArg,true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "i", "pos");
    ReturnType = FirstArg->getType();
    break;
  case IBITS: {
    auto ThirdArg = Args[2];
    CheckIntegerArgument(SecondArg, true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "i", "pos");
    CheckIntegerArgument(ThirdArg, true);
    CheckArrayArgumentsDimensionCompability(FirstArg, ThirdArg, "i", "len");
    ReturnType = FirstArg->getType();
    break;
  }
  case ISHFT:
  case ISHFTC: // FIXME: optional size
    CheckIntegerArgument(SecondArg, true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "i", "shift");
    ReturnType = FirstArg->getType();
    break;
  case IAND:
  case IEOR:
  case IOR:
    CheckArgumentsTypeCompability(FirstArg, SecondArg, "i", "j", true);
    CheckArrayArgumentsDimensionCompability(FirstArg, SecondArg, "i", "j");
    ReturnType = FirstArg->getType();
    break;
  }

  return false;
}

} // end namespace flang