aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/text/AttributedString.java
blob: d689d87ac58d4fc7fd3ca6d6622315d1e1131e61 (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
/* AttributedString.java -- Models text with attributes
   Copyright (C) 1998, 1999 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.text;

import java.util.Iterator;
import java.util.Hashtable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
  * This class models a <code>String</code> with attributes over various
  * subranges of the string.  It allows applications to access this 
  * information via the <code>AttributedCharcterIterator</code> interface.
  *
  * @version 0.0
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  */
public class AttributedString
{

/*************************************************************************/

/*
 * Inner Classes
 */

/**
  * This class contains the attributes and ranges of text over which
  * that attributes apply.
  */
final class AttributeRange
{

/*
 * Instance Variables
 */

/**
  * A Map of the attributes
  */
Map attribs;

/**
  * The beginning index of the attributes
  */
int begin_index;

/**
  * The ending index of the attributes
  */
int end_index;

/*************************************************************************/

/*
 * Constructors
 */

AttributeRange(Map attribs, int begin_index, int end_index)
{
  this.attribs = attribs;
  this.begin_index = begin_index;
  this.end_index = end_index;
}

} // Inner class AttributeRange

/*************************************************************************/

/*
 * Instance Variables
 */

/**
  * This object holds the string we are representing.
  */
private StringCharacterIterator sci;

/**
  * This is the attribute information 
  */
private AttributeRange[] attribs;

/*************************************************************************/

/*
 * Constructors
 */

/**
  * This method initializes a new instance of <code>AttributedString</code>
  * that represents the specified <code>String</code> with no attributes.
  *
  * @param str The <code>String</code> to be attributed.
  */
public
AttributedString(String str)
{
  sci = new StringCharacterIterator(str);
  attribs = new AttributeRange[0];
}

/*************************************************************************/

/**
  * This method initializes a new instance of <code>AttributedString</code>
  * that represents that specified <code>String</code> with the specified
  * attributes over the entire length of the <code>String</code>.
  *
  * @param str The <code>String</code> to be attributed.
  * @param attributes The attribute list.
  */
public
AttributedString(String str, Map attributes)
{
  this(str);

  attribs = new AttributeRange[1];
  attribs[0] = new AttributeRange(attributes, 0, str.length());
}

/*************************************************************************/

/**
  * This method initializes a new instance of <code>AttributedString</code>
  * that will use the text and attribute information from the specified
  * <code>AttributedCharacterIterator</code>.
  *
  * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
  */
public
AttributedString(AttributedCharacterIterator aci)
{
  this(aci, aci.getBeginIndex(), aci.getEndIndex(), null);
}

/*************************************************************************/

/**
  * This method initializes a new instance of <code>AttributedString</code>
  * that will use the text and attribute information from the specified
  * subrange of the specified <code>AttributedCharacterIterator</code>.
  *
  * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
  * @param begin_index The beginning index of the text subrange.
  * @param end_index The ending index of the text subrange.
  */
public
AttributedString(AttributedCharacterIterator aci, int begin_index,
                 int end_index)
{
  this(aci, begin_index, end_index, null);
}

/*************************************************************************/

/**
  * This method initializes a new instance of <code>AttributedString</code>
  * that will use the text and attribute information from the specified
  * subrange of the specified <code>AttributedCharacterIterator</code>.
  * Only attributes from the source iterator that are present in the
  * specified array of attributes will be included in the attribute list
  * for this object.
  *
  * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
  * @param begin_index The beginning index of the text subrange.
  * @param end_index The ending index of the text subrange.
  * @param attributes A list of attributes to include from the iterator, or <code>null</code> to include all attributes.
  */
public
AttributedString(AttributedCharacterIterator aci, int begin_index, 
                 int end_index, AttributedCharacterIterator.Attribute[] attributes)
{
  // Validate some arguments
  if ((begin_index < 0) || (end_index < begin_index))
    throw new IllegalArgumentException("Bad index values");

  StringBuffer sb = new StringBuffer("");

  // Get the valid attribute list
  Set all_attribs = aci.getAllAttributeKeys();
  if (attributes != null)
    {
      Set valid_attribs = new HashSet();
      Iterator iter = all_attribs.iterator();
      while (iter.hasNext())
        {
          Object obj = iter.next();

          int i;
          for (i = 0; i < attributes.length; i++)
            if (obj.equals(attributes[0]))
              break;

          if (i == attributes.length)
            continue;

          valid_attribs.add(obj);
        }

      all_attribs = valid_attribs;
    } 

  // Loop through and extract the attributes
  char c = aci.setIndex(begin_index);

  do
    { 
      sb.append(c);

      Iterator iter = all_attribs.iterator();
      while(iter.hasNext())
        {
          Object obj = iter.next();

          // What should we do if this is not true?
          if (!(obj instanceof AttributedCharacterIterator.Attribute))
            continue;

          AttributedCharacterIterator.Attribute attrib = 
            (AttributedCharacterIterator.Attribute)obj;

          // Make sure the attribute is defined.
          int rl = aci.getRunLimit(attrib);
          if (rl == -1)
            continue;
          if (rl > end_index)
            rl = end_index;
          rl -= begin_index;

          // Check to see if we already processed this one
          int rs = aci.getRunStart(attrib);
          if ((rs < aci.getIndex()) && (aci.getIndex() != begin_index))
            continue;

          // If the attribute run starts before the beginning index, we
          // need to junk it if it is an Annotation.
          Object attrib_obj = aci.getAttribute(attrib);
          if (rs < begin_index)
            {
              if (attrib_obj instanceof Annotation)
                 continue;

              rs = begin_index;
            }
          else
            {
              rs -= begin_index;
            }

          // Create a map object.  Yes this will only contain one attribute
          Map new_map = new Hashtable();
          new_map.put(attrib, attrib_obj);

          // Add it to the attribute list.  Yes this is a bad way to do things.
          AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
          System.arraycopy(attribs, 0, new_list, 0, attribs.length);
          attribs = new_list;
          attribs[attribs.length - 1] = new AttributeRange(new_map, rs, rl);
        }

      c = aci.next();
    }
  while(c != CharacterIterator.DONE);

  sci = new StringCharacterIterator(sb.toString());
}

/*************************************************************************/

/*
 * Instance Methods
 */

/**
  * This method adds a new attribute that will cover the entire string.
  *
  * @param attrib The attribute to add.
  * @param value The value of the attribute.
  */
public void
addAttribute(AttributedCharacterIterator.Attribute attrib, Object value)
{
  addAttribute(attrib, value, 0, sci.getEndIndex() - 1);
}

/*************************************************************************/

/**
  * This method adds a new attribute that will cover the specified subrange
  * of the string.
  *
  * @param attrib The attribute to add.
  * @param value The value of the attribute.
  * @param begin_index The beginning index of the subrange.
  * @param end_index The ending index of the subrange.
  *
  * @exception IllegalArgumentException If attribute is <code>null</code> or the subrange is not valid.
  */
public void
addAttribute(AttributedCharacterIterator.Attribute attrib, Object value,
             int begin_index, int end_index)
{
  if (attrib == null)
    throw new IllegalArgumentException("null attribute");

  Hashtable ht = new Hashtable();
  ht.put(attrib, value);

  addAttributes(ht, begin_index, end_index);
}

/*************************************************************************/

/**
  * This method adds all of the attributes in the specified list to the
  * specified subrange of the string.
  *
  * @param attributes The list of attributes.
  * @param begin_index The beginning index.
  * @param end_index The ending index
  *
  * @param IllegalArgumentException If the list is <code>null</code> or the subrange is not valid.
  */
public void
addAttributes(Map attributes, int begin_index, int end_index)
{
  if (attributes == null)
    throw new IllegalArgumentException("null attribute");

  if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
      (end_index < begin_index))
    throw new IllegalArgumentException("bad range");

  AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
  System.arraycopy(attribs, 0, new_list, 0, attribs.length);
  attribs = new_list;
  attribs[attribs.length - 1] = new AttributeRange(attributes, begin_index, 
                                                   end_index);
} 

/*************************************************************************/

/**
  * This method returns an <code>AttributedCharacterIterator</code> that 
  * will iterate over the entire string.
  *
  * @return An <code>AttributedCharacterIterator</code> for the entire string.
  */
public AttributedCharacterIterator
getIterator()
{
  return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex() - 1,
                                      null));
}

/*************************************************************************/

/**
  * This method returns an <code>AttributedCharacterIterator</code> that
  * will iterate over the entire string.  This iterator will return information
  * about the list of attributes in the specified array.  Attributes not in
  * the array may or may not be returned by the iterator.  If the specified
  * array is <code>null</code>, all attributes will be returned.
  *
  * @param attributes A list of attributes to include in the returned iterator.
  *
  * @return An <code>AttributedCharacterIterator</code> for this string.
  */
public AttributedCharacterIterator
getIterator(AttributedCharacterIterator.Attribute[] attributes)
{
  return(getIterator(attributes, 0, sci.getEndIndex() - 1));
}

/*************************************************************************/

/**
  * This method returns an <code>AttributedCharacterIterator</code> that
  * will iterate over the specified subrange.  This iterator will return information
  * about the list of attributes in the specified array.  Attributes not in
  * the array may or may not be returned by the iterator.  If the specified
  * array is <code>null</code>, all attributes will be returned.  
  *
  * @param attributes A list of attributes to include in the returned iterator.
  * @param begin_index The beginning index of the subrange.
  * @param end_index The ending index of the subrange.
  *
  * @return An <code>AttributedCharacterIterator</code> for this string.
  */
public AttributedCharacterIterator
getIterator(AttributedCharacterIterator.Attribute[] attributes, 
            int begin_index, int end_index)
{
  if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
      (end_index < begin_index))
    throw new IllegalArgumentException("bad range");

  return(new AttributedStringIterator(sci, attribs, begin_index, end_index,
                                      attributes));
}

} // class AttributedString

/a> 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394
/* OpenMP directive matching and resolving.
   Copyright (C) 2005-2021 Free Software Foundation, Inc.
   Contributed by Jakub Jelinek

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "gfortran.h"
#include "arith.h"
#include "match.h"
#include "parse.h"
#include "diagnostic.h"
#include "gomp-constants.h"

/* Match an end of OpenMP directive.  End of OpenMP directive is optional
   whitespace, followed by '\n' or comment '!'.  */

static match
gfc_match_omp_eos (void)
{
  locus old_loc;
  char c;

  old_loc = gfc_current_locus;
  gfc_gobble_whitespace ();

  c = gfc_next_ascii_char ();
  switch (c)
    {
    case '!':
      do
	c = gfc_next_ascii_char ();
      while (c != '\n');
      /* Fall through */

    case '\n':
      return MATCH_YES;
    }

  gfc_current_locus = old_loc;
  return MATCH_NO;
}

match
gfc_match_omp_eos_error (void)
{
  if (gfc_match_omp_eos() == MATCH_YES)
    return MATCH_YES;

  gfc_error ("Unexpected junk at %C");
  return MATCH_ERROR;
}


/* Free an omp_clauses structure.  */

void
gfc_free_omp_clauses (gfc_omp_clauses *c)
{
  int i;
  if (c == NULL)
    return;

  gfc_free_expr (c->if_expr);
  gfc_free_expr (c->final_expr);
  gfc_free_expr (c->num_threads);
  gfc_free_expr (c->chunk_size);
  gfc_free_expr (c->safelen_expr);
  gfc_free_expr (c->simdlen_expr);
  gfc_free_expr (c->num_teams);
  gfc_free_expr (c->device);
  gfc_free_expr (c->thread_limit);
  gfc_free_expr (c->dist_chunk_size);
  gfc_free_expr (c->grainsize);
  gfc_free_expr (c->hint);
  gfc_free_expr (c->num_tasks);
  gfc_free_expr (c->priority);
  gfc_free_expr (c->detach);
  for (i = 0; i < OMP_IF_LAST; i++)
    gfc_free_expr (c->if_exprs[i]);
  gfc_free_expr (c->async_expr);
  gfc_free_expr (c->gang_num_expr);
  gfc_free_expr (c->gang_static_expr);
  gfc_free_expr (c->worker_expr);
  gfc_free_expr (c->vector_expr);
  gfc_free_expr (c->num_gangs_expr);
  gfc_free_expr (c->num_workers_expr);
  gfc_free_expr (c->vector_length_expr);
  for (i = 0; i < OMP_LIST_NUM; i++)
    gfc_free_omp_namelist (c->lists[i]);
  gfc_free_expr_list (c->wait_list);
  gfc_free_expr_list (c->tile_list);
  free (CONST_CAST (char *, c->critical_name));
  free (c);
}

/* Free oacc_declare structures.  */

void
gfc_free_oacc_declare_clauses (struct gfc_oacc_declare *oc)
{
  struct gfc_oacc_declare *decl = oc;

  do
    {
      struct gfc_oacc_declare *next;

      next = decl->next;
      gfc_free_omp_clauses (decl->clauses);
      free (decl);
      decl = next;
    }
  while (decl);
}

/* Free expression list. */
void
gfc_free_expr_list (gfc_expr_list *list)
{
  gfc_expr_list *n;

  for (; list; list = n)
    {
      n = list->next;
      free (list);
    }
}

/* Free an !$omp declare simd construct list.  */

void
gfc_free_omp_declare_simd (gfc_omp_declare_simd *ods)
{
  if (ods)
    {
      gfc_free_omp_clauses (ods->clauses);
      free (ods);
    }
}

void
gfc_free_omp_declare_simd_list (gfc_omp_declare_simd *list)
{
  while (list)
    {
      gfc_omp_declare_simd *current = list;
      list = list->next;
      gfc_free_omp_declare_simd (current);
    }
}

/* Free an !$omp declare reduction.  */

void
gfc_free_omp_udr (gfc_omp_udr *omp_udr)
{
  if (omp_udr)
    {
      gfc_free_omp_udr (omp_udr->next);
      gfc_free_namespace (omp_udr->combiner_ns);
      if (omp_udr->initializer_ns)
	gfc_free_namespace (omp_udr->initializer_ns);
      free (omp_udr);
    }
}


static gfc_omp_udr *
gfc_find_omp_udr (gfc_namespace *ns, const char *name, gfc_typespec *ts)
{
  gfc_symtree *st;

  if (ns == NULL)
    ns = gfc_current_ns;
  do
    {
      gfc_omp_udr *omp_udr;

      st = gfc_find_symtree (ns->omp_udr_root, name);
      if (st != NULL)
	{
	  for (omp_udr = st->n.omp_udr; omp_udr; omp_udr = omp_udr->next)
	    if (ts == NULL)
	      return omp_udr;
	    else if (gfc_compare_types (&omp_udr->ts, ts))
	      {
		if (ts->type == BT_CHARACTER)
		  {
		    if (omp_udr->ts.u.cl->length == NULL)
		      return omp_udr;
		    if (ts->u.cl->length == NULL)
		      continue;
		    if (gfc_compare_expr (omp_udr->ts.u.cl->length,
					  ts->u.cl->length,
					  INTRINSIC_EQ) != 0)
		      continue;
		  }
		return omp_udr;
	      }
	}

      /* Don't escape an interface block.  */
      if (ns && !ns->has_import_set
	  && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
	break;

      ns = ns->parent;
    }
  while (ns != NULL);

  return NULL;
}


/* Match a variable/common block list and construct a namelist from it.  */

static match
gfc_match_omp_variable_list (const char *str, gfc_omp_namelist **list,
			     bool allow_common, bool *end_colon = NULL,
			     gfc_omp_namelist ***headp = NULL,
			     bool allow_sections = false,
			     bool allow_derived = false)
{
  gfc_omp_namelist *head, *tail, *p;
  locus old_loc, cur_loc;
  char n[GFC_MAX_SYMBOL_LEN+1];
  gfc_symbol *sym;
  match m;
  gfc_symtree *st;

  head = tail = NULL;

  old_loc = gfc_current_locus;

  m = gfc_match (str);
  if (m != MATCH_YES)
    return m;

  for (;;)
    {
      cur_loc = gfc_current_locus;
      m = gfc_match_symbol (&sym, 1);
      switch (m)
	{
	case MATCH_YES:
	  gfc_expr *expr;
	  expr = NULL;
	  if ((allow_sections && gfc_peek_ascii_char () == '(')
	      || (allow_derived && gfc_peek_ascii_char () == '%'))
	    {
	      gfc_current_locus = cur_loc;
	      m = gfc_match_variable (&expr, 0);
	      switch (m)
		{
		case MATCH_ERROR:
		  goto cleanup;
		case MATCH_NO:
		  goto syntax;
		default:
		  break;
		}
	      if (gfc_is_coindexed (expr))
		{
		  gfc_error ("List item shall not be coindexed at %C");
		  goto cleanup;
		}
	    }
	  gfc_set_sym_referenced (sym);
	  p = gfc_get_omp_namelist ();
	  if (head == NULL)
	    head = tail = p;
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	    }
	  tail->sym = sym;
	  tail->expr = expr;
	  tail->where = cur_loc;
	  goto next_item;
	case MATCH_NO:
	  break;
	case MATCH_ERROR:
	  goto cleanup;
	}

      if (!allow_common)
	goto syntax;

      m = gfc_match (" / %n /", n);
      if (m == MATCH_ERROR)
	goto cleanup;
      if (m == MATCH_NO)
	goto syntax;

      st = gfc_find_symtree (gfc_current_ns->common_root, n);
      if (st == NULL)
	{
	  gfc_error ("COMMON block /%s/ not found at %C", n);
	  goto cleanup;
	}
      for (sym = st->n.common->head; sym; sym = sym->common_next)
	{
	  gfc_set_sym_referenced (sym);
	  p = gfc_get_omp_namelist ();
	  if (head == NULL)
	    head = tail = p;
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	    }
	  tail->sym = sym;
	  tail->where = cur_loc;
	}

    next_item:
      if (end_colon && gfc_match_char (':') == MATCH_YES)
	{
	  *end_colon = true;
	  break;
	}
      if (gfc_match_char (')') == MATCH_YES)
	break;
      if (gfc_match_char (',') != MATCH_YES)
	goto syntax;
    }

  while (*list)
    list = &(*list)->next;

  *list = head;
  if (headp)
    *headp = list;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in OpenMP variable list at %C");

cleanup:
  gfc_free_omp_namelist (head);
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}

/* Match a variable/procedure/common block list and construct a namelist
   from it.  */

static match
gfc_match_omp_to_link (const char *str, gfc_omp_namelist **list)
{
  gfc_omp_namelist *head, *tail, *p;
  locus old_loc, cur_loc;
  char n[GFC_MAX_SYMBOL_LEN+1];
  gfc_symbol *sym;
  match m;
  gfc_symtree *st;

  head = tail = NULL;

  old_loc = gfc_current_locus;

  m = gfc_match (str);
  if (m != MATCH_YES)
    return m;

  for (;;)
    {
      cur_loc = gfc_current_locus;
      m = gfc_match_symbol (&sym, 1);
      switch (m)
	{
	case MATCH_YES:
	  p = gfc_get_omp_namelist ();
	  if (head == NULL)
	    head = tail = p;
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	    }
	  tail->sym = sym;
	  tail->where = cur_loc;
	  goto next_item;
	case MATCH_NO:
	  break;
	case MATCH_ERROR:
	  goto cleanup;
	}

      m = gfc_match (" / %n /", n);
      if (m == MATCH_ERROR)
	goto cleanup;
      if (m == MATCH_NO)
	goto syntax;

      st = gfc_find_symtree (gfc_current_ns->common_root, n);
      if (st == NULL)
	{
	  gfc_error ("COMMON block /%s/ not found at %C", n);
	  goto cleanup;
	}
      p = gfc_get_omp_namelist ();
      if (head == NULL)
	head = tail = p;
      else
	{
	  tail->next = p;
	  tail = tail->next;
	}
      tail->u.common = st->n.common;
      tail->where = cur_loc;

    next_item:
      if (gfc_match_char (')') == MATCH_YES)
	break;
      if (gfc_match_char (',') != MATCH_YES)
	goto syntax;
    }

  while (*list)
    list = &(*list)->next;

  *list = head;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in OpenMP variable list at %C");

cleanup:
  gfc_free_omp_namelist (head);
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}

/* Match detach(event-handle).  */

static match
gfc_match_omp_detach (gfc_expr **expr)
{
  locus old_loc = gfc_current_locus;

  if (gfc_match ("detach ( ") != MATCH_YES)
    goto syntax_error;

  if (gfc_match_variable (expr, 0) != MATCH_YES)
    goto syntax_error;

  if ((*expr)->ts.type != BT_INTEGER || (*expr)->ts.kind != gfc_c_intptr_kind)
    {
      gfc_error ("%qs at %L should be of type "
		 "integer(kind=omp_event_handle_kind)",
		 (*expr)->symtree->n.sym->name, &(*expr)->where);
      return MATCH_ERROR;
    }

  if (gfc_match_char (')') != MATCH_YES)
    goto syntax_error;

  return MATCH_YES;

syntax_error:
   gfc_error ("Syntax error in OpenMP detach clause at %C");
   gfc_current_locus = old_loc;
   return MATCH_ERROR;

}

/* Match depend(sink : ...) construct a namelist from it.  */

static match
gfc_match_omp_depend_sink (gfc_omp_namelist **list)
{
  gfc_omp_namelist *head, *tail, *p;
  locus old_loc, cur_loc;
  gfc_symbol *sym;

  head = tail = NULL;

  old_loc = gfc_current_locus;

  for (;;)
    {
      cur_loc = gfc_current_locus;
      switch (gfc_match_symbol (&sym, 1))
	{
	case MATCH_YES:
	  gfc_set_sym_referenced (sym);
	  p = gfc_get_omp_namelist ();
	  if (head == NULL)
	    {
	      head = tail = p;
	      head->u.depend_op = OMP_DEPEND_SINK_FIRST;
	    }
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	      tail->u.depend_op = OMP_DEPEND_SINK;
	    }
	  tail->sym = sym;
	  tail->expr = NULL;
	  tail->where = cur_loc;
	  if (gfc_match_char ('+') == MATCH_YES)
	    {
	      if (gfc_match_literal_constant (&tail->expr, 0) != MATCH_YES)
		goto syntax;
	    }
	  else if (gfc_match_char ('-') == MATCH_YES)
	    {
	      if (gfc_match_literal_constant (&tail->expr, 0) != MATCH_YES)
		goto syntax;
	      tail->expr = gfc_uminus (tail->expr);
	    }
	  break;
	case MATCH_NO:
	  goto syntax;
	case MATCH_ERROR:
	  goto cleanup;
	}

      if (gfc_match_char (')') == MATCH_YES)
	break;
      if (gfc_match_char (',') != MATCH_YES)
	goto syntax;
    }

  while (*list)
    list = &(*list)->next;

  *list = head;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in OpenMP DEPEND SINK list at %C");

cleanup:
  gfc_free_omp_namelist (head);
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}

static match
match_oacc_expr_list (const char *str, gfc_expr_list **list,
		      bool allow_asterisk)
{
  gfc_expr_list *head, *tail, *p;
  locus old_loc;
  gfc_expr *expr;
  match m;

  head = tail = NULL;

  old_loc = gfc_current_locus;

  m = gfc_match (str);
  if (m != MATCH_YES)
    return m;

  for (;;)
    {
      m = gfc_match_expr (&expr);
      if (m == MATCH_YES || allow_asterisk)
	{
	  p = gfc_get_expr_list ();
	  if (head == NULL)
	    head = tail = p;
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	    }
	  if (m == MATCH_YES)
	    tail->expr = expr;
	  else if (gfc_match (" *") != MATCH_YES)
	    goto syntax;
	  goto next_item;
	}
      if (m == MATCH_ERROR)
	goto cleanup;
      goto syntax;

    next_item:
      if (gfc_match_char (')') == MATCH_YES)
	break;
      if (gfc_match_char (',') != MATCH_YES)
	goto syntax;
    }

  while (*list)
    list = &(*list)->next;

  *list = head;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in OpenACC expression list at %C");

cleanup:
  gfc_free_expr_list (head);
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}

static match
match_oacc_clause_gwv (gfc_omp_clauses *cp, unsigned gwv)
{
  match ret = MATCH_YES;

  if (gfc_match (" ( ") != MATCH_YES)
    return MATCH_NO;

  if (gwv == GOMP_DIM_GANG)
    {
        /* The gang clause accepts two optional arguments, num and static.
	 The num argument may either be explicit (num: <val>) or
	 implicit without (<val> without num:).  */

      while (ret == MATCH_YES)
	{
	  if (gfc_match (" static :") == MATCH_YES)
	    {
	      if (cp->gang_static)
		return MATCH_ERROR;
	      else
		cp->gang_static = true;
	      if (gfc_match_char ('*') == MATCH_YES)
		cp->gang_static_expr = NULL;
	      else if (gfc_match (" %e ", &cp->gang_static_expr) != MATCH_YES)
		return MATCH_ERROR;
	    }
	  else
	    {
	      if (cp->gang_num_expr)
		return MATCH_ERROR;

	      /* The 'num' argument is optional.  */
	      gfc_match (" num :");

	      if (gfc_match (" %e ", &cp->gang_num_expr) != MATCH_YES)
		return MATCH_ERROR;
	    }

	  ret = gfc_match (" , ");
	}
    }
  else if (gwv == GOMP_DIM_WORKER)
    {
      /* The 'num' argument is optional.  */
      gfc_match (" num :");

      if (gfc_match (" %e ", &cp->worker_expr) != MATCH_YES)
	return MATCH_ERROR;
    }
  else if (gwv == GOMP_DIM_VECTOR)
    {
      /* The 'length' argument is optional.  */
      gfc_match (" length :");

      if (gfc_match (" %e ", &cp->vector_expr) != MATCH_YES)
	return MATCH_ERROR;
    }
  else
    gfc_fatal_error ("Unexpected OpenACC parallelism.");

  return gfc_match (" )");
}

static match
gfc_match_oacc_clause_link (const char *str, gfc_omp_namelist **list)
{
  gfc_omp_namelist *head = NULL;
  gfc_omp_namelist *tail, *p;
  locus old_loc;
  char n[GFC_MAX_SYMBOL_LEN+1];
  gfc_symbol *sym;
  match m;
  gfc_symtree *st;

  old_loc = gfc_current_locus;

  m = gfc_match (str);
  if (m != MATCH_YES)
    return m;

  m = gfc_match (" (");

  for (;;)
    {
      m = gfc_match_symbol (&sym, 0);
      switch (m)
	{
	case MATCH_YES:
	  if (sym->attr.in_common)
	    {
	      gfc_error_now ("Variable at %C is an element of a COMMON block");
	      goto cleanup;
	    }
	  gfc_set_sym_referenced (sym);
	  p = gfc_get_omp_namelist ();
	  if (head == NULL)
	    head = tail = p;
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	    }
	  tail->sym = sym;
	  tail->expr = NULL;
	  tail->where = gfc_current_locus;
	  goto next_item;
	case MATCH_NO:
	  break;

	case MATCH_ERROR:
	  goto cleanup;
	}

      m = gfc_match (" / %n /", n);
      if (m == MATCH_ERROR)
	goto cleanup;
      if (m == MATCH_NO || n[0] == '\0')
	goto syntax;

      st = gfc_find_symtree (gfc_current_ns->common_root, n);
      if (st == NULL)
	{
	  gfc_error ("COMMON block /%s/ not found at %C", n);
	  goto cleanup;
	}

      for (sym = st->n.common->head; sym; sym = sym->common_next)
	{
	  gfc_set_sym_referenced (sym);
	  p = gfc_get_omp_namelist ();
	  if (head == NULL)
	    head = tail = p;
	  else
	    {
	      tail->next = p;
	      tail = tail->next;
	    }
	  tail->sym = sym;
	  tail->where = gfc_current_locus;
	}

    next_item:
      if (gfc_match_char (')') == MATCH_YES)
	break;
      if (gfc_match_char (',') != MATCH_YES)
	goto syntax;
    }

  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after !$ACC DECLARE at %C");
      goto cleanup;
    }

  while (*list)
    list = &(*list)->next;
  *list = head;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in !$ACC DECLARE list at %C");

cleanup:
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}

/* OpenMP clauses.  */
enum omp_mask1
{
  OMP_CLAUSE_PRIVATE,
  OMP_CLAUSE_FIRSTPRIVATE,
  OMP_CLAUSE_LASTPRIVATE,
  OMP_CLAUSE_COPYPRIVATE,
  OMP_CLAUSE_SHARED,
  OMP_CLAUSE_COPYIN,
  OMP_CLAUSE_REDUCTION,
  OMP_CLAUSE_IN_REDUCTION,
  OMP_CLAUSE_TASK_REDUCTION,
  OMP_CLAUSE_IF,
  OMP_CLAUSE_NUM_THREADS,
  OMP_CLAUSE_SCHEDULE,
  OMP_CLAUSE_DEFAULT,
  OMP_CLAUSE_ORDER,
  OMP_CLAUSE_ORDERED,
  OMP_CLAUSE_COLLAPSE,
  OMP_CLAUSE_UNTIED,
  OMP_CLAUSE_FINAL,
  OMP_CLAUSE_MERGEABLE,
  OMP_CLAUSE_ALIGNED,
  OMP_CLAUSE_DEPEND,
  OMP_CLAUSE_INBRANCH,
  OMP_CLAUSE_LINEAR,
  OMP_CLAUSE_NOTINBRANCH,
  OMP_CLAUSE_PROC_BIND,
  OMP_CLAUSE_SAFELEN,
  OMP_CLAUSE_SIMDLEN,
  OMP_CLAUSE_UNIFORM,
  OMP_CLAUSE_DEVICE,
  OMP_CLAUSE_MAP,
  OMP_CLAUSE_TO,
  OMP_CLAUSE_FROM,
  OMP_CLAUSE_NUM_TEAMS,
  OMP_CLAUSE_THREAD_LIMIT,
  OMP_CLAUSE_DIST_SCHEDULE,
  OMP_CLAUSE_DEFAULTMAP,
  OMP_CLAUSE_GRAINSIZE,
  OMP_CLAUSE_HINT,
  OMP_CLAUSE_IS_DEVICE_PTR,
  OMP_CLAUSE_LINK,
  OMP_CLAUSE_NOGROUP,
  OMP_CLAUSE_NOTEMPORAL,
  OMP_CLAUSE_NUM_TASKS,
  OMP_CLAUSE_PRIORITY,
  OMP_CLAUSE_SIMD,
  OMP_CLAUSE_THREADS,
  OMP_CLAUSE_USE_DEVICE_PTR,
  OMP_CLAUSE_USE_DEVICE_ADDR,  /* OpenMP 5.0.  */
  OMP_CLAUSE_DEVICE_TYPE,  /* OpenMP 5.0.  */
  OMP_CLAUSE_ATOMIC,  /* OpenMP 5.0.  */
  OMP_CLAUSE_CAPTURE,  /* OpenMP 5.0.  */
  OMP_CLAUSE_MEMORDER,  /* OpenMP 5.0.  */
  OMP_CLAUSE_DETACH,  /* OpenMP 5.0.  */
  OMP_CLAUSE_NOWAIT,
  /* This must come last.  */
  OMP_MASK1_LAST
};

/* OpenACC 2.0+ specific clauses. */
enum omp_mask2
{
  OMP_CLAUSE_ASYNC,
  OMP_CLAUSE_NUM_GANGS,
  OMP_CLAUSE_NUM_WORKERS,
  OMP_CLAUSE_VECTOR_LENGTH,
  OMP_CLAUSE_COPY,
  OMP_CLAUSE_COPYOUT,
  OMP_CLAUSE_CREATE,
  OMP_CLAUSE_NO_CREATE,
  OMP_CLAUSE_PRESENT,
  OMP_CLAUSE_DEVICEPTR,
  OMP_CLAUSE_GANG,
  OMP_CLAUSE_WORKER,
  OMP_CLAUSE_VECTOR,
  OMP_CLAUSE_SEQ,
  OMP_CLAUSE_INDEPENDENT,
  OMP_CLAUSE_USE_DEVICE,
  OMP_CLAUSE_DEVICE_RESIDENT,
  OMP_CLAUSE_HOST_SELF,
  OMP_CLAUSE_WAIT,
  OMP_CLAUSE_DELETE,
  OMP_CLAUSE_AUTO,
  OMP_CLAUSE_TILE,
  OMP_CLAUSE_IF_PRESENT,
  OMP_CLAUSE_FINALIZE,
  OMP_CLAUSE_ATTACH,
  /* This must come last.  */
  OMP_MASK2_LAST
};

struct omp_inv_mask;

/* Customized bitset for up to 128-bits.
   The two enums above provide bit numbers to use, and which of the
   two enums it is determines which of the two mask fields is used.
   Supported operations are defining a mask, like:
   #define XXX_CLAUSES \
     (omp_mask (OMP_CLAUSE_XXX) | OMP_CLAUSE_YYY | OMP_CLAUSE_ZZZ)
   oring such bitsets together or removing selected bits:
   (XXX_CLAUSES | YYY_CLAUSES) & ~(omp_mask (OMP_CLAUSE_VVV))
   and testing individual bits:
   if (mask & OMP_CLAUSE_UUU)  */

struct omp_mask {
  const uint64_t mask1;
  const uint64_t mask2;
  inline omp_mask ();
  inline omp_mask (omp_mask1);
  inline omp_mask (omp_mask2);
  inline omp_mask (uint64_t, uint64_t);
  inline omp_mask operator| (omp_mask1) const;
  inline omp_mask operator| (omp_mask2) const;
  inline omp_mask operator| (omp_mask) const;
  inline omp_mask operator& (const omp_inv_mask &) const;
  inline bool operator& (omp_mask1) const;
  inline bool operator& (omp_mask2) const;
  inline omp_inv_mask operator~ () const;
};

struct omp_inv_mask : public omp_mask {
  inline omp_inv_mask (const omp_mask &);
};

omp_mask::omp_mask () : mask1 (0), mask2 (0)
{
}

omp_mask::omp_mask (omp_mask1 m) : mask1 (((uint64_t) 1) << m), mask2 (0)
{
}

omp_mask::omp_mask (omp_mask2 m) : mask1 (0), mask2 (((uint64_t) 1) << m)
{
}

omp_mask::omp_mask (uint64_t m1, uint64_t m2) : mask1 (m1), mask2 (m2)
{
}

omp_mask
omp_mask::operator| (omp_mask1 m) const
{
  return omp_mask (mask1 | (((uint64_t) 1) << m), mask2);
}

omp_mask
omp_mask::operator| (omp_mask2 m) const
{
  return omp_mask (mask1, mask2 | (((uint64_t) 1) << m));
}

omp_mask
omp_mask::operator| (omp_mask m) const
{
  return omp_mask (mask1 | m.mask1, mask2 | m.mask2);
}

omp_mask
omp_mask::operator& (const omp_inv_mask &m) const
{
  return omp_mask (mask1 & ~m.mask1, mask2 & ~m.mask2);
}

bool
omp_mask::operator& (omp_mask1 m) const
{
  return (mask1 & (((uint64_t) 1) << m)) != 0;
}

bool
omp_mask::operator& (omp_mask2 m) const
{
  return (mask2 & (((uint64_t) 1) << m)) != 0;
}

omp_inv_mask
omp_mask::operator~ () const
{
  return omp_inv_mask (*this);
}

omp_inv_mask::omp_inv_mask (const omp_mask &m) : omp_mask (m)
{
}

/* Helper function for OpenACC and OpenMP clauses involving memory
   mapping.  */

static bool
gfc_match_omp_map_clause (gfc_omp_namelist **list, gfc_omp_map_op map_op,
			  bool allow_common, bool allow_derived)
{
  gfc_omp_namelist **head = NULL;
  if (gfc_match_omp_variable_list ("", list, allow_common, NULL, &head, true,
				   allow_derived)
      == MATCH_YES)
    {
      gfc_omp_namelist *n;
      for (n = *head; n; n = n->next)
	n->u.map_op = map_op;
      return true;
    }

  return false;
}

/* reduction ( reduction-modifier, reduction-operator : variable-list )
   in_reduction ( reduction-operator : variable-list )
   task_reduction ( reduction-operator : variable-list )  */

static match
gfc_match_omp_clause_reduction (char pc, gfc_omp_clauses *c, bool openacc,
				bool allow_derived)
{
  if (pc == 'r' && gfc_match ("reduction ( ") != MATCH_YES)
    return MATCH_NO;
  else if (pc == 'i' && gfc_match ("in_reduction ( ") != MATCH_YES)
    return MATCH_NO;
  else if (pc == 't' && gfc_match ("task_reduction ( ") != MATCH_YES)
    return MATCH_NO;

  locus old_loc = gfc_current_locus;
  int list_idx = 0;

  if (pc == 'r' && !openacc)
    {
      if (gfc_match ("inscan") == MATCH_YES)
	list_idx = OMP_LIST_REDUCTION_INSCAN;
      else if (gfc_match ("task") == MATCH_YES)
	list_idx = OMP_LIST_REDUCTION_TASK;
      else if (gfc_match ("default") == MATCH_YES)
	list_idx = OMP_LIST_REDUCTION;
      if (list_idx != 0 && gfc_match (", ") != MATCH_YES)
	{
	  gfc_error ("Comma expected at %C");
	  gfc_current_locus = old_loc;
	  return MATCH_NO;
	}
      if (list_idx == 0)
	list_idx = OMP_LIST_REDUCTION;
    }
  else if (pc == 'i')
    list_idx = OMP_LIST_IN_REDUCTION;
  else if (pc == 't')
    list_idx = OMP_LIST_TASK_REDUCTION;
  else
    list_idx = OMP_LIST_REDUCTION;

  gfc_omp_reduction_op rop = OMP_REDUCTION_NONE;
  char buffer[GFC_MAX_SYMBOL_LEN + 3];
  if (gfc_match_char ('+') == MATCH_YES)
    rop = OMP_REDUCTION_PLUS;
  else if (gfc_match_char ('*') == MATCH_YES)
    rop = OMP_REDUCTION_TIMES;
  else if (gfc_match_char ('-') == MATCH_YES)
    rop = OMP_REDUCTION_MINUS;
  else if (gfc_match (".and.") == MATCH_YES)
    rop = OMP_REDUCTION_AND;
  else if (gfc_match (".or.") == MATCH_YES)
    rop = OMP_REDUCTION_OR;
  else if (gfc_match (".eqv.") == MATCH_YES)
    rop = OMP_REDUCTION_EQV;
  else if (gfc_match (".neqv.") == MATCH_YES)
    rop = OMP_REDUCTION_NEQV;
  if (rop != OMP_REDUCTION_NONE)
    snprintf (buffer, sizeof buffer, "operator %s",
	      gfc_op2string ((gfc_intrinsic_op) rop));
  else if (gfc_match_defined_op_name (buffer + 1, 1) == MATCH_YES)
    {
      buffer[0] = '.';
      strcat (buffer, ".");
    }
  else if (gfc_match_name (buffer) == MATCH_YES)
    {
      gfc_symbol *sym;
      const char *n = buffer;

      gfc_find_symbol (buffer, NULL, 1, &sym);
      if (sym != NULL)
	{
	  if (sym->attr.intrinsic)
	    n = sym->name;
	  else if ((sym->attr.flavor != FL_UNKNOWN
		    && sym->attr.flavor != FL_PROCEDURE)
		   || sym->attr.external
		   || sym->attr.generic
		   || sym->attr.entry
		   || sym->attr.result
		   || sym->attr.dummy
		   || sym->attr.subroutine
		   || sym->attr.pointer
		   || sym->attr.target
		   || sym->attr.cray_pointer
		   || sym->attr.cray_pointee
		   || (sym->attr.proc != PROC_UNKNOWN
		       && sym->attr.proc != PROC_INTRINSIC)
		   || sym->attr.if_source != IFSRC_UNKNOWN
		   || sym == sym->ns->proc_name)
		{
		  sym = NULL;
		  n = NULL;
		}
	      else
		n = sym->name;
	    }
	  if (n == NULL)
	    rop = OMP_REDUCTION_NONE;
	  else if (strcmp (n, "max") == 0)
	    rop = OMP_REDUCTION_MAX;
	  else if (strcmp (n, "min") == 0)
	    rop = OMP_REDUCTION_MIN;
	  else if (strcmp (n, "iand") == 0)
	    rop = OMP_REDUCTION_IAND;
	  else if (strcmp (n, "ior") == 0)
	    rop = OMP_REDUCTION_IOR;
	  else if (strcmp (n, "ieor") == 0)
	    rop = OMP_REDUCTION_IEOR;
	  if (rop != OMP_REDUCTION_NONE
	      && sym != NULL
	      && ! sym->attr.intrinsic
	      && ! sym->attr.use_assoc
	      && ((sym->attr.flavor == FL_UNKNOWN
		   && !gfc_add_flavor (&sym->attr, FL_PROCEDURE,
					      sym->name, NULL))
		  || !gfc_add_intrinsic (&sym->attr, NULL)))
	    rop = OMP_REDUCTION_NONE;
    }
  else
    buffer[0] = '\0';
  gfc_omp_udr *udr = (buffer[0] ? gfc_find_omp_udr (gfc_current_ns, buffer, NULL)
				: NULL);
  gfc_omp_namelist **head = NULL;
  if (rop == OMP_REDUCTION_NONE && udr)
    rop = OMP_REDUCTION_USER;

  if (gfc_match_omp_variable_list (" :", &c->lists[list_idx], false, NULL,
				   &head, openacc, allow_derived) != MATCH_YES)
    {
      gfc_current_locus = old_loc;
      return MATCH_NO;
    }
  gfc_omp_namelist *n;
  if (rop == OMP_REDUCTION_NONE)
    {
      n = *head;
      *head = NULL;
      gfc_error_now ("!$OMP DECLARE REDUCTION %s not found at %L",
		     buffer, &old_loc);
      gfc_free_omp_namelist (n);
    }
  else
    for (n = *head; n; n = n->next)
      {
	n->u.reduction_op = rop;
	if (udr)
	  {
	    n->udr = gfc_get_omp_namelist_udr ();
	    n->udr->udr = udr;
	  }
     }
  return MATCH_YES;
}

/* Match OpenMP and OpenACC directive clauses. MASK is a bitmask of
   clauses that are allowed for a particular directive.  */

static match
gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
		       bool first = true, bool needs_space = true,
		       bool openacc = false)
{
  gfc_omp_clauses *c = gfc_get_omp_clauses ();
  locus old_loc;
  /* Determine whether we're dealing with an OpenACC directive that permits
     derived type member accesses.  This in particular disallows
     "!$acc declare" from using such accesses, because it's not clear if/how
     that should work.  */
  bool allow_derived = (openacc
			&& ((mask & OMP_CLAUSE_ATTACH)
			    || (mask & OMP_CLAUSE_DETACH)
			    || (mask & OMP_CLAUSE_HOST_SELF)));

  gcc_checking_assert (OMP_MASK1_LAST <= 64 && OMP_MASK2_LAST <= 64);
  *cp = NULL;
  while (1)
    {
      if ((first || gfc_match_char (',') != MATCH_YES)
	  && (needs_space && gfc_match_space () != MATCH_YES))
	break;
      needs_space = false;
      first = false;
      gfc_gobble_whitespace ();
      bool end_colon;
      gfc_omp_namelist **head;
      old_loc = gfc_current_locus;
      char pc = gfc_peek_ascii_char ();
      switch (pc)
	{
	case 'a':
	  end_colon = false;
	  head = NULL;
	  if ((mask & OMP_CLAUSE_ALIGNED)
	      && gfc_match_omp_variable_list ("aligned (",
					      &c->lists[OMP_LIST_ALIGNED],
					      false, &end_colon,
					      &head) == MATCH_YES)
	    {
	      gfc_expr *alignment = NULL;
	      gfc_omp_namelist *n;

	      if (end_colon && gfc_match (" %e )", &alignment) != MATCH_YES)
		{
		  gfc_free_omp_namelist (*head);
		  gfc_current_locus = old_loc;
		  *head = NULL;
		  break;
		}
	      for (n = *head; n; n = n->next)
		if (n->next && alignment)
		  n->expr = gfc_copy_expr (alignment);
		else
		  n->expr = alignment;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("acq_rel") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_ACQ_REL;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("acquire") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_ACQUIRE;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_ASYNC)
	      && !c->async
	      && gfc_match ("async") == MATCH_YES)
	    {
	      c->async = true;
	      match m = gfc_match (" ( %e )", &c->async_expr);
	      if (m == MATCH_ERROR)
		{
		  gfc_current_locus = old_loc;
		  break;
		}
	      else if (m == MATCH_NO)
		{
		  c->async_expr
		    = gfc_get_constant_expr (BT_INTEGER,
					     gfc_default_integer_kind,
					     &gfc_current_locus);
		  mpz_set_si (c->async_expr->value.integer, GOMP_ASYNC_NOVAL);
		  needs_space = true;
		}
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_AUTO)
	      && !c->par_auto
	      && gfc_match ("auto") == MATCH_YES)
	    {
	      c->par_auto = true;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_ATTACH)
	      && gfc_match ("attach ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_ATTACH, false,
					   allow_derived))
	    continue;
	  break;
	case 'c':
	  if ((mask & OMP_CLAUSE_CAPTURE)
	      && !c->capture
	      && gfc_match ("capture") == MATCH_YES)
	    {
	      c->capture = true;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_COLLAPSE)
	      && !c->collapse)
	    {
	      gfc_expr *cexpr = NULL;
	      match m = gfc_match ("collapse ( %e )", &cexpr);

	      if (m == MATCH_YES)
		{
		  int collapse;
		  if (gfc_extract_int (cexpr, &collapse, -1))
		    collapse = 1;
		  else if (collapse <= 0)
		    {
		      gfc_error_now ("COLLAPSE clause argument not"
				     " constant positive integer at %C");
		      collapse = 1;
		    }
		  c->collapse = collapse;
		  gfc_free_expr (cexpr);
		  continue;
		}
	    }
	  if ((mask & OMP_CLAUSE_COPY)
	      && gfc_match ("copy ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_TOFROM, true,
					   allow_derived))
	    continue;
	  if (mask & OMP_CLAUSE_COPYIN)
	    {
	      if (openacc)
		{
		  if (gfc_match ("copyin ( ") == MATCH_YES
		      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
						   OMP_MAP_TO, true,
						   allow_derived))
		    continue;
		}
	      else if (gfc_match_omp_variable_list ("copyin (",
						    &c->lists[OMP_LIST_COPYIN],
						    true) == MATCH_YES)
		continue;
	    }
	  if ((mask & OMP_CLAUSE_COPYOUT)
	      && gfc_match ("copyout ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FROM, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_COPYPRIVATE)
	      && gfc_match_omp_variable_list ("copyprivate (",
					      &c->lists[OMP_LIST_COPYPRIVATE],
					      true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_CREATE)
	      && gfc_match ("create ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_ALLOC, true, allow_derived))
	    continue;
	  break;
	case 'd':
	  if ((mask & OMP_CLAUSE_DEFAULT)
	      && c->default_sharing == OMP_DEFAULT_UNKNOWN)
	    {
	      if (gfc_match ("default ( none )") == MATCH_YES)
		c->default_sharing = OMP_DEFAULT_NONE;
	      else if (openacc)
		{
		  if (gfc_match ("default ( present )") == MATCH_YES)
		    c->default_sharing = OMP_DEFAULT_PRESENT;
		}
	      else
		{
		  if (gfc_match ("default ( firstprivate )") == MATCH_YES)
		    c->default_sharing = OMP_DEFAULT_FIRSTPRIVATE;
		  else if (gfc_match ("default ( private )") == MATCH_YES)
		    c->default_sharing = OMP_DEFAULT_PRIVATE;
		  else if (gfc_match ("default ( shared )") == MATCH_YES)
		    c->default_sharing = OMP_DEFAULT_SHARED;
		}
	      if (c->default_sharing != OMP_DEFAULT_UNKNOWN)
		continue;
	    }
	  if ((mask & OMP_CLAUSE_DEFAULTMAP)
	      && !c->defaultmap
	      && gfc_match ("defaultmap ( tofrom : scalar )") == MATCH_YES)
	    {
	      c->defaultmap = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_DELETE)
	      && gfc_match ("delete ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_RELEASE, true,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_DEPEND)
	      && gfc_match ("depend ( ") == MATCH_YES)
	    {
	      match m = MATCH_YES;
	      gfc_omp_depend_op depend_op = OMP_DEPEND_OUT;
	      if (gfc_match ("inout") == MATCH_YES)
		depend_op = OMP_DEPEND_INOUT;
	      else if (gfc_match ("in") == MATCH_YES)
		depend_op = OMP_DEPEND_IN;
	      else if (gfc_match ("out") == MATCH_YES)
		depend_op = OMP_DEPEND_OUT;
	      else if (!c->depend_source
		       && gfc_match ("source )") == MATCH_YES)
		{
		  c->depend_source = true;
		  continue;
		}
	      else if (gfc_match ("sink : ") == MATCH_YES)
		{
		  if (gfc_match_omp_depend_sink (&c->lists[OMP_LIST_DEPEND])
		      == MATCH_YES)
		    continue;
		  m = MATCH_NO;
		}
	      else
		m = MATCH_NO;
	      head = NULL;
	      if (m == MATCH_YES
		  && gfc_match_omp_variable_list (" : ",
						  &c->lists[OMP_LIST_DEPEND],
						  false, NULL, &head,
						  true) == MATCH_YES)
		{
		  gfc_omp_namelist *n;
		  for (n = *head; n; n = n->next)
		    n->u.depend_op = depend_op;
		  continue;
		}
	      else
		gfc_current_locus = old_loc;
	    }
	  if ((mask & OMP_CLAUSE_DETACH)
	      && !openacc
	      && !c->detach
	      && gfc_match_omp_detach (&c->detach) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_DETACH)
	      && openacc
	      && gfc_match ("detach ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_DETACH, false,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_DEVICE)
	      && !openacc
	      && c->device == NULL
	      && gfc_match ("device ( %e )", &c->device) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_DEVICE)
	      && openacc
	      && gfc_match ("device ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FORCE_TO, true,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_DEVICEPTR)
	      && gfc_match ("deviceptr ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FORCE_DEVICEPTR, false,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_DEVICE_TYPE)
	      && gfc_match ("device_type ( ") == MATCH_YES)
	    {
	      if (gfc_match ("host") == MATCH_YES)
		c->device_type = OMP_DEVICE_TYPE_HOST;
	      else if (gfc_match ("nohost") == MATCH_YES)
		c->device_type = OMP_DEVICE_TYPE_NOHOST;
	      else if (gfc_match ("any") == MATCH_YES)
		c->device_type = OMP_DEVICE_TYPE_ANY;
	      else
		{
		  gfc_error ("Expected HOST, NOHOST or ANY at %C");
		  break;
		}
	      if (gfc_match (" )") != MATCH_YES)
		break;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_DEVICE_RESIDENT)
	      && gfc_match_omp_variable_list
		   ("device_resident (",
		    &c->lists[OMP_LIST_DEVICE_RESIDENT], true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_DIST_SCHEDULE)
	      && c->dist_sched_kind == OMP_SCHED_NONE
	      && gfc_match ("dist_schedule ( static") == MATCH_YES)
	    {
	      match m = MATCH_NO;
	      c->dist_sched_kind = OMP_SCHED_STATIC;
	      m = gfc_match (" , %e )", &c->dist_chunk_size);
	      if (m != MATCH_YES)
		m = gfc_match_char (')');
	      if (m != MATCH_YES)
		{
		  c->dist_sched_kind = OMP_SCHED_NONE;
		  gfc_current_locus = old_loc;
		}
	      else
		continue;
	    }
	  break;
	case 'f':
	  if ((mask & OMP_CLAUSE_FINAL)
	      && c->final_expr == NULL
	      && gfc_match ("final ( %e )", &c->final_expr) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_FINALIZE)
	      && !c->finalize
	      && gfc_match ("finalize") == MATCH_YES)
	    {
	      c->finalize = true;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_FIRSTPRIVATE)
	      && gfc_match_omp_variable_list ("firstprivate (",
					      &c->lists[OMP_LIST_FIRSTPRIVATE],
					      true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_FROM)
	      && gfc_match_omp_variable_list ("from (",
					      &c->lists[OMP_LIST_FROM], false,
					      NULL, &head, true) == MATCH_YES)
	    continue;
	  break;
	case 'g':
	  if ((mask & OMP_CLAUSE_GANG)
	      && !c->gang
	      && gfc_match ("gang") == MATCH_YES)
	    {
	      c->gang = true;
	      match m = match_oacc_clause_gwv (c, GOMP_DIM_GANG);
	      if (m == MATCH_ERROR)
		{
		  gfc_current_locus = old_loc;
		  break;
		}
	      else if (m == MATCH_NO)
		needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_GRAINSIZE)
	      && c->grainsize == NULL
	      && gfc_match ("grainsize ( %e )", &c->grainsize) == MATCH_YES)
	    continue;
	  break;
	case 'h':
	  if ((mask & OMP_CLAUSE_HINT)
	      && c->hint == NULL
	      && gfc_match ("hint ( %e )", &c->hint) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_HOST_SELF)
	      && gfc_match ("host ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FORCE_FROM, true,
					   allow_derived))
	    continue;
	  break;
	case 'i':
	  if ((mask & OMP_CLAUSE_IF)
	      && c->if_expr == NULL
	      && gfc_match ("if ( ") == MATCH_YES)
	    {
	      if (!openacc)
		{
		  /* This should match the enum gfc_omp_if_kind order.  */
		  static const char *ifs[OMP_IF_LAST] = {
		    " cancel : %e )",
		    " parallel : %e )",
		    " simd : %e )",
		    " task : %e )",
		    " taskloop : %e )",
		    " target : %e )",
		    " target data : %e )",
		    " target update : %e )",
		    " target enter data : %e )",
		    " target exit data : %e )" };
		  int i;
		  for (i = 0; i < OMP_IF_LAST; i++)
		    if (c->if_exprs[i] == NULL
			&& gfc_match (ifs[i], &c->if_exprs[i]) == MATCH_YES)
		      break;
		  if (i < OMP_IF_LAST)
		    continue;
		}
	      if (gfc_match ("%e )", &c->if_expr) == MATCH_YES)
		continue;
	      gfc_current_locus = old_loc;
	    }
	  if ((mask & OMP_CLAUSE_IF_PRESENT)
	      && !c->if_present
	      && gfc_match ("if_present") == MATCH_YES)
	    {
	      c->if_present = true;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_IN_REDUCTION)
	      && gfc_match_omp_clause_reduction (pc, c, openacc,
						 allow_derived) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_INBRANCH)
	      && !c->inbranch
	      && !c->notinbranch
	      && gfc_match ("inbranch") == MATCH_YES)
	    {
	      c->inbranch = needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_INDEPENDENT)
	      && !c->independent
	      && gfc_match ("independent") == MATCH_YES)
	    {
	      c->independent = true;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_IS_DEVICE_PTR)
	      && gfc_match_omp_variable_list
		   ("is_device_ptr (",
		    &c->lists[OMP_LIST_IS_DEVICE_PTR], false) == MATCH_YES)
	    continue;
	  break;
	case 'l':
	  if ((mask & OMP_CLAUSE_LASTPRIVATE)
	      && gfc_match ("lastprivate ( ") == MATCH_YES)
	    {
	      bool conditional = gfc_match ("conditional : ") == MATCH_YES;
	      head = NULL;
	      if (gfc_match_omp_variable_list ("",
					       &c->lists[OMP_LIST_LASTPRIVATE],
					       false, NULL, &head) == MATCH_YES)
		{
		  gfc_omp_namelist *n;
		  for (n = *head; n; n = n->next)
		    n->u.lastprivate_conditional = conditional;
		  continue;
		}
	      gfc_current_locus = old_loc;
	      break;
	    }
	  end_colon = false;
	  head = NULL;
	  if ((mask & OMP_CLAUSE_LINEAR)
	      && gfc_match ("linear (") == MATCH_YES)
	    {
	      gfc_omp_linear_op linear_op = OMP_LINEAR_DEFAULT;
	      gfc_expr *step = NULL;

	      if (gfc_match_omp_variable_list (" ref (",
					       &c->lists[OMP_LIST_LINEAR],
					       false, NULL, &head)
		  == MATCH_YES)
		linear_op = OMP_LINEAR_REF;
	      else if (gfc_match_omp_variable_list (" val (",
						    &c->lists[OMP_LIST_LINEAR],
						    false, NULL, &head)
		       == MATCH_YES)
		linear_op = OMP_LINEAR_VAL;
	      else if (gfc_match_omp_variable_list (" uval (",
						    &c->lists[OMP_LIST_LINEAR],
						    false, NULL, &head)
		       == MATCH_YES)
		linear_op = OMP_LINEAR_UVAL;
	      else if (gfc_match_omp_variable_list ("",
						    &c->lists[OMP_LIST_LINEAR],
						    false, &end_colon, &head)
		       == MATCH_YES)
		linear_op = OMP_LINEAR_DEFAULT;
	      else
		{
		  gfc_current_locus = old_loc;
		  break;
		}
	      if (linear_op != OMP_LINEAR_DEFAULT)
		{
		  if (gfc_match (" :") == MATCH_YES)
		    end_colon = true;
		  else if (gfc_match (" )") != MATCH_YES)
		    {
		      gfc_free_omp_namelist (*head);
		      gfc_current_locus = old_loc;
		      *head = NULL;
		      break;
		    }
		}
	      if (end_colon && gfc_match (" %e )", &step) != MATCH_YES)
		{
		  gfc_free_omp_namelist (*head);
		  gfc_current_locus = old_loc;
		  *head = NULL;
		  break;
		}
	      else if (!end_colon)
		{
		  step = gfc_get_constant_expr (BT_INTEGER,
						gfc_default_integer_kind,
						&old_loc);
		  mpz_set_si (step->value.integer, 1);
		}
	      (*head)->expr = step;
	      if (linear_op != OMP_LINEAR_DEFAULT)
		for (gfc_omp_namelist *n = *head; n; n = n->next)
		  n->u.linear_op = linear_op;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_LINK)
	      && openacc
	      && (gfc_match_oacc_clause_link ("link (",
					      &c->lists[OMP_LIST_LINK])
		  == MATCH_YES))
	    continue;
	  else if ((mask & OMP_CLAUSE_LINK)
		   && !openacc
		   && (gfc_match_omp_to_link ("link (",
					      &c->lists[OMP_LIST_LINK])
		       == MATCH_YES))
	    continue;
	  break;
	case 'm':
	  if ((mask & OMP_CLAUSE_MAP)
	      && gfc_match ("map ( ") == MATCH_YES)
	    {
	      locus old_loc2 = gfc_current_locus;
	      bool always = false;
	      gfc_omp_map_op map_op = OMP_MAP_TOFROM;
	      if (gfc_match ("always , ") == MATCH_YES)
		always = true;
	      if (gfc_match ("alloc : ") == MATCH_YES)
		map_op = OMP_MAP_ALLOC;
	      else if (gfc_match ("tofrom : ") == MATCH_YES)
		map_op = always ? OMP_MAP_ALWAYS_TOFROM : OMP_MAP_TOFROM;
	      else if (gfc_match ("to : ") == MATCH_YES)
		map_op = always ? OMP_MAP_ALWAYS_TO : OMP_MAP_TO;
	      else if (gfc_match ("from : ") == MATCH_YES)
		map_op = always ? OMP_MAP_ALWAYS_FROM : OMP_MAP_FROM;
	      else if (gfc_match ("release : ") == MATCH_YES)
		map_op = OMP_MAP_RELEASE;
	      else if (gfc_match ("delete : ") == MATCH_YES)
		map_op = OMP_MAP_DELETE;
	      else if (always)
		{
		  gfc_current_locus = old_loc2;
		  always = false;
		}
	      head = NULL;
	      if (gfc_match_omp_variable_list ("", &c->lists[OMP_LIST_MAP],
					       false, NULL, &head,
					       true, true) == MATCH_YES)
		{
		  gfc_omp_namelist *n;
		  for (n = *head; n; n = n->next)
		    n->u.map_op = map_op;
		  continue;
		}
	      else
		gfc_current_locus = old_loc;
	    }
	  if ((mask & OMP_CLAUSE_MERGEABLE) && !c->mergeable
	      && gfc_match ("mergeable") == MATCH_YES)
	    {
	      c->mergeable = needs_space = true;
	      continue;
	    }
	  break;
	case 'n':
	  if ((mask & OMP_CLAUSE_NO_CREATE)
	      && gfc_match ("no_create ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_IF_PRESENT, true,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_NOGROUP)
	      && !c->nogroup
	      && gfc_match ("nogroup") == MATCH_YES)
	    {
	      c->nogroup = needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_NOTEMPORAL)
	      && gfc_match_omp_variable_list ("nontemporal (",
					      &c->lists[OMP_LIST_NONTEMPORAL],
					      true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_NOTINBRANCH)
	      && !c->notinbranch
	      && !c->inbranch
	      && gfc_match ("notinbranch") == MATCH_YES)
	    {
	      c->notinbranch = needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_NOWAIT)
	      && !c->nowait
	      && gfc_match ("nowait") == MATCH_YES)
	    {
	      c->nowait = needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_NUM_GANGS)
	      && c->num_gangs_expr == NULL
	      && gfc_match ("num_gangs ( %e )",
			    &c->num_gangs_expr) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_NUM_TASKS)
	      && c->num_tasks == NULL
	      && gfc_match ("num_tasks ( %e )", &c->num_tasks) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_NUM_TEAMS)
	      && c->num_teams == NULL
	      && gfc_match ("num_teams ( %e )", &c->num_teams) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_NUM_THREADS)
	      && c->num_threads == NULL
	      && (gfc_match ("num_threads ( %e )", &c->num_threads)
		  == MATCH_YES))
	    continue;
	  if ((mask & OMP_CLAUSE_NUM_WORKERS)
	      && c->num_workers_expr == NULL
	      && gfc_match ("num_workers ( %e )",
			    &c->num_workers_expr) == MATCH_YES)
	    continue;
	  break;
	case 'o':
	  if ((mask & OMP_CLAUSE_ORDER)
	      && !c->order_concurrent
	      && gfc_match ("order ( concurrent )") == MATCH_YES)
	    {
	      c->order_concurrent = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_ORDERED)
	      && !c->ordered
	      && gfc_match ("ordered") == MATCH_YES)
	    {
	      gfc_expr *cexpr = NULL;
	      match m = gfc_match (" ( %e )", &cexpr);

	      c->ordered = true;
	      if (m == MATCH_YES)
		{
		  int ordered = 0;
		  if (gfc_extract_int (cexpr, &ordered, -1))
		    ordered = 0;
		  else if (ordered <= 0)
		    {
		      gfc_error_now ("ORDERED clause argument not"
				     " constant positive integer at %C");
		      ordered = 0;
		    }
		  c->orderedc = ordered;
		  gfc_free_expr (cexpr);
		  continue;
		}

	      needs_space = true;
	      continue;
	    }
	  break;
	case 'p':
	  if ((mask & OMP_CLAUSE_COPY)
	      && gfc_match ("pcopy ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_TOFROM, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_COPYIN)
	      && gfc_match ("pcopyin ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_TO, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_COPYOUT)
	      && gfc_match ("pcopyout ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FROM, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_CREATE)
	      && gfc_match ("pcreate ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_ALLOC, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_PRESENT)
	      && gfc_match ("present ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FORCE_PRESENT, false,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_COPY)
	      && gfc_match ("present_or_copy ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_TOFROM, true,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_COPYIN)
	      && gfc_match ("present_or_copyin ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_TO, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_COPYOUT)
	      && gfc_match ("present_or_copyout ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FROM, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_CREATE)
	      && gfc_match ("present_or_create ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_ALLOC, true, allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_PRIORITY)
	      && c->priority == NULL
	      && gfc_match ("priority ( %e )", &c->priority) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_PRIVATE)
	      && gfc_match_omp_variable_list ("private (",
					      &c->lists[OMP_LIST_PRIVATE],
					      true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_PROC_BIND)
	      && c->proc_bind == OMP_PROC_BIND_UNKNOWN)
	    {
	      if (gfc_match ("proc_bind ( master )") == MATCH_YES)
		c->proc_bind = OMP_PROC_BIND_MASTER;
	      else if (gfc_match ("proc_bind ( spread )") == MATCH_YES)
		c->proc_bind = OMP_PROC_BIND_SPREAD;
	      else if (gfc_match ("proc_bind ( close )") == MATCH_YES)
		c->proc_bind = OMP_PROC_BIND_CLOSE;
	      if (c->proc_bind != OMP_PROC_BIND_UNKNOWN)
		continue;
	    }
	  break;
	case 'r':
	  if ((mask & OMP_CLAUSE_ATOMIC)
	      && c->atomic_op == GFC_OMP_ATOMIC_UNSET
	      && gfc_match ("read") == MATCH_YES)
	    {
	      c->atomic_op = GFC_OMP_ATOMIC_READ;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_REDUCTION)
	      && gfc_match_omp_clause_reduction (pc, c, openacc,
						 allow_derived) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("relaxed") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_RELAXED;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("release") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_RELEASE;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("relaxed") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_RELAXED;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("release") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_RELEASE;
	      needs_space = true;
	      continue;
	    }
	  break;
	case 's':
	  if ((mask & OMP_CLAUSE_SAFELEN)
	      && c->safelen_expr == NULL
	      && gfc_match ("safelen ( %e )", &c->safelen_expr) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_SCHEDULE)
	      && c->sched_kind == OMP_SCHED_NONE
	      && gfc_match ("schedule ( ") == MATCH_YES)
	    {
	      int nmodifiers = 0;
	      locus old_loc2 = gfc_current_locus;
	      do
		{
		  if (gfc_match ("simd") == MATCH_YES)
		    {
		      c->sched_simd = true;
		      nmodifiers++;
		    }
		  else if (gfc_match ("monotonic") == MATCH_YES)
		    {
		      c->sched_monotonic = true;
		      nmodifiers++;
		    }
		  else if (gfc_match ("nonmonotonic") == MATCH_YES)
		    {
		      c->sched_nonmonotonic = true;
		      nmodifiers++;
		    }
		  else
		    {
		      if (nmodifiers)
			gfc_current_locus = old_loc2;
		      break;
		    }
		  if (nmodifiers == 1
		      && gfc_match (" , ") == MATCH_YES)
		    continue;
		  else if (gfc_match (" : ") == MATCH_YES)
		    break;
		  gfc_current_locus = old_loc2;
		  break;
		}
	      while (1);
	      if (gfc_match ("static") == MATCH_YES)
		c->sched_kind = OMP_SCHED_STATIC;
	      else if (gfc_match ("dynamic") == MATCH_YES)
		c->sched_kind = OMP_SCHED_DYNAMIC;
	      else if (gfc_match ("guided") == MATCH_YES)
		c->sched_kind = OMP_SCHED_GUIDED;
	      else if (gfc_match ("runtime") == MATCH_YES)
		c->sched_kind = OMP_SCHED_RUNTIME;
	      else if (gfc_match ("auto") == MATCH_YES)
		c->sched_kind = OMP_SCHED_AUTO;
	      if (c->sched_kind != OMP_SCHED_NONE)
		{
		  match m = MATCH_NO;
		  if (c->sched_kind != OMP_SCHED_RUNTIME
		      && c->sched_kind != OMP_SCHED_AUTO)
		    m = gfc_match (" , %e )", &c->chunk_size);
		  if (m != MATCH_YES)
		    m = gfc_match_char (')');
		  if (m != MATCH_YES)
		    c->sched_kind = OMP_SCHED_NONE;
		}
	      if (c->sched_kind != OMP_SCHED_NONE)
		continue;
	      else
		gfc_current_locus = old_loc;
	    }
	  if ((mask & OMP_CLAUSE_HOST_SELF)
	      && gfc_match ("self ( ") == MATCH_YES
	      && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
					   OMP_MAP_FORCE_FROM, true,
					   allow_derived))
	    continue;
	  if ((mask & OMP_CLAUSE_SEQ)
	      && !c->seq
	      && gfc_match ("seq") == MATCH_YES)
	    {
	      c->seq = true;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_MEMORDER)
	      && c->memorder == OMP_MEMORDER_UNSET
	      && gfc_match ("seq_cst") == MATCH_YES)
	    {
	      c->memorder = OMP_MEMORDER_SEQ_CST;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_SHARED)
	      && gfc_match_omp_variable_list ("shared (",
					      &c->lists[OMP_LIST_SHARED],
					      true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_SIMDLEN)
	      && c->simdlen_expr == NULL
	      && gfc_match ("simdlen ( %e )", &c->simdlen_expr) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_SIMD)
	      && !c->simd
	      && gfc_match ("simd") == MATCH_YES)
	    {
	      c->simd = needs_space = true;
	      continue;
	    }
	  break;
	case 't':
	  if ((mask & OMP_CLAUSE_TASK_REDUCTION)
	      && gfc_match_omp_clause_reduction (pc, c, openacc,
						 allow_derived) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_THREAD_LIMIT)
	      && c->thread_limit == NULL
	      && gfc_match ("thread_limit ( %e )",
			    &c->thread_limit) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_THREADS)
	      && !c->threads
	      && gfc_match ("threads") == MATCH_YES)
	    {
	      c->threads = needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_TILE)
	      && !c->tile_list
	      && match_oacc_expr_list ("tile (", &c->tile_list,
				       true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_TO) && (mask & OMP_CLAUSE_LINK))
	    {
	      if (gfc_match_omp_to_link ("to (", &c->lists[OMP_LIST_TO])
		  == MATCH_YES)
		continue;
	    }
	  else if ((mask & OMP_CLAUSE_TO)
	      && gfc_match_omp_variable_list ("to (",
					      &c->lists[OMP_LIST_TO], false,
					      NULL, &head, true) == MATCH_YES)
	    continue;
	  break;
	case 'u':
	  if ((mask & OMP_CLAUSE_UNIFORM)
	      && gfc_match_omp_variable_list ("uniform (",
					      &c->lists[OMP_LIST_UNIFORM],
					      false) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_UNTIED)
	      && !c->untied
	      && gfc_match ("untied") == MATCH_YES)
	    {
	      c->untied = needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_ATOMIC)
	      && c->atomic_op == GFC_OMP_ATOMIC_UNSET
	      && gfc_match ("update") == MATCH_YES)
	    {
	      c->atomic_op = GFC_OMP_ATOMIC_UPDATE;
	      needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_USE_DEVICE)
	      && gfc_match_omp_variable_list ("use_device (",
					      &c->lists[OMP_LIST_USE_DEVICE],
					      true) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_USE_DEVICE_PTR)
	      && gfc_match_omp_variable_list
		   ("use_device_ptr (",
		    &c->lists[OMP_LIST_USE_DEVICE_PTR], false) == MATCH_YES)
	    continue;
	  if ((mask & OMP_CLAUSE_USE_DEVICE_ADDR)
	      && gfc_match_omp_variable_list
		   ("use_device_addr (",
		    &c->lists[OMP_LIST_USE_DEVICE_ADDR], false) == MATCH_YES)
	    continue;
	  break;
	case 'v':
	  /* VECTOR_LENGTH must be matched before VECTOR, because the latter
	     doesn't unconditionally match '('.  */
	  if ((mask & OMP_CLAUSE_VECTOR_LENGTH)
	      && c->vector_length_expr == NULL
	      && (gfc_match ("vector_length ( %e )", &c->vector_length_expr)
		  == MATCH_YES))
	    continue;
	  if ((mask & OMP_CLAUSE_VECTOR)
	      && !c->vector
	      && gfc_match ("vector") == MATCH_YES)
	    {
	      c->vector = true;
	      match m = match_oacc_clause_gwv (c, GOMP_DIM_VECTOR);
	      if (m == MATCH_ERROR)
		{
		  gfc_current_locus = old_loc;
		  break;
		}
	      if (m == MATCH_NO)
		needs_space = true;
	      continue;
	    }
	  break;
	case 'w':
	  if ((mask & OMP_CLAUSE_WAIT)
	      && gfc_match ("wait") == MATCH_YES)
	    {
	      match m = match_oacc_expr_list (" (", &c->wait_list, false);
	      if (m == MATCH_ERROR)
		{
		  gfc_current_locus = old_loc;
		  break;
		}
	      else if (m == MATCH_NO)
		{
		  gfc_expr *expr
		    = gfc_get_constant_expr (BT_INTEGER,
					     gfc_default_integer_kind,
					     &gfc_current_locus);
		  mpz_set_si (expr->value.integer, GOMP_ASYNC_NOVAL);
		  gfc_expr_list **expr_list = &c->wait_list;
		  while (*expr_list)
		    expr_list = &(*expr_list)->next;
		  *expr_list = gfc_get_expr_list ();
		  (*expr_list)->expr = expr;
		  needs_space = true;
		}
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_WORKER)
	      && !c->worker
	      && gfc_match ("worker") == MATCH_YES)
	    {
	      c->worker = true;
	      match m = match_oacc_clause_gwv (c, GOMP_DIM_WORKER);
	      if (m == MATCH_ERROR)
		{
		  gfc_current_locus = old_loc;
		  break;
		}
	      else if (m == MATCH_NO)
		needs_space = true;
	      continue;
	    }
	  if ((mask & OMP_CLAUSE_ATOMIC)
	      && c->atomic_op == GFC_OMP_ATOMIC_UNSET
	      && gfc_match ("write") == MATCH_YES)
	    {
	      c->atomic_op = GFC_OMP_ATOMIC_WRITE;
	      needs_space = true;
	      continue;
	    }
	  break;
	}
      break;
    }

  if (gfc_match_omp_eos () != MATCH_YES)
    {
      if (!gfc_error_flag_test ())
	gfc_error ("Failed to match clause at %C");
      gfc_free_omp_clauses (c);
      return MATCH_ERROR;
    }

  *cp = c;
  return MATCH_YES;
}


#define OACC_PARALLEL_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_NUM_GANGS	      \
   | OMP_CLAUSE_NUM_WORKERS | OMP_CLAUSE_VECTOR_LENGTH | OMP_CLAUSE_REDUCTION \
   | OMP_CLAUSE_COPY | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT		      \
   | OMP_CLAUSE_CREATE | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT	      \
   | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_FIRSTPRIVATE      \
   | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_WAIT | OMP_CLAUSE_ATTACH)
#define OACC_KERNELS_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_NUM_GANGS	      \
   | OMP_CLAUSE_NUM_WORKERS | OMP_CLAUSE_VECTOR_LENGTH | OMP_CLAUSE_DEVICEPTR \
   | OMP_CLAUSE_COPY | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT		      \
   | OMP_CLAUSE_CREATE | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT	      \
   | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_WAIT | OMP_CLAUSE_ATTACH)
#define OACC_SERIAL_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_REDUCTION	      \
   | OMP_CLAUSE_COPY | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT		      \
   | OMP_CLAUSE_CREATE | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT	      \
   | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_FIRSTPRIVATE      \
   | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_WAIT | OMP_CLAUSE_ATTACH)
#define OACC_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_DEVICEPTR  | OMP_CLAUSE_COPY	      \
   | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT | OMP_CLAUSE_CREATE		      \
   | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT | OMP_CLAUSE_ATTACH)
#define OACC_LOOP_CLAUSES \
  (omp_mask (OMP_CLAUSE_COLLAPSE) | OMP_CLAUSE_GANG | OMP_CLAUSE_WORKER	      \
   | OMP_CLAUSE_VECTOR | OMP_CLAUSE_SEQ | OMP_CLAUSE_INDEPENDENT	      \
   | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_AUTO	      \
   | OMP_CLAUSE_TILE)
#define OACC_PARALLEL_LOOP_CLAUSES \
  (OACC_LOOP_CLAUSES | OACC_PARALLEL_CLAUSES)
#define OACC_KERNELS_LOOP_CLAUSES \
  (OACC_LOOP_CLAUSES | OACC_KERNELS_CLAUSES)
#define OACC_SERIAL_LOOP_CLAUSES \
  (OACC_LOOP_CLAUSES | OACC_SERIAL_CLAUSES)
#define OACC_HOST_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_USE_DEVICE)					      \
   | OMP_CLAUSE_IF							      \
   | OMP_CLAUSE_IF_PRESENT)
#define OACC_DECLARE_CLAUSES \
  (omp_mask (OMP_CLAUSE_COPY) | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT	      \
   | OMP_CLAUSE_CREATE | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_DEVICE_RESIDENT    \
   | OMP_CLAUSE_PRESENT			      \
   | OMP_CLAUSE_LINK)
#define OACC_UPDATE_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_HOST_SELF	      \
   | OMP_CLAUSE_DEVICE | OMP_CLAUSE_WAIT | OMP_CLAUSE_IF_PRESENT)
#define OACC_ENTER_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_WAIT	      \
   | OMP_CLAUSE_COPYIN | OMP_CLAUSE_CREATE | OMP_CLAUSE_ATTACH)
#define OACC_EXIT_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_WAIT	      \
   | OMP_CLAUSE_COPYOUT | OMP_CLAUSE_DELETE | OMP_CLAUSE_FINALIZE	      \
   | OMP_CLAUSE_DETACH)
#define OACC_WAIT_CLAUSES \
  omp_mask (OMP_CLAUSE_ASYNC)
#define OACC_ROUTINE_CLAUSES \
  (omp_mask (OMP_CLAUSE_GANG) | OMP_CLAUSE_WORKER | OMP_CLAUSE_VECTOR	      \
   | OMP_CLAUSE_SEQ)


static match
match_acc (gfc_exec_op op, const omp_mask mask)
{
  gfc_omp_clauses *c;
  if (gfc_match_omp_clauses (&c, mask, false, false, true) != MATCH_YES)
    return MATCH_ERROR;
  new_st.op = op;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}

match
gfc_match_oacc_parallel_loop (void)
{
  return match_acc (EXEC_OACC_PARALLEL_LOOP, OACC_PARALLEL_LOOP_CLAUSES);
}


match
gfc_match_oacc_parallel (void)
{
  return match_acc (EXEC_OACC_PARALLEL, OACC_PARALLEL_CLAUSES);
}


match
gfc_match_oacc_kernels_loop (void)
{
  return match_acc (EXEC_OACC_KERNELS_LOOP, OACC_KERNELS_LOOP_CLAUSES);
}


match
gfc_match_oacc_kernels (void)
{
  return match_acc (EXEC_OACC_KERNELS, OACC_KERNELS_CLAUSES);
}


match
gfc_match_oacc_serial_loop (void)
{
  return match_acc (EXEC_OACC_SERIAL_LOOP, OACC_SERIAL_LOOP_CLAUSES);
}


match
gfc_match_oacc_serial (void)
{
  return match_acc (EXEC_OACC_SERIAL, OACC_SERIAL_CLAUSES);
}


match
gfc_match_oacc_data (void)
{
  return match_acc (EXEC_OACC_DATA, OACC_DATA_CLAUSES);
}


match
gfc_match_oacc_host_data (void)
{
  return match_acc (EXEC_OACC_HOST_DATA, OACC_HOST_DATA_CLAUSES);
}


match
gfc_match_oacc_loop (void)
{
  return match_acc (EXEC_OACC_LOOP, OACC_LOOP_CLAUSES);
}


match
gfc_match_oacc_declare (void)
{
  gfc_omp_clauses *c;
  gfc_omp_namelist *n;
  gfc_namespace *ns = gfc_current_ns;
  gfc_oacc_declare *new_oc;
  bool module_var = false;
  locus where = gfc_current_locus;

  if (gfc_match_omp_clauses (&c, OACC_DECLARE_CLAUSES, false, false, true)
      != MATCH_YES)
    return MATCH_ERROR;

  for (n = c->lists[OMP_LIST_DEVICE_RESIDENT]; n != NULL; n = n->next)
    n->sym->attr.oacc_declare_device_resident = 1;

  for (n = c->lists[OMP_LIST_LINK]; n != NULL; n = n->next)
    n->sym->attr.oacc_declare_link = 1;

  for (n = c->lists[OMP_LIST_MAP]; n != NULL; n = n->next)
    {
      gfc_symbol *s = n->sym;

      if (gfc_current_ns->proc_name
	  && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
	{
	  if (n->u.map_op != OMP_MAP_ALLOC && n->u.map_op != OMP_MAP_TO)
	    {
	      gfc_error ("Invalid clause in module with !$ACC DECLARE at %L",
			 &where);
	      return MATCH_ERROR;
	    }

	  module_var = true;
	}

      if (s->attr.use_assoc)
	{
	  gfc_error ("Variable is USE-associated with !$ACC DECLARE at %L",
		     &where);
	  return MATCH_ERROR;
	}

      if ((s->result == s && s->ns->contained != gfc_current_ns)
	  || ((s->attr.flavor == FL_UNKNOWN || s->attr.flavor == FL_VARIABLE)
	      && s->ns != gfc_current_ns))
	{
	  gfc_error ("Variable %qs shall be declared in the same scoping unit "
		     "as !$ACC DECLARE at %L", s->name, &where);
	  return MATCH_ERROR;
	}

      if ((s->attr.dimension || s->attr.codimension)
	  && s->attr.dummy && s->as->type != AS_EXPLICIT)
	{
	  gfc_error ("Assumed-size dummy array with !$ACC DECLARE at %L",
		     &where);
	  return MATCH_ERROR;
	}

      switch (n->u.map_op)
	{
	  case OMP_MAP_FORCE_ALLOC:
	  case OMP_MAP_ALLOC:
	    s->attr.oacc_declare_create = 1;
	    break;

	  case OMP_MAP_FORCE_TO:
	  case OMP_MAP_TO:
	    s->attr.oacc_declare_copyin = 1;
	    break;

	  case OMP_MAP_FORCE_DEVICEPTR:
	    s->attr.oacc_declare_deviceptr = 1;
	    break;

	  default:
	    break;
	}
    }

  new_oc = gfc_get_oacc_declare ();
  new_oc->next = ns->oacc_declare;
  new_oc->module_var = module_var;
  new_oc->clauses = c;
  new_oc->loc = gfc_current_locus;
  ns->oacc_declare = new_oc;

  return MATCH_YES;
}


match
gfc_match_oacc_update (void)
{
  gfc_omp_clauses *c;
  locus here = gfc_current_locus;

  if (gfc_match_omp_clauses (&c, OACC_UPDATE_CLAUSES, false, false, true)
      != MATCH_YES)
    return MATCH_ERROR;

  if (!c->lists[OMP_LIST_MAP])
    {
      gfc_error ("%<acc update%> must contain at least one "
		 "%<device%> or %<host%> or %<self%> clause at %L", &here);
      return MATCH_ERROR;
    }

  new_st.op = EXEC_OACC_UPDATE;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_oacc_enter_data (void)
{
  return match_acc (EXEC_OACC_ENTER_DATA, OACC_ENTER_DATA_CLAUSES);
}


match
gfc_match_oacc_exit_data (void)
{
  return match_acc (EXEC_OACC_EXIT_DATA, OACC_EXIT_DATA_CLAUSES);
}


match
gfc_match_oacc_wait (void)
{
  gfc_omp_clauses *c = gfc_get_omp_clauses ();
  gfc_expr_list *wait_list = NULL, *el;
  bool space = true;
  match m;

  m = match_oacc_expr_list (" (", &wait_list, true);
  if (m == MATCH_ERROR)
    return m;
  else if (m == MATCH_YES)
    space = false;

  if (gfc_match_omp_clauses (&c, OACC_WAIT_CLAUSES, space, space, true)
      == MATCH_ERROR)
    return MATCH_ERROR;

  if (wait_list)
    for (el = wait_list; el; el = el->next)
      {
	if (el->expr == NULL)
	  {
	    gfc_error ("Invalid argument to !$ACC WAIT at %C");
	    return MATCH_ERROR;
	  }

	if (!gfc_resolve_expr (el->expr)
	    || el->expr->ts.type != BT_INTEGER || el->expr->rank != 0)
	  {
	    gfc_error ("WAIT clause at %L requires a scalar INTEGER expression",
		       &el->expr->where);

	    return MATCH_ERROR;
	  }
      }
  c->wait_list = wait_list;
  new_st.op = EXEC_OACC_WAIT;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_oacc_cache (void)
{
  gfc_omp_clauses *c = gfc_get_omp_clauses ();
  /* The OpenACC cache directive explicitly only allows "array elements or
     subarrays", which we're currently not checking here.  Either check this
     after the call of gfc_match_omp_variable_list, or add something like a
     only_sections variant next to its allow_sections parameter.  */
  match m = gfc_match_omp_variable_list (" (",
					 &c->lists[OMP_LIST_CACHE], true,
					 NULL, NULL, true);
  if (m != MATCH_YES)
    {
      gfc_free_omp_clauses(c);
      return m;
    }

  if (gfc_current_state() != COMP_DO 
      && gfc_current_state() != COMP_DO_CONCURRENT)
    {
      gfc_error ("ACC CACHE directive must be inside of loop %C");
      gfc_free_omp_clauses(c);
      return MATCH_ERROR;
    }

  new_st.op = EXEC_OACC_CACHE;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}

/* Determine the OpenACC 'routine' directive's level of parallelism.  */

static oacc_routine_lop
gfc_oacc_routine_lop (gfc_omp_clauses *clauses)
{
  oacc_routine_lop ret = OACC_ROUTINE_LOP_SEQ;

  if (clauses)
    {
      unsigned n_lop_clauses = 0;

      if (clauses->gang)
	{
	  ++n_lop_clauses;
	  ret = OACC_ROUTINE_LOP_GANG;
	}
      if (clauses->worker)
	{
	  ++n_lop_clauses;
	  ret = OACC_ROUTINE_LOP_WORKER;
	}
      if (clauses->vector)
	{
	  ++n_lop_clauses;
	  ret = OACC_ROUTINE_LOP_VECTOR;
	}
      if (clauses->seq)
	{
	  ++n_lop_clauses;
	  ret = OACC_ROUTINE_LOP_SEQ;
	}

      if (n_lop_clauses > 1)
	ret = OACC_ROUTINE_LOP_ERROR;
    }

  return ret;
}

match
gfc_match_oacc_routine (void)
{
  locus old_loc;
  match m;
  gfc_intrinsic_sym *isym = NULL;
  gfc_symbol *sym = NULL;
  gfc_omp_clauses *c = NULL;
  gfc_oacc_routine_name *n = NULL;
  oacc_routine_lop lop = OACC_ROUTINE_LOP_NONE;

  old_loc = gfc_current_locus;

  m = gfc_match (" (");

  if (gfc_current_ns->proc_name
      && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
      && m == MATCH_YES)
    {
      gfc_error ("Only the !$ACC ROUTINE form without "
		 "list is allowed in interface block at %C");
      goto cleanup;
    }

  if (m == MATCH_YES)
    {
      char buffer[GFC_MAX_SYMBOL_LEN + 1];

      m = gfc_match_name (buffer);
      if (m == MATCH_YES)
	{
	  gfc_symtree *st = NULL;

	  /* First look for an intrinsic symbol.  */
	  isym = gfc_find_function (buffer);
	  if (!isym)
	    isym = gfc_find_subroutine (buffer);
	  /* If no intrinsic symbol found, search the current namespace.  */
	  if (!isym)
	    st = gfc_find_symtree (gfc_current_ns->sym_root, buffer);
	  if (st)
	    {
	      sym = st->n.sym;
	      /* If the name in a 'routine' directive refers to the containing
		 subroutine or function, then make sure that we'll later handle
		 this accordingly.  */
	      if (gfc_current_ns->proc_name != NULL
		  && strcmp (sym->name, gfc_current_ns->proc_name->name) == 0)
	        sym = NULL;
	    }

	  if (isym == NULL && st == NULL)
	    {
	      gfc_error ("Invalid NAME %qs in !$ACC ROUTINE ( NAME ) at %C",
			 buffer);
	      gfc_current_locus = old_loc;
	      return MATCH_ERROR;
	    }
	}
      else
        {
	  gfc_error ("Syntax error in !$ACC ROUTINE ( NAME ) at %C");
	  gfc_current_locus = old_loc;
	  return MATCH_ERROR;
	}

      if (gfc_match_char (')') != MATCH_YES)
	{
	  gfc_error ("Syntax error in !$ACC ROUTINE ( NAME ) at %C, expecting"
		     " ')' after NAME");
	  gfc_current_locus = old_loc;
	  return MATCH_ERROR;
	}
    }

  if (gfc_match_omp_eos () != MATCH_YES
      && (gfc_match_omp_clauses (&c, OACC_ROUTINE_CLAUSES, false, false, true)
	  != MATCH_YES))
    return MATCH_ERROR;

  lop = gfc_oacc_routine_lop (c);
  if (lop == OACC_ROUTINE_LOP_ERROR)
    {
      gfc_error ("Multiple loop axes specified for routine at %C");
      goto cleanup;
    }

  if (isym != NULL)
    {
      /* Diagnose any OpenACC 'routine' directive that doesn't match the
	 (implicit) one with a 'seq' clause.  */
      if (c && (c->gang || c->worker || c->vector))
	{
	  gfc_error ("Intrinsic symbol specified in !$ACC ROUTINE ( NAME )"
		     " at %C marked with incompatible GANG, WORKER, or VECTOR"
		     " clause");
	  goto cleanup;
	}
    }
  else if (sym != NULL)
    {
      bool add = true;

      /* For a repeated OpenACC 'routine' directive, diagnose if it doesn't
	 match the first one.  */
      for (gfc_oacc_routine_name *n_p = gfc_current_ns->oacc_routine_names;
	   n_p;
	   n_p = n_p->next)
	if (n_p->sym == sym)
	  {
	    add = false;
	    if (lop != gfc_oacc_routine_lop (n_p->clauses))
	      {
		gfc_error ("!$ACC ROUTINE already applied at %C");
		goto cleanup;
	      }
	  }

      if (add)
	{
	  sym->attr.oacc_routine_lop = lop;

	  n = gfc_get_oacc_routine_name ();
	  n->sym = sym;
	  n->clauses = c;
	  n->next = gfc_current_ns->oacc_routine_names;
	  n->loc = old_loc;
	  gfc_current_ns->oacc_routine_names = n;
	}
    }
  else if (gfc_current_ns->proc_name)
    {
      /* For a repeated OpenACC 'routine' directive, diagnose if it doesn't
	 match the first one.  */
      oacc_routine_lop lop_p = gfc_current_ns->proc_name->attr.oacc_routine_lop;
      if (lop_p != OACC_ROUTINE_LOP_NONE
	  && lop != lop_p)
	{
	  gfc_error ("!$ACC ROUTINE already applied at %C");
	  goto cleanup;
	}

      if (!gfc_add_omp_declare_target (&gfc_current_ns->proc_name->attr,
				       gfc_current_ns->proc_name->name,
				       &old_loc))
	goto cleanup;
      gfc_current_ns->proc_name->attr.oacc_routine_lop = lop;
    }
  else
    /* Something has gone wrong, possibly a syntax error.  */
    goto cleanup;

  if (gfc_pure (NULL) && c && (c->gang || c->worker || c->vector))
    {
      gfc_error ("!$ACC ROUTINE with GANG, WORKER, or VECTOR clause is not "
		 "permitted in PURE procedure at %C");
      goto cleanup;
    }


  if (n)
    n->clauses = c;
  else if (gfc_current_ns->oacc_routine)
    gfc_current_ns->oacc_routine_clauses = c;

  new_st.op = EXEC_OACC_ROUTINE;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;  

cleanup:
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}


#define OMP_PARALLEL_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE		\
   | OMP_CLAUSE_SHARED | OMP_CLAUSE_COPYIN | OMP_CLAUSE_REDUCTION	\
   | OMP_CLAUSE_IF | OMP_CLAUSE_NUM_THREADS | OMP_CLAUSE_DEFAULT	\
   | OMP_CLAUSE_PROC_BIND)
#define OMP_DECLARE_SIMD_CLAUSES \
  (omp_mask (OMP_CLAUSE_SIMDLEN) | OMP_CLAUSE_LINEAR			\
   | OMP_CLAUSE_UNIFORM	| OMP_CLAUSE_ALIGNED | OMP_CLAUSE_INBRANCH	\
   | OMP_CLAUSE_NOTINBRANCH)
#define OMP_DO_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE		\
   | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_REDUCTION			\
   | OMP_CLAUSE_SCHEDULE | OMP_CLAUSE_ORDERED | OMP_CLAUSE_COLLAPSE	\
   | OMP_CLAUSE_LINEAR | OMP_CLAUSE_ORDER)
#define OMP_SECTIONS_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE		\
   | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_REDUCTION)
#define OMP_SIMD_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_LASTPRIVATE		\
   | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_SAFELEN	\
   | OMP_CLAUSE_LINEAR | OMP_CLAUSE_ALIGNED | OMP_CLAUSE_SIMDLEN	\
   | OMP_CLAUSE_IF | OMP_CLAUSE_ORDER | OMP_CLAUSE_NOTEMPORAL)
#define OMP_TASK_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE		\
   | OMP_CLAUSE_SHARED | OMP_CLAUSE_IF | OMP_CLAUSE_DEFAULT		\
   | OMP_CLAUSE_UNTIED | OMP_CLAUSE_FINAL | OMP_CLAUSE_MERGEABLE	\
   | OMP_CLAUSE_DEPEND | OMP_CLAUSE_PRIORITY | OMP_CLAUSE_IN_REDUCTION	\
   | OMP_CLAUSE_DETACH)
#define OMP_TASKLOOP_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE		\
   | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_SHARED | OMP_CLAUSE_IF		\
   | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_UNTIED | OMP_CLAUSE_FINAL		\
   | OMP_CLAUSE_MERGEABLE | OMP_CLAUSE_PRIORITY | OMP_CLAUSE_GRAINSIZE	\
   | OMP_CLAUSE_NUM_TASKS | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_NOGROUP	\
   | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_IN_REDUCTION)
#define OMP_TARGET_CLAUSES \
  (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF	\
   | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT | OMP_CLAUSE_PRIVATE		\
   | OMP_CLAUSE_FIRSTPRIVATE | OMP_CLAUSE_DEFAULTMAP			\
   | OMP_CLAUSE_IS_DEVICE_PTR | OMP_CLAUSE_IN_REDUCTION)
#define OMP_TARGET_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF	\
   | OMP_CLAUSE_USE_DEVICE_PTR | OMP_CLAUSE_USE_DEVICE_ADDR)
#define OMP_TARGET_ENTER_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF	\
   | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT)
#define OMP_TARGET_EXIT_DATA_CLAUSES \
  (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF	\
   | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT)
#define OMP_TARGET_UPDATE_CLAUSES \
  (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_IF | OMP_CLAUSE_TO		\
   | OMP_CLAUSE_FROM | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT)
#define OMP_TEAMS_CLAUSES \
  (omp_mask (OMP_CLAUSE_NUM_TEAMS) | OMP_CLAUSE_THREAD_LIMIT		\
   | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_FIRSTPRIVATE	\
   | OMP_CLAUSE_SHARED | OMP_CLAUSE_REDUCTION)
#define OMP_DISTRIBUTE_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE		\
   | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_DIST_SCHEDULE)
#define OMP_SINGLE_CLAUSES \
  (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE)
#define OMP_ORDERED_CLAUSES \
  (omp_mask (OMP_CLAUSE_THREADS) | OMP_CLAUSE_SIMD)
#define OMP_DECLARE_TARGET_CLAUSES \
  (omp_mask (OMP_CLAUSE_TO) | OMP_CLAUSE_LINK | OMP_CLAUSE_DEVICE_TYPE)
#define OMP_ATOMIC_CLAUSES \
  (omp_mask (OMP_CLAUSE_ATOMIC) | OMP_CLAUSE_CAPTURE | OMP_CLAUSE_HINT	\
   | OMP_CLAUSE_MEMORDER)


static match
match_omp (gfc_exec_op op, const omp_mask mask)
{
  gfc_omp_clauses *c;
  if (gfc_match_omp_clauses (&c, mask) != MATCH_YES)
    return MATCH_ERROR;
  new_st.op = op;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_omp_critical (void)
{
  char n[GFC_MAX_SYMBOL_LEN+1];
  gfc_omp_clauses *c = NULL;

  if (gfc_match (" ( %n )", n) != MATCH_YES)
    n[0] = '\0';

  if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_HINT),
			     /* first = */ n[0] == '\0') != MATCH_YES)
    return MATCH_ERROR;

  new_st.op = EXEC_OMP_CRITICAL;
  new_st.ext.omp_clauses = c;
  if (n[0])
    c->critical_name = xstrdup (n);
  return MATCH_YES;
}


match
gfc_match_omp_end_critical (void)
{
  char n[GFC_MAX_SYMBOL_LEN+1];

  if (gfc_match (" ( %n )", n) != MATCH_YES)
    n[0] = '\0';
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after $OMP CRITICAL statement at %C");
      return MATCH_ERROR;
    }

  new_st.op = EXEC_OMP_END_CRITICAL;
  new_st.ext.omp_name = n[0] ? xstrdup (n) : NULL;
  return MATCH_YES;
}


match
gfc_match_omp_distribute (void)
{
  return match_omp (EXEC_OMP_DISTRIBUTE, OMP_DISTRIBUTE_CLAUSES);
}


match
gfc_match_omp_distribute_parallel_do (void)
{
  return match_omp (EXEC_OMP_DISTRIBUTE_PARALLEL_DO,
		    (OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
		     | OMP_DO_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_ORDERED))
		    & ~(omp_mask (OMP_CLAUSE_LINEAR)));
}


match
gfc_match_omp_distribute_parallel_do_simd (void)
{
  return match_omp (EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD,
		    (OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
		     | OMP_DO_CLAUSES | OMP_SIMD_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_ORDERED)));
}


match
gfc_match_omp_distribute_simd (void)
{
  return match_omp (EXEC_OMP_DISTRIBUTE_SIMD,
		    OMP_DISTRIBUTE_CLAUSES | OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_do (void)
{
  return match_omp (EXEC_OMP_DO, OMP_DO_CLAUSES);
}


match
gfc_match_omp_do_simd (void)
{
  return match_omp (EXEC_OMP_DO_SIMD, OMP_DO_CLAUSES | OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_flush (void)
{
  gfc_omp_namelist *list = NULL;
  gfc_omp_clauses *c = NULL;
  gfc_gobble_whitespace ();
  enum gfc_omp_memorder mo = OMP_MEMORDER_UNSET;
  if (gfc_match_omp_eos () == MATCH_NO && gfc_peek_ascii_char () != '(')
    {
      if (gfc_match ("acq_rel") == MATCH_YES)
	mo = OMP_MEMORDER_ACQ_REL;
      else if (gfc_match ("release") == MATCH_YES)
	mo = OMP_MEMORDER_RELEASE;
      else if (gfc_match ("acquire") == MATCH_YES)
	mo = OMP_MEMORDER_ACQUIRE;
      else
	{
	  gfc_error ("Expected AQC_REL, RELEASE, or ACQUIRE at %C");
	  return MATCH_ERROR;
	}
      c = gfc_get_omp_clauses ();
      c->memorder = mo;
    }
  gfc_match_omp_variable_list (" (", &list, true);
  if (list && mo != OMP_MEMORDER_UNSET)
    {
      gfc_error ("List specified together with memory order clause in FLUSH "
		 "directive at %C");
      gfc_free_omp_namelist (list);
      gfc_free_omp_clauses (c);
      return MATCH_ERROR;
    }
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after $OMP FLUSH statement at %C");
      gfc_free_omp_namelist (list);
      gfc_free_omp_clauses (c);
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_FLUSH;
  new_st.ext.omp_namelist = list;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_omp_declare_simd (void)
{
  locus where = gfc_current_locus;
  gfc_symbol *proc_name;
  gfc_omp_clauses *c;
  gfc_omp_declare_simd *ods;
  bool needs_space = false;

  switch (gfc_match (" ( %s ) ", &proc_name))
    {
    case MATCH_YES: break;
    case MATCH_NO: proc_name = NULL; needs_space = true; break;
    case MATCH_ERROR: return MATCH_ERROR;
    }

  if (gfc_match_omp_clauses (&c, OMP_DECLARE_SIMD_CLAUSES, true,
			     needs_space) != MATCH_YES)
    return MATCH_ERROR;

  if (gfc_current_ns->is_block_data)
    {
      gfc_free_omp_clauses (c);
      return MATCH_YES;
    }

  ods = gfc_get_omp_declare_simd ();
  ods->where = where;
  ods->proc_name = proc_name;
  ods->clauses = c;
  ods->next = gfc_current_ns->omp_declare_simd;
  gfc_current_ns->omp_declare_simd = ods;
  return MATCH_YES;
}


static bool
match_udr_expr (gfc_symtree *omp_sym1, gfc_symtree *omp_sym2)
{
  match m;
  locus old_loc = gfc_current_locus;
  char sname[GFC_MAX_SYMBOL_LEN + 1];
  gfc_symbol *sym;
  gfc_namespace *ns = gfc_current_ns;
  gfc_expr *lvalue = NULL, *rvalue = NULL;
  gfc_symtree *st;
  gfc_actual_arglist *arglist;

  m = gfc_match (" %v =", &lvalue);
  if (m != MATCH_YES)
    gfc_current_locus = old_loc;
  else
    {
      m = gfc_match (" %e )", &rvalue);
      if (m == MATCH_YES)
	{
	  ns->code = gfc_get_code (EXEC_ASSIGN);
	  ns->code->expr1 = lvalue;
	  ns->code->expr2 = rvalue;
	  ns->code->loc = old_loc;
	  return true;
	}

      gfc_current_locus = old_loc;
      gfc_free_expr (lvalue);
    }

  m = gfc_match (" %n", sname);
  if (m != MATCH_YES)
    return false;

  if (strcmp (sname, omp_sym1->name) == 0
      || strcmp (sname, omp_sym2->name) == 0)
    return false;

  gfc_current_ns = ns->parent;
  if (gfc_get_ha_sym_tree (sname, &st))
    return false;

  sym = st->n.sym;
  if (sym->attr.flavor != FL_PROCEDURE
      && sym->attr.flavor != FL_UNKNOWN)
    return false;

  if (!sym->attr.generic
      && !sym->attr.subroutine
      && !sym->attr.function)
    {
      if (!(sym->attr.external && !sym->attr.referenced))
	{
	  /* ...create a symbol in this scope...  */
	  if (sym->ns != gfc_current_ns
	      && gfc_get_sym_tree (sname, NULL, &st, false) == 1)
	    return false;

	  if (sym != st->n.sym)
	    sym = st->n.sym;
	}

      /* ...and then to try to make the symbol into a subroutine.  */
      if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
	return false;
    }

  gfc_set_sym_referenced (sym);
  gfc_gobble_whitespace ();
  if (gfc_peek_ascii_char () != '(')
    return false;

  gfc_current_ns = ns;
  m = gfc_match_actual_arglist (1, &arglist);
  if (m != MATCH_YES)
    return false;

  if (gfc_match_char (')') != MATCH_YES)
    return false;

  ns->code = gfc_get_code (EXEC_CALL);
  ns->code->symtree = st;
  ns->code->ext.actual = arglist;
  ns->code->loc = old_loc;
  return true;
}

static bool
gfc_omp_udr_predef (gfc_omp_reduction_op rop, const char *name,
		    gfc_typespec *ts, const char **n)
{
  if (!gfc_numeric_ts (ts) && ts->type != BT_LOGICAL)
    return false;

  switch (rop)
    {
    case OMP_REDUCTION_PLUS:
    case OMP_REDUCTION_MINUS:
    case OMP_REDUCTION_TIMES:
      return ts->type != BT_LOGICAL;
    case OMP_REDUCTION_AND:
    case OMP_REDUCTION_OR:
    case OMP_REDUCTION_EQV:
    case OMP_REDUCTION_NEQV:
      return ts->type == BT_LOGICAL;
    case OMP_REDUCTION_USER:
      if (name[0] != '.' && (ts->type == BT_INTEGER || ts->type == BT_REAL))
	{
	  gfc_symbol *sym;

	  gfc_find_symbol (name, NULL, 1, &sym);
	  if (sym != NULL)
	    {
	      if (sym->attr.intrinsic)
		*n = sym->name;
	      else if ((sym->attr.flavor != FL_UNKNOWN
			&& sym->attr.flavor != FL_PROCEDURE)
		       || sym->attr.external
		       || sym->attr.generic
		       || sym->attr.entry
		       || sym->attr.result
		       || sym->attr.dummy
		       || sym->attr.subroutine
		       || sym->attr.pointer
		       || sym->attr.target
		       || sym->attr.cray_pointer
		       || sym->attr.cray_pointee
		       || (sym->attr.proc != PROC_UNKNOWN
			   && sym->attr.proc != PROC_INTRINSIC)
		       || sym->attr.if_source != IFSRC_UNKNOWN
		       || sym == sym->ns->proc_name)
		*n = NULL;
	      else
		*n = sym->name;
	    }
	  else
	    *n = name;
	  if (*n
	      && (strcmp (*n, "max") == 0 || strcmp (*n, "min") == 0))
	    return true;
	  else if (*n
		   && ts->type == BT_INTEGER
		   && (strcmp (*n, "iand") == 0
		       || strcmp (*n, "ior") == 0
		       || strcmp (*n, "ieor") == 0))
	    return true;
	}
      break;
    default:
      break;
    }
  return false;
}

gfc_omp_udr *
gfc_omp_udr_find (gfc_symtree *st, gfc_typespec *ts)
{
  gfc_omp_udr *omp_udr;

  if (st == NULL)
    return NULL;

  for (omp_udr = st->n.omp_udr; omp_udr; omp_udr = omp_udr->next)
    if (omp_udr->ts.type == ts->type
	|| ((omp_udr->ts.type == BT_DERIVED || omp_udr->ts.type == BT_CLASS)
	    && (ts->type == BT_DERIVED || ts->type == BT_CLASS)))
      {
	if (omp_udr->ts.type == BT_DERIVED || omp_udr->ts.type == BT_CLASS)
	  {
	    if (strcmp (omp_udr->ts.u.derived->name, ts->u.derived->name) == 0)
	      return omp_udr;
	  }
	else if (omp_udr->ts.kind == ts->kind)
	  {
	    if (omp_udr->ts.type == BT_CHARACTER)
	      {
		if (omp_udr->ts.u.cl->length == NULL
		    || ts->u.cl->length == NULL)
		  return omp_udr;
		if (omp_udr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
		  return omp_udr;
		if (ts->u.cl->length->expr_type != EXPR_CONSTANT)
		  return omp_udr;
		if (omp_udr->ts.u.cl->length->ts.type != BT_INTEGER)
		  return omp_udr;
		if (ts->u.cl->length->ts.type != BT_INTEGER)
		  return omp_udr;
		if (gfc_compare_expr (omp_udr->ts.u.cl->length,
				      ts->u.cl->length, INTRINSIC_EQ) != 0)
		  continue;
	      }
	    return omp_udr;
	  }
      }
  return NULL;
}

match
gfc_match_omp_declare_reduction (void)
{
  match m;
  gfc_intrinsic_op op;
  char name[GFC_MAX_SYMBOL_LEN + 3];
  auto_vec<gfc_typespec, 5> tss;
  gfc_typespec ts;
  unsigned int i;
  gfc_symtree *st;
  locus where = gfc_current_locus;
  locus end_loc = gfc_current_locus;
  bool end_loc_set = false;
  gfc_omp_reduction_op rop = OMP_REDUCTION_NONE;

  if (gfc_match_char ('(') != MATCH_YES)
    return MATCH_ERROR;

  m = gfc_match (" %o : ", &op);
  if (m == MATCH_ERROR)
    return MATCH_ERROR;
  if (m == MATCH_YES)
    {
      snprintf (name, sizeof name, "operator %s", gfc_op2string (op));
      rop = (gfc_omp_reduction_op) op;
    }
  else
    {
      m = gfc_match_defined_op_name (name + 1, 1);
      if (m == MATCH_ERROR)
	return MATCH_ERROR;
      if (m == MATCH_YES)
	{
	  name[0] = '.';
	  strcat (name, ".");
	  if (gfc_match (" : ") != MATCH_YES)
	    return MATCH_ERROR;
	}
      else
	{
	  if (gfc_match (" %n : ", name) != MATCH_YES)
	    return MATCH_ERROR;
	}
      rop = OMP_REDUCTION_USER;
    }

  m = gfc_match_type_spec (&ts);
  if (m != MATCH_YES)
    return MATCH_ERROR;
  /* Treat len=: the same as len=*.  */
  if (ts.type == BT_CHARACTER)
    ts.deferred = false;
  tss.safe_push (ts);

  while (gfc_match_char (',') == MATCH_YES)
    {
      m = gfc_match_type_spec (&ts);
      if (m != MATCH_YES)
	return MATCH_ERROR;
      tss.safe_push (ts);
    }
  if (gfc_match_char (':') != MATCH_YES)
    return MATCH_ERROR;

  st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
  for (i = 0; i < tss.length (); i++)
    {
      gfc_symtree *omp_out, *omp_in;
      gfc_symtree *omp_priv = NULL, *omp_orig = NULL;
      gfc_namespace *combiner_ns, *initializer_ns = NULL;
      gfc_omp_udr *prev_udr, *omp_udr;
      const char *predef_name = NULL;

      omp_udr = gfc_get_omp_udr ();
      omp_udr->name = gfc_get_string ("%s", name);
      omp_udr->rop = rop;
      omp_udr->ts = tss[i];
      omp_udr->where = where;

      gfc_current_ns = combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
      combiner_ns->proc_name = combiner_ns->parent->proc_name;

      gfc_get_sym_tree ("omp_out", combiner_ns, &omp_out, false);
      gfc_get_sym_tree ("omp_in", combiner_ns, &omp_in, false);
      combiner_ns->omp_udr_ns = 1;
      omp_out->n.sym->ts = tss[i];
      omp_in->n.sym->ts = tss[i];
      omp_out->n.sym->attr.omp_udr_artificial_var = 1;
      omp_in->n.sym->attr.omp_udr_artificial_var = 1;
      omp_out->n.sym->attr.flavor = FL_VARIABLE;
      omp_in->n.sym->attr.flavor = FL_VARIABLE;
      gfc_commit_symbols ();
      omp_udr->combiner_ns = combiner_ns;
      omp_udr->omp_out = omp_out->n.sym;
      omp_udr->omp_in = omp_in->n.sym;

      locus old_loc = gfc_current_locus;

      if (!match_udr_expr (omp_out, omp_in))
	{
	 syntax:
	  gfc_current_locus = old_loc;
	  gfc_current_ns = combiner_ns->parent;
	  gfc_undo_symbols ();
	  gfc_free_omp_udr (omp_udr);
	  return MATCH_ERROR;
	}

      if (gfc_match (" initializer ( ") == MATCH_YES)
	{
	  gfc_current_ns = combiner_ns->parent;
	  initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
	  gfc_current_ns = initializer_ns;
	  initializer_ns->proc_name = initializer_ns->parent->proc_name;

	  gfc_get_sym_tree ("omp_priv", initializer_ns, &omp_priv, false);
	  gfc_get_sym_tree ("omp_orig", initializer_ns, &omp_orig, false);
	  initializer_ns->omp_udr_ns = 1;
	  omp_priv->n.sym->ts = tss[i];
	  omp_orig->n.sym->ts = tss[i];
	  omp_priv->n.sym->attr.omp_udr_artificial_var = 1;
	  omp_orig->n.sym->attr.omp_udr_artificial_var = 1;
	  omp_priv->n.sym->attr.flavor = FL_VARIABLE;
	  omp_orig->n.sym->attr.flavor = FL_VARIABLE;
	  gfc_commit_symbols ();
	  omp_udr->initializer_ns = initializer_ns;
	  omp_udr->omp_priv = omp_priv->n.sym;
	  omp_udr->omp_orig = omp_orig->n.sym;

	  if (!match_udr_expr (omp_priv, omp_orig))
	    goto syntax;
	}

      gfc_current_ns = combiner_ns->parent;
      if (!end_loc_set)
	{
	  end_loc_set = true;
	  end_loc = gfc_current_locus;
	}
      gfc_current_locus = old_loc;

      prev_udr = gfc_omp_udr_find (st, &tss[i]);
      if (gfc_omp_udr_predef (rop, name, &tss[i], &predef_name)
	  /* Don't error on !$omp declare reduction (min : integer : ...)
	     just yet, there could be integer :: min afterwards,
	     making it valid.  When the UDR is resolved, we'll get
	     to it again.  */
	  && (rop != OMP_REDUCTION_USER || name[0] == '.'))
	{
	  if (predef_name)
	    gfc_error_now ("Redefinition of predefined %s "
			   "!$OMP DECLARE REDUCTION at %L",
			   predef_name, &where);
	  else
	    gfc_error_now ("Redefinition of predefined "
			   "!$OMP DECLARE REDUCTION at %L", &where);
	}
      else if (prev_udr)
	{
	  gfc_error_now ("Redefinition of !$OMP DECLARE REDUCTION at %L",
			 &where);
	  gfc_error_now ("Previous !$OMP DECLARE REDUCTION at %L",
			 &prev_udr->where);
	}
      else if (st)
	{
	  omp_udr->next = st->n.omp_udr;
	  st->n.omp_udr = omp_udr;
	}
      else
	{
	  st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
	  st->n.omp_udr = omp_udr;
	}
    }

  if (end_loc_set)
    {
      gfc_current_locus = end_loc;
      if (gfc_match_omp_eos () != MATCH_YES)
	{
	  gfc_error ("Unexpected junk after !$OMP DECLARE REDUCTION at %C");
	  gfc_current_locus = where;
	  return MATCH_ERROR;
	}

      return MATCH_YES;
    }
  gfc_clear_error ();
  return MATCH_ERROR;
}


match
gfc_match_omp_declare_target (void)
{
  locus old_loc;
  match m;
  gfc_omp_clauses *c = NULL;
  int list;
  gfc_omp_namelist *n;
  gfc_symbol *s;

  old_loc = gfc_current_locus;

  if (gfc_current_ns->proc_name
      && gfc_match_omp_eos () == MATCH_YES)
    {
      if (!gfc_add_omp_declare_target (&gfc_current_ns->proc_name->attr,
				       gfc_current_ns->proc_name->name,
				       &old_loc))
	goto cleanup;
      return MATCH_YES;
    }

  if (gfc_current_ns->proc_name
      && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
    {
      gfc_error ("Only the !$OMP DECLARE TARGET form without "
		 "clauses is allowed in interface block at %C");
      goto cleanup;
    }

  m = gfc_match (" (");
  if (m == MATCH_YES)
    {
      c = gfc_get_omp_clauses ();
      gfc_current_locus = old_loc;
      m = gfc_match_omp_to_link (" (", &c->lists[OMP_LIST_TO]);
      if (m != MATCH_YES)
	goto syntax;
      if (gfc_match_omp_eos () != MATCH_YES)
	{
	  gfc_error ("Unexpected junk after !$OMP DECLARE TARGET at %C");
	  goto cleanup;
	}
    }
  else if (gfc_match_omp_clauses (&c, OMP_DECLARE_TARGET_CLAUSES) != MATCH_YES)
    return MATCH_ERROR;

  gfc_buffer_error (false);

  for (list = OMP_LIST_TO; list != OMP_LIST_NUM;
       list = (list == OMP_LIST_TO ? OMP_LIST_LINK : OMP_LIST_NUM))
    for (n = c->lists[list]; n; n = n->next)
      if (n->sym)
	n->sym->mark = 0;
      else if (n->u.common->head)
	n->u.common->head->mark = 0;

  for (list = OMP_LIST_TO; list != OMP_LIST_NUM;
       list = (list == OMP_LIST_TO ? OMP_LIST_LINK : OMP_LIST_NUM))
    for (n = c->lists[list]; n; n = n->next)
      if (n->sym)
	{
	  if (n->sym->attr.in_common)
	    gfc_error_now ("OMP DECLARE TARGET variable at %L is an "
			   "element of a COMMON block", &n->where);
	  else if (n->sym->attr.omp_declare_target
		   && n->sym->attr.omp_declare_target_link
		   && list != OMP_LIST_LINK)
	    gfc_error_now ("OMP DECLARE TARGET variable at %L previously "
			   "mentioned in LINK clause and later in TO clause",
			   &n->where);
	  else if (n->sym->attr.omp_declare_target
		   && !n->sym->attr.omp_declare_target_link
		   && list == OMP_LIST_LINK)
	    gfc_error_now ("OMP DECLARE TARGET variable at %L previously "
			   "mentioned in TO clause and later in LINK clause",
			   &n->where);
	  else if (n->sym->mark)
	    gfc_error_now ("Variable at %L mentioned multiple times in "
			   "clauses of the same OMP DECLARE TARGET directive",
			   &n->where);
	  else if (gfc_add_omp_declare_target (&n->sym->attr, n->sym->name,
					       &n->sym->declared_at))
	    {
	      if (list == OMP_LIST_LINK)
		gfc_add_omp_declare_target_link (&n->sym->attr, n->sym->name,
						 &n->sym->declared_at);
	    }
	  if (c->device_type != OMP_DEVICE_TYPE_UNSET)
	    {
	      if (n->sym->attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
		  && n->sym->attr.omp_device_type != c->device_type)
		gfc_error_now ("List item %qs at %L set in previous OMP DECLARE "
			       "TARGET directive to a different DEVICE_TYPE",
			       n->sym->name, &n->where);
	      n->sym->attr.omp_device_type = c->device_type;
	    }
	  n->sym->mark = 1;
	}
      else if (n->u.common->omp_declare_target
	       && n->u.common->omp_declare_target_link
	       && list != OMP_LIST_LINK)
	gfc_error_now ("OMP DECLARE TARGET COMMON at %L previously "
		       "mentioned in LINK clause and later in TO clause",
		       &n->where);
      else if (n->u.common->omp_declare_target
	       && !n->u.common->omp_declare_target_link
	       && list == OMP_LIST_LINK)
	gfc_error_now ("OMP DECLARE TARGET COMMON at %L previously "
		       "mentioned in TO clause and later in LINK clause",
		       &n->where);
      else if (n->u.common->head && n->u.common->head->mark)
	gfc_error_now ("COMMON at %L mentioned multiple times in "
		       "clauses of the same OMP DECLARE TARGET directive",
		       &n->where);
      else
	{
	  n->u.common->omp_declare_target = 1;
	  n->u.common->omp_declare_target_link = (list == OMP_LIST_LINK);
	  if (n->u.common->omp_device_type != OMP_DEVICE_TYPE_UNSET
	      && n->u.common->omp_device_type != c->device_type)
	    gfc_error_now ("COMMON at %L set in previous OMP DECLARE "
			   "TARGET directive to a different DEVICE_TYPE",
			   &n->where);
	  n->u.common->omp_device_type = c->device_type;

	  for (s = n->u.common->head; s; s = s->common_next)
	    {
	      s->mark = 1;
	      if (gfc_add_omp_declare_target (&s->attr, s->name,
					      &s->declared_at))
		{
		  if (list == OMP_LIST_LINK)
		    gfc_add_omp_declare_target_link (&s->attr, s->name,
						     &s->declared_at);
		}
	      if (s->attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
		  && s->attr.omp_device_type != c->device_type)
		gfc_error_now ("List item %qs at %L set in previous OMP DECLARE"
			       " TARGET directive to a different DEVICE_TYPE",
			       s->name, &n->where);
	      s->attr.omp_device_type = c->device_type;
	    }
	}
  if (c->device_type && !c->lists[OMP_LIST_TO] && !c->lists[OMP_LIST_LINK])
    gfc_warning_now (0, "OMP DECLARE TARGET directive at %L with only "
			"DEVICE_TYPE clause is ignored", &old_loc);

  gfc_buffer_error (true);

  if (c)
    gfc_free_omp_clauses (c);
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in !$OMP DECLARE TARGET list at %C");

cleanup:
  gfc_current_locus = old_loc;
  if (c)
    gfc_free_omp_clauses (c);
  return MATCH_ERROR;
}


match
gfc_match_omp_threadprivate (void)
{
  locus old_loc;
  char n[GFC_MAX_SYMBOL_LEN+1];
  gfc_symbol *sym;
  match m;
  gfc_symtree *st;

  old_loc = gfc_current_locus;

  m = gfc_match (" (");
  if (m != MATCH_YES)
    return m;

  for (;;)
    {
      m = gfc_match_symbol (&sym, 0);
      switch (m)
	{
	case MATCH_YES:
	  if (sym->attr.in_common)
	    gfc_error_now ("Threadprivate variable at %C is an element of "
			   "a COMMON block");
	  else if (!gfc_add_threadprivate (&sym->attr, sym->name, &sym->declared_at))
	    goto cleanup;
	  goto next_item;
	case MATCH_NO:
	  break;
	case MATCH_ERROR:
	  goto cleanup;
	}

      m = gfc_match (" / %n /", n);
      if (m == MATCH_ERROR)
	goto cleanup;
      if (m == MATCH_NO || n[0] == '\0')
	goto syntax;

      st = gfc_find_symtree (gfc_current_ns->common_root, n);
      if (st == NULL)
	{
	  gfc_error ("COMMON block /%s/ not found at %C", n);
	  goto cleanup;
	}
      st->n.common->threadprivate = 1;
      for (sym = st->n.common->head; sym; sym = sym->common_next)
	if (!gfc_add_threadprivate (&sym->attr, sym->name, &sym->declared_at))
	  goto cleanup;

    next_item:
      if (gfc_match_char (')') == MATCH_YES)
	break;
      if (gfc_match_char (',') != MATCH_YES)
	goto syntax;
    }

  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after OMP THREADPRIVATE at %C");
      goto cleanup;
    }

  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in !$OMP THREADPRIVATE list at %C");

cleanup:
  gfc_current_locus = old_loc;
  return MATCH_ERROR;
}


match
gfc_match_omp_parallel (void)
{
  return match_omp (EXEC_OMP_PARALLEL, OMP_PARALLEL_CLAUSES);
}


match
gfc_match_omp_parallel_do (void)
{
  return match_omp (EXEC_OMP_PARALLEL_DO,
		    OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES);
}


match
gfc_match_omp_parallel_do_simd (void)
{
  return match_omp (EXEC_OMP_PARALLEL_DO_SIMD,
		    OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES | OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_parallel_sections (void)
{
  return match_omp (EXEC_OMP_PARALLEL_SECTIONS,
		    OMP_PARALLEL_CLAUSES | OMP_SECTIONS_CLAUSES);
}


match
gfc_match_omp_parallel_workshare (void)
{
  return match_omp (EXEC_OMP_PARALLEL_WORKSHARE, OMP_PARALLEL_CLAUSES);
}

void
gfc_check_omp_requires (gfc_namespace *ns, int ref_omp_requires)
{
  if (ns->omp_target_seen
      && (ns->omp_requires & OMP_REQ_TARGET_MASK)
	 != (ref_omp_requires & OMP_REQ_TARGET_MASK))
    {
      gcc_assert (ns->proc_name);
      if ((ref_omp_requires & OMP_REQ_REVERSE_OFFLOAD)
	  && !(ns->omp_requires & OMP_REQ_REVERSE_OFFLOAD))
	gfc_error ("Program unit at %L has OpenMP device constructs/routines "
		   "but does not set !$OMP REQUIRES REVERSE_OFFSET but other "
		   "program units do", &ns->proc_name->declared_at);
      if ((ref_omp_requires & OMP_REQ_UNIFIED_ADDRESS)
	  && !(ns->omp_requires & OMP_REQ_UNIFIED_ADDRESS))
	gfc_error ("Program unit at %L has OpenMP device constructs/routines "
		   "but does not set !$OMP REQUIRES UNIFIED_ADDRESS but other "
		   "program units do", &ns->proc_name->declared_at);
      if ((ref_omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY)
	  && !(ns->omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY))
	gfc_error ("Program unit at %L has OpenMP device constructs/routines "
		   "but does not set !$OMP REQUIRES UNIFIED_SHARED_MEMORY but "
		   "other program units do", &ns->proc_name->declared_at);
    }
}

bool
gfc_omp_requires_add_clause (gfc_omp_requires_kind clause,
			     const char *clause_name, locus *loc,
			     const char *module_name)
{
  gfc_namespace *prog_unit = gfc_current_ns;
  while (prog_unit->parent)
    {
      if (gfc_state_stack->previous
	  && gfc_state_stack->previous->state == COMP_INTERFACE)
	break;
      prog_unit = prog_unit->parent;
    }

  /* Requires added after use.  */
  if (prog_unit->omp_target_seen
      && (clause & OMP_REQ_TARGET_MASK)
      && !(prog_unit->omp_requires & clause))
    {
      if (module_name)
	gfc_error ("!$OMP REQUIRES clause %qs specified via module %qs use "
		   "at %L comes after using a device construct/routine",
		   clause_name, module_name, loc);
      else
	gfc_error ("!$OMP REQUIRES clause %qs specified at %L comes after "
		   "using a device construct/routine", clause_name, loc);
      return false;
    }

  /* Overriding atomic_default_mem_order clause value.  */
  if ((clause & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
      && (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
      && (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
	 != (int) clause)
    {
      const char *other;
      if (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST)
	other = "seq_cst";
      else if (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL)
	other = "acq_rel";
      else if (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_RELAXED)
	other = "relaxed";
      else
	gcc_unreachable ();

      if (module_name)
	gfc_error ("!$OMP REQUIRES clause %<atomic_default_mem_order(%s)%> "
		   "specified via module %qs use at %L overrides a previous "
		   "%<atomic_default_mem_order(%s)%> (which might be through "
		   "using a module)", clause_name, module_name, loc, other);
      else
	gfc_error ("!$OMP REQUIRES clause %<atomic_default_mem_order(%s)%> "
		   "specified at %L overrides a previous "
		   "%<atomic_default_mem_order(%s)%> (which might be through "
		   "using a module)", clause_name, loc, other);
      return false;
    }

  /* Requires via module not at program-unit level and not repeating clause.  */
  if (prog_unit != gfc_current_ns && !(prog_unit->omp_requires & clause))
    {
      if (clause & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
	gfc_error ("!$OMP REQUIRES clause %<atomic_default_mem_order(%s)%> "
		   "specified via module %qs use at %L but same clause is "
		   "not set at for the program unit", clause_name, module_name,
		   loc);
      else
	gfc_error ("!$OMP REQUIRES clause %qs specified via module %qs use at "
		   "%L but same clause is not set at for the program unit",
		   clause_name, module_name, loc);
      return false;
    }

  if (!gfc_state_stack->previous
      || gfc_state_stack->previous->state != COMP_INTERFACE)
    prog_unit->omp_requires |= clause;
  return true;
}

match
gfc_match_omp_requires (void)
{
  static const char *clauses[] = {"reverse_offload",
				  "unified_address",
				  "unified_shared_memory",
				  "dynamic_allocators",
				  "atomic_default"};
  const char *clause = NULL;
  int requires_clauses = 0;
  bool first = true;
  locus old_loc;

  if (gfc_current_ns->parent
      && (!gfc_state_stack->previous
	  || gfc_state_stack->previous->state != COMP_INTERFACE))
    {
      gfc_error ("!$OMP REQUIRES at %C must appear in the specification part "
		 "of a program unit");
      return MATCH_ERROR;
    }

  while (true)
    {
      old_loc = gfc_current_locus;
      gfc_omp_requires_kind requires_clause;
      if ((first || gfc_match_char (',') != MATCH_YES)
	  && (first && gfc_match_space () != MATCH_YES))
	goto error;
      first = false;
      gfc_gobble_whitespace ();
      old_loc = gfc_current_locus;

      if (gfc_match_omp_eos () != MATCH_NO)
	break;
      if (gfc_match (clauses[0]) == MATCH_YES)
	{
	  clause = clauses[0];
	  requires_clause = OMP_REQ_REVERSE_OFFLOAD;
	  if (requires_clauses & OMP_REQ_REVERSE_OFFLOAD)
	    goto duplicate_clause;
	}
      else if (gfc_match (clauses[1]) == MATCH_YES)
	{
	  clause = clauses[1];
	  requires_clause = OMP_REQ_UNIFIED_ADDRESS;
	  if (requires_clauses & OMP_REQ_UNIFIED_ADDRESS)
	    goto duplicate_clause;
	}
      else if (gfc_match (clauses[2]) == MATCH_YES)
	{
	  clause = clauses[2];
	  requires_clause = OMP_REQ_UNIFIED_SHARED_MEMORY;
	  if (requires_clauses & OMP_REQ_UNIFIED_SHARED_MEMORY)
	    goto duplicate_clause;
	}
      else if (gfc_match (clauses[3]) == MATCH_YES)
	{
	  clause = clauses[3];
	  requires_clause = OMP_REQ_DYNAMIC_ALLOCATORS;
	  if (requires_clauses & OMP_REQ_DYNAMIC_ALLOCATORS)
	    goto duplicate_clause;
	}
      else if (gfc_match ("atomic_default_mem_order (") == MATCH_YES)
	{
	  clause = clauses[4];
	  if (requires_clauses & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
	    goto duplicate_clause;
	  if (gfc_match (" seq_cst )") == MATCH_YES)
	    {
	      clause = "seq_cst";
	      requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST;
	    }
	  else if (gfc_match (" acq_rel )") == MATCH_YES)
	    {
	      clause = "acq_rel";
	      requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL;
	    }
	  else if (gfc_match (" relaxed )") == MATCH_YES)
	    {
	      clause = "relaxed";
	      requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_RELAXED;
	    }
	  else
	    {
	      gfc_error ("Expected SEQ_CST, ACQ_REL or RELAXED for "
			 "ATOMIC_DEFAULT_MEM_ORDER clause at %C");
	      goto error;
	    }
	}
      else
	goto error;

      if (requires_clause & ~OMP_REQ_ATOMIC_MEM_ORDER_MASK)
	gfc_error_now ("Sorry, %qs clause at %L on REQUIRES directive is not "
		       "yet supported", clause, &old_loc);
      if (!gfc_omp_requires_add_clause (requires_clause, clause, &old_loc, NULL))
	goto error;
      requires_clauses |= requires_clause;
    }

  if (requires_clauses == 0)
    {
      if (!gfc_error_flag_test ())
	gfc_error ("Clause expected at %C");
      goto error;
    }
  return MATCH_YES;

duplicate_clause:
  gfc_error ("%qs clause at %L specified more than once", clause, &old_loc);
error:
  if (!gfc_error_flag_test ())
    gfc_error ("Expected UNIFIED_ADDRESS, UNIFIED_SHARED_MEMORY, "
	       "DYNAMIC_ALLOCATORS, REVERSE_OFFLOAD, or "
	       "ATOMIC_DEFAULT_MEM_ORDER clause at %L", &old_loc);
  return MATCH_ERROR;
}


match
gfc_match_omp_scan (void)
{
  bool incl;
  gfc_omp_clauses *c = gfc_get_omp_clauses ();
  gfc_gobble_whitespace ();
  if ((incl = (gfc_match ("inclusive") == MATCH_YES))
      || gfc_match ("exclusive") == MATCH_YES)
    {
      if (gfc_match_omp_variable_list (" (", &c->lists[incl ? OMP_LIST_SCAN_IN
							    : OMP_LIST_SCAN_EX],
				       false) != MATCH_YES)
	{
	  gfc_free_omp_clauses (c);
	  return MATCH_ERROR;
	}
    }
  else
    {
      gfc_error ("Expected INCLUSIVE or EXCLUSIVE clause at %C");
      gfc_free_omp_clauses (c);
      return MATCH_ERROR;
    }
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after !$OMP SCAN at %C");
      gfc_free_omp_clauses (c);
      return MATCH_ERROR;
    }

  new_st.op = EXEC_OMP_SCAN;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_omp_sections (void)
{
  return match_omp (EXEC_OMP_SECTIONS, OMP_SECTIONS_CLAUSES);
}


match
gfc_match_omp_simd (void)
{
  return match_omp (EXEC_OMP_SIMD, OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_single (void)
{
  return match_omp (EXEC_OMP_SINGLE, OMP_SINGLE_CLAUSES);
}


match
gfc_match_omp_target (void)
{
  return match_omp (EXEC_OMP_TARGET, OMP_TARGET_CLAUSES);
}


match
gfc_match_omp_target_data (void)
{
  return match_omp (EXEC_OMP_TARGET_DATA, OMP_TARGET_DATA_CLAUSES);
}


match
gfc_match_omp_target_enter_data (void)
{
  return match_omp (EXEC_OMP_TARGET_ENTER_DATA, OMP_TARGET_ENTER_DATA_CLAUSES);
}


match
gfc_match_omp_target_exit_data (void)
{
  return match_omp (EXEC_OMP_TARGET_EXIT_DATA, OMP_TARGET_EXIT_DATA_CLAUSES);
}


match
gfc_match_omp_target_parallel (void)
{
  return match_omp (EXEC_OMP_TARGET_PARALLEL,
		    (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_COPYIN)));
}


match
gfc_match_omp_target_parallel_do (void)
{
  return match_omp (EXEC_OMP_TARGET_PARALLEL_DO,
		    (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES
		     | OMP_DO_CLAUSES) & ~(omp_mask (OMP_CLAUSE_COPYIN)));
}


match
gfc_match_omp_target_parallel_do_simd (void)
{
  return match_omp (EXEC_OMP_TARGET_PARALLEL_DO_SIMD,
		    (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES
		     | OMP_SIMD_CLAUSES) & ~(omp_mask (OMP_CLAUSE_COPYIN)));
}


match
gfc_match_omp_target_simd (void)
{
  return match_omp (EXEC_OMP_TARGET_SIMD,
		    OMP_TARGET_CLAUSES | OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_target_teams (void)
{
  return match_omp (EXEC_OMP_TARGET_TEAMS,
		    OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES);
}


match
gfc_match_omp_target_teams_distribute (void)
{
  return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE,
		    OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
		    | OMP_DISTRIBUTE_CLAUSES);
}


match
gfc_match_omp_target_teams_distribute_parallel_do (void)
{
  return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO,
		    (OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
		     | OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
		     | OMP_DO_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_ORDERED))
		    & ~(omp_mask (OMP_CLAUSE_LINEAR)));
}


match
gfc_match_omp_target_teams_distribute_parallel_do_simd (void)
{
  return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD,
		    (OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
		     | OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
		     | OMP_DO_CLAUSES | OMP_SIMD_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_ORDERED)));
}


match
gfc_match_omp_target_teams_distribute_simd (void)
{
  return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD,
		    OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
		    | OMP_DISTRIBUTE_CLAUSES | OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_target_update (void)
{
  return match_omp (EXEC_OMP_TARGET_UPDATE, OMP_TARGET_UPDATE_CLAUSES);
}


match
gfc_match_omp_task (void)
{
  return match_omp (EXEC_OMP_TASK, OMP_TASK_CLAUSES);
}


match
gfc_match_omp_taskloop (void)
{
  return match_omp (EXEC_OMP_TASKLOOP, OMP_TASKLOOP_CLAUSES);
}


match
gfc_match_omp_taskloop_simd (void)
{
  return match_omp (EXEC_OMP_TASKLOOP_SIMD,
		    (OMP_TASKLOOP_CLAUSES | OMP_SIMD_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_REDUCTION)));
}


match
gfc_match_omp_taskwait (void)
{
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after TASKWAIT clause at %C");
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_TASKWAIT;
  new_st.ext.omp_clauses = NULL;
  return MATCH_YES;
}


match
gfc_match_omp_taskyield (void)
{
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after TASKYIELD clause at %C");
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_TASKYIELD;
  new_st.ext.omp_clauses = NULL;
  return MATCH_YES;
}


match
gfc_match_omp_teams (void)
{
  return match_omp (EXEC_OMP_TEAMS, OMP_TEAMS_CLAUSES);
}


match
gfc_match_omp_teams_distribute (void)
{
  return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE,
		    OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES);
}


match
gfc_match_omp_teams_distribute_parallel_do (void)
{
  return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO,
		    (OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES
		     | OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES)
		    & ~(omp_mask (OMP_CLAUSE_ORDERED))
		    & ~(omp_mask (OMP_CLAUSE_LINEAR)));
}


match
gfc_match_omp_teams_distribute_parallel_do_simd (void)
{
  return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD,
		    (OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES
		     | OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES
		     | OMP_SIMD_CLAUSES) & ~(omp_mask (OMP_CLAUSE_ORDERED)));
}


match
gfc_match_omp_teams_distribute_simd (void)
{
  return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE_SIMD,
		    OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES
		    | OMP_SIMD_CLAUSES);
}


match
gfc_match_omp_workshare (void)
{
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after $OMP WORKSHARE statement at %C");
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_WORKSHARE;
  new_st.ext.omp_clauses = gfc_get_omp_clauses ();
  return MATCH_YES;
}


match
gfc_match_omp_master (void)
{
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after $OMP MASTER statement at %C");
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_MASTER;
  new_st.ext.omp_clauses = NULL;
  return MATCH_YES;
}


match
gfc_match_omp_ordered (void)
{
  return match_omp (EXEC_OMP_ORDERED, OMP_ORDERED_CLAUSES);
}


match
gfc_match_omp_ordered_depend (void)
{
  return match_omp (EXEC_OMP_ORDERED, omp_mask (OMP_CLAUSE_DEPEND));
}


/* omp atomic [clause-list]
   - atomic-clause:  read | write | update
   - capture
   - memory-order-clause: seq_cst | acq_rel | release | acquire | relaxed
   - hint(hint-expr)
*/

match
gfc_match_omp_atomic (void)
{
  gfc_omp_clauses *c;
  locus loc = gfc_current_locus;

  if (gfc_match_omp_clauses (&c, OMP_ATOMIC_CLAUSES, true, true) != MATCH_YES)
    return MATCH_ERROR;

  if (c->capture && c->atomic_op != GFC_OMP_ATOMIC_UNSET)
    gfc_error ("OMP ATOMIC at %L with multiple atomic clauses", &loc);

  if (c->atomic_op == GFC_OMP_ATOMIC_UNSET)
    c->atomic_op = GFC_OMP_ATOMIC_UPDATE;

  if (c->memorder == OMP_MEMORDER_UNSET)
    {
      gfc_namespace *prog_unit = gfc_current_ns;
      while (prog_unit->parent)
	prog_unit = prog_unit->parent;
      switch (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
	{
	case 0:
	case OMP_REQ_ATOMIC_MEM_ORDER_RELAXED:
	  c->memorder = OMP_MEMORDER_RELAXED;
	  break;
	case OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST:
	  c->memorder = OMP_MEMORDER_SEQ_CST;
	  break;
	case OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL:
	  if (c->capture)
	    c->memorder = OMP_MEMORDER_ACQ_REL;
	  else if (c->atomic_op == GFC_OMP_ATOMIC_READ)
	    c->memorder = OMP_MEMORDER_ACQUIRE;
	  else
	    c->memorder = OMP_MEMORDER_RELEASE;
	  break;
	default:
	  gcc_unreachable ();
	}
    }
  else
    switch (c->atomic_op)
      {
      case GFC_OMP_ATOMIC_READ:
	if (c->memorder == OMP_MEMORDER_ACQ_REL
	    || c->memorder == OMP_MEMORDER_RELEASE)
	  {
	    gfc_error ("!$OMP ATOMIC READ at %L incompatible with "
		       "ACQ_REL or RELEASE clauses", &loc);
	    c->memorder = OMP_MEMORDER_SEQ_CST;
	  }
	break;
      case GFC_OMP_ATOMIC_WRITE:
	if (c->memorder == OMP_MEMORDER_ACQ_REL
	    || c->memorder == OMP_MEMORDER_ACQUIRE)
	  {
	    gfc_error ("!$OMP ATOMIC WRITE at %L incompatible with "
		       "ACQ_REL or ACQUIRE clauses", &loc);
	    c->memorder = OMP_MEMORDER_SEQ_CST;
	  }
	break;
      case GFC_OMP_ATOMIC_UPDATE:
	if ((c->memorder == OMP_MEMORDER_ACQ_REL
	     || c->memorder == OMP_MEMORDER_ACQUIRE)
	    && !c->capture)
	  {
	    gfc_error ("!$OMP ATOMIC UPDATE at %L incompatible with "
		       "ACQ_REL or ACQUIRE clauses", &loc);
	    c->memorder = OMP_MEMORDER_SEQ_CST;
	  }
	break;
      default:
	break;
      }
  gfc_error_check ();
  new_st.ext.omp_clauses = c;
  new_st.op = EXEC_OMP_ATOMIC;
  return MATCH_YES;
}


/* acc atomic [ read | write | update | capture]  */

match
gfc_match_oacc_atomic (void)
{
  gfc_omp_clauses *c = gfc_get_omp_clauses ();
  c->atomic_op = GFC_OMP_ATOMIC_UPDATE;
  c->memorder = OMP_MEMORDER_RELAXED;
  gfc_gobble_whitespace ();
  if (gfc_match ("update") == MATCH_YES)
    ;
  else if (gfc_match ("read") == MATCH_YES)
    c->atomic_op = GFC_OMP_ATOMIC_READ;
  else if (gfc_match ("write") == MATCH_YES)
    c->atomic_op = GFC_OMP_ATOMIC_WRITE;
  else if (gfc_match ("capture") == MATCH_YES)
    c->capture = true;
  gfc_gobble_whitespace ();
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after !$ACC ATOMIC statement at %C");
      gfc_free_omp_clauses (c);
      return MATCH_ERROR;
    }
  new_st.ext.omp_clauses = c;
  new_st.op = EXEC_OACC_ATOMIC;
  return MATCH_YES;
}


match
gfc_match_omp_barrier (void)
{
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after $OMP BARRIER statement at %C");
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_BARRIER;
  new_st.ext.omp_clauses = NULL;
  return MATCH_YES;
}


match
gfc_match_omp_taskgroup (void)
{
  return match_omp (EXEC_OMP_TASKGROUP, OMP_CLAUSE_TASK_REDUCTION);
}


static enum gfc_omp_cancel_kind
gfc_match_omp_cancel_kind (void)
{
  if (gfc_match_space () != MATCH_YES)
    return OMP_CANCEL_UNKNOWN;
  if (gfc_match ("parallel") == MATCH_YES)
    return OMP_CANCEL_PARALLEL;
  if (gfc_match ("sections") == MATCH_YES)
    return OMP_CANCEL_SECTIONS;
  if (gfc_match ("do") == MATCH_YES)
    return OMP_CANCEL_DO;
  if (gfc_match ("taskgroup") == MATCH_YES)
    return OMP_CANCEL_TASKGROUP;
  return OMP_CANCEL_UNKNOWN;
}


match
gfc_match_omp_cancel (void)
{
  gfc_omp_clauses *c;
  enum gfc_omp_cancel_kind kind = gfc_match_omp_cancel_kind ();
  if (kind == OMP_CANCEL_UNKNOWN)
    return MATCH_ERROR;
  if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_IF), false) != MATCH_YES)
    return MATCH_ERROR;
  c->cancel = kind;
  new_st.op = EXEC_OMP_CANCEL;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_omp_cancellation_point (void)
{
  gfc_omp_clauses *c;
  enum gfc_omp_cancel_kind kind = gfc_match_omp_cancel_kind ();
  if (kind == OMP_CANCEL_UNKNOWN)
    return MATCH_ERROR;
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after $OMP CANCELLATION POINT statement "
		 "at %C");
      return MATCH_ERROR;
    }
  c = gfc_get_omp_clauses ();
  c->cancel = kind;
  new_st.op = EXEC_OMP_CANCELLATION_POINT;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


match
gfc_match_omp_end_nowait (void)
{
  bool nowait = false;
  if (gfc_match ("% nowait") == MATCH_YES)
    nowait = true;
  if (gfc_match_omp_eos () != MATCH_YES)
    {
      gfc_error ("Unexpected junk after NOWAIT clause at %C");
      return MATCH_ERROR;
    }
  new_st.op = EXEC_OMP_END_NOWAIT;
  new_st.ext.omp_bool = nowait;
  return MATCH_YES;
}


match
gfc_match_omp_end_single (void)
{
  gfc_omp_clauses *c;
  if (gfc_match ("% nowait") == MATCH_YES)
    {
      new_st.op = EXEC_OMP_END_NOWAIT;
      new_st.ext.omp_bool = true;
      return MATCH_YES;
    }
  if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_COPYPRIVATE))
      != MATCH_YES)
    return MATCH_ERROR;
  new_st.op = EXEC_OMP_END_SINGLE;
  new_st.ext.omp_clauses = c;
  return MATCH_YES;
}


static bool
oacc_is_loop (gfc_code *code)
{
  return code->op == EXEC_OACC_PARALLEL_LOOP
	 || code->op == EXEC_OACC_KERNELS_LOOP
	 || code->op == EXEC_OACC_SERIAL_LOOP
	 || code->op == EXEC_OACC_LOOP;
}

static void
resolve_scalar_int_expr (gfc_expr *expr, const char *clause)
{
  if (!gfc_resolve_expr (expr)
      || expr->ts.type != BT_INTEGER
      || expr->rank != 0)
    gfc_error ("%s clause at %L requires a scalar INTEGER expression",
	       clause, &expr->where);
}

static void
resolve_positive_int_expr (gfc_expr *expr, const char *clause)
{
  resolve_scalar_int_expr (expr, clause);
  if (expr->expr_type == EXPR_CONSTANT
      && expr->ts.type == BT_INTEGER
      && mpz_sgn (expr->value.integer) <= 0)
    gfc_warning (0, "INTEGER expression of %s clause at %L must be positive",
		 clause, &expr->where);
}

static void
resolve_nonnegative_int_expr (gfc_expr *expr, const char *clause)
{
  resolve_scalar_int_expr (expr, clause);
  if (expr->expr_type == EXPR_CONSTANT
      && expr->ts.type == BT_INTEGER
      && mpz_sgn (expr->value.integer) < 0)
    gfc_warning (0, "INTEGER expression of %s clause at %L must be "
		 "non-negative", clause, &expr->where);
}

/* Emits error when symbol is pointer, cray pointer or cray pointee
   of derived of polymorphic type.  */

static void
check_symbol_not_pointer (gfc_symbol *sym, locus loc, const char *name)
{
  if (sym->ts.type == BT_DERIVED && sym->attr.cray_pointer)
    gfc_error ("Cray pointer object %qs of derived type in %s clause at %L",
	       sym->name, name, &loc);
  if (sym->ts.type == BT_DERIVED && sym->attr.cray_pointee)
    gfc_error ("Cray pointee object %qs of derived type in %s clause at %L",
	       sym->name, name, &loc);

  if ((sym->ts.type == BT_ASSUMED && sym->attr.pointer)
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.pointer))
    gfc_error ("POINTER object %qs of polymorphic type in %s clause at %L",
	       sym->name, name, &loc);
  if ((sym->ts.type == BT_ASSUMED && sym->attr.cray_pointer)
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.cray_pointer))
    gfc_error ("Cray pointer object %qs of polymorphic type in %s clause at %L",
	       sym->name, name, &loc);
  if ((sym->ts.type == BT_ASSUMED && sym->attr.cray_pointee)
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.cray_pointee))
    gfc_error ("Cray pointee object %qs of polymorphic type in %s clause at %L",
	       sym->name, name, &loc);
}

/* Emits error when symbol represents assumed size/rank array.  */

static void
check_array_not_assumed (gfc_symbol *sym, locus loc, const char *name)
{
  if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
    gfc_error ("Assumed size array %qs in %s clause at %L",
	       sym->name, name, &loc);
  if (sym->as && sym->as->type == AS_ASSUMED_RANK)
    gfc_error ("Assumed rank array %qs in %s clause at %L",
	       sym->name, name, &loc);
}

static void
resolve_oacc_data_clauses (gfc_symbol *sym, locus loc, const char *name)
{
  check_array_not_assumed (sym, loc, name);
}

static void
resolve_oacc_deviceptr_clause (gfc_symbol *sym, locus loc, const char *name)
{
  if (sym->attr.pointer
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.class_pointer))
    gfc_error ("POINTER object %qs in %s clause at %L",
	       sym->name, name, &loc);
  if (sym->attr.cray_pointer
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.cray_pointer))
    gfc_error ("Cray pointer object %qs in %s clause at %L",
	       sym->name, name, &loc);
  if (sym->attr.cray_pointee
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.cray_pointee))
    gfc_error ("Cray pointee object %qs in %s clause at %L",
	       sym->name, name, &loc);
  if (sym->attr.allocatable
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
	  && CLASS_DATA (sym)->attr.allocatable))
    gfc_error ("ALLOCATABLE object %qs in %s clause at %L",
	       sym->name, name, &loc);
  if (sym->attr.value)
    gfc_error ("VALUE object %qs in %s clause at %L",
	       sym->name, name, &loc);
  check_array_not_assumed (sym, loc, name);
}


struct resolve_omp_udr_callback_data
{
  gfc_symbol *sym1, *sym2;
};


static int
resolve_omp_udr_callback (gfc_expr **e, int *, void *data)
{
  struct resolve_omp_udr_callback_data *rcd
    = (struct resolve_omp_udr_callback_data *) data;
  if ((*e)->expr_type == EXPR_VARIABLE
      && ((*e)->symtree->n.sym == rcd->sym1
	  || (*e)->symtree->n.sym == rcd->sym2))
    {
      gfc_ref *ref = gfc_get_ref ();
      ref->type = REF_ARRAY;
      ref->u.ar.where = (*e)->where;
      ref->u.ar.as = (*e)->symtree->n.sym->as;
      ref->u.ar.type = AR_FULL;
      ref->u.ar.dimen = 0;
      ref->next = (*e)->ref;
      (*e)->ref = ref;
    }
  return 0;
}


static int
resolve_omp_udr_callback2 (gfc_expr **e, int *, void *)
{
  if ((*e)->expr_type == EXPR_FUNCTION
      && (*e)->value.function.isym == NULL)
    {
      gfc_symbol *sym = (*e)->symtree->n.sym;
      if (!sym->attr.intrinsic
	  && sym->attr.if_source == IFSRC_UNKNOWN)
	gfc_error ("Implicitly declared function %s used in "
		   "!$OMP DECLARE REDUCTION at %L", sym->name, &(*e)->where);
    }
  return 0;
}


static gfc_code *
resolve_omp_udr_clause (gfc_omp_namelist *n, gfc_namespace *ns,
			gfc_symbol *sym1, gfc_symbol *sym2)
{
  gfc_code *copy;
  gfc_symbol sym1_copy, sym2_copy;

  if (ns->code->op == EXEC_ASSIGN)
    {
      copy = gfc_get_code (EXEC_ASSIGN);
      copy->expr1 = gfc_copy_expr (ns->code->expr1);
      copy->expr2 = gfc_copy_expr (ns->code->expr2);
    }
  else
    {
      copy = gfc_get_code (EXEC_CALL);
      copy->symtree = ns->code->symtree;
      copy->ext.actual = gfc_copy_actual_arglist (ns->code->ext.actual);
    }
  copy->loc = ns->code->loc;
  sym1_copy = *sym1;
  sym2_copy = *sym2;
  *sym1 = *n->sym;
  *sym2 = *n->sym;
  sym1->name = sym1_copy.name;
  sym2->name = sym2_copy.name;
  ns->proc_name = ns->parent->proc_name;
  if (n->sym->attr.dimension)
    {
      struct resolve_omp_udr_callback_data rcd;
      rcd.sym1 = sym1;
      rcd.sym2 = sym2;
      gfc_code_walker (&copy, gfc_dummy_code_callback,
		       resolve_omp_udr_callback, &rcd);
    }
  gfc_resolve_code (copy, gfc_current_ns);
  if (copy->op == EXEC_CALL && copy->resolved_isym == NULL)
    {
      gfc_symbol *sym = copy->resolved_sym;
      if (sym
	  && !sym->attr.intrinsic
	  && sym->attr.if_source == IFSRC_UNKNOWN)
	gfc_error ("Implicitly declared subroutine %s used in "
		   "!$OMP DECLARE REDUCTION at %L", sym->name,
		   &copy->loc);
    }
  gfc_code_walker (&copy, gfc_dummy_code_callback,
		   resolve_omp_udr_callback2, NULL);
  *sym1 = sym1_copy;
  *sym2 = sym2_copy;
  return copy;
}

/* OpenMP directive resolving routines.  */

static void
resolve_omp_clauses (gfc_code *code, gfc_omp_clauses *omp_clauses,
		     gfc_namespace *ns, bool openacc = false)
{
  gfc_omp_namelist *n;
  gfc_expr_list *el;
  int list;
  int ifc;
  bool if_without_mod = false;
  gfc_omp_linear_op linear_op = OMP_LINEAR_DEFAULT;
  static const char *clause_names[]
    = { "PRIVATE", "FIRSTPRIVATE", "LASTPRIVATE", "COPYPRIVATE", "SHARED",
	"COPYIN", "UNIFORM", "ALIGNED", "LINEAR", "DEPEND", "MAP",
	"TO", "FROM", "INCLUSIVE", "EXCLUSIVE",
	"REDUCTION", "REDUCTION" /*inscan*/, "REDUCTION" /*task*/,
	"IN_REDUCTION", "TASK_REDUCTION",
	"DEVICE_RESIDENT", "LINK", "USE_DEVICE",
	"CACHE", "IS_DEVICE_PTR", "USE_DEVICE_PTR", "USE_DEVICE_ADDR",
	"NONTEMPORAL" };
  STATIC_ASSERT (ARRAY_SIZE (clause_names) == OMP_LIST_NUM);

  if (omp_clauses == NULL)
    return;

  if (omp_clauses->orderedc && omp_clauses->orderedc < omp_clauses->collapse)
    gfc_error ("ORDERED clause parameter is less than COLLAPSE at %L",
	       &code->loc);

  if (omp_clauses->if_expr)
    {
      gfc_expr *expr = omp_clauses->if_expr;
      if (!gfc_resolve_expr (expr)
	  || expr->ts.type != BT_LOGICAL || expr->rank != 0)
	gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
		   &expr->where);
      if_without_mod = true;
    }
  for (ifc = 0; ifc < OMP_IF_LAST; ifc++)
    if (omp_clauses->if_exprs[ifc])
      {
	gfc_expr *expr = omp_clauses->if_exprs[ifc];
	bool ok = true;
	if (!gfc_resolve_expr (expr)
	    || expr->ts.type != BT_LOGICAL || expr->rank != 0)
	  gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
		     &expr->where);
	else if (if_without_mod)
	  {
	    gfc_error ("IF clause without modifier at %L used together with "
		       "IF clauses with modifiers",
		       &omp_clauses->if_expr->where);
	    if_without_mod = false;
	  }
	else
	  switch (code->op)
	    {
	    case EXEC_OMP_CANCEL:
	      ok = ifc == OMP_IF_CANCEL;
	      break;

	    case EXEC_OMP_PARALLEL:
	    case EXEC_OMP_PARALLEL_DO:
	    case EXEC_OMP_PARALLEL_SECTIONS:
	    case EXEC_OMP_PARALLEL_WORKSHARE:
	    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
	    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
	      ok = ifc == OMP_IF_PARALLEL;
	      break;

	    case EXEC_OMP_PARALLEL_DO_SIMD:
	    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
	    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
	      ok = ifc == OMP_IF_PARALLEL || ifc == OMP_IF_SIMD;
	      break;

	    case EXEC_OMP_SIMD:
	    case EXEC_OMP_DO_SIMD:
	    case EXEC_OMP_DISTRIBUTE_SIMD:
	    case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
	      ok = ifc == OMP_IF_SIMD;
	      break;

	    case EXEC_OMP_TASK:
	      ok = ifc == OMP_IF_TASK;
	      break;

	    case EXEC_OMP_TASKLOOP:
	      ok = ifc == OMP_IF_TASKLOOP;
	      break;

	    case EXEC_OMP_TASKLOOP_SIMD:
	      ok = ifc == OMP_IF_TASKLOOP || ifc == OMP_IF_SIMD;
	      break;

	    case EXEC_OMP_TARGET:
	    case EXEC_OMP_TARGET_TEAMS:
	    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
	      ok = ifc == OMP_IF_TARGET;
	      break;

	    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
	    case EXEC_OMP_TARGET_SIMD:
	      ok = ifc == OMP_IF_TARGET || ifc == OMP_IF_SIMD;
	      break;

	    case EXEC_OMP_TARGET_DATA:
	      ok = ifc == OMP_IF_TARGET_DATA;
	      break;

	    case EXEC_OMP_TARGET_UPDATE:
	      ok = ifc == OMP_IF_TARGET_UPDATE;
	      break;

	    case EXEC_OMP_TARGET_ENTER_DATA:
	      ok = ifc == OMP_IF_TARGET_ENTER_DATA;
	      break;

	    case EXEC_OMP_TARGET_EXIT_DATA:
	      ok = ifc == OMP_IF_TARGET_EXIT_DATA;
	      break;

	    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
	    case EXEC_OMP_TARGET_PARALLEL:
	    case EXEC_OMP_TARGET_PARALLEL_DO:
	      ok = ifc == OMP_IF_TARGET || ifc == OMP_IF_PARALLEL;
	      break;

	    case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
	    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
	      ok = (ifc == OMP_IF_TARGET
		    || ifc == OMP_IF_PARALLEL
		    || ifc == OMP_IF_SIMD);
	      break;

	    default:
	      ok = false;
	      break;
	  }
	if (!ok)
	  {
	    static const char *ifs[] = {
	      "CANCEL",
	      "PARALLEL",
	      "SIMD",
	      "TASK",
	      "TASKLOOP",
	      "TARGET",
	      "TARGET DATA",
	      "TARGET UPDATE",
	      "TARGET ENTER DATA",
	      "TARGET EXIT DATA"
	    };
	    gfc_error ("IF clause modifier %s at %L not appropriate for "
		       "the current OpenMP construct", ifs[ifc], &expr->where);
	  }
      }

  if (omp_clauses->final_expr)
    {
      gfc_expr *expr = omp_clauses->final_expr;
      if (!gfc_resolve_expr (expr)
	  || expr->ts.type != BT_LOGICAL || expr->rank != 0)
	gfc_error ("FINAL clause at %L requires a scalar LOGICAL expression",
		   &expr->where);
    }
  if (omp_clauses->num_threads)
    resolve_positive_int_expr (omp_clauses->num_threads, "NUM_THREADS");
  if (omp_clauses->chunk_size)
    {
      gfc_expr *expr = omp_clauses->chunk_size;
      if (!gfc_resolve_expr (expr)
	  || expr->ts.type != BT_INTEGER || expr->rank != 0)
	gfc_error ("SCHEDULE clause's chunk_size at %L requires "
		   "a scalar INTEGER expression", &expr->where);
      else if (expr->expr_type == EXPR_CONSTANT
	       && expr->ts.type == BT_INTEGER
	       && mpz_sgn (expr->value.integer) <= 0)
	gfc_warning (0, "INTEGER expression of SCHEDULE clause's chunk_size "
		     "at %L must be positive", &expr->where);
    }
  if (omp_clauses->sched_kind != OMP_SCHED_NONE
      && omp_clauses->sched_nonmonotonic)
    {
      if (omp_clauses->sched_monotonic)
	gfc_error ("Both MONOTONIC and NONMONOTONIC schedule modifiers "
		   "specified at %L", &code->loc);
      else if (omp_clauses->ordered)
	gfc_error ("NONMONOTONIC schedule modifier specified with ORDERED "
		   "clause at %L", &code->loc);
    }

  /* Check that no symbol appears on multiple clauses, except that
     a symbol can appear on both firstprivate and lastprivate.  */
  for (list = 0; list < OMP_LIST_NUM; list++)
    for (n = omp_clauses->lists[list]; n; n = n->next)
      {
	n->sym->mark = 0;
	n->sym->comp_mark = 0;
	if (n->sym->attr.flavor == FL_VARIABLE
	    || n->sym->attr.proc_pointer
	    || (!code && (!n->sym->attr.dummy || n->sym->ns != ns)))
	  {
	    if (!code && (!n->sym->attr.dummy || n->sym->ns != ns))
	      gfc_error ("Variable %qs is not a dummy argument at %L",
			 n->sym->name, &n->where);
	    continue;
	  }
	if (n->sym->attr.flavor == FL_PROCEDURE
	    && n->sym->result == n->sym
	    && n->sym->attr.function)
	  {
	    if (gfc_current_ns->proc_name == n->sym
		|| (gfc_current_ns->parent
		    && gfc_current_ns->parent->proc_name == n->sym))
	      continue;
	    if (gfc_current_ns->proc_name->attr.entry_master)
	      {
		gfc_entry_list *el = gfc_current_ns->entries;
		for (; el; el = el->next)
		  if (el->sym == n->sym)
		    break;
		if (el)
		  continue;
	      }
	    if (gfc_current_ns->parent
		&& gfc_current_ns->parent->proc_name->attr.entry_master)
	      {
		gfc_entry_list *el = gfc_current_ns->parent->entries;
		for (; el; el = el->next)
		  if (el->sym == n->sym)
		    break;
		if (el)
		  continue;
	      }
	  }
	if (list == OMP_LIST_MAP
	    && n->sym->attr.flavor == FL_PARAMETER)
	  {
	    if (openacc)
	      gfc_error ("Object %qs is not a variable at %L; parameters"
			 " cannot be and need not be copied", n->sym->name,
			 &n->where);
	    else
	      gfc_error ("Object %qs is not a variable at %L; parameters"
			 " cannot be and need not be mapped", n->sym->name,
			 &n->where);
	  }
	else
	  gfc_error ("Object %qs is not a variable at %L", n->sym->name,
		     &n->where);
      }
  if (omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]
      && code->op != EXEC_OMP_DO
      && code->op != EXEC_OMP_SIMD
      && code->op != EXEC_OMP_DO_SIMD
      && code->op != EXEC_OMP_PARALLEL_DO
      && code->op != EXEC_OMP_PARALLEL_DO_SIMD)
    gfc_error ("%<inscan%> REDUCTION clause on construct other than DO, SIMD, "
	       "DO SIMD, PARALLEL DO, PARALLEL DO SIMD at %L",
	       &omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where);

  for (list = 0; list < OMP_LIST_NUM; list++)
    if (list != OMP_LIST_FIRSTPRIVATE
	&& list != OMP_LIST_LASTPRIVATE
	&& list != OMP_LIST_ALIGNED
	&& list != OMP_LIST_DEPEND
	&& (list != OMP_LIST_MAP || openacc)
	&& list != OMP_LIST_FROM
	&& list != OMP_LIST_TO
	&& (list != OMP_LIST_REDUCTION || !openacc)
	&& list != OMP_LIST_REDUCTION_INSCAN
	&& list != OMP_LIST_REDUCTION_TASK
	&& list != OMP_LIST_IN_REDUCTION
	&& list != OMP_LIST_TASK_REDUCTION)
      for (n = omp_clauses->lists[list]; n; n = n->next)
	{
	  bool component_ref_p = false;

	  /* Allow multiple components of the same (e.g. derived-type)
	     variable here.  Duplicate components are detected elsewhere.  */
	  if (n->expr && n->expr->expr_type == EXPR_VARIABLE)
	    for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
	      if (ref->type == REF_COMPONENT)
		component_ref_p = true;
	  if ((!component_ref_p && n->sym->comp_mark)
	      || (component_ref_p && n->sym->mark))
	    gfc_error ("Symbol %qs has mixed component and non-component "
		       "accesses at %L", n->sym->name, &n->where);
	  else if (n->sym->mark)
	    gfc_error ("Symbol %qs present on multiple clauses at %L",
		       n->sym->name, &n->where);
	  else
	    {
	      if (component_ref_p)
		n->sym->comp_mark = 1;
	      else
		n->sym->mark = 1;
	    }
	}

  gcc_assert (OMP_LIST_LASTPRIVATE == OMP_LIST_FIRSTPRIVATE + 1);
  for (list = OMP_LIST_FIRSTPRIVATE; list <= OMP_LIST_LASTPRIVATE; list++)
    for (n = omp_clauses->lists[list]; n; n = n->next)
      if (n->sym->mark)
	{
	  gfc_error ("Symbol %qs present on multiple clauses at %L",
		     n->sym->name, &n->where);
	  n->sym->mark = 0;
	}

  for (n = omp_clauses->lists[OMP_LIST_FIRSTPRIVATE]; n; n = n->next)
    {
      if (n->sym->mark)
	gfc_error ("Symbol %qs present on multiple clauses at %L",
		   n->sym->name, &n->where);
      else
	n->sym->mark = 1;
    }
  for (n = omp_clauses->lists[OMP_LIST_LASTPRIVATE]; n; n = n->next)
    n->sym->mark = 0;

  for (n = omp_clauses->lists[OMP_LIST_LASTPRIVATE]; n; n = n->next)
    {
      if (n->sym->mark)
	gfc_error ("Symbol %qs present on multiple clauses at %L",
		   n->sym->name, &n->where);
      else
	n->sym->mark = 1;
    }

  for (n = omp_clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
    n->sym->mark = 0;

  for (n = omp_clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
    {
      if (n->sym->mark)
	gfc_error ("Symbol %qs present on multiple clauses at %L",
		   n->sym->name, &n->where);
      else
	n->sym->mark = 1;
    }

  /* OpenACC reductions.  */
  if (openacc)
    {
      for (n = omp_clauses->lists[OMP_LIST_REDUCTION]; n; n = n->next)
	n->sym->mark = 0;

      for (n = omp_clauses->lists[OMP_LIST_REDUCTION]; n; n = n->next)
	{
	  if (n->sym->mark)
	    gfc_error ("Symbol %qs present on multiple clauses at %L",
		       n->sym->name, &n->where);
	  else
	    n->sym->mark = 1;

	  /* OpenACC does not support reductions on arrays.  */
	  if (n->sym->as)
	    gfc_error ("Array %qs is not permitted in reduction at %L",
		       n->sym->name, &n->where);
	}
    }
  
  for (n = omp_clauses->lists[OMP_LIST_TO]; n; n = n->next)
    n->sym->mark = 0;
  for (n = omp_clauses->lists[OMP_LIST_FROM]; n; n = n->next)
    if (n->expr == NULL)
      n->sym->mark = 1;
  for (n = omp_clauses->lists[OMP_LIST_TO]; n; n = n->next)
    {
      if (n->expr == NULL && n->sym->mark)
	gfc_error ("Symbol %qs present on both FROM and TO clauses at %L",
		   n->sym->name, &n->where);
      else
	n->sym->mark = 1;
    }

  bool has_inscan = false, has_notinscan = false;
  for (list = 0; list < OMP_LIST_NUM; list++)
    if ((n = omp_clauses->lists[list]) != NULL)
      {
	const char *name = clause_names[list];

	switch (list)
	  {
	  case OMP_LIST_COPYIN:
	    for (; n != NULL; n = n->next)
	      {
		if (!n->sym->attr.threadprivate)
		  gfc_error ("Non-THREADPRIVATE object %qs in COPYIN clause"
			     " at %L", n->sym->name, &n->where);
	      }
	    break;
	  case OMP_LIST_COPYPRIVATE:
	    for (; n != NULL; n = n->next)
	      {
		if (n->sym->as && n->sym->as->type == AS_ASSUMED_SIZE)
		  gfc_error ("Assumed size array %qs in COPYPRIVATE clause "
			     "at %L", n->sym->name, &n->where);
		if (n->sym->attr.pointer && n->sym->attr.intent == INTENT_IN)
		  gfc_error ("INTENT(IN) POINTER %qs in COPYPRIVATE clause "
			     "at %L", n->sym->name, &n->where);
	      }
	    break;
	  case OMP_LIST_SHARED:
	    for (; n != NULL; n = n->next)
	      {
		if (n->sym->attr.threadprivate)
		  gfc_error ("THREADPRIVATE object %qs in SHARED clause at "
			     "%L", n->sym->name, &n->where);
		if (n->sym->attr.cray_pointee)
		  gfc_error ("Cray pointee %qs in SHARED clause at %L",
			    n->sym->name, &n->where);
		if (n->sym->attr.associate_var)
		  gfc_error ("ASSOCIATE name %qs in SHARED clause at %L",
			     n->sym->name, &n->where);
		if (omp_clauses->detach
		    && n->sym == omp_clauses->detach->symtree->n.sym)
		  gfc_error ("DETACH event handle %qs in SHARED clause at %L",
			     n->sym->name, &n->where);
	      }
	    break;
	  case OMP_LIST_ALIGNED:
	    for (; n != NULL; n = n->next)
	      {
		if (!n->sym->attr.pointer
		    && !n->sym->attr.allocatable
		    && !n->sym->attr.cray_pointer
		    && (n->sym->ts.type != BT_DERIVED
			|| (n->sym->ts.u.derived->from_intmod
			    != INTMOD_ISO_C_BINDING)
			|| (n->sym->ts.u.derived->intmod_sym_id
			    != ISOCBINDING_PTR)))
		  gfc_error ("%qs in ALIGNED clause must be POINTER, "
			     "ALLOCATABLE, Cray pointer or C_PTR at %L",
			     n->sym->name, &n->where);
		else if (n->expr)
		  {
		    gfc_expr *expr = n->expr;
		    int alignment = 0;
		    if (!gfc_resolve_expr (expr)
			|| expr->ts.type != BT_INTEGER
			|| expr->rank != 0
			|| gfc_extract_int (expr, &alignment)
			|| alignment <= 0)
		      gfc_error ("%qs in ALIGNED clause at %L requires a scalar "
				 "positive constant integer alignment "
				 "expression", n->sym->name, &n->where);
		  }
	      }
	    break;
	  case OMP_LIST_DEPEND:
	  case OMP_LIST_MAP:
	  case OMP_LIST_TO:
	  case OMP_LIST_FROM:
	  case OMP_LIST_CACHE:
	    for (; n != NULL; n = n->next)
	      {
		if (list == OMP_LIST_DEPEND)
		  {
		    if (n->u.depend_op == OMP_DEPEND_SINK_FIRST
			|| n->u.depend_op == OMP_DEPEND_SINK)
		      {
			if (code->op != EXEC_OMP_ORDERED)
			  gfc_error ("SINK dependence type only allowed "
				     "on ORDERED directive at %L", &n->where);
			else if (omp_clauses->depend_source)
			  {
			    gfc_error ("DEPEND SINK used together with "
				       "DEPEND SOURCE on the same construct "
				       "at %L", &n->where);
			    omp_clauses->depend_source = false;
			  }
			else if (n->expr)
			  {
			    if (!gfc_resolve_expr (n->expr)
				|| n->expr->ts.type != BT_INTEGER
				|| n->expr->rank != 0)
			      gfc_error ("SINK addend not a constant integer "
					 "at %L", &n->where);
			  }
			continue;
		      }
		    else if (code->op == EXEC_OMP_ORDERED)
		      gfc_error ("Only SOURCE or SINK dependence types "
				 "are allowed on ORDERED directive at %L",
				 &n->where);
		  }
		gfc_ref *array_ref = NULL;
		bool resolved = false;
		if (n->expr)
		  {
		    array_ref = n->expr->ref;
		    resolved = gfc_resolve_expr (n->expr);

		    /* Look through component refs to find last array
		       reference.  */
		    if (resolved)
		      {
			/* The "!$acc cache" directive allows rectangular
			   subarrays to be specified, with some restrictions
			   on the form of bounds (not implemented).
			   Only raise an error here if we're really sure the
			   array isn't contiguous.  An expression such as
			   arr(-n:n,-n:n) could be contiguous even if it looks
			   like it may not be.  */
			if (list != OMP_LIST_CACHE
			    && list != OMP_LIST_DEPEND
			    && !gfc_is_simply_contiguous (n->expr, false, true)
			    && gfc_is_not_contiguous (n->expr))
			  gfc_error ("Array is not contiguous at %L",
				     &n->where);

			while (array_ref
			       && (array_ref->type == REF_COMPONENT
				   || (array_ref->type == REF_ARRAY
				       && array_ref->next
				       && (array_ref->next->type
					   == REF_COMPONENT))))
			  array_ref = array_ref->next;
		      }
		  }
		if (array_ref
		    || (n->expr
			&& (!resolved || n->expr->expr_type != EXPR_VARIABLE)))
		  {
		    if (array_ref
			&& (array_ref->type == REF_SUBSTRING
			    || (array_ref->next
				&& array_ref->next->type == REF_SUBSTRING)))
		      gfc_error ("Unexpected substring reference in %s clause "
				 "at %L", name, &n->where);
		    else if (!resolved
			|| n->expr->expr_type != EXPR_VARIABLE
			|| array_ref->next
			|| array_ref->type != REF_ARRAY)
		      gfc_error ("%qs in %s clause at %L is not a proper "
				 "array section", n->sym->name, name,
				 &n->where);
		    else
		      {
			int i;
			gfc_array_ref *ar = &array_ref->u.ar;
			for (i = 0; i < ar->dimen; i++)
			  if (ar->stride[i])
			    {
			      gfc_error ("Stride should not be specified for "
					 "array section in %s clause at %L",
					 name, &n->where);
			      break;
			    }
			  else if (ar->dimen_type[i] != DIMEN_ELEMENT
				   && ar->dimen_type[i] != DIMEN_RANGE)
			    {
			      gfc_error ("%qs in %s clause at %L is not a "
					 "proper array section",
					 n->sym->name, name, &n->where);
			      break;
			    }
			  else if (list == OMP_LIST_DEPEND
				   && ar->start[i]
				   && ar->start[i]->expr_type == EXPR_CONSTANT
				   && ar->end[i]
				   && ar->end[i]->expr_type == EXPR_CONSTANT
				   && mpz_cmp (ar->start[i]->value.integer,
					       ar->end[i]->value.integer) > 0)
			    {
			      gfc_error ("%qs in DEPEND clause at %L is a "
					 "zero size array section",
					 n->sym->name, &n->where);
			      break;
			    }
		      }
		  }
		else if (openacc)
		  {
		    if (list == OMP_LIST_MAP
			&& n->u.map_op == OMP_MAP_FORCE_DEVICEPTR)
		      resolve_oacc_deviceptr_clause (n->sym, n->where, name);
		    else
		      resolve_oacc_data_clauses (n->sym, n->where, name);
		  }
		else if (list != OMP_LIST_DEPEND
			 && n->sym->as
			 && n->sym->as->type == AS_ASSUMED_SIZE)
		  gfc_error ("Assumed size array %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (!openacc
		    && list == OMP_LIST_MAP
		    && n->sym->ts.type == BT_DERIVED
		    && n->sym->ts.u.derived->attr.alloc_comp)
		  gfc_error ("List item %qs with allocatable components is not "
			     "permitted in map clause at %L", n->sym->name,
			     &n->where);
		if (list == OMP_LIST_MAP && !openacc)
		  switch (code->op)
		    {
		    case EXEC_OMP_TARGET:
		    case EXEC_OMP_TARGET_DATA:
		      switch (n->u.map_op)
			{
			case OMP_MAP_TO:
			case OMP_MAP_ALWAYS_TO:
			case OMP_MAP_FROM:
			case OMP_MAP_ALWAYS_FROM:
			case OMP_MAP_TOFROM:
			case OMP_MAP_ALWAYS_TOFROM:
			case OMP_MAP_ALLOC:
			  break;
			default:
			  gfc_error ("TARGET%s with map-type other than TO, "
				     "FROM, TOFROM, or ALLOC on MAP clause "
				     "at %L",
				     code->op == EXEC_OMP_TARGET
				     ? "" : " DATA", &n->where);
			  break;
			}
		      break;
		    case EXEC_OMP_TARGET_ENTER_DATA:
		      switch (n->u.map_op)
			{
			case OMP_MAP_TO:
			case OMP_MAP_ALWAYS_TO:
			case OMP_MAP_ALLOC:
			  break;
			default:
			  gfc_error ("TARGET ENTER DATA with map-type other "
				     "than TO, or ALLOC on MAP clause at %L",
				     &n->where);
			  break;
			}
		      break;
		    case EXEC_OMP_TARGET_EXIT_DATA:
		      switch (n->u.map_op)
			{
			case OMP_MAP_FROM:
			case OMP_MAP_ALWAYS_FROM:
			case OMP_MAP_RELEASE:
			case OMP_MAP_DELETE:
			  break;
			default:
			  gfc_error ("TARGET EXIT DATA with map-type other "
				     "than FROM, RELEASE, or DELETE on MAP "
				     "clause at %L", &n->where);
			  break;
			}
		      break;
		    default:
		      break;
		    }
	      }

	    if (list != OMP_LIST_DEPEND)
	      for (n = omp_clauses->lists[list]; n != NULL; n = n->next)
		{
		  n->sym->attr.referenced = 1;
		  if (n->sym->attr.threadprivate)
		    gfc_error ("THREADPRIVATE object %qs in %s clause at %L",
			       n->sym->name, name, &n->where);
		  if (n->sym->attr.cray_pointee)
		    gfc_error ("Cray pointee %qs in %s clause at %L",
			       n->sym->name, name, &n->where);
		}
	    break;
	  case OMP_LIST_IS_DEVICE_PTR:
	    for (n = omp_clauses->lists[list]; n != NULL; n = n->next)
	      {
		if (!n->sym->attr.dummy)
		  gfc_error ("Non-dummy object %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (n->sym->attr.allocatable
		    || (n->sym->ts.type == BT_CLASS
			&& CLASS_DATA (n->sym)->attr.allocatable))
		  gfc_error ("ALLOCATABLE object %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (n->sym->attr.pointer
		    || (n->sym->ts.type == BT_CLASS
			&& CLASS_DATA (n->sym)->attr.pointer))
		  gfc_error ("POINTER object %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (n->sym->attr.value)
		  gfc_error ("VALUE object %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
	      }
	    break;
	  case OMP_LIST_USE_DEVICE_PTR:
	  case OMP_LIST_USE_DEVICE_ADDR:
	    /* FIXME: Handle OMP_LIST_USE_DEVICE_PTR.  */
	    break;
	  default:
	    for (; n != NULL; n = n->next)
	      {
		bool bad = false;
		bool is_reduction = (list == OMP_LIST_REDUCTION
				     || list == OMP_LIST_REDUCTION_INSCAN
				     || list == OMP_LIST_REDUCTION_TASK
				     || list == OMP_LIST_IN_REDUCTION
				     || list == OMP_LIST_TASK_REDUCTION);
		if (list == OMP_LIST_REDUCTION_INSCAN)
		  has_inscan = true;
		else if (is_reduction)
		  has_notinscan = true;
		if (has_inscan && has_notinscan && is_reduction)
		  {
		    gfc_error ("%<inscan%> and non-%<inscan%> %<reduction%> "
			       "clauses on the same construct %L",
			       &n->where);
		    break;
		  }
		if (n->sym->attr.threadprivate)
		  gfc_error ("THREADPRIVATE object %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (n->sym->attr.cray_pointee)
		  gfc_error ("Cray pointee %qs in %s clause at %L",
			    n->sym->name, name, &n->where);
		if (n->sym->attr.associate_var)
		  gfc_error ("ASSOCIATE name %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (list != OMP_LIST_PRIVATE && is_reduction)
		  {
		    if (n->sym->attr.proc_pointer)
		      gfc_error ("Procedure pointer %qs in %s clause at %L",
				 n->sym->name, name, &n->where);
		    if (n->sym->attr.pointer)
		      gfc_error ("POINTER object %qs in %s clause at %L",
				 n->sym->name, name, &n->where);
		    if (n->sym->attr.cray_pointer)
		      gfc_error ("Cray pointer %qs in %s clause at %L",
				 n->sym->name, name, &n->where);
		  }
		if (code
		    && (oacc_is_loop (code)
			|| code->op == EXEC_OACC_PARALLEL
			|| code->op == EXEC_OACC_SERIAL))
		  check_array_not_assumed (n->sym, n->where, name);
		else if (n->sym->as && n->sym->as->type == AS_ASSUMED_SIZE)
		  gfc_error ("Assumed size array %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		if (n->sym->attr.in_namelist && !is_reduction)
		  gfc_error ("Variable %qs in %s clause is used in "
			     "NAMELIST statement at %L",
			     n->sym->name, name, &n->where);
		if (n->sym->attr.pointer && n->sym->attr.intent == INTENT_IN)
		  switch (list)
		    {
		    case OMP_LIST_PRIVATE:
		    case OMP_LIST_LASTPRIVATE:
		    case OMP_LIST_LINEAR:
		    /* case OMP_LIST_REDUCTION: */
		      gfc_error ("INTENT(IN) POINTER %qs in %s clause at %L",
				 n->sym->name, name, &n->where);
		      break;
		    default:
		      break;
		    }
		if (omp_clauses->detach
		    && (list == OMP_LIST_PRIVATE
			|| list == OMP_LIST_FIRSTPRIVATE
			|| list == OMP_LIST_LASTPRIVATE)
		    && n->sym == omp_clauses->detach->symtree->n.sym)
		  gfc_error ("DETACH event handle %qs in %s clause at %L",
			     n->sym->name, name, &n->where);
		switch (list)
		  {
		  case OMP_LIST_REDUCTION_INSCAN:
		  case OMP_LIST_REDUCTION_TASK:
		    if (code && (code->op == EXEC_OMP_TASKLOOP
				 || code->op == EXEC_OMP_TEAMS
				 || code->op == EXEC_OMP_TEAMS_DISTRIBUTE))
		      {
			gfc_error ("Only DEFAULT permitted as reduction-"
				   "modifier in REDUCTION clause at %L",
				   &n->where);
			break;
		      }
		    gcc_fallthrough ();
		  case OMP_LIST_REDUCTION:
		  case OMP_LIST_IN_REDUCTION:
		  case OMP_LIST_TASK_REDUCTION:
		    switch (n->u.reduction_op)
		      {
		      case OMP_REDUCTION_PLUS:
		      case OMP_REDUCTION_TIMES:
		      case OMP_REDUCTION_MINUS:
			if (!gfc_numeric_ts (&n->sym->ts))
			  bad = true;
			break;
		      case OMP_REDUCTION_AND:
		      case OMP_REDUCTION_OR:
		      case OMP_REDUCTION_EQV:
		      case OMP_REDUCTION_NEQV:
			if (n->sym->ts.type != BT_LOGICAL)
			  bad = true;
			break;
		      case OMP_REDUCTION_MAX:
		      case OMP_REDUCTION_MIN:
			if (n->sym->ts.type != BT_INTEGER
			    && n->sym->ts.type != BT_REAL)
			  bad = true;
			break;
		      case OMP_REDUCTION_IAND:
		      case OMP_REDUCTION_IOR:
		      case OMP_REDUCTION_IEOR:
			if (n->sym->ts.type != BT_INTEGER)
			  bad = true;
			break;
		      case OMP_REDUCTION_USER:
			bad = true;
			break;
		      default:
			break;
		      }
		    if (!bad)
		      n->udr = NULL;
		    else
		      {
			const char *udr_name = NULL;
			if (n->udr)
			  {
			    udr_name = n->udr->udr->name;
			    n->udr->udr
			      = gfc_find_omp_udr (NULL, udr_name,
						  &n->sym->ts);
			    if (n->udr->udr == NULL)
			      {
				free (n->udr);
				n->udr = NULL;
			      }
			  }
			if (n->udr == NULL)
			  {
			    if (udr_name == NULL)
			      switch (n->u.reduction_op)
				{
				case OMP_REDUCTION_PLUS:
				case OMP_REDUCTION_TIMES:
				case OMP_REDUCTION_MINUS:
				case OMP_REDUCTION_AND:
				case OMP_REDUCTION_OR:
				case OMP_REDUCTION_EQV:
				case OMP_REDUCTION_NEQV:
				  udr_name = gfc_op2string ((gfc_intrinsic_op)
							    n->u.reduction_op);
				  break;
				case OMP_REDUCTION_MAX:
				  udr_name = "max";
				  break;
				case OMP_REDUCTION_MIN:
				  udr_name = "min";
				  break;
				case OMP_REDUCTION_IAND:
				  udr_name = "iand";
				  break;
				case OMP_REDUCTION_IOR:
				  udr_name = "ior";
				  break;
				case OMP_REDUCTION_IEOR:
				  udr_name = "ieor";
				  break;
				default:
				  gcc_unreachable ();
				}
			    gfc_error ("!$OMP DECLARE REDUCTION %s not found "
				       "for type %s at %L", udr_name,
				       gfc_typename (&n->sym->ts), &n->where);
			  }
			else
			  {
			    gfc_omp_udr *udr = n->udr->udr;
			    n->u.reduction_op = OMP_REDUCTION_USER;
			    n->udr->combiner
			      = resolve_omp_udr_clause (n, udr->combiner_ns,
							udr->omp_out,
							udr->omp_in);
			    if (udr->initializer_ns)
			      n->udr->initializer
				= resolve_omp_udr_clause (n,
							  udr->initializer_ns,
							  udr->omp_priv,
							  udr->omp_orig);
			  }
		      }
		    break;
		  case OMP_LIST_LINEAR:
		    if (code
			&& n->u.linear_op != OMP_LINEAR_DEFAULT
			&& n->u.linear_op != linear_op)
		      {
			gfc_error ("LINEAR clause modifier used on DO or SIMD"
				   " construct at %L", &n->where);
			linear_op = n->u.linear_op;
		      }
		    else if (omp_clauses->orderedc)
		      gfc_error ("LINEAR clause specified together with "
				 "ORDERED clause with argument at %L",
				 &n->where);
		    else if (n->u.linear_op != OMP_LINEAR_REF
			     && n->sym->ts.type != BT_INTEGER)
		      gfc_error ("LINEAR variable %qs must be INTEGER "
				 "at %L", n->sym->name, &n->where);
		    else if ((n->u.linear_op == OMP_LINEAR_REF
			      || n->u.linear_op == OMP_LINEAR_UVAL)
			     && n->sym->attr.value)
		      gfc_error ("LINEAR dummy argument %qs with VALUE "
				 "attribute with %s modifier at %L",
				 n->sym->name,
				 n->u.linear_op == OMP_LINEAR_REF
				 ? "REF" : "UVAL", &n->where);
		    else if (n->expr)
		      {
			gfc_expr *expr = n->expr;
			if (!gfc_resolve_expr (expr)
			    || expr->ts.type != BT_INTEGER
			    || expr->rank != 0)
			  gfc_error ("%qs in LINEAR clause at %L requires "
				     "a scalar integer linear-step expression",
				     n->sym->name, &n->where);
			else if (!code && expr->expr_type != EXPR_CONSTANT)
			  {
			    if (expr->expr_type == EXPR_VARIABLE
				&& expr->symtree->n.sym->attr.dummy
				&& expr->symtree->n.sym->ns == ns)
			      {
				gfc_omp_namelist *n2;
				for (n2 = omp_clauses->lists[OMP_LIST_UNIFORM];
				     n2; n2 = n2->next)
				  if (n2->sym == expr->symtree->n.sym)
				    break;
				if (n2)
				  break;
			      }
			    gfc_error ("%qs in LINEAR clause at %L requires "
				       "a constant integer linear-step "
				       "expression or dummy argument "
				       "specified in UNIFORM clause",
				       n->sym->name, &n->where);
			  }
		      }
		    break;
		  /* Workaround for PR middle-end/26316, nothing really needs
		     to be done here for OMP_LIST_PRIVATE.  */
		  case OMP_LIST_PRIVATE:
		    gcc_assert (code && code->op != EXEC_NOP);
		    break;
		  case OMP_LIST_USE_DEVICE:
		      if (n->sym->attr.allocatable
			  || (n->sym->ts.type == BT_CLASS && CLASS_DATA (n->sym)
			      && CLASS_DATA (n->sym)->attr.allocatable))
			gfc_error ("ALLOCATABLE object %qs in %s clause at %L",
				   n->sym->name, name, &n->where);
		      if (n->sym->ts.type == BT_CLASS
			  && CLASS_DATA (n->sym)
			  && CLASS_DATA (n->sym)->attr.class_pointer)
			gfc_error ("POINTER object %qs of polymorphic type in "
				   "%s clause at %L", n->sym->name, name,
				   &n->where);
		      if (n->sym->attr.cray_pointer)
			gfc_error ("Cray pointer object %qs in %s clause at %L",
				   n->sym->name, name, &n->where);
		      else if (n->sym->attr.cray_pointee)
			gfc_error ("Cray pointee object %qs in %s clause at %L",
				   n->sym->name, name, &n->where);
		      else if (n->sym->attr.flavor == FL_VARIABLE
			       && !n->sym->as
			       && !n->sym->attr.pointer)
			gfc_error ("%s clause variable %qs at %L is neither "
				   "a POINTER nor an array", name,
				   n->sym->name, &n->where);
		      /* FALLTHRU */
		  case OMP_LIST_DEVICE_RESIDENT:
		    check_symbol_not_pointer (n->sym, n->where, name);
		    check_array_not_assumed (n->sym, n->where, name);
		    break;
		  default:
		    break;
		  }
	      }
	    break;
	  }
      }
  /* OpenMP 5.1: use_device_ptr acts like use_device_addr, except for
     type(c_ptr).  */
  if (omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR])
    {
      gfc_omp_namelist *n_prev, *n_next, *n_addr;
      n_addr = omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR];
      for (; n_addr && n_addr->next; n_addr = n_addr->next)
	;
      n_prev = NULL;
      n = omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR];
      while (n)
	{
	  n_next = n->next;
	  if (n->sym->ts.type != BT_DERIVED
	      || n->sym->ts.u.derived->ts.f90_type != BT_VOID)
	    {
	      n->next = NULL;
	      if (n_addr)
		n_addr->next = n;
	      else
		omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR] = n;
	      n_addr = n;
	      if (n_prev)
		n_prev->next = n_next;
	      else
		omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR] = n_next;
	    }
	  else
	    n_prev = n;
	  n = n_next;
	}
    }
  if (omp_clauses->safelen_expr)
    resolve_positive_int_expr (omp_clauses->safelen_expr, "SAFELEN");
  if (omp_clauses->simdlen_expr)
    resolve_positive_int_expr (omp_clauses->simdlen_expr, "SIMDLEN");
  if (omp_clauses->num_teams)
    resolve_positive_int_expr (omp_clauses->num_teams, "NUM_TEAMS");
  if (omp_clauses->device)
    resolve_nonnegative_int_expr (omp_clauses->device, "DEVICE");
  if (omp_clauses->hint)
    {
      resolve_scalar_int_expr (omp_clauses->hint, "HINT");
    if (omp_clauses->hint->ts.type != BT_INTEGER
	|| omp_clauses->hint->expr_type != EXPR_CONSTANT
	|| mpz_sgn (omp_clauses->hint->value.integer) < 0)
      gfc_error ("Value of HINT clause at %L shall be a valid "
		 "constant hint expression", &omp_clauses->hint->where);
    }
  if (omp_clauses->priority)
    resolve_nonnegative_int_expr (omp_clauses->priority, "PRIORITY");
  if (omp_clauses->dist_chunk_size)
    {
      gfc_expr *expr = omp_clauses->dist_chunk_size;
      if (!gfc_resolve_expr (expr)
	  || expr->ts.type != BT_INTEGER || expr->rank != 0)
	gfc_error ("DIST_SCHEDULE clause's chunk_size at %L requires "
		   "a scalar INTEGER expression", &expr->where);
    }
  if (omp_clauses->thread_limit)
    resolve_positive_int_expr (omp_clauses->thread_limit, "THREAD_LIMIT");
  if (omp_clauses->grainsize)
    resolve_positive_int_expr (omp_clauses->grainsize, "GRAINSIZE");
  if (omp_clauses->num_tasks)
    resolve_positive_int_expr (omp_clauses->num_tasks, "NUM_TASKS");
  if (omp_clauses->async)
    if (omp_clauses->async_expr)
      resolve_scalar_int_expr (omp_clauses->async_expr, "ASYNC");
  if (omp_clauses->num_gangs_expr)
    resolve_positive_int_expr (omp_clauses->num_gangs_expr, "NUM_GANGS");
  if (omp_clauses->num_workers_expr)
    resolve_positive_int_expr (omp_clauses->num_workers_expr, "NUM_WORKERS");
  if (omp_clauses->vector_length_expr)
    resolve_positive_int_expr (omp_clauses->vector_length_expr,
			       "VECTOR_LENGTH");
  if (omp_clauses->gang_num_expr)
    resolve_positive_int_expr (omp_clauses->gang_num_expr, "GANG");
  if (omp_clauses->gang_static_expr)
    resolve_positive_int_expr (omp_clauses->gang_static_expr, "GANG");
  if (omp_clauses->worker_expr)
    resolve_positive_int_expr (omp_clauses->worker_expr, "WORKER");
  if (omp_clauses->vector_expr)
    resolve_positive_int_expr (omp_clauses->vector_expr, "VECTOR");
  for (el = omp_clauses->wait_list; el; el = el->next)
    resolve_scalar_int_expr (el->expr, "WAIT");
  if (omp_clauses->collapse && omp_clauses->tile_list)
    gfc_error ("Incompatible use of TILE and COLLAPSE at %L", &code->loc);
  if (omp_clauses->depend_source && code->op != EXEC_OMP_ORDERED)
    gfc_error ("SOURCE dependence type only allowed "
	       "on ORDERED directive at %L", &code->loc);
  if (!openacc
      && code
      && omp_clauses->lists[OMP_LIST_MAP] == NULL
      && omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR] == NULL
      && omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR] == NULL)
    {
      const char *p = NULL;
      switch (code->op)
	{
	case EXEC_OMP_TARGET_ENTER_DATA: p = "TARGET ENTER DATA"; break;
	case EXEC_OMP_TARGET_EXIT_DATA: p = "TARGET EXIT DATA"; break;
	default: break;
	}
      if (code->op == EXEC_OMP_TARGET_DATA)
	gfc_error ("TARGET DATA must contain at least one MAP, USE_DEVICE_PTR, "
		   "or USE_DEVICE_ADDR clause at %L", &code->loc);
      else if (p)
	gfc_error ("%s must contain at least one MAP clause at %L",
		   p, &code->loc);
    }
  if (!openacc && omp_clauses->mergeable && omp_clauses->detach)
    gfc_error ("%<DETACH%> clause at %L must not be used together with "
	       "%<MERGEABLE%> clause", &omp_clauses->detach->where);
}


/* Return true if SYM is ever referenced in EXPR except in the SE node.  */

static bool
expr_references_sym (gfc_expr *e, gfc_symbol *s, gfc_expr *se)
{
  gfc_actual_arglist *arg;
  if (e == NULL || e == se)
    return false;
  switch (e->expr_type)
    {
    case EXPR_CONSTANT:
    case EXPR_NULL:
    case EXPR_VARIABLE:
    case EXPR_STRUCTURE:
    case EXPR_ARRAY:
      if (e->symtree != NULL
	  && e->symtree->n.sym == s)
	return true;
      return false;
    case EXPR_SUBSTRING:
      if (e->ref != NULL
	  && (expr_references_sym (e->ref->u.ss.start, s, se)
	      || expr_references_sym (e->ref->u.ss.end, s, se)))
	return true;
      return false;
    case EXPR_OP:
      if (expr_references_sym (e->value.op.op2, s, se))
	return true;
      return expr_references_sym (e->value.op.op1, s, se);
    case EXPR_FUNCTION:
      for (arg = e->value.function.actual; arg; arg = arg->next)
	if (expr_references_sym (arg->expr, s, se))
	  return true;
      return false;
    default:
      gcc_unreachable ();
    }
}


/* If EXPR is a conversion function that widens the type
   if WIDENING is true or narrows the type if WIDENING is false,
   return the inner expression, otherwise return NULL.  */

static gfc_expr *
is_conversion (gfc_expr *expr, bool widening)
{
  gfc_typespec *ts1, *ts2;

  if (expr->expr_type != EXPR_FUNCTION
      || expr->value.function.isym == NULL
      || expr->value.function.esym != NULL
      || expr->value.function.isym->id != GFC_ISYM_CONVERSION)
    return NULL;

  if (widening)
    {
      ts1 = &expr->ts;
      ts2 = &expr->value.function.actual->expr->ts;
    }
  else
    {
      ts1 = &expr->value.function.actual->expr->ts;
      ts2 = &expr->ts;
    }

  if (ts1->type > ts2->type
      || (ts1->type == ts2->type && ts1->kind > ts2->kind))
    return expr->value.function.actual->expr;

  return NULL;
}


static void
resolve_omp_atomic (gfc_code *code)
{
  gfc_code *atomic_code = code->block;
  gfc_symbol *var;
  gfc_expr *expr2, *expr2_tmp;
  gfc_omp_atomic_op aop
    = (gfc_omp_atomic_op) (atomic_code->ext.omp_clauses->atomic_op
			   & GFC_OMP_ATOMIC_MASK);

  code = code->block->next;
  /* resolve_blocks asserts this is initially EXEC_ASSIGN.
     If it changed to EXEC_NOP, assume an error has been emitted already.  */
  if (code->op == EXEC_NOP)
    return;
  if (code->op != EXEC_ASSIGN)
    {
    unexpected:
      gfc_error ("unexpected !$OMP ATOMIC expression at %L", &code->loc);
      return;
    }
  if (!atomic_code->ext.omp_clauses->capture)
    {
      if (code->next != NULL)
	goto unexpected;
    }
  else
    {
      if (code->next == NULL)
	goto unexpected;
      if (code->next->op == EXEC_NOP)
	return;
      if (code->next->op != EXEC_ASSIGN || code->next->next)
	{
	  code = code->next;
	  goto unexpected;
	}
    }

  if (code->expr1->expr_type != EXPR_VARIABLE
      || code->expr1->symtree == NULL
      || code->expr1->rank != 0
      || (code->expr1->ts.type != BT_INTEGER
	  && code->expr1->ts.type != BT_REAL
	  && code->expr1->ts.type != BT_COMPLEX
	  && code->expr1->ts.type != BT_LOGICAL))
    {
      gfc_error ("!$OMP ATOMIC statement must set a scalar variable of "
		 "intrinsic type at %L", &code->loc);
      return;
    }

  var = code->expr1->symtree->n.sym;
  expr2 = is_conversion (code->expr2, false);
  if (expr2 == NULL)
    {
      if (aop == GFC_OMP_ATOMIC_READ || aop == GFC_OMP_ATOMIC_WRITE)
	expr2 = is_conversion (code->expr2, true);
      if (expr2 == NULL)
	expr2 = code->expr2;
    }

  switch (aop)
    {
    case GFC_OMP_ATOMIC_READ:
      if (expr2->expr_type != EXPR_VARIABLE
	  || expr2->symtree == NULL
	  || expr2->rank != 0
	  || (expr2->ts.type != BT_INTEGER
	      && expr2->ts.type != BT_REAL
	      && expr2->ts.type != BT_COMPLEX
	      && expr2->ts.type != BT_LOGICAL))
	gfc_error ("!$OMP ATOMIC READ statement must read from a scalar "
		   "variable of intrinsic type at %L", &expr2->where);
      return;
    case GFC_OMP_ATOMIC_WRITE:
      if (expr2->rank != 0 || expr_references_sym (code->expr2, var, NULL))
	gfc_error ("expr in !$OMP ATOMIC WRITE assignment var = expr "
		   "must be scalar and cannot reference var at %L",
		   &expr2->where);
      return;
    default:
      break;
    }
  if (atomic_code->ext.omp_clauses->capture)
    {
      expr2_tmp = expr2;
      if (expr2 == code->expr2)
	{
	  expr2_tmp = is_conversion (code->expr2, true);
	  if (expr2_tmp == NULL)
	    expr2_tmp = expr2;
	}
      if (expr2_tmp->expr_type == EXPR_VARIABLE)
	{
	  if (expr2_tmp->symtree == NULL
	      || expr2_tmp->rank != 0
	      || (expr2_tmp->ts.type != BT_INTEGER
		  && expr2_tmp->ts.type != BT_REAL
		  && expr2_tmp->ts.type != BT_COMPLEX
		  && expr2_tmp->ts.type != BT_LOGICAL)
	      || expr2_tmp->symtree->n.sym == var)
	    {
	      gfc_error ("!$OMP ATOMIC CAPTURE capture statement must read from "
			 "a scalar variable of intrinsic type at %L",
			 &expr2_tmp->where);
	      return;
	    }
	  var = expr2_tmp->symtree->n.sym;
	  code = code->next;
	  if (code->expr1->expr_type != EXPR_VARIABLE
	      || code->expr1->symtree == NULL
	      || code->expr1->rank != 0
	      || (code->expr1->ts.type != BT_INTEGER
		  && code->expr1->ts.type != BT_REAL
		  && code->expr1->ts.type != BT_COMPLEX
		  && code->expr1->ts.type != BT_LOGICAL))
	    {
	      gfc_error ("!$OMP ATOMIC CAPTURE update statement must set "
			 "a scalar variable of intrinsic type at %L",
			 &code->expr1->where);
	      return;
	    }
	  if (code->expr1->symtree->n.sym != var)
	    {
	      gfc_error ("!$OMP ATOMIC CAPTURE capture statement reads from "
			 "different variable than update statement writes "
			 "into at %L", &code->expr1->where);
	      return;
	    }
	  expr2 = is_conversion (code->expr2, false);
	  if (expr2 == NULL)
	    expr2 = code->expr2;
	}
    }

  if (gfc_expr_attr (code->expr1).allocatable)
    {
      gfc_error ("!$OMP ATOMIC with ALLOCATABLE variable at %L",
		 &code->loc);
      return;
    }

  if (atomic_code->ext.omp_clauses->capture
      && code->next == NULL
      && code->expr2->rank == 0
      && !expr_references_sym (code->expr2, var, NULL))
    atomic_code->ext.omp_clauses->atomic_op
      = (gfc_omp_atomic_op) (atomic_code->ext.omp_clauses->atomic_op
			     | GFC_OMP_ATOMIC_SWAP);
  else if (expr2->expr_type == EXPR_OP)
    {
      gfc_expr *v = NULL, *e, *c;
      gfc_intrinsic_op op = expr2->value.op.op;
      gfc_intrinsic_op alt_op = INTRINSIC_NONE;

      switch (op)
	{
	case INTRINSIC_PLUS:
	  alt_op = INTRINSIC_MINUS;
	  break;
	case INTRINSIC_TIMES:
	  alt_op = INTRINSIC_DIVIDE;
	  break;
	case INTRINSIC_MINUS:
	  alt_op = INTRINSIC_PLUS;
	  break;
	case INTRINSIC_DIVIDE:
	  alt_op = INTRINSIC_TIMES;
	  break;
	case INTRINSIC_AND:
	case INTRINSIC_OR:
	  break;
	case INTRINSIC_EQV:
	  alt_op = INTRINSIC_NEQV;
	  break;
	case INTRINSIC_NEQV:
	  alt_op = INTRINSIC_EQV;
	  break;
	default:
	  gfc_error ("!$OMP ATOMIC assignment operator must be binary "
		     "+, *, -, /, .AND., .OR., .EQV. or .NEQV. at %L",
		     &expr2->where);
	  return;
	}

      /* Check for var = var op expr resp. var = expr op var where
	 expr doesn't reference var and var op expr is mathematically
	 equivalent to var op (expr) resp. expr op var equivalent to
	 (expr) op var.  We rely here on the fact that the matcher
	 for x op1 y op2 z where op1 and op2 have equal precedence
	 returns (x op1 y) op2 z.  */
      e = expr2->value.op.op2;
      if (e->expr_type == EXPR_VARIABLE
	  && e->symtree != NULL
	  && e->symtree->n.sym == var)
	v = e;
      else if ((c = is_conversion (e, true)) != NULL
	       && c->expr_type == EXPR_VARIABLE
	       && c->symtree != NULL
	       && c->symtree->n.sym == var)
	v = c;
      else
	{
	  gfc_expr **p = NULL, **q;
	  for (q = &expr2->value.op.op1; (e = *q) != NULL; )
	    if (e->expr_type == EXPR_VARIABLE
		&& e->symtree != NULL
		&& e->symtree->n.sym == var)
	      {
		v = e;
		break;
	      }
	    else if ((c = is_conversion (e, true)) != NULL)
	      q = &e->value.function.actual->expr;
	    else if (e->expr_type != EXPR_OP
		     || (e->value.op.op != op
			 && e->value.op.op != alt_op)
		     || e->rank != 0)
	      break;
	    else
	      {
		p = q;
		q = &e->value.op.op1;
	      }

	  if (v == NULL)
	    {
	      gfc_error ("!$OMP ATOMIC assignment must be var = var op expr "
			 "or var = expr op var at %L", &expr2->where);
	      return;
	    }

	  if (p != NULL)
	    {
	      e = *p;
	      switch (e->value.op.op)
		{
		case INTRINSIC_MINUS:
		case INTRINSIC_DIVIDE:
		case INTRINSIC_EQV:
		case INTRINSIC_NEQV:
		  gfc_error ("!$OMP ATOMIC var = var op expr not "
			     "mathematically equivalent to var = var op "
			     "(expr) at %L", &expr2->where);
		  break;
		default:
		  break;
		}

	      /* Canonicalize into var = var op (expr).  */
	      *p = e->value.op.op2;
	      e->value.op.op2 = expr2;
	      e->ts = expr2->ts;
	      if (code->expr2 == expr2)
		code->expr2 = expr2 = e;
	      else
		code->expr2->value.function.actual->expr = expr2 = e;

	      if (!gfc_compare_types (&expr2->value.op.op1->ts, &expr2->ts))
		{
		  for (p = &expr2->value.op.op1; *p != v;
		       p = &(*p)->value.function.actual->expr)
		    ;
		  *p = NULL;
		  gfc_free_expr (expr2->value.op.op1);
		  expr2->value.op.op1 = v;
		  gfc_convert_type (v, &expr2->ts, 2);
		}
	    }
	}

      if (e->rank != 0 || expr_references_sym (code->expr2, var, v))
	{
	  gfc_error ("expr in !$OMP ATOMIC assignment var = var op expr "
		     "must be scalar and cannot reference var at %L",
		     &expr2->where);
	  return;
	}
    }
  else if (expr2->expr_type == EXPR_FUNCTION
	   && expr2->value.function.isym != NULL
	   && expr2->value.function.esym == NULL
	   && expr2->value.function.actual != NULL
	   && expr2->value.function.actual->next != NULL)
    {
      gfc_actual_arglist *arg, *var_arg;

      switch (expr2->value.function.isym->id)
	{
	case GFC_ISYM_MIN:
	case GFC_ISYM_MAX:
	  break;
	case GFC_ISYM_IAND:
	case GFC_ISYM_IOR:
	case GFC_ISYM_IEOR:
	  if (expr2->value.function.actual->next->next != NULL)
	    {
	      gfc_error ("!$OMP ATOMIC assignment intrinsic IAND, IOR "
			 "or IEOR must have two arguments at %L",
			 &expr2->where);
	      return;
	    }
	  break;
	default:
	  gfc_error ("!$OMP ATOMIC assignment intrinsic must be "
		     "MIN, MAX, IAND, IOR or IEOR at %L",
		     &expr2->where);
	  return;
	}

      var_arg = NULL;
      for (arg = expr2->value.function.actual; arg; arg = arg->next)
	{
	  if ((arg == expr2->value.function.actual
	       || (var_arg == NULL && arg->next == NULL))
	      && arg->expr->expr_type == EXPR_VARIABLE
	      && arg->expr->symtree != NULL
	      && arg->expr->symtree->n.sym == var)
	    var_arg = arg;
	  else if (expr_references_sym (arg->expr, var, NULL))
	    {
	      gfc_error ("!$OMP ATOMIC intrinsic arguments except one must "
			 "not reference %qs at %L",
			 var->name, &arg->expr->where);
	      return;
	    }
	  if (arg->expr->rank != 0)
	    {
	      gfc_error ("!$OMP ATOMIC intrinsic arguments must be scalar "
			 "at %L", &arg->expr->where);
	      return;
	    }
	}

      if (var_arg == NULL)
	{
	  gfc_error ("First or last !$OMP ATOMIC intrinsic argument must "
		     "be %qs at %L", var->name, &expr2->where);
	  return;
	}

      if (var_arg != expr2->value.function.actual)
	{
	  /* Canonicalize, so that var comes first.  */
	  gcc_assert (var_arg->next == NULL);
	  for (arg = expr2->value.function.actual;
	       arg->next != var_arg; arg = arg->next)
	    ;
	  var_arg->next = expr2->value.function.actual;
	  expr2->value.function.actual = var_arg;
	  arg->next = NULL;
	}
    }
  else
    gfc_error ("!$OMP ATOMIC assignment must have an operator or "
	       "intrinsic on right hand side at %L", &expr2->where);

  if (atomic_code->ext.omp_clauses->capture && code->next)
    {
      code = code->next;
      if (code->expr1->expr_type != EXPR_VARIABLE
	  || code->expr1->symtree == NULL
	  || code->expr1->rank != 0
	  || (code->expr1->ts.type != BT_INTEGER
	      && code->expr1->ts.type != BT_REAL
	      && code->expr1->ts.type != BT_COMPLEX
	      && code->expr1->ts.type != BT_LOGICAL))
	{
	  gfc_error ("!$OMP ATOMIC CAPTURE capture statement must set "
		     "a scalar variable of intrinsic type at %L",
		     &code->expr1->where);
	  return;
	}

      expr2 = is_conversion (code->expr2, false);
      if (expr2 == NULL)
	{
	  expr2 = is_conversion (code->expr2, true);
	  if (expr2 == NULL)
	    expr2 = code->expr2;
	}

      if (expr2->expr_type != EXPR_VARIABLE
	  || expr2->symtree == NULL
	  || expr2->rank != 0
	  || (expr2->ts.type != BT_INTEGER
	      && expr2->ts.type != BT_REAL
	      && expr2->ts.type != BT_COMPLEX
	      && expr2->ts.type != BT_LOGICAL))
	{
	  gfc_error ("!$OMP ATOMIC CAPTURE capture statement must read "
		     "from a scalar variable of intrinsic type at %L",
		     &expr2->where);
	  return;
	}
      if (expr2->symtree->n.sym != var)
	{
	  gfc_error ("!$OMP ATOMIC CAPTURE capture statement reads from "
		     "different variable than update statement writes "
		     "into at %L", &expr2->where);
	  return;
	}
    }
}


static struct fortran_omp_context
{
  gfc_code *code;
  hash_set<gfc_symbol *> *sharing_clauses;
  hash_set<gfc_symbol *> *private_iterators;
  struct fortran_omp_context *previous;
  bool is_openmp;
} *omp_current_ctx;
static gfc_code *omp_current_do_code;
static int omp_current_do_collapse;

void
gfc_resolve_omp_do_blocks (gfc_code *code, gfc_namespace *ns)
{
  if (code->block->next && code->block->next->op == EXEC_DO)
    {
      int i;
      gfc_code *c;

      omp_current_do_code = code->block->next;
      if (code->ext.omp_clauses->orderedc)
	omp_current_do_collapse = code->ext.omp_clauses->orderedc;
      else
	omp_current_do_collapse = code->ext.omp_clauses->collapse;
      for (i = 1, c = omp_current_do_code; i < omp_current_do_collapse; i++)
	{
	  c = c->block;
	  if (c->op != EXEC_DO || c->next == NULL)
	    break;
	  c = c->next;
	  if (c->op != EXEC_DO)
	    break;
	}
      if (i < omp_current_do_collapse || omp_current_do_collapse <= 0)
	omp_current_do_collapse = 1;
      if (code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
	{
	  locus *loc
	    = &code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where;
	  if (code->ext.omp_clauses->ordered)
	    gfc_error ("ORDERED clause specified together with %<inscan%> "
		       "REDUCTION clause at %L", loc);
	  if (code->ext.omp_clauses->sched_kind != OMP_SCHED_NONE)
	    gfc_error ("SCHEDULE clause specified together with %<inscan%> "
		       "REDUCTION clause at %L", loc);
	  if (!c->block
	      || !c->block->next
	      || !c->block->next->next
	      || c->block->next->next->op != EXEC_OMP_SCAN
	      || !c->block->next->next->next
	      || c->block->next->next->next->next)
	    gfc_error ("With INSCAN at %L, expected loop body with !$OMP SCAN "
		       "between two structured-block-sequences", loc);
	  else
	    /* Mark as checked; flag will be unset later.  */
	    c->block->next->next->ext.omp_clauses->if_present = true;
	}
    }
  gfc_resolve_blocks (code->block, ns);
  omp_current_do_collapse = 0;
  omp_current_do_code = NULL;
}


void
gfc_resolve_omp_parallel_blocks (gfc_code *code, gfc_namespace *ns)
{
  struct fortran_omp_context ctx;
  gfc_omp_clauses *omp_clauses = code->ext.omp_clauses;
  gfc_omp_namelist *n;
  int list;

  ctx.code = code;
  ctx.sharing_clauses = new hash_set<gfc_symbol *>;
  ctx.private_iterators = new hash_set<gfc_symbol *>;
  ctx.previous = omp_current_ctx;
  ctx.is_openmp = true;
  omp_current_ctx = &ctx;

  for (list = 0; list < OMP_LIST_NUM; list++)
    switch (list)
      {
      case OMP_LIST_SHARED:
      case OMP_LIST_PRIVATE:
      case OMP_LIST_FIRSTPRIVATE:
      case OMP_LIST_LASTPRIVATE:
      case OMP_LIST_REDUCTION:
      case OMP_LIST_REDUCTION_INSCAN:
      case OMP_LIST_REDUCTION_TASK:
      case OMP_LIST_IN_REDUCTION:
      case OMP_LIST_TASK_REDUCTION:
      case OMP_LIST_LINEAR:
	for (n = omp_clauses->lists[list]; n; n = n->next)
	  ctx.sharing_clauses->add (n->sym);
	break;
      default:
	break;
      }

  switch (code->op)
    {
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
    case EXEC_OMP_PARALLEL_DO:
    case EXEC_OMP_PARALLEL_DO_SIMD:
    case EXEC_OMP_TARGET_PARALLEL_DO:
    case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
    case EXEC_OMP_TASKLOOP:
    case EXEC_OMP_TASKLOOP_SIMD:
    case EXEC_OMP_TEAMS_DISTRIBUTE:
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
      gfc_resolve_omp_do_blocks (code, ns);
      break;
    default:
      gfc_resolve_blocks (code->block, ns);
    }

  omp_current_ctx = ctx.previous;
  delete ctx.sharing_clauses;
  delete ctx.private_iterators;
}


/* Save and clear openmp.c private state.  */

void
gfc_omp_save_and_clear_state (struct gfc_omp_saved_state *state)
{
  state->ptrs[0] = omp_current_ctx;
  state->ptrs[1] = omp_current_do_code;
  state->ints[0] = omp_current_do_collapse;
  omp_current_ctx = NULL;
  omp_current_do_code = NULL;
  omp_current_do_collapse = 0;
}


/* Restore openmp.c private state from the saved state.  */

void
gfc_omp_restore_state (struct gfc_omp_saved_state *state)
{
  omp_current_ctx = (struct fortran_omp_context *) state->ptrs[0];
  omp_current_do_code = (gfc_code *) state->ptrs[1];
  omp_current_do_collapse = state->ints[0];
}


/* Note a DO iterator variable.  This is special in !$omp parallel
   construct, where they are predetermined private.  */

void
gfc_resolve_do_iterator (gfc_code *code, gfc_symbol *sym, bool add_clause)
{
  if (omp_current_ctx == NULL)
    return;

  int i = omp_current_do_collapse;
  gfc_code *c = omp_current_do_code;

  if (sym->attr.threadprivate)
    return;

  /* !$omp do and !$omp parallel do iteration variable is predetermined
     private just in the !$omp do resp. !$omp parallel do construct,
     with no implications for the outer parallel constructs.  */

  while (i-- >= 1)
    {
      if (code == c)
	return;

      c = c->block->next;
    }

  /* An openacc context may represent a data clause.  Abort if so.  */
  if (!omp_current_ctx->is_openmp && !oacc_is_loop (omp_current_ctx->code))
    return;

  if (omp_current_ctx->sharing_clauses->contains (sym))
    return;

  if (! omp_current_ctx->private_iterators->add (sym) && add_clause)
    {
      gfc_omp_clauses *omp_clauses = omp_current_ctx->code->ext.omp_clauses;
      gfc_omp_namelist *p;

      p = gfc_get_omp_namelist ();
      p->sym = sym;
      p->next = omp_clauses->lists[OMP_LIST_PRIVATE];
      omp_clauses->lists[OMP_LIST_PRIVATE] = p;
    }
}

static void
handle_local_var (gfc_symbol *sym)
{
  if (sym->attr.flavor != FL_VARIABLE
      || sym->as != NULL
      || (sym->ts.type != BT_INTEGER && sym->ts.type != BT_REAL))
    return;
  gfc_resolve_do_iterator (sym->ns->code, sym, false);
}

void
gfc_resolve_omp_local_vars (gfc_namespace *ns)
{
  if (omp_current_ctx)
    gfc_traverse_ns (ns, handle_local_var);
}

static void
resolve_omp_do (gfc_code *code)
{
  gfc_code *do_code, *c;
  int list, i, collapse;
  gfc_omp_namelist *n;
  gfc_symbol *dovar;
  const char *name;
  bool is_simd = false;

  switch (code->op)
    {
    case EXEC_OMP_DISTRIBUTE: name = "!$OMP DISTRIBUTE"; break;
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
      name = "!$OMP DISTRIBUTE PARALLEL DO";
      break;
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
      name = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_DISTRIBUTE_SIMD:
      name = "!$OMP DISTRIBUTE SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_DO: name = "!$OMP DO"; break;
    case EXEC_OMP_DO_SIMD: name = "!$OMP DO SIMD"; is_simd = true; break;
    case EXEC_OMP_PARALLEL_DO: name = "!$OMP PARALLEL DO"; break;
    case EXEC_OMP_PARALLEL_DO_SIMD:
      name = "!$OMP PARALLEL DO SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_SIMD: name = "!$OMP SIMD"; is_simd = true; break;
    case EXEC_OMP_TARGET_PARALLEL_DO: name = "!$OMP TARGET PARALLEL DO"; break;
    case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
      name = "!$OMP TARGET PARALLEL DO SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_TARGET_SIMD:
      name = "!$OMP TARGET SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
      name = "!$OMP TARGET TEAMS DISTRIBUTE";
      break;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
      name = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
      break;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
      name = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
      name = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_TASKLOOP: name = "!$OMP TASKLOOP"; break;
    case EXEC_OMP_TASKLOOP_SIMD:
      name = "!$OMP TASKLOOP SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_TEAMS_DISTRIBUTE: name = "!$OMP TEAMS DISTRIBUTE"; break;
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
      name = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
      break;
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
      name = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
      is_simd = true;
      break;
    case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
      name = "!$OMP TEAMS DISTRIBUTE SIMD";
      is_simd = true;
      break;
    default: gcc_unreachable ();
    }

  if (code->ext.omp_clauses)
    resolve_omp_clauses (code, code->ext.omp_clauses, NULL);

  do_code = code->block->next;
  if (code->ext.omp_clauses->orderedc)
    collapse = code->ext.omp_clauses->orderedc;
  else
    {
      collapse = code->ext.omp_clauses->collapse;
      if (collapse <= 0)
	collapse = 1;
    }
  for (i = 1; i <= collapse; i++)
    {
      if (do_code->op == EXEC_DO_WHILE)
	{
	  gfc_error ("%s cannot be a DO WHILE or DO without loop control "
		     "at %L", name, &do_code->loc);
	  break;
	}
      if (do_code->op == EXEC_DO_CONCURRENT)
	{
	  gfc_error ("%s cannot be a DO CONCURRENT loop at %L", name,
		     &do_code->loc);
	  break;
	}
      gcc_assert (do_code->op == EXEC_DO);
      if (do_code->ext.iterator->var->ts.type != BT_INTEGER)
	gfc_error ("%s iteration variable must be of type integer at %L",
		   name, &do_code->loc);
      dovar = do_code->ext.iterator->var->symtree->n.sym;
      if (dovar->attr.threadprivate)
	gfc_error ("%s iteration variable must not be THREADPRIVATE "
		   "at %L", name, &do_code->loc);
      if (code->ext.omp_clauses)
	for (list = 0; list < OMP_LIST_NUM; list++)
	  if (!is_simd || code->ext.omp_clauses->collapse > 1
	      ? (list != OMP_LIST_PRIVATE && list != OMP_LIST_LASTPRIVATE)
	      : (list != OMP_LIST_PRIVATE && list != OMP_LIST_LASTPRIVATE
		 && list != OMP_LIST_LINEAR))
	    for (n = code->ext.omp_clauses->lists[list]; n; n = n->next)
	      if (dovar == n->sym)
		{
		  if (!is_simd || code->ext.omp_clauses->collapse > 1)
		    gfc_error ("%s iteration variable present on clause "
			       "other than PRIVATE or LASTPRIVATE at %L",
			       name, &do_code->loc);
		  else
		    gfc_error ("%s iteration variable present on clause "
			       "other than PRIVATE, LASTPRIVATE or "
			       "LINEAR at %L", name, &do_code->loc);
		  break;
		}
      if (i > 1)
	{
	  gfc_code *do_code2 = code->block->next;
	  int j;

	  for (j = 1; j < i; j++)
	    {
	      gfc_symbol *ivar = do_code2->ext.iterator->var->symtree->n.sym;
	      if (dovar == ivar
		  || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->start)
		  || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->end)
		  || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->step))
		{
		  gfc_error ("%s collapsed loops don't form rectangular "
			     "iteration space at %L", name, &do_code->loc);
		  break;
		}
	      do_code2 = do_code2->block->next;
	    }
	}
      for (c = do_code->next; c; c = c->next)
	if (c->op != EXEC_NOP && c->op != EXEC_CONTINUE)
	  {
	    gfc_error ("collapsed %s loops not perfectly nested at %L",
		       name, &c->loc);
	    break;
	  }
      if (i == collapse || c)
	break;
      do_code = do_code->block;
      if (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE)
	{
	  gfc_error ("not enough DO loops for collapsed %s at %L",
		     name, &code->loc);
	  break;
	}
      do_code = do_code->next;
      if (do_code == NULL
	  || (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE))
	{
	  gfc_error ("not enough DO loops for collapsed %s at %L",
		     name, &code->loc);
	  break;
	}
    }
}


static gfc_statement
omp_code_to_statement (gfc_code *code)
{
  switch (code->op)
    {
    case EXEC_OMP_PARALLEL:
      return ST_OMP_PARALLEL;
    case EXEC_OMP_PARALLEL_SECTIONS:
      return ST_OMP_PARALLEL_SECTIONS;
    case EXEC_OMP_SECTIONS:
      return ST_OMP_SECTIONS;
    case EXEC_OMP_ORDERED:
      return ST_OMP_ORDERED;
    case EXEC_OMP_CRITICAL:
      return ST_OMP_CRITICAL;
    case EXEC_OMP_MASTER:
      return ST_OMP_MASTER;
    case EXEC_OMP_SINGLE:
      return ST_OMP_SINGLE;
    case EXEC_OMP_TASK:
      return ST_OMP_TASK;
    case EXEC_OMP_WORKSHARE:
      return ST_OMP_WORKSHARE;
    case EXEC_OMP_PARALLEL_WORKSHARE:
      return ST_OMP_PARALLEL_WORKSHARE;
    case EXEC_OMP_DO:
      return ST_OMP_DO;
    case EXEC_OMP_ATOMIC:
      return ST_OMP_ATOMIC;
    case EXEC_OMP_BARRIER:
      return ST_OMP_BARRIER;
    case EXEC_OMP_CANCEL:
      return ST_OMP_CANCEL;
    case EXEC_OMP_CANCELLATION_POINT:
      return ST_OMP_CANCELLATION_POINT;
    case EXEC_OMP_FLUSH:
      return ST_OMP_FLUSH;
    case EXEC_OMP_DISTRIBUTE:
      return ST_OMP_DISTRIBUTE;
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
      return ST_OMP_DISTRIBUTE_PARALLEL_DO;
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
      return ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD;
    case EXEC_OMP_DISTRIBUTE_SIMD:
      return ST_OMP_DISTRIBUTE_SIMD;
    case EXEC_OMP_DO_SIMD:
      return ST_OMP_DO_SIMD;
    case EXEC_OMP_SCAN:
      return ST_OMP_SCAN;
    case EXEC_OMP_SIMD:
      return ST_OMP_SIMD;
    case EXEC_OMP_TARGET:
      return ST_OMP_TARGET;
    case EXEC_OMP_TARGET_DATA:
      return ST_OMP_TARGET_DATA;
    case EXEC_OMP_TARGET_ENTER_DATA:
      return ST_OMP_TARGET_ENTER_DATA;
    case EXEC_OMP_TARGET_EXIT_DATA:
      return ST_OMP_TARGET_EXIT_DATA;
    case EXEC_OMP_TARGET_PARALLEL:
      return ST_OMP_TARGET_PARALLEL;
    case EXEC_OMP_TARGET_PARALLEL_DO:
      return ST_OMP_TARGET_PARALLEL_DO;
    case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
      return ST_OMP_TARGET_PARALLEL_DO_SIMD;
    case EXEC_OMP_TARGET_SIMD:
      return ST_OMP_TARGET_SIMD;
    case EXEC_OMP_TARGET_TEAMS:
      return ST_OMP_TARGET_TEAMS;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
      return ST_OMP_TARGET_TEAMS_DISTRIBUTE;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
      return ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
      return ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
      return ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD;
    case EXEC_OMP_TARGET_UPDATE:
      return ST_OMP_TARGET_UPDATE;
    case EXEC_OMP_TASKGROUP:
      return ST_OMP_TASKGROUP;
    case EXEC_OMP_TASKLOOP:
      return ST_OMP_TASKLOOP;
    case EXEC_OMP_TASKLOOP_SIMD:
      return ST_OMP_TASKLOOP_SIMD;
    case EXEC_OMP_TASKWAIT:
      return ST_OMP_TASKWAIT;
    case EXEC_OMP_TASKYIELD:
      return ST_OMP_TASKYIELD;
    case EXEC_OMP_TEAMS:
      return ST_OMP_TEAMS;
    case EXEC_OMP_TEAMS_DISTRIBUTE:
      return ST_OMP_TEAMS_DISTRIBUTE;
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
      return ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO;
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
      return ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
    case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
      return ST_OMP_TEAMS_DISTRIBUTE_SIMD;
    case EXEC_OMP_PARALLEL_DO:
      return ST_OMP_PARALLEL_DO;
    case EXEC_OMP_PARALLEL_DO_SIMD:
      return ST_OMP_PARALLEL_DO_SIMD;

    default:
      gcc_unreachable ();
    }
}

static gfc_statement
oacc_code_to_statement (gfc_code *code)
{
  switch (code->op)
    {
    case EXEC_OACC_PARALLEL:
      return ST_OACC_PARALLEL;
    case EXEC_OACC_KERNELS:
      return ST_OACC_KERNELS;
    case EXEC_OACC_SERIAL:
      return ST_OACC_SERIAL;
    case EXEC_OACC_DATA:
      return ST_OACC_DATA;
    case EXEC_OACC_HOST_DATA:
      return ST_OACC_HOST_DATA;
    case EXEC_OACC_PARALLEL_LOOP:
      return ST_OACC_PARALLEL_LOOP;
    case EXEC_OACC_KERNELS_LOOP:
      return ST_OACC_KERNELS_LOOP;
    case EXEC_OACC_SERIAL_LOOP:
      return ST_OACC_SERIAL_LOOP;
    case EXEC_OACC_LOOP:
      return ST_OACC_LOOP;
    case EXEC_OACC_ATOMIC:
      return ST_OACC_ATOMIC;
    case EXEC_OACC_ROUTINE:
      return ST_OACC_ROUTINE;
    case EXEC_OACC_UPDATE:
      return ST_OACC_UPDATE;
    case EXEC_OACC_WAIT:
      return ST_OACC_WAIT;
    case EXEC_OACC_CACHE:
      return ST_OACC_CACHE;
    case EXEC_OACC_ENTER_DATA:
      return ST_OACC_ENTER_DATA;
    case EXEC_OACC_EXIT_DATA:
      return ST_OACC_EXIT_DATA;
    case EXEC_OACC_DECLARE:
      return ST_OACC_DECLARE;
    default:
      gcc_unreachable ();
    }
}

static void
resolve_oacc_directive_inside_omp_region (gfc_code *code)
{
  if (omp_current_ctx != NULL && omp_current_ctx->is_openmp)
    {
      gfc_statement st = omp_code_to_statement (omp_current_ctx->code);
      gfc_statement oacc_st = oacc_code_to_statement (code);
      gfc_error ("The %s directive cannot be specified within "
		 "a %s region at %L", gfc_ascii_statement (oacc_st), 
		 gfc_ascii_statement (st), &code->loc);
    }
}

static void
resolve_omp_directive_inside_oacc_region (gfc_code *code)
{
  if (omp_current_ctx != NULL && !omp_current_ctx->is_openmp)
    {
      gfc_statement st = oacc_code_to_statement (omp_current_ctx->code);
      gfc_statement omp_st = omp_code_to_statement (code);
      gfc_error ("The %s directive cannot be specified within "
		 "a %s region at %L", gfc_ascii_statement (omp_st), 
		 gfc_ascii_statement (st), &code->loc);
    }
}


static void
resolve_oacc_nested_loops (gfc_code *code, gfc_code* do_code, int collapse,
			  const char *clause)
{
  gfc_symbol *dovar;
  gfc_code *c;
  int i;

  for (i = 1; i <= collapse; i++)
    {
      if (do_code->op == EXEC_DO_WHILE)
	{
	  gfc_error ("!$ACC LOOP cannot be a DO WHILE or DO without loop control "
		     "at %L", &do_code->loc);
	  break;
	}
      if (do_code->op == EXEC_DO_CONCURRENT)
	{
	  gfc_error ("!$ACC LOOP cannot be a DO CONCURRENT loop at %L",
		     &do_code->loc);
	  break;
	}
      gcc_assert (do_code->op == EXEC_DO);
      if (do_code->ext.iterator->var->ts.type != BT_INTEGER)
	gfc_error ("!$ACC LOOP iteration variable must be of type integer at %L",
		   &do_code->loc);
      dovar = do_code->ext.iterator->var->symtree->n.sym;
      if (i > 1)
	{
	  gfc_code *do_code2 = code->block->next;
	  int j;

	  for (j = 1; j < i; j++)
	    {
	      gfc_symbol *ivar = do_code2->ext.iterator->var->symtree->n.sym;
	      if (dovar == ivar
		  || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->start)
		  || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->end)
		  || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->step))
		{
		  gfc_error ("!$ACC LOOP %s loops don't form rectangular "
			     "iteration space at %L", clause, &do_code->loc);
		  break;
		}
	      do_code2 = do_code2->block->next;
	    }
	}
      if (i == collapse)
	break;
      for (c = do_code->next; c; c = c->next)
	if (c->op != EXEC_NOP && c->op != EXEC_CONTINUE)
	  {
	    gfc_error ("%s !$ACC LOOP loops not perfectly nested at %L",
		       clause, &c->loc);
	    break;
	  }
      if (c)
	break;
      do_code = do_code->block;
      if (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE
	  && do_code->op != EXEC_DO_CONCURRENT)
	{
	  gfc_error ("not enough DO loops for %s !$ACC LOOP at %L",
		     clause, &code->loc);
	  break;
	}
      do_code = do_code->next;
      if (do_code == NULL
	  || (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE
	      && do_code->op != EXEC_DO_CONCURRENT))
	{
	  gfc_error ("not enough DO loops for %s !$ACC LOOP at %L",
		     clause, &code->loc);
	  break;
	}
    }
}


static void
resolve_oacc_loop_blocks (gfc_code *code)
{
  if (!oacc_is_loop (code))
    return;

  if (code->ext.omp_clauses->tile_list && code->ext.omp_clauses->gang
      && code->ext.omp_clauses->worker && code->ext.omp_clauses->vector)
    gfc_error ("Tiled loop cannot be parallelized across gangs, workers and "
	       "vectors at the same time at %L", &code->loc);

  if (code->ext.omp_clauses->tile_list)
    {
      gfc_expr_list *el;
      for (el = code->ext.omp_clauses->tile_list; el; el = el->next)
	{
	  if (el->expr == NULL)
	    {
	      /* NULL expressions are used to represent '*' arguments.
		 Convert those to a 0 expressions.  */
	      el->expr = gfc_get_constant_expr (BT_INTEGER,
						gfc_default_integer_kind,
						&code->loc);
	      mpz_set_si (el->expr->value.integer, 0);
	    }
	  else
	    {
	      resolve_positive_int_expr (el->expr, "TILE");
	      if (el->expr->expr_type != EXPR_CONSTANT)
		gfc_error ("TILE requires constant expression at %L",
			   &code->loc);
	    }
	}
    }
}


void
gfc_resolve_oacc_blocks (gfc_code *code, gfc_namespace *ns)
{
  fortran_omp_context ctx;
  gfc_omp_clauses *omp_clauses = code->ext.omp_clauses;
  gfc_omp_namelist *n;
  int list;

  resolve_oacc_loop_blocks (code);

  ctx.code = code;
  ctx.sharing_clauses = new hash_set<gfc_symbol *>;
  ctx.private_iterators = new hash_set<gfc_symbol *>;
  ctx.previous = omp_current_ctx;
  ctx.is_openmp = false;
  omp_current_ctx = &ctx;

  for (list = 0; list < OMP_LIST_NUM; list++)
    switch (list)
      {
      case OMP_LIST_PRIVATE:
	for (n = omp_clauses->lists[list]; n; n = n->next)
	  ctx.sharing_clauses->add (n->sym);
	break;
      default:
	break;
      }

  gfc_resolve_blocks (code->block, ns);

  omp_current_ctx = ctx.previous;
  delete ctx.sharing_clauses;
  delete ctx.private_iterators;
}


static void
resolve_oacc_loop (gfc_code *code)
{
  gfc_code *do_code;
  int collapse;

  if (code->ext.omp_clauses)
    resolve_omp_clauses (code, code->ext.omp_clauses, NULL, true);

  do_code = code->block->next;
  collapse = code->ext.omp_clauses->collapse;

  /* Both collapsed and tiled loops are lowered the same way, but are not
     compatible.  In gfc_trans_omp_do, the tile is prioritized.  */
  if (code->ext.omp_clauses->tile_list)
    {
      int num = 0;
      gfc_expr_list *el;
      for (el = code->ext.omp_clauses->tile_list; el; el = el->next)
	++num;
      resolve_oacc_nested_loops (code, code->block->next, num, "tiled");
      return;
    }

  if (collapse <= 0)
    collapse = 1;
  resolve_oacc_nested_loops (code, do_code, collapse, "collapsed");
}

void
gfc_resolve_oacc_declare (gfc_namespace *ns)
{
  int list;
  gfc_omp_namelist *n;
  gfc_oacc_declare *oc;

  if (ns->oacc_declare == NULL)
    return;

  for (oc = ns->oacc_declare; oc; oc = oc->next)
    {
      for (list = 0; list < OMP_LIST_NUM; list++)
	for (n = oc->clauses->lists[list]; n; n = n->next)
	  {
	    n->sym->mark = 0;
	    if (n->sym->attr.flavor != FL_VARIABLE
		&& (n->sym->attr.flavor != FL_PROCEDURE
		    || n->sym->result != n->sym))
	      {
		gfc_error ("Object %qs is not a variable at %L",
			   n->sym->name, &oc->loc);
		continue;
	      }

	    if (n->expr && n->expr->ref->type == REF_ARRAY)
	      {
		gfc_error ("Array sections: %qs not allowed in"
			   " !$ACC DECLARE at %L", n->sym->name, &oc->loc);
		continue;
	      }
	  }

      for (n = oc->clauses->lists[OMP_LIST_DEVICE_RESIDENT]; n; n = n->next)
	check_array_not_assumed (n->sym, oc->loc, "DEVICE_RESIDENT");
    }

  for (oc = ns->oacc_declare; oc; oc = oc->next)
    {
      for (list = 0; list < OMP_LIST_NUM; list++)
	for (n = oc->clauses->lists[list]; n; n = n->next)
	  {
	    if (n->sym->mark)
	      {
		gfc_error ("Symbol %qs present on multiple clauses at %L",
			   n->sym->name, &oc->loc);
		continue;
	      }
	    else
	      n->sym->mark = 1;
	  }
    }

  for (oc = ns->oacc_declare; oc; oc = oc->next)
    {
      for (list = 0; list < OMP_LIST_NUM; list++)
	for (n = oc->clauses->lists[list]; n; n = n->next)
	  n->sym->mark = 0;
    }
}


void
gfc_resolve_oacc_routines (gfc_namespace *ns)
{
  for (gfc_oacc_routine_name *orn = ns->oacc_routine_names;
       orn;
       orn = orn->next)
    {
      gfc_symbol *sym = orn->sym;
      if (!sym->attr.external
	  && !sym->attr.function
	  && !sym->attr.subroutine)
	{
	  gfc_error ("NAME %qs does not refer to a subroutine or function"
		     " in !$ACC ROUTINE ( NAME ) at %L", sym->name, &orn->loc);
	  continue;
	}
      if (!gfc_add_omp_declare_target (&sym->attr, sym->name, &orn->loc))
	{
	  gfc_error ("NAME %qs invalid"
		     " in !$ACC ROUTINE ( NAME ) at %L", sym->name, &orn->loc);
	  continue;
	}
    }
}


void
gfc_resolve_oacc_directive (gfc_code *code, gfc_namespace *ns ATTRIBUTE_UNUSED)
{
  resolve_oacc_directive_inside_omp_region (code);

  switch (code->op)
    {
    case EXEC_OACC_PARALLEL:
    case EXEC_OACC_KERNELS:
    case EXEC_OACC_SERIAL:
    case EXEC_OACC_DATA:
    case EXEC_OACC_HOST_DATA:
    case EXEC_OACC_UPDATE:
    case EXEC_OACC_ENTER_DATA:
    case EXEC_OACC_EXIT_DATA:
    case EXEC_OACC_WAIT:
    case EXEC_OACC_CACHE:
      resolve_omp_clauses (code, code->ext.omp_clauses, NULL, true);
      break;
    case EXEC_OACC_PARALLEL_LOOP:
    case EXEC_OACC_KERNELS_LOOP:
    case EXEC_OACC_SERIAL_LOOP:
    case EXEC_OACC_LOOP:
      resolve_oacc_loop (code);
      break;
    case EXEC_OACC_ATOMIC:
      resolve_omp_atomic (code);
      break;
    default:
      break;
    }
}


/* Resolve OpenMP directive clauses and check various requirements
   of each directive.  */

void
gfc_resolve_omp_directive (gfc_code *code, gfc_namespace *ns)
{
  resolve_omp_directive_inside_oacc_region (code);

  if (code->op != EXEC_OMP_ATOMIC)
    gfc_maybe_initialize_eh ();

  switch (code->op)
    {
    case EXEC_OMP_DISTRIBUTE:
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
    case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
    case EXEC_OMP_DISTRIBUTE_SIMD:
    case EXEC_OMP_DO:
    case EXEC_OMP_DO_SIMD:
    case EXEC_OMP_PARALLEL_DO:
    case EXEC_OMP_PARALLEL_DO_SIMD:
    case EXEC_OMP_SIMD:
    case EXEC_OMP_TARGET_PARALLEL_DO:
    case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
    case EXEC_OMP_TARGET_SIMD:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
    case EXEC_OMP_TASKLOOP:
    case EXEC_OMP_TASKLOOP_SIMD:
    case EXEC_OMP_TEAMS_DISTRIBUTE:
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
    case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
      resolve_omp_do (code);
      break;
    case EXEC_OMP_CANCEL:
    case EXEC_OMP_PARALLEL_WORKSHARE:
    case EXEC_OMP_PARALLEL:
    case EXEC_OMP_PARALLEL_SECTIONS:
    case EXEC_OMP_SECTIONS:
    case EXEC_OMP_SINGLE:
    case EXEC_OMP_TARGET:
    case EXEC_OMP_TARGET_DATA:
    case EXEC_OMP_TARGET_ENTER_DATA:
    case EXEC_OMP_TARGET_EXIT_DATA:
    case EXEC_OMP_TARGET_PARALLEL:
    case EXEC_OMP_TARGET_TEAMS:
    case EXEC_OMP_TASK:
    case EXEC_OMP_TEAMS:
    case EXEC_OMP_WORKSHARE:
      if (code->ext.omp_clauses)
	resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
      break;
    case EXEC_OMP_TARGET_UPDATE:
      if (code->ext.omp_clauses)
	resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
      if (code->ext.omp_clauses == NULL
	  || (code->ext.omp_clauses->lists[OMP_LIST_TO] == NULL
	      && code->ext.omp_clauses->lists[OMP_LIST_FROM] == NULL))
	gfc_error ("OMP TARGET UPDATE at %L requires at least one TO or "
		   "FROM clause", &code->loc);
      break;
    case EXEC_OMP_ATOMIC:
      resolve_omp_clauses (code, code->block->ext.omp_clauses, NULL);
      resolve_omp_atomic (code);
      break;
    case EXEC_OMP_CRITICAL:
      resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
      if (!code->ext.omp_clauses->critical_name
	  && code->ext.omp_clauses->hint
	  && code->ext.omp_clauses->hint->ts.type == BT_INTEGER
	  && code->ext.omp_clauses->hint->expr_type == EXPR_CONSTANT
	  && mpz_sgn (code->ext.omp_clauses->hint->value.integer) != 0)
	gfc_error ("OMP CRITICAL at %L with HINT clause requires a NAME, "
		   "except when omp_sync_hint_none is used", &code->loc);
      break;
    case EXEC_OMP_SCAN:
      /* Flag is only used to checking, hence, it is unset afterwards.  */
      if (!code->ext.omp_clauses->if_present)
	gfc_error ("Unexpected !$OMP SCAN at %L outside loop construct with "
		   "%<inscan%> REDUCTION clause", &code->loc);
      code->ext.omp_clauses->if_present = false;
      resolve_omp_clauses (code, code->ext.omp_clauses, ns);
      break;
    default:
      break;
    }
}

/* Resolve !$omp declare simd constructs in NS.  */

void
gfc_resolve_omp_declare_simd (gfc_namespace *ns)
{
  gfc_omp_declare_simd *ods;

  for (ods = ns->omp_declare_simd; ods; ods = ods->next)
    {
      if (ods->proc_name != NULL
	  && ods->proc_name != ns->proc_name)
	gfc_error ("!$OMP DECLARE SIMD should refer to containing procedure "
		   "%qs at %L", ns->proc_name->name, &ods->where);
      if (ods->clauses)
	resolve_omp_clauses (NULL, ods->clauses, ns);
    }
}

struct omp_udr_callback_data
{
  gfc_omp_udr *omp_udr;
  bool is_initializer;
};

static int
omp_udr_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
		  void *data)
{
  struct omp_udr_callback_data *cd = (struct omp_udr_callback_data *) data;
  if ((*e)->expr_type == EXPR_VARIABLE)
    {
      if (cd->is_initializer)
	{
	  if ((*e)->symtree->n.sym != cd->omp_udr->omp_priv
	      && (*e)->symtree->n.sym != cd->omp_udr->omp_orig)
	    gfc_error ("Variable other than OMP_PRIV or OMP_ORIG used in "
		       "INITIALIZER clause of !$OMP DECLARE REDUCTION at %L",
		       &(*e)->where);
	}
      else
	{
	  if ((*e)->symtree->n.sym != cd->omp_udr->omp_out
	      && (*e)->symtree->n.sym != cd->omp_udr->omp_in)
	    gfc_error ("Variable other than OMP_OUT or OMP_IN used in "
		       "combiner of !$OMP DECLARE REDUCTION at %L",
		       &(*e)->where);
	}
    }
  return 0;
}

/* Resolve !$omp declare reduction constructs.  */

static void
gfc_resolve_omp_udr (gfc_omp_udr *omp_udr)
{
  gfc_actual_arglist *a;
  const char *predef_name = NULL;

  switch (omp_udr->rop)
    {
    case OMP_REDUCTION_PLUS:
    case OMP_REDUCTION_TIMES:
    case OMP_REDUCTION_MINUS:
    case OMP_REDUCTION_AND:
    case OMP_REDUCTION_OR:
    case OMP_REDUCTION_EQV:
    case OMP_REDUCTION_NEQV:
    case OMP_REDUCTION_MAX:
    case OMP_REDUCTION_USER:
      break;
    default:
      gfc_error ("Invalid operator for !$OMP DECLARE REDUCTION %s at %L",
		 omp_udr->name, &omp_udr->where);
      return;
    }

  if (gfc_omp_udr_predef (omp_udr->rop, omp_udr->name,
			  &omp_udr->ts, &predef_name))
    {
      if (predef_name)
	gfc_error_now ("Redefinition of predefined %s "
		       "!$OMP DECLARE REDUCTION at %L",
		       predef_name, &omp_udr->where);
      else
	gfc_error_now ("Redefinition of predefined "
		       "!$OMP DECLARE REDUCTION at %L", &omp_udr->where);
      return;
    }

  if (omp_udr->ts.type == BT_CHARACTER
      && omp_udr->ts.u.cl->length
      && omp_udr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    {
      gfc_error ("CHARACTER length in !$OMP DECLARE REDUCTION %s not "
		 "constant at %L", omp_udr->name, &omp_udr->where);
      return;
    }

  struct omp_udr_callback_data cd;
  cd.omp_udr = omp_udr;
  cd.is_initializer = false;
  gfc_code_walker (&omp_udr->combiner_ns->code, gfc_dummy_code_callback,
		   omp_udr_callback, &cd);
  if (omp_udr->combiner_ns->code->op == EXEC_CALL)
    {
      for (a = omp_udr->combiner_ns->code->ext.actual; a; a = a->next)
	if (a->expr == NULL)
	  break;
      if (a)
	gfc_error ("Subroutine call with alternate returns in combiner "
		   "of !$OMP DECLARE REDUCTION at %L",
		   &omp_udr->combiner_ns->code->loc);
    }
  if (omp_udr->initializer_ns)
    {
      cd.is_initializer = true;
      gfc_code_walker (&omp_udr->initializer_ns->code, gfc_dummy_code_callback,
		       omp_udr_callback, &cd);
      if (omp_udr->initializer_ns->code->op == EXEC_CALL)
	{
	  for (a = omp_udr->initializer_ns->code->ext.actual; a; a = a->next)
	    if (a->expr == NULL)
	      break;
	  if (a)
	    gfc_error ("Subroutine call with alternate returns in "
		       "INITIALIZER clause of !$OMP DECLARE REDUCTION "
		       "at %L", &omp_udr->initializer_ns->code->loc);
	  for (a = omp_udr->initializer_ns->code->ext.actual; a; a = a->next)
	    if (a->expr
		&& a->expr->expr_type == EXPR_VARIABLE
		&& a->expr->symtree->n.sym == omp_udr->omp_priv
		&& a->expr->ref == NULL)
	      break;
	  if (a == NULL)
	    gfc_error ("One of actual subroutine arguments in INITIALIZER "
		       "clause of !$OMP DECLARE REDUCTION must be OMP_PRIV "
		       "at %L", &omp_udr->initializer_ns->code->loc);
	}
    }
  else if (omp_udr->ts.type == BT_DERIVED
	   && !gfc_has_default_initializer (omp_udr->ts.u.derived))
    {
      gfc_error ("Missing INITIALIZER clause for !$OMP DECLARE REDUCTION "
		 "of derived type without default initializer at %L",
		 &omp_udr->where);
      return;
    }
}

void
gfc_resolve_omp_udrs (gfc_symtree *st)
{
  gfc_omp_udr *omp_udr;

  if (st == NULL)
    return;
  gfc_resolve_omp_udrs (st->left);
  gfc_resolve_omp_udrs (st->right);
  for (omp_udr = st->n.omp_udr; omp_udr; omp_udr = omp_udr->next)
    gfc_resolve_omp_udr (omp_udr);
}