Home | History | Annotate | Download | only in Basic
      1 //===--- OpenMPKinds.cpp - Token Kinds Support ----------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 /// \file
     10 /// \brief This file implements the OpenMP enum and support functions.
     11 ///
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Basic/OpenMPKinds.h"
     15 #include "clang/Basic/IdentifierTable.h"
     16 #include "llvm/ADT/StringRef.h"
     17 #include "llvm/ADT/StringSwitch.h"
     18 #include "llvm/Support/ErrorHandling.h"
     19 #include <cassert>
     20 
     21 using namespace clang;
     22 
     23 OpenMPDirectiveKind clang::getOpenMPDirectiveKind(StringRef Str) {
     24   return llvm::StringSwitch<OpenMPDirectiveKind>(Str)
     25 #define OPENMP_DIRECTIVE(Name) .Case(#Name, OMPD_##Name)
     26 #define OPENMP_DIRECTIVE_EXT(Name, Str) .Case(Str, OMPD_##Name)
     27 #include "clang/Basic/OpenMPKinds.def"
     28       .Default(OMPD_unknown);
     29 }
     30 
     31 const char *clang::getOpenMPDirectiveName(OpenMPDirectiveKind Kind) {
     32   assert(Kind <= OMPD_unknown);
     33   switch (Kind) {
     34   case OMPD_unknown:
     35     return "unknown";
     36 #define OPENMP_DIRECTIVE(Name)                                                 \
     37   case OMPD_##Name:                                                            \
     38     return #Name;
     39 #define OPENMP_DIRECTIVE_EXT(Name, Str)                                        \
     40   case OMPD_##Name:                                                            \
     41     return Str;
     42 #include "clang/Basic/OpenMPKinds.def"
     43     break;
     44   }
     45   llvm_unreachable("Invalid OpenMP directive kind");
     46 }
     47 
     48 OpenMPClauseKind clang::getOpenMPClauseKind(StringRef Str) {
     49   // 'flush' clause cannot be specified explicitly, because this is an implicit
     50   // clause for 'flush' directive. If the 'flush' clause is explicitly specified
     51   // the Parser should generate a warning about extra tokens at the end of the
     52   // directive.
     53   if (Str == "flush")
     54     return OMPC_unknown;
     55   return llvm::StringSwitch<OpenMPClauseKind>(Str)
     56 #define OPENMP_CLAUSE(Name, Class) .Case(#Name, OMPC_##Name)
     57 #include "clang/Basic/OpenMPKinds.def"
     58       .Default(OMPC_unknown);
     59 }
     60 
     61 const char *clang::getOpenMPClauseName(OpenMPClauseKind Kind) {
     62   assert(Kind <= OMPC_unknown);
     63   switch (Kind) {
     64   case OMPC_unknown:
     65     return "unknown";
     66 #define OPENMP_CLAUSE(Name, Class)                                             \
     67   case OMPC_##Name:                                                            \
     68     return #Name;
     69 #include "clang/Basic/OpenMPKinds.def"
     70   case OMPC_threadprivate:
     71     return "threadprivate or thread local";
     72   }
     73   llvm_unreachable("Invalid OpenMP clause kind");
     74 }
     75 
     76 unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
     77                                           StringRef Str) {
     78   switch (Kind) {
     79   case OMPC_default:
     80     return llvm::StringSwitch<OpenMPDefaultClauseKind>(Str)
     81 #define OPENMP_DEFAULT_KIND(Name) .Case(#Name, OMPC_DEFAULT_##Name)
     82 #include "clang/Basic/OpenMPKinds.def"
     83         .Default(OMPC_DEFAULT_unknown);
     84   case OMPC_proc_bind:
     85     return llvm::StringSwitch<OpenMPProcBindClauseKind>(Str)
     86 #define OPENMP_PROC_BIND_KIND(Name) .Case(#Name, OMPC_PROC_BIND_##Name)
     87 #include "clang/Basic/OpenMPKinds.def"
     88         .Default(OMPC_PROC_BIND_unknown);
     89   case OMPC_schedule:
     90     return llvm::StringSwitch<OpenMPScheduleClauseKind>(Str)
     91 #define OPENMP_SCHEDULE_KIND(Name) .Case(#Name, OMPC_SCHEDULE_##Name)
     92 #include "clang/Basic/OpenMPKinds.def"
     93         .Default(OMPC_SCHEDULE_unknown);
     94   case OMPC_depend:
     95     return llvm::StringSwitch<OpenMPDependClauseKind>(Str)
     96 #define OPENMP_DEPEND_KIND(Name) .Case(#Name, OMPC_DEPEND_##Name)
     97 #include "clang/Basic/OpenMPKinds.def"
     98         .Default(OMPC_DEPEND_unknown);
     99   case OMPC_linear:
    100     return llvm::StringSwitch<OpenMPLinearClauseKind>(Str)
    101 #define OPENMP_LINEAR_KIND(Name) .Case(#Name, OMPC_LINEAR_##Name)
    102 #include "clang/Basic/OpenMPKinds.def"
    103         .Default(OMPC_LINEAR_unknown);
    104   case OMPC_map:
    105     return llvm::StringSwitch<OpenMPMapClauseKind>(Str)
    106 #define OPENMP_MAP_KIND(Name) .Case(#Name, OMPC_MAP_##Name)
    107 #include "clang/Basic/OpenMPKinds.def"
    108         .Default(OMPC_MAP_unknown);
    109   case OMPC_unknown:
    110   case OMPC_threadprivate:
    111   case OMPC_if:
    112   case OMPC_final:
    113   case OMPC_num_threads:
    114   case OMPC_safelen:
    115   case OMPC_simdlen:
    116   case OMPC_collapse:
    117   case OMPC_private:
    118   case OMPC_firstprivate:
    119   case OMPC_lastprivate:
    120   case OMPC_shared:
    121   case OMPC_reduction:
    122   case OMPC_aligned:
    123   case OMPC_copyin:
    124   case OMPC_copyprivate:
    125   case OMPC_ordered:
    126   case OMPC_nowait:
    127   case OMPC_untied:
    128   case OMPC_mergeable:
    129   case OMPC_flush:
    130   case OMPC_read:
    131   case OMPC_write:
    132   case OMPC_update:
    133   case OMPC_capture:
    134   case OMPC_seq_cst:
    135   case OMPC_device:
    136   case OMPC_threads:
    137   case OMPC_simd:
    138   case OMPC_num_teams:
    139   case OMPC_thread_limit:
    140   case OMPC_priority:
    141   case OMPC_grainsize:
    142   case OMPC_nogroup:
    143   case OMPC_num_tasks:
    144   case OMPC_hint:
    145     break;
    146   }
    147   llvm_unreachable("Invalid OpenMP simple clause kind");
    148 }
    149 
    150 const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
    151                                                  unsigned Type) {
    152   switch (Kind) {
    153   case OMPC_default:
    154     switch (Type) {
    155     case OMPC_DEFAULT_unknown:
    156       return "unknown";
    157 #define OPENMP_DEFAULT_KIND(Name)                                              \
    158   case OMPC_DEFAULT_##Name:                                                    \
    159     return #Name;
    160 #include "clang/Basic/OpenMPKinds.def"
    161     }
    162     llvm_unreachable("Invalid OpenMP 'default' clause type");
    163   case OMPC_proc_bind:
    164     switch (Type) {
    165     case OMPC_PROC_BIND_unknown:
    166       return "unknown";
    167 #define OPENMP_PROC_BIND_KIND(Name)                                            \
    168   case OMPC_PROC_BIND_##Name:                                                  \
    169     return #Name;
    170 #include "clang/Basic/OpenMPKinds.def"
    171     }
    172     llvm_unreachable("Invalid OpenMP 'proc_bind' clause type");
    173   case OMPC_schedule:
    174     switch (Type) {
    175     case OMPC_SCHEDULE_unknown:
    176       return "unknown";
    177 #define OPENMP_SCHEDULE_KIND(Name)                                             \
    178   case OMPC_SCHEDULE_##Name:                                                   \
    179     return #Name;
    180 #include "clang/Basic/OpenMPKinds.def"
    181     }
    182   case OMPC_depend:
    183     switch (Type) {
    184     case OMPC_DEPEND_unknown:
    185       return "unknown";
    186 #define OPENMP_DEPEND_KIND(Name)                                             \
    187   case OMPC_DEPEND_##Name:                                                   \
    188     return #Name;
    189 #include "clang/Basic/OpenMPKinds.def"
    190     }
    191     llvm_unreachable("Invalid OpenMP 'schedule' clause type");
    192   case OMPC_linear:
    193     switch (Type) {
    194     case OMPC_LINEAR_unknown:
    195       return "unknown";
    196 #define OPENMP_LINEAR_KIND(Name)                                             \
    197   case OMPC_LINEAR_##Name:                                                   \
    198     return #Name;
    199 #include "clang/Basic/OpenMPKinds.def"
    200     }
    201     llvm_unreachable("Invalid OpenMP 'linear' clause type");
    202   case OMPC_map:
    203     switch (Type) {
    204     case OMPC_MAP_unknown:
    205       return "unknown";
    206 #define OPENMP_MAP_KIND(Name)                                                \
    207   case OMPC_MAP_##Name:                                                      \
    208     return #Name;
    209 #include "clang/Basic/OpenMPKinds.def"
    210     default:
    211       break;
    212     }
    213     llvm_unreachable("Invalid OpenMP 'map' clause type");
    214   case OMPC_unknown:
    215   case OMPC_threadprivate:
    216   case OMPC_if:
    217   case OMPC_final:
    218   case OMPC_num_threads:
    219   case OMPC_safelen:
    220   case OMPC_simdlen:
    221   case OMPC_collapse:
    222   case OMPC_private:
    223   case OMPC_firstprivate:
    224   case OMPC_lastprivate:
    225   case OMPC_shared:
    226   case OMPC_reduction:
    227   case OMPC_aligned:
    228   case OMPC_copyin:
    229   case OMPC_copyprivate:
    230   case OMPC_ordered:
    231   case OMPC_nowait:
    232   case OMPC_untied:
    233   case OMPC_mergeable:
    234   case OMPC_flush:
    235   case OMPC_read:
    236   case OMPC_write:
    237   case OMPC_update:
    238   case OMPC_capture:
    239   case OMPC_seq_cst:
    240   case OMPC_device:
    241   case OMPC_threads:
    242   case OMPC_simd:
    243   case OMPC_num_teams:
    244   case OMPC_thread_limit:
    245   case OMPC_priority:
    246   case OMPC_grainsize:
    247   case OMPC_nogroup:
    248   case OMPC_num_tasks:
    249   case OMPC_hint:
    250     break;
    251   }
    252   llvm_unreachable("Invalid OpenMP simple clause kind");
    253 }
    254 
    255 bool clang::isAllowedClauseForDirective(OpenMPDirectiveKind DKind,
    256                                         OpenMPClauseKind CKind) {
    257   assert(DKind <= OMPD_unknown);
    258   assert(CKind <= OMPC_unknown);
    259   switch (DKind) {
    260   case OMPD_parallel:
    261     switch (CKind) {
    262 #define OPENMP_PARALLEL_CLAUSE(Name)                                           \
    263   case OMPC_##Name:                                                            \
    264     return true;
    265 #include "clang/Basic/OpenMPKinds.def"
    266     default:
    267       break;
    268     }
    269     break;
    270   case OMPD_simd:
    271     switch (CKind) {
    272 #define OPENMP_SIMD_CLAUSE(Name)                                               \
    273   case OMPC_##Name:                                                            \
    274     return true;
    275 #include "clang/Basic/OpenMPKinds.def"
    276     default:
    277       break;
    278     }
    279     break;
    280   case OMPD_for:
    281     switch (CKind) {
    282 #define OPENMP_FOR_CLAUSE(Name)                                                \
    283   case OMPC_##Name:                                                            \
    284     return true;
    285 #include "clang/Basic/OpenMPKinds.def"
    286     default:
    287       break;
    288     }
    289     break;
    290   case OMPD_for_simd:
    291     switch (CKind) {
    292 #define OPENMP_FOR_SIMD_CLAUSE(Name)                                           \
    293   case OMPC_##Name:                                                            \
    294     return true;
    295 #include "clang/Basic/OpenMPKinds.def"
    296     default:
    297       break;
    298     }
    299     break;
    300   case OMPD_sections:
    301     switch (CKind) {
    302 #define OPENMP_SECTIONS_CLAUSE(Name)                                           \
    303   case OMPC_##Name:                                                            \
    304     return true;
    305 #include "clang/Basic/OpenMPKinds.def"
    306     default:
    307       break;
    308     }
    309     break;
    310   case OMPD_single:
    311     switch (CKind) {
    312 #define OPENMP_SINGLE_CLAUSE(Name)                                             \
    313   case OMPC_##Name:                                                            \
    314     return true;
    315 #include "clang/Basic/OpenMPKinds.def"
    316     default:
    317       break;
    318     }
    319     break;
    320   case OMPD_parallel_for:
    321     switch (CKind) {
    322 #define OPENMP_PARALLEL_FOR_CLAUSE(Name)                                       \
    323   case OMPC_##Name:                                                            \
    324     return true;
    325 #include "clang/Basic/OpenMPKinds.def"
    326     default:
    327       break;
    328     }
    329     break;
    330   case OMPD_parallel_for_simd:
    331     switch (CKind) {
    332 #define OPENMP_PARALLEL_FOR_SIMD_CLAUSE(Name)                                  \
    333   case OMPC_##Name:                                                            \
    334     return true;
    335 #include "clang/Basic/OpenMPKinds.def"
    336     default:
    337       break;
    338     }
    339     break;
    340   case OMPD_parallel_sections:
    341     switch (CKind) {
    342 #define OPENMP_PARALLEL_SECTIONS_CLAUSE(Name)                                  \
    343   case OMPC_##Name:                                                            \
    344     return true;
    345 #include "clang/Basic/OpenMPKinds.def"
    346     default:
    347       break;
    348     }
    349     break;
    350   case OMPD_task:
    351     switch (CKind) {
    352 #define OPENMP_TASK_CLAUSE(Name)                                               \
    353   case OMPC_##Name:                                                            \
    354     return true;
    355 #include "clang/Basic/OpenMPKinds.def"
    356     default:
    357       break;
    358     }
    359     break;
    360   case OMPD_flush:
    361     return CKind == OMPC_flush;
    362     break;
    363   case OMPD_atomic:
    364     switch (CKind) {
    365 #define OPENMP_ATOMIC_CLAUSE(Name)                                             \
    366   case OMPC_##Name:                                                            \
    367     return true;
    368 #include "clang/Basic/OpenMPKinds.def"
    369     default:
    370       break;
    371     }
    372     break;
    373   case OMPD_target:
    374     switch (CKind) {
    375 #define OPENMP_TARGET_CLAUSE(Name)                                             \
    376   case OMPC_##Name:                                                            \
    377     return true;
    378 #include "clang/Basic/OpenMPKinds.def"
    379     default:
    380       break;
    381     }
    382     break;
    383   case OMPD_target_data:
    384     switch (CKind) {
    385 #define OPENMP_TARGET_DATA_CLAUSE(Name)                                        \
    386   case OMPC_##Name:                                                            \
    387     return true;
    388 #include "clang/Basic/OpenMPKinds.def"
    389     default:
    390       break;
    391     }
    392     break;
    393   case OMPD_teams:
    394     switch (CKind) {
    395 #define OPENMP_TEAMS_CLAUSE(Name)                                              \
    396   case OMPC_##Name:                                                            \
    397     return true;
    398 #include "clang/Basic/OpenMPKinds.def"
    399     default:
    400       break;
    401     }
    402     break;
    403   case OMPD_cancel:
    404     switch (CKind) {
    405 #define OPENMP_CANCEL_CLAUSE(Name)                                             \
    406   case OMPC_##Name:                                                            \
    407     return true;
    408 #include "clang/Basic/OpenMPKinds.def"
    409     default:
    410       break;
    411     }
    412     break;
    413   case OMPD_ordered:
    414     switch (CKind) {
    415 #define OPENMP_ORDERED_CLAUSE(Name)                                            \
    416   case OMPC_##Name:                                                            \
    417     return true;
    418 #include "clang/Basic/OpenMPKinds.def"
    419     default:
    420       break;
    421     }
    422     break;
    423   case OMPD_taskloop:
    424     switch (CKind) {
    425 #define OPENMP_TASKLOOP_CLAUSE(Name)                                           \
    426   case OMPC_##Name:                                                            \
    427     return true;
    428 #include "clang/Basic/OpenMPKinds.def"
    429     default:
    430       break;
    431     }
    432     break;
    433   case OMPD_taskloop_simd:
    434     switch (CKind) {
    435 #define OPENMP_TASKLOOP_SIMD_CLAUSE(Name)                                      \
    436   case OMPC_##Name:                                                            \
    437     return true;
    438 #include "clang/Basic/OpenMPKinds.def"
    439     default:
    440       break;
    441     }
    442     break;
    443   case OMPD_critical:
    444     switch (CKind) {
    445 #define OPENMP_CRITICAL_CLAUSE(Name)                                           \
    446   case OMPC_##Name:                                                            \
    447     return true;
    448 #include "clang/Basic/OpenMPKinds.def"
    449     default:
    450       break;
    451     }
    452     break;
    453   case OMPD_distribute:
    454     switch (CKind) {
    455 #define OPENMP_DISTRIBUTE_CLAUSE(Name)                                         \
    456   case OMPC_##Name:                                                            \
    457     return true;
    458 #include "clang/Basic/OpenMPKinds.def"
    459     default:
    460       break;
    461     }
    462     break;
    463   case OMPD_unknown:
    464   case OMPD_threadprivate:
    465   case OMPD_section:
    466   case OMPD_master:
    467   case OMPD_taskyield:
    468   case OMPD_barrier:
    469   case OMPD_taskwait:
    470   case OMPD_taskgroup:
    471   case OMPD_cancellation_point:
    472     break;
    473   }
    474   return false;
    475 }
    476 
    477 bool clang::isOpenMPLoopDirective(OpenMPDirectiveKind DKind) {
    478   return DKind == OMPD_simd || DKind == OMPD_for || DKind == OMPD_for_simd ||
    479          DKind == OMPD_parallel_for || DKind == OMPD_parallel_for_simd ||
    480          DKind == OMPD_taskloop ||
    481          DKind == OMPD_taskloop_simd ||
    482          DKind == OMPD_distribute; // TODO add next directives.
    483 }
    484 
    485 bool clang::isOpenMPWorksharingDirective(OpenMPDirectiveKind DKind) {
    486   return DKind == OMPD_for || DKind == OMPD_for_simd ||
    487          DKind == OMPD_sections || DKind == OMPD_section ||
    488          DKind == OMPD_single || DKind == OMPD_parallel_for ||
    489          DKind == OMPD_parallel_for_simd ||
    490          DKind == OMPD_parallel_sections; // TODO add next directives.
    491 }
    492 
    493 bool clang::isOpenMPTaskLoopDirective(OpenMPDirectiveKind DKind) {
    494   return DKind == OMPD_taskloop || DKind == OMPD_taskloop_simd;
    495 }
    496 
    497 bool clang::isOpenMPParallelDirective(OpenMPDirectiveKind DKind) {
    498   return DKind == OMPD_parallel || DKind == OMPD_parallel_for ||
    499          DKind == OMPD_parallel_for_simd ||
    500          DKind == OMPD_parallel_sections; // TODO add next directives.
    501 }
    502 
    503 bool clang::isOpenMPTargetDirective(OpenMPDirectiveKind DKind) {
    504   return DKind == OMPD_target; // TODO add next directives.
    505 }
    506 
    507 bool clang::isOpenMPTeamsDirective(OpenMPDirectiveKind DKind) {
    508   return DKind == OMPD_teams; // TODO add next directives.
    509 }
    510 
    511 bool clang::isOpenMPSimdDirective(OpenMPDirectiveKind DKind) {
    512   return DKind == OMPD_simd || DKind == OMPD_for_simd ||
    513          DKind == OMPD_parallel_for_simd ||
    514          DKind == OMPD_taskloop_simd; // TODO add next directives.
    515 }
    516 
    517 bool clang::isOpenMPDistributeDirective(OpenMPDirectiveKind Kind) {
    518   return Kind == OMPD_distribute; // TODO add next directives.
    519 }
    520 
    521 bool clang::isOpenMPPrivate(OpenMPClauseKind Kind) {
    522   return Kind == OMPC_private || Kind == OMPC_firstprivate ||
    523          Kind == OMPC_lastprivate || Kind == OMPC_linear ||
    524          Kind == OMPC_reduction; // TODO add next clauses like 'reduction'.
    525 }
    526 
    527 bool clang::isOpenMPThreadPrivate(OpenMPClauseKind Kind) {
    528   return Kind == OMPC_threadprivate || Kind == OMPC_copyin;
    529 }
    530 
    531