]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/fortran/primary.c
Implement -Wimplicit-fallthrough.
[thirdparty/gcc.git] / gcc / fortran / primary.c
index afc4684682fd5514523feb9331c735f523514727..85589eedc36cbab36faddf856ce2d4ee807e2c88 100644 (file)
@@ -1,6 +1,5 @@
 /* Primary expression subroutines
-   Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
-   Free Software Foundation, Inc.
+   Copyright (C) 2000-2016 Free Software Foundation, Inc.
    Contributed by Andy Vaught
 
 This file is part of GCC.
@@ -21,7 +20,8 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "config.h"
 #include "system.h"
-#include "flags.h"
+#include "coretypes.h"
+#include "options.h"
 #include "gfortran.h"
 #include "arith.h"
 #include "match.h"
@@ -143,8 +143,8 @@ gfc_check_digit (char c, int radix)
 
 
 /* Match the digit string part of an integer if signflag is not set,
-   the signed digit string part if signflag is set.  If the buffer 
-   is NULL, we just count characters for the resolution pass.  Returns 
+   the signed digit string part if signflag is set.  If the buffer
+   is NULL, we just count characters for the resolution pass.  Returns
    the number of characters matched, -1 for no match.  */
 
 static int
@@ -192,7 +192,7 @@ match_digits (int signflag, int radix, char *buffer)
 }
 
 
-/* Match an integer (digit string and optional kind).  
+/* Match an integer (digit string and optional kind).
    A sign will be accepted if signflag is set.  */
 
 static match
@@ -224,6 +224,9 @@ match_integer_constant (gfc_expr **result, int signflag)
   if (kind == -1)
     return MATCH_ERROR;
 
+  if (kind == 4 && flag_integer4_kind == 8)
+    kind = 8;
+
   if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
     {
       gfc_error ("Integer kind %d at %C not available", kind);
@@ -256,7 +259,7 @@ match_hollerith_constant (gfc_expr **result)
   gfc_expr *e = NULL;
   const char *msg;
   int num, pad;
-  int i;  
+  int i;
 
   old_loc = gfc_current_locus;
   gfc_gobble_whitespace ();
@@ -264,8 +267,7 @@ match_hollerith_constant (gfc_expr **result)
   if (match_integer_constant (&e, 0) == MATCH_YES
       && gfc_match_char ('h') == MATCH_YES)
     {
-      if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Hollerith constant "
-                         "at %C") == FAILURE)
+      if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
        goto cleanup;
 
       msg = gfc_extract_int (e, &num);
@@ -388,9 +390,8 @@ match_boz_constant (gfc_expr **result)
     goto backup;
 
   if (x_hex
-      && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
-                         "constant at %C uses non-standard syntax")
-         == FAILURE))
+      && (!gfc_notify_std(GFC_STD_GNU, "Hexadecimal "
+                         "constant at %C uses non-standard syntax")))
       return MATCH_ERROR;
 
   old_loc = gfc_current_locus;
@@ -427,9 +428,8 @@ match_boz_constant (gfc_expr **result)
          goto backup;
        }
 
-      if (gfc_notify_std (GFC_STD_GNU, "Extension: BOZ constant "
-                         "at %C uses non-standard postfix syntax")
-         == FAILURE)
+      if (!gfc_notify_std (GFC_STD_GNU, "BOZ constant "
+                          "at %C uses non-standard postfix syntax"))
        return MATCH_ERROR;
     }
 
@@ -464,9 +464,8 @@ match_boz_constant (gfc_expr **result)
     }
 
   if (!gfc_in_match_data ()
-      && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BOZ used outside a DATA "
-                         "statement at %C")
-         == FAILURE))
+      && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
+                         "statement at %C")))
       return MATCH_ERROR;
 
   *result = e;
@@ -519,7 +518,7 @@ match_real_constant (gfc_expr **result, int signflag)
          if (seen_dp)
            goto done;
 
-         /* Check to see if "." goes with a following operator like 
+         /* Check to see if "." goes with a following operator like
             ".eq.".  */
          temp_loc = gfc_current_locus;
          c = gfc_next_ascii_char ();
@@ -555,12 +554,13 @@ match_real_constant (gfc_expr **result, int signflag)
 
   if (c == 'q')
     {
-      if (gfc_notify_std (GFC_STD_GNU, "Extension: exponent-letter 'q' in "
-                        "real-literal-constant at %C") == FAILURE)
+      if (!gfc_notify_std (GFC_STD_GNU, "exponent-letter 'q' in "
+                          "real-literal-constant at %C"))
        return MATCH_ERROR;
-      else if (gfc_option.warn_real_q_constant)
-       gfc_warning("Extension: exponent-letter 'q' in real-literal-constant "
-                   "at %C");
+      else if (warn_real_q_constant)
+       gfc_warning (OPT_Wreal_q_constant,
+                    "Extension: exponent-letter %<q%> in real-literal-constant "
+                    "at %C");
     }
 
   /* Scan exponent.  */
@@ -631,17 +631,37 @@ done:
     case 'd':
       if (kind != -2)
        {
-         gfc_error ("Real number at %C has a 'd' exponent and an explicit "
+         gfc_error ("Real number at %C has a %<d%> exponent and an explicit "
                     "kind");
          goto cleanup;
        }
       kind = gfc_default_double_kind;
+
+      if (kind == 4)
+       {
+         if (flag_real4_kind == 8)
+           kind = 8;
+         if (flag_real4_kind == 10)
+           kind = 10;
+         if (flag_real4_kind == 16)
+           kind = 16;
+       }
+
+      if (kind == 8)
+       {
+         if (flag_real8_kind == 4)
+           kind = 4;
+         if (flag_real8_kind == 10)
+           kind = 10;
+         if (flag_real8_kind == 16)
+           kind = 16;
+       }
       break;
 
     case 'q':
       if (kind != -2)
        {
-         gfc_error ("Real number at %C has a 'q' exponent and an explicit "
+         gfc_error ("Real number at %C has a %<q%> exponent and an explicit "
                     "kind");
          goto cleanup;
        }
@@ -655,7 +675,7 @@ done:
          kind = 10;
           if (gfc_validate_kind (BT_REAL, kind, true) < 0)
            {
-             gfc_error ("Invalid exponent-letter 'q' in "
+             gfc_error ("Invalid exponent-letter %<q%> in "
                         "real-literal-constant at %C");
              goto cleanup;
            }
@@ -666,6 +686,26 @@ done:
       if (kind == -2)
        kind = gfc_default_real_kind;
 
+      if (kind == 4)
+       {
+         if (flag_real4_kind == 8)
+           kind = 8;
+         if (flag_real4_kind == 10)
+           kind = 10;
+         if (flag_real4_kind == 16)
+           kind = 16;
+       }
+
+      if (kind == 8)
+       {
+         if (flag_real8_kind == 4)
+           kind = 4;
+         if (flag_real8_kind == 10)
+           kind = 10;
+         if (flag_real8_kind == 16)
+           kind = 16;
+       }
+
       if (gfc_validate_kind (BT_REAL, kind, true) < 0)
        {
          gfc_error ("Invalid real kind %d at %C", kind);
@@ -687,8 +727,8 @@ done:
       goto cleanup;
 
     case ARITH_UNDERFLOW:
-      if (gfc_option.warn_underflow)
-       gfc_warning ("Real constant underflows its kind at %C");
+      if (warn_underflow)
+       gfc_warning (OPT_Wunderflow, "Real constant underflows its kind at %C");
       mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
       break;
 
@@ -696,6 +736,58 @@ done:
       gfc_internal_error ("gfc_range_check() returned bad value");
     }
 
+  /* Warn about trailing digits which suggest the user added too many
+     trailing digits, which may cause the appearance of higher pecision
+     than the kind kan support.
+
+     This is done by replacing the rightmost non-zero digit with zero
+     and comparing with the original value.  If these are equal, we
+     assume the user supplied more digits than intended (or forgot to
+     convert to the correct kind).
+  */
+
+  if (warn_conversion_extra)
+    {
+      mpfr_t r;
+      char *c, *p;
+      bool did_break;
+
+      c = strchr (buffer, 'e');
+      if (c == NULL)
+       c = buffer + strlen(buffer);
+
+      did_break = false;
+      for (p = c - 1; p >= buffer; p--)
+       {
+         if (*p == '.')
+           continue;
+
+         if (*p != '0')
+           {
+             *p = '0';
+             did_break = true;
+             break;
+           }
+       }
+
+      if (did_break)
+       {
+         mpfr_init (r);
+         mpfr_set_str (r, buffer, 10, GFC_RND_MODE);
+         if (negate)
+           mpfr_neg (r, r, GFC_RND_MODE);
+
+         mpfr_sub (r, r, e->value.real, GFC_RND_MODE);
+
+         if (mpfr_cmp_ui (r, 0) == 0)
+           gfc_warning (OPT_Wconversion_extra, "Non-significant digits "
+                        "in %qs number at %C, maybe incorrect KIND",
+                        gfc_typename (&e->ts));
+
+         mpfr_clear (r);
+       }
+    }
+
   *result = e;
   return MATCH_YES;
 
@@ -708,7 +800,7 @@ cleanup:
 /* Match a substring reference.  */
 
 static match
-match_substring (gfc_charlen *cl, int init, gfc_ref **result)
+match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
 {
   gfc_expr *start, *end;
   locus old_loc;
@@ -760,7 +852,7 @@ match_substring (gfc_charlen *cl, int init, gfc_ref **result)
     }
 
   /* Optimize away the (:) reference.  */
-  if (start == NULL && end == NULL)
+  if (start == NULL && end == NULL && !deferred)
     ref = NULL;
   else
     {
@@ -817,7 +909,7 @@ next_string_char (gfc_char_t delimiter, int *ret)
       return 0;
     }
 
-  if (gfc_option.flag_backslash && c == '\\')
+  if (flag_backslash && c == '\\')
     {
       old_locus = gfc_current_locus;
 
@@ -825,7 +917,7 @@ next_string_char (gfc_char_t delimiter, int *ret)
        gfc_current_locus = old_locus;
 
       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
-       gfc_warning ("Extension: backslash character at %C");
+       gfc_warning (0, "Extension: backslash character at %C");
     }
 
   if (c != delimiter)
@@ -889,7 +981,7 @@ match_charkind_name (char *name)
 
       if (!ISALNUM (c)
          && c != '_'
-         && (c != '$' || !gfc_option.flag_dollar_ok))
+         && (c != '$' || !flag_dollar_ok))
        break;
 
       *name++ = c;
@@ -912,7 +1004,7 @@ static match
 match_string_constant (gfc_expr **result)
 {
   char name[GFC_MAX_SYMBOL_LEN + 1], peek;
-  int i, kind, length, warn_ampersand, ret;
+  int i, kind, length, save_warn_ampersand, ret;
   locus old_locus, start_locus;
   gfc_symbol *sym;
   gfc_expr *e;
@@ -1032,8 +1124,8 @@ got_delim:
 
   /* We disable the warning for the following loop as the warning has already
      been printed in the loop above.  */
-  warn_ampersand = gfc_option.warn_ampersand;
-  gfc_option.warn_ampersand = 0;
+  save_warn_ampersand = warn_ampersand;
+  warn_ampersand = false;
 
   p = e->value.character.string;
   for (i = 0; i < length; i++)
@@ -1042,7 +1134,8 @@ got_delim:
 
       if (!gfc_check_character_range (c, kind))
        {
-         gfc_error ("Character '%s' in string at %C is not representable "
+         gfc_free_expr (e);
+         gfc_error ("Character %qs in string at %C is not representable "
                     "in character kind %d", gfc_print_wide_char (c), kind);
          return MATCH_ERROR;
        }
@@ -1051,13 +1144,13 @@ got_delim:
     }
 
   *p = '\0';   /* TODO: C-style string is for development/debug purposes.  */
-  gfc_option.warn_ampersand = warn_ampersand;
+  warn_ampersand = save_warn_ampersand;
 
   next_string_char (delimiter, &ret);
   if (ret != -1)
     gfc_internal_error ("match_string_constant(): Delimiter not found");
 
-  if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
+  if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
     e->expr_type = EXPR_SUBSTRING;
 
   *result = e;
@@ -1161,6 +1254,9 @@ match_sym_complex_part (gfc_expr **result)
       return MATCH_ERROR;
     }
 
+  if (!sym->value)
+    goto error;
+
   if (!gfc_numeric_ts (&sym->value->ts))
     {
       gfc_error ("Numeric PARAMETER required in complex constant at %C");
@@ -1173,8 +1269,8 @@ match_sym_complex_part (gfc_expr **result)
       return MATCH_ERROR;
     }
 
-  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PARAMETER symbol in "
-                     "complex constant at %C") == FAILURE)
+  if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
+                      "complex constant at %C"))
     return MATCH_ERROR;
 
   switch (sym->value->ts.type)
@@ -1233,7 +1329,7 @@ static match
 match_complex_constant (gfc_expr **result)
 {
   gfc_expr *e, *real, *imag;
-  gfc_error_buf old_error;
+  gfc_error_buffer old_error;
   gfc_typespec target;
   locus old_loc;
   int kind;
@@ -1456,14 +1552,21 @@ match_actual_arg (gfc_expr **result)
 
          sym = symtree->n.sym;
          gfc_set_sym_referenced (sym);
+         if (sym->attr.flavor == FL_NAMELIST)
+           {
+             gfc_error ("Namelist '%s' can not be an argument at %L",
+             sym->name, &where);
+             break;
+           }
          if (sym->attr.flavor != FL_PROCEDURE
              && sym->attr.flavor != FL_UNKNOWN)
            break;
 
          if (sym->attr.in_common && !sym->attr.proc_pointer)
            {
-             gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name,
-                             &sym->declared_at);
+             if (!gfc_add_flavor (&sym->attr, FL_VARIABLE,
+                                  sym->name, &sym->declared_at))
+               return MATCH_ERROR;
              break;
            }
 
@@ -1537,7 +1640,7 @@ match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
       for (a = base; a; a = a->next)
        if (a->name != NULL && strcmp (a->name, name) == 0)
          {
-           gfc_error ("Keyword '%s' at %C has already appeared in the "
+           gfc_error ("Keyword %qs at %C has already appeared in the "
                       "current argument list", name);
            return MATCH_ERROR;
          }
@@ -1583,26 +1686,28 @@ match_arg_list_function (gfc_actual_arglist *result)
              result->name = "%LOC";
              break;
            }
+         /* FALLTHRU */
        case 'r':
          if (strncmp (name, "ref", 3) == 0)
            {
              result->name = "%REF";
              break;
            }
+         /* FALLTHRU */
        case 'v':
          if (strncmp (name, "val", 3) == 0)
            {
              result->name = "%VAL";
              break;
            }
+         /* FALLTHRU */
        default:
          m = MATCH_ERROR;
          goto cleanup;
        }
     }
 
-  if (gfc_notify_std (GFC_STD_GNU, "Extension: argument list "
-                     "function at %C") == FAILURE)
+  if (!gfc_notify_std (GFC_STD_GNU, "argument list function at %C"))
     {
       m = MATCH_ERROR;
       goto cleanup;
@@ -1674,6 +1779,10 @@ gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
          if (m != MATCH_YES)
            goto cleanup;
 
+         if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
+                              "at %C"))
+           goto cleanup;
+
          tail->label = label;
          goto next;
        }
@@ -1777,11 +1886,12 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
                   bool ppc_arg)
 {
   char name[GFC_MAX_SYMBOL_LEN + 1];
-  gfc_ref *substring, *tail;
+  gfc_ref *substring, *tail, *tmp;
   gfc_component *component;
   gfc_symbol *sym = primary->symtree->n.sym;
   match m;
   bool unknown;
+  char sep;
 
   tail = NULL;
 
@@ -1801,7 +1911,7 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
          || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
              && !CLASS_DATA (sym)->attr.codimension))
        {
-         gfc_error ("Coarray designator at %C but '%s' is not a coarray",
+         gfc_error ("Coarray designator at %C but %qs is not a coarray",
                     sym->name);
          return MATCH_ERROR;
        }
@@ -1811,31 +1921,41 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
      Thus if we have one and parentheses follow, we have to assume that it
      actually is one for now.  The final decision will be made at
      resolution time, of course.  */
-  if (sym->assoc && gfc_peek_ascii_char () == '(')
+  if (sym->assoc && gfc_peek_ascii_char () == '('
+      && !(sym->assoc->dangling && sym->assoc->st
+          && sym->assoc->st->n.sym
+          && sym->assoc->st->n.sym->attr.dimension == 0)
+      && sym->ts.type != BT_CLASS)
     sym->attr.dimension = 1;
 
   if ((equiv_flag && gfc_peek_ascii_char () == '(')
       || gfc_peek_ascii_char () == '[' || sym->attr.codimension
       || (sym->attr.dimension && sym->ts.type != BT_CLASS
-         && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary, NULL)
+         && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
          && !(gfc_matching_procptr_assignment
               && sym->attr.flavor == FL_PROCEDURE))
       || (sym->ts.type == BT_CLASS && sym->attr.class_ok
          && (CLASS_DATA (sym)->attr.dimension
              || CLASS_DATA (sym)->attr.codimension)))
     {
+      gfc_array_spec *as;
+
+      tail = extend_ref (primary, tail);
+      tail->type = REF_ARRAY;
+
       /* In EQUIVALENCE, we don't know yet whether we are seeing
         an array, character variable or array of character
         variables.  We'll leave the decision till resolve time.  */
-      tail = extend_ref (primary, tail);
-      tail->type = REF_ARRAY;
 
-      m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
-                              equiv_flag,
-                              sym->ts.type == BT_CLASS && CLASS_DATA (sym)
-                              ? (CLASS_DATA (sym)->as
-                                 ? CLASS_DATA (sym)->as->corank : 0)
-                              : (sym->as ? sym->as->corank : 0));
+      if (equiv_flag)
+       as = NULL;
+      else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
+       as = CLASS_DATA (sym)->as;
+      else
+       as = sym->as;
+
+      m = gfc_match_array_ref (&tail->u.ar, as, equiv_flag,
+                              as ? as->corank : 0);
       if (m != MATCH_YES)
        return m;
 
@@ -1856,19 +1976,38 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
   if (equiv_flag)
     return MATCH_YES;
 
-  if (sym->ts.type == BT_UNKNOWN && gfc_peek_ascii_char () == '%'
+  /* With DEC extensions, member separator may be '.' or '%'.  */
+  sep = gfc_peek_ascii_char ();
+  m = gfc_match_member_sep (sym);
+  if (m == MATCH_ERROR)
+    return MATCH_ERROR;
+
+  if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
       && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
     gfc_set_default_type (sym, 0, sym->ns);
 
+  if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES)
+    {
+      gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
+      return MATCH_ERROR;
+    }
+  else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
+           && m == MATCH_YES)
+    {
+      gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
+                sep, sym->name);
+      return MATCH_ERROR;
+    }
+
   if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
-      || gfc_match_char ('%') != MATCH_YES)
+      || m != MATCH_YES)
     goto check_substring;
 
   sym = sym->ts.u.derived;
 
   for (;;)
     {
-      gfc_try t;
+      bool t;
       gfc_symtree *tbp;
 
       m = gfc_match_name (name);
@@ -1886,14 +2025,16 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
        {
          gfc_symbol* tbp_sym;
 
-         if (t == FAILURE)
+         if (!t)
            return MATCH_ERROR;
 
          gcc_assert (!tail || !tail->next);
-         gcc_assert (primary->expr_type == EXPR_VARIABLE
-                     || (primary->expr_type == EXPR_STRUCTURE
-                         && primary->symtree && primary->symtree->n.sym
-                         && primary->symtree->n.sym->attr.flavor));
+
+         if (!(primary->expr_type == EXPR_VARIABLE
+               || (primary->expr_type == EXPR_STRUCTURE
+                   && primary->symtree && primary->symtree->n.sym
+                   && primary->symtree->n.sym->attr.flavor)))
+           return MATCH_ERROR;
 
          if (tbp->n.tb->is_generic)
            tbp_sym = NULL;
@@ -1909,6 +2050,8 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
          gcc_assert (primary->symtree->n.sym->attr.referenced);
          if (tbp_sym)
            primary->ts = tbp_sym->ts;
+         else
+           gfc_clear_ts (&primary->ts);
 
          m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
                                        &primary->value.compcall.actual);
@@ -1928,20 +2071,28 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
          break;
        }
 
-      component = gfc_find_component (sym, name, false, false);
+      component = gfc_find_component (sym, name, false, false, &tmp);
       if (component == NULL)
        return MATCH_ERROR;
 
-      tail = extend_ref (primary, tail);
-      tail->type = REF_COMPONENT;
+      /* Extend the reference chain determined by gfc_find_component.  */
+      if (primary->ref == NULL)
+        primary->ref = tmp;
+      else
+        {
+          /* Set by the for loop below for the last component ref.  */
+          gcc_assert (tail != NULL);
+          tail->next = tmp;
+        }
 
-      tail->u.c.component = component;
-      tail->u.c.sym = sym;
+      /* The reference chain may be longer than one hop for union
+         subcomponents; find the new tail.  */
+      for (tail = tmp; tail->next; tail = tail->next)
+        ;
 
       primary->ts = component->ts;
 
-      if (component->attr.proc_pointer && ppc_arg
-         && !gfc_matching_procptr_assignment)
+      if (component->attr.proc_pointer && ppc_arg)
        {
          /* Procedure pointer component call: Look for argument list.  */
          m = gfc_match_actual_arglist (sub_flag,
@@ -1950,9 +2101,9 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
            return MATCH_ERROR;
 
          if (m == MATCH_NO && !gfc_matching_ptr_assignment
-             && !matching_actual_arglist)
+             && !gfc_matching_procptr_assignment && !matching_actual_arglist)
            {
-             gfc_error ("Procedure pointer component '%s' requires an "
+             gfc_error ("Procedure pointer component %qs requires an "
                         "argument list at %C", component->name);
              return MATCH_ERROR;
            }
@@ -1973,9 +2124,8 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
          if (m != MATCH_YES)
            return m;
        }
-      else if (component->ts.type == BT_CLASS
-              && CLASS_DATA (component)->as != NULL
-              && !component->attr.proc_pointer)
+      else if (component->ts.type == BT_CLASS && component->attr.class_ok
+              && CLASS_DATA (component)->as && !component->attr.proc_pointer)
        {
          tail = extend_ref (primary, tail);
          tail->type = REF_ARRAY;
@@ -1988,7 +2138,7 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
        }
 
       if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
-         || gfc_match_char ('%') != MATCH_YES)
+         || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
        break;
 
       sym = component->ts.u.derived;
@@ -1996,7 +2146,7 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
 
 check_substring:
   unknown = false;
-  if (primary->ts.type == BT_UNKNOWN && sym->attr.flavor != FL_DERIVED)
+  if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
     {
       if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
        {
@@ -2008,7 +2158,8 @@ check_substring:
 
   if (primary->ts.type == BT_CHARACTER)
     {
-      switch (match_substring (primary->ts.u.cl, equiv_flag, &substring))
+      bool def = primary->ts.deferred == 1;
+      switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
        {
        case MATCH_YES:
          if (tail == NULL)
@@ -2068,7 +2219,7 @@ check_substring:
 symbol_attribute
 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
 {
-  int dimension, pointer, allocatable, target;
+  int dimension, codimension, pointer, allocatable, target;
   symbol_attribute attr;
   gfc_ref *ref;
   gfc_symbol *sym;
@@ -2083,12 +2234,14 @@ gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
   if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
     {
       dimension = CLASS_DATA (sym)->attr.dimension;
+      codimension = CLASS_DATA (sym)->attr.codimension;
       pointer = CLASS_DATA (sym)->attr.class_pointer;
       allocatable = CLASS_DATA (sym)->attr.allocatable;
     }
   else
     {
       dimension = attr.dimension;
+      codimension = attr.codimension;
       pointer = attr.pointer;
       allocatable = attr.allocatable;
     }
@@ -2123,7 +2276,12 @@ gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
            break;
 
          case AR_UNKNOWN:
-           gfc_internal_error ("gfc_variable_attr(): Bad array reference");
+           /* If any of start, end or stride is not integer, there will
+              already have been an error issued.  */
+           int errors;
+           gfc_get_errors (NULL, &errors);
+           if (errors == 0)
+             gfc_internal_error ("gfc_variable_attr(): Bad array reference");
          }
 
        break;
@@ -2143,11 +2301,13 @@ gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
 
        if (comp->ts.type == BT_CLASS)
          {
+           codimension = CLASS_DATA (comp)->attr.codimension;
            pointer = CLASS_DATA (comp)->attr.class_pointer;
            allocatable = CLASS_DATA (comp)->attr.allocatable;
          }
        else
          {
+           codimension = comp->attr.codimension;
            pointer = comp->attr.pointer;
            allocatable = comp->attr.allocatable;
          }
@@ -2162,6 +2322,7 @@ gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
       }
 
   attr.dimension = dimension;
+  attr.codimension = codimension;
   attr.pointer = pointer;
   attr.allocatable = allocatable;
   attr.target = target;
@@ -2187,7 +2348,7 @@ gfc_expr_attr (gfc_expr *e)
     case EXPR_FUNCTION:
       gfc_clear_attr (&attr);
 
-      if (e->value.function.esym != NULL)
+      if (e->value.function.esym && e->value.function.esym->result)
        {
          gfc_symbol *sym = e->value.function.esym->result;
          attr = sym->attr;
@@ -2215,6 +2376,163 @@ gfc_expr_attr (gfc_expr *e)
 }
 
 
+/* Given an expression, figure out what the ultimate expression
+   attribute is.  This routine is similar to gfc_variable_attr with
+   parts of gfc_expr_attr, but focuses more on the needs of
+   coarrays.  For coarrays a codimension attribute is kind of
+   "infectious" being propagated once set and never cleared.  */
+
+static symbol_attribute
+caf_variable_attr (gfc_expr *expr, bool in_allocate)
+{
+  int dimension, codimension, pointer, allocatable, target, coarray_comp,
+      alloc_comp;
+  symbol_attribute attr;
+  gfc_ref *ref;
+  gfc_symbol *sym;
+  gfc_component *comp;
+
+  if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
+    gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
+
+  sym = expr->symtree->n.sym;
+  gfc_clear_attr (&attr);
+
+  if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
+    {
+      dimension = CLASS_DATA (sym)->attr.dimension;
+      codimension = CLASS_DATA (sym)->attr.codimension;
+      pointer = CLASS_DATA (sym)->attr.class_pointer;
+      allocatable = CLASS_DATA (sym)->attr.allocatable;
+      coarray_comp = CLASS_DATA (sym)->attr.coarray_comp;
+      alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
+    }
+  else
+    {
+      dimension = sym->attr.dimension;
+      codimension = sym->attr.codimension;
+      pointer = sym->attr.pointer;
+      allocatable = sym->attr.allocatable;
+      coarray_comp = sym->attr.coarray_comp;
+      alloc_comp = sym->ts.type == BT_DERIVED
+         ? sym->ts.u.derived->attr.alloc_comp : 0;
+    }
+
+  target = attr.target;
+  if (pointer || attr.proc_pointer)
+    target = 1;
+
+  for (ref = expr->ref; ref; ref = ref->next)
+    switch (ref->type)
+      {
+      case REF_ARRAY:
+
+       switch (ref->u.ar.type)
+         {
+         case AR_FULL:
+         case AR_SECTION:
+           dimension = 1;
+           break;
+
+         case AR_ELEMENT:
+           /* Handle coarrays.  */
+           if (ref->u.ar.dimen > 0 && !in_allocate)
+             allocatable = pointer = 0;
+           break;
+
+         case AR_UNKNOWN:
+           /* If any of start, end or stride is not integer, there will
+              already have been an error issued.  */
+           int errors;
+           gfc_get_errors (NULL, &errors);
+           if (errors == 0)
+             gfc_internal_error ("gfc_caf_attr(): Bad array reference");
+         }
+
+       break;
+
+      case REF_COMPONENT:
+       comp = ref->u.c.component;
+
+       if (comp->ts.type == BT_CLASS)
+         {
+           codimension |= CLASS_DATA (comp)->attr.codimension;
+           pointer = CLASS_DATA (comp)->attr.class_pointer;
+           allocatable = CLASS_DATA (comp)->attr.allocatable;
+           coarray_comp |= CLASS_DATA (comp)->attr.coarray_comp;
+         }
+       else
+         {
+           codimension |= comp->attr.codimension;
+           pointer = comp->attr.pointer;
+           allocatable = comp->attr.allocatable;
+           coarray_comp |= comp->attr.coarray_comp;
+         }
+
+       if (pointer || attr.proc_pointer)
+         target = 1;
+
+       break;
+
+      case REF_SUBSTRING:
+       allocatable = pointer = 0;
+       break;
+      }
+
+  attr.dimension = dimension;
+  attr.codimension = codimension;
+  attr.pointer = pointer;
+  attr.allocatable = allocatable;
+  attr.target = target;
+  attr.save = sym->attr.save;
+  attr.coarray_comp = coarray_comp;
+  attr.alloc_comp = alloc_comp;
+
+  return attr;
+}
+
+
+symbol_attribute
+gfc_caf_attr (gfc_expr *e, bool in_allocate)
+{
+  symbol_attribute attr;
+
+  switch (e->expr_type)
+    {
+    case EXPR_VARIABLE:
+      attr = caf_variable_attr (e, in_allocate);
+      break;
+
+    case EXPR_FUNCTION:
+      gfc_clear_attr (&attr);
+
+      if (e->value.function.esym && e->value.function.esym->result)
+       {
+         gfc_symbol *sym = e->value.function.esym->result;
+         attr = sym->attr;
+         if (sym->ts.type == BT_CLASS)
+           {
+             attr.dimension = CLASS_DATA (sym)->attr.dimension;
+             attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
+             attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
+             attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
+           }
+       }
+      else if (e->symtree)
+       attr = caf_variable_attr (e, in_allocate);
+      else
+       gfc_clear_attr (&attr);
+      break;
+
+    default:
+      gfc_clear_attr (&attr);
+      break;
+    }
+
+  return attr;
+}
+
+
 /* Match a structure constructor.  The initial symbol has already been
    seen.  */
 
@@ -2242,7 +2560,7 @@ gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
    the order required; this also checks along the way that each and every
    component actually has an initializer and handles default initializers
    for components without explicit value given.  */
-static gfc_try
+static bool
 build_actual_constructor (gfc_structure_ctor_component **comp_head,
                          gfc_constructor_base *ctor_head, gfc_symbol *sym)
 {
@@ -2272,11 +2590,12 @@ build_actual_constructor (gfc_structure_ctor_component **comp_head,
                                                      &gfc_current_locus);
          value->ts = comp->ts;
 
-         if (build_actual_constructor (comp_head, &value->value.constructor,
-                                       comp->ts.u.derived) == FAILURE)
+         if (!build_actual_constructor (comp_head,
+                                        &value->value.constructor,
+                                        comp->ts.u.derived))
            {
              gfc_free_expr (value);
-             return FAILURE;
+             return false;
            }
 
          gfc_constructor_append_expr (ctor_head, value, NULL);
@@ -2284,22 +2603,30 @@ build_actual_constructor (gfc_structure_ctor_component **comp_head,
        }
 
       /* If it was not found, try the default initializer if there's any;
-        otherwise, it's an error.  */
+        otherwise, it's an error unless this is a deferred parameter.  */
       if (!comp_iter)
        {
          if (comp->initializer)
            {
-             if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Structure"
-                                 " constructor with missing optional arguments"
-                                 " at %C") == FAILURE)
-               return FAILURE;
+             if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
+                                  "with missing optional arguments at %C"))
+               return false;
              value = gfc_copy_expr (comp->initializer);
            }
-         else
+         else if (comp->attr.allocatable
+                  || (comp->ts.type == BT_CLASS
+                      && CLASS_DATA (comp)->attr.allocatable))
            {
-             gfc_error ("No initializer for component '%s' given in the"
+             if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
+                                  "allocatable component '%qs' given in the "
+                                  "structure constructor at %C", comp->name))
+               return false;
+           }
+         else if (!comp->attr.artificial)
+           {
+             gfc_error ("No initializer for component %qs given in the"
                         " structure constructor at %C!", comp->name);
-             return FAILURE;
+             return false;
            }
        }
       else
@@ -2317,11 +2644,11 @@ build_actual_constructor (gfc_structure_ctor_component **comp_head,
          gfc_free_structure_ctor_component (comp_iter);
        }
     }
-  return SUCCESS;
+  return true;
 }
 
 
-gfc_try
+bool
 gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
                                      gfc_actual_arglist **arglist,
                                      bool parent)
@@ -2345,7 +2672,7 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
 
   if (!parent && sym->attr.abstract)
     {
-      gfc_error ("Can't construct ABSTRACT type '%s' at %L",
+      gfc_error ("Can't construct ABSTRACT type %qs at %L",
                 sym->name, &expr->where);
       goto cleanup;
     }
@@ -2365,9 +2692,8 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
                }
       if (actual->name)
        {
-         if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Structure"
-                             " constructor with named arguments at %C")
-             == FAILURE)
+         if (!gfc_notify_std (GFC_STD_F2003, "Structure"
+                              " constructor with named arguments at %C"))
            goto cleanup;
 
          comp_tail->name = xstrdup (actual->name);
@@ -2378,7 +2704,7 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
        {
          /* Components without name are not allowed after the first named
             component initializer!  */
-         if (!comp)
+         if (!comp || comp->attr.artificial)
            {
              if (last_name)
                gfc_error ("Component initializer without name after component"
@@ -2398,11 +2724,11 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
       /* Find the current component in the structure definition and check
             its access is not private.  */
       if (comp)
-       this_comp = gfc_find_component (sym, comp->name, false, false);
+       this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
       else
        {
          this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
-                                         false, false);
+                                         false, false, NULL);
          comp = NULL; /* Reset needed!  */
        }
 
@@ -2417,13 +2743,13 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
       actual->expr = NULL;
 
       /* Check if this component is already given a value.  */
-      for (comp_iter = comp_head; comp_iter != comp_tail; 
+      for (comp_iter = comp_head; comp_iter != comp_tail;
           comp_iter = comp_iter->next)
        {
          gcc_assert (comp_iter);
          if (!strcmp (comp_iter->name, comp_tail->name))
            {
-             gfc_error ("Component '%s' is initialized twice in the structure"
+             gfc_error ("Component %qs is initialized twice in the structure"
                         " constructor at %L!", comp_tail->name,
                         comp_tail->val ? &comp_tail->where
                                        : &gfc_current_locus);
@@ -2435,7 +2761,7 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
       if (this_comp->attr.pointer && comp_tail->val
          && gfc_is_coindexed (comp_tail->val))
        {
-                 gfc_error ("Coindexed expression to pointer component '%s' in "
+         gfc_error ("Coindexed expression to pointer component %qs in "
                     "structure constructor at %L!", comp_tail->name,
                     &comp_tail->where);
          goto cleanup;
@@ -2446,11 +2772,11 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
           if (comp && comp == sym->components
                 && sym->attr.extension
                && comp_tail->val
-                && (comp_tail->val->ts.type != BT_DERIVED
+                && (!gfc_bt_struct (comp_tail->val->ts.type)
                       ||
                     comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
             {
-              gfc_try m;
+              bool m;
              gfc_actual_arglist *arg_null = NULL;
 
              actual->expr = comp_tail->val;
@@ -2460,7 +2786,7 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
                                        comp->ts.u.derived, &comp_tail->val,
                                        comp->ts.u.derived->attr.zero_comp
                                          ? &arg_null : &actual, true);
-              if (m == FAILURE)
+              if (!m)
                 goto cleanup;
 
              if (comp->ts.u.derived->attr.zero_comp)
@@ -2475,10 +2801,11 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
       if (parent && !comp)
        break;
 
-      actual = actual->next;
+      if (actual)
+       actual = actual->next;
     }
 
-  if (build_actual_constructor (&comp_head, &ctor_head, sym) == FAILURE)
+  if (!build_actual_constructor (&comp_head, &ctor_head, sym))
     goto cleanup;
 
   /* No component should be left, as this should have caused an error in the
@@ -2488,7 +2815,7 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
     {
       for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
        {
-         gfc_error ("component '%s' at %L has already been set by a "
+         gfc_error ("component %qs at %L has already been set by a "
                     "parent derived type constructor", comp_iter->name,
                     &comp_iter->where);
        }
@@ -2513,13 +2840,13 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
       expr->expr_type = EXPR_STRUCTURE;
     }
 
-  gfc_current_locus = old_locus; 
+  gfc_current_locus = old_locus;
   if (parent)
     *arglist = actual;
-  return SUCCESS;
+  return true;
 
   cleanup:
-  gfc_current_locus = old_locus; 
+  gfc_current_locus = old_locus;
 
   for (comp_iter = comp_head; comp_iter; )
     {
@@ -2529,7 +2856,7 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
     }
   gfc_constructor_free (ctor_head);
 
-  return FAILURE;
+  return false;
 }
 
 
@@ -2540,33 +2867,38 @@ gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
   gfc_expr *e;
   gfc_symtree *symtree;
 
-  gfc_get_sym_tree (sym->name, NULL, &symtree, false);   /* Can't fail */
+  gfc_get_ha_sym_tree (sym->name, &symtree);
 
   e = gfc_get_expr ();
   e->symtree = symtree;
   e->expr_type = EXPR_FUNCTION;
 
-  gcc_assert (sym->attr.flavor == FL_DERIVED
+  gcc_assert (gfc_fl_struct (sym->attr.flavor)
              && symtree->n.sym->attr.flavor == FL_PROCEDURE);
   e->value.function.esym = sym;
   e->symtree->n.sym->attr.generic = 1;
 
-   m = gfc_match_actual_arglist (0, &e->value.function.actual);
-   if (m != MATCH_YES)
-     {
-       gfc_free_expr (e);
-       return m;
-     }
-
-   if (gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false)
-       != SUCCESS)
-     {
-       gfc_free_expr (e);
-       return MATCH_ERROR;
-     }
-
-   *result = e;
-   return MATCH_YES;
+  m = gfc_match_actual_arglist (0, &e->value.function.actual);
+  if (m != MATCH_YES)
+    {
+      gfc_free_expr (e);
+      return m;
+    }
+
+  if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
+    {
+      gfc_free_expr (e);
+      return MATCH_ERROR;
+    }
+
+  /* If a structure constructor is in a DATA statement, then each entity
+     in the structure constructor must be a constant.  Try to reduce the
+     expression here.  */
+  if (gfc_in_match_data ())
+    gfc_reduce_init_expr (e);
+  *result = e;
+  return MATCH_YES;
 }
 
 
@@ -2595,7 +2927,7 @@ check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
 /* Procedure pointer as function result: Replace the function symbol by the
    auto-generated hidden result variable named "ppr@".  */
 
-static gfc_try
+static bool
 replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
 {
   /* Check for procedure pointer result variable.  */
@@ -2610,9 +2942,9 @@ replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
       (*sym)->result->attr.referenced = (*sym)->attr.referenced;
       *sym = (*sym)->result;
       *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
-      return SUCCESS;
+      return true;
     }
-  return FAILURE;
+  return false;
 }
 
 
@@ -2639,15 +2971,29 @@ gfc_match_rvalue (gfc_expr **result)
   if (m != MATCH_YES)
     return m;
 
-  if (gfc_find_state (COMP_INTERFACE) == SUCCESS
-      && !gfc_current_ns->has_import_set)
-    i = gfc_get_sym_tree (name, NULL, &symtree, false);
-  else
-    i = gfc_get_ha_sym_tree (name, &symtree);
-
-  if (i)
+  /* Check if the symbol exists.  */
+  if (gfc_find_sym_tree (name, NULL, 1, &symtree))
     return MATCH_ERROR;
 
+  /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
+     type. For derived types we create a generic symbol which links to the
+     derived type symbol; STRUCTUREs are simpler and must not conflict with
+     variables.  */
+  if (!symtree)
+    if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
+      return MATCH_ERROR;
+  if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
+    {
+      if (gfc_find_state (COMP_INTERFACE)
+          && !gfc_current_ns->has_import_set)
+        i = gfc_get_sym_tree (name, NULL, &symtree, false);
+      else
+        i = gfc_get_ha_sym_tree (name, &symtree);
+      if (i)
+        return MATCH_ERROR;
+    }
+
+
   sym = symtree->n.sym;
   e = NULL;
   where = gfc_current_locus;
@@ -2672,7 +3018,7 @@ gfc_match_rvalue (gfc_expr **result)
          && gfc_current_ns->proc_name == sym
          && !sym->attr.dimension)
        {
-         gfc_error ("'%s' at %C is the name of a recursive function "
+         gfc_error ("%qs at %C is the name of a recursive function "
                     "and so refers to the result variable. Use an "
                     "explicit RESULT variable for direct recursion "
                     "(12.5.2.1)", sym->name);
@@ -2687,7 +3033,7 @@ gfc_match_rvalue (gfc_expr **result)
              || sym->ns == gfc_current_ns->parent))
        {
          gfc_entry_list *el = NULL;
-         
+
          for (el = sym->ns->entries; el; el = el->next)
            if (sym == el->sym)
              goto variable;
@@ -2717,7 +3063,7 @@ gfc_match_rvalue (gfc_expr **result)
 
     case FL_PARAMETER:
       /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
-        end up here.  Unfortunately, sym->value->expr_type is set to 
+        end up here.  Unfortunately, sym->value->expr_type is set to
         EXPR_CONSTANT, and so the if () branch would be followed without
         the !sym->as check.  */
       if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
@@ -2758,6 +3104,7 @@ gfc_match_rvalue (gfc_expr **result)
 
       break;
 
+    case FL_STRUCT:
     case FL_DERIVED:
       sym = gfc_use_derived (sym);
       if (sym == NULL)
@@ -2770,7 +3117,7 @@ gfc_match_rvalue (gfc_expr **result)
        procedure, yet it is not sure to be the name of a function.  */
     case FL_PROCEDURE:
 
-    /* Procedure Pointer Assignments. */
+    /* Procedure Pointer Assignments.  */
     procptr0:
       if (gfc_matching_procptr_assignment)
        {
@@ -2779,19 +3126,23 @@ gfc_match_rvalue (gfc_expr **result)
            /* Parse functions returning a procptr.  */
            goto function0;
 
-         if (gfc_is_intrinsic (sym, 0, gfc_current_locus)
-             || gfc_is_intrinsic (sym, 1, gfc_current_locus))
-           sym->attr.intrinsic = 1;
          e = gfc_get_expr ();
          e->expr_type = EXPR_VARIABLE;
          e->symtree = symtree;
          m = gfc_match_varspec (e, 0, false, true);
+         if (!e->ref && sym->attr.flavor == FL_UNKNOWN
+             && sym->ts.type == BT_UNKNOWN
+             && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
+           {
+             m = MATCH_ERROR;
+             break;
+           }
          break;
        }
 
       if (sym->attr.subroutine)
        {
-         gfc_error ("Unexpected use of subroutine name '%s' at %C",
+         gfc_error ("Unexpected use of subroutine name %qs at %C",
                     sym->name);
          m = MATCH_ERROR;
          break;
@@ -2804,7 +3155,8 @@ gfc_match_rvalue (gfc_expr **result)
 
       st = gfc_enclosing_unit (NULL);
 
-      if (st != NULL && st->state == COMP_FUNCTION
+      if (st != NULL
+         && st->state == COMP_FUNCTION
          && st->sym == sym
          && !sym->attr.recursive)
        {
@@ -2822,10 +3174,10 @@ gfc_match_rvalue (gfc_expr **result)
       if (m == MATCH_NO)
        {
          if (sym->attr.proc == PROC_ST_FUNCTION)
-           gfc_error ("Statement function '%s' requires argument list at %C",
+           gfc_error ("Statement function %qs requires argument list at %C",
                       sym->name);
          else
-           gfc_error ("Function '%s' requires an argument list at %C",
+           gfc_error ("Function %qs requires an argument list at %C",
                       sym->name);
 
          m = MATCH_ERROR;
@@ -2849,11 +3201,14 @@ gfc_match_rvalue (gfc_expr **result)
       e->value.function.actual = actual_arglist;
       e->where = gfc_current_locus;
 
-      if (sym->as != NULL)
+      if (sym->ts.type == BT_CLASS && sym->attr.class_ok
+         && CLASS_DATA (sym)->as)
+       e->rank = CLASS_DATA (sym)->as->rank;
+      else if (sym->as != NULL)
        e->rank = sym->as->rank;
 
       if (!sym->attr.function
-         && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
+         && !gfc_add_function (&sym->attr, sym->name, NULL))
        {
          m = MATCH_ERROR;
          break;
@@ -2872,7 +3227,7 @@ gfc_match_rvalue (gfc_expr **result)
           /* make sure we were given a param */
           if (actual_arglist == NULL)
             {
-              gfc_error ("Missing argument to '%s' at %C", sym->name);
+              gfc_error ("Missing argument to %qs at %C", sym->name);
               m = MATCH_ERROR;
               break;
             }
@@ -2890,18 +3245,19 @@ gfc_match_rvalue (gfc_expr **result)
         via an IMPLICIT statement.  This can't wait for the
         resolution phase.  */
 
-      if (gfc_peek_ascii_char () == '%'
+      old_loc = gfc_current_locus;
+      if (gfc_match_member_sep (sym) == MATCH_YES
          && sym->ts.type == BT_UNKNOWN
          && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
        gfc_set_default_type (sym, 0, sym->ns);
+      gfc_current_locus = old_loc;
 
       /* If the symbol has a (co)dimension attribute, the expression is a
         variable.  */
 
       if (sym->attr.dimension || sym->attr.codimension)
        {
-         if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
-                             sym->name, NULL) == FAILURE)
+         if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
            {
              m = MATCH_ERROR;
              break;
@@ -2914,12 +3270,11 @@ gfc_match_rvalue (gfc_expr **result)
          break;
        }
 
-      if (sym->ts.type == BT_CLASS
+      if (sym->ts.type == BT_CLASS && sym->attr.class_ok
          && (CLASS_DATA (sym)->attr.dimension
              || CLASS_DATA (sym)->attr.codimension))
        {
-         if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
-                             sym->name, NULL) == FAILURE)
+         if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
            {
              m = MATCH_ERROR;
              break;
@@ -2944,8 +3299,7 @@ gfc_match_rvalue (gfc_expr **result)
          e->symtree = symtree;
          e->expr_type = EXPR_VARIABLE;
 
-         if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
-                             sym->name, NULL) == FAILURE)
+         if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
            {
              m = MATCH_ERROR;
              break;
@@ -2971,7 +3325,7 @@ gfc_match_rvalue (gfc_expr **result)
       if (m2 != MATCH_YES)
        {
          /* Try to figure out whether we're dealing with a character type.
-            We're peeking ahead here, because we don't want to call 
+            We're peeking ahead here, because we don't want to call
             match_substring if we're dealing with an implicitly typed
             non-character variable.  */
          implicit_char = false;
@@ -2986,21 +3340,21 @@ gfc_match_rvalue (gfc_expr **result)
             that we're not sure is a variable yet.  */
 
          if ((implicit_char || sym->ts.type == BT_CHARACTER)
-             && match_substring (sym->ts.u.cl, 0, &e->ref) == MATCH_YES)
+             && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
            {
 
              e->expr_type = EXPR_VARIABLE;
 
              if (sym->attr.flavor != FL_VARIABLE
-                 && gfc_add_flavor (&sym->attr, FL_VARIABLE,
-                                    sym->name, NULL) == FAILURE)
+                 && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
+                                     sym->name, NULL))
                {
                  m = MATCH_ERROR;
                  break;
                }
 
              if (sym->ts.type == BT_UNKNOWN
-                 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
+                 && !gfc_set_default_type (sym, 1, NULL))
                {
                  m = MATCH_ERROR;
                  break;
@@ -3021,7 +3375,7 @@ gfc_match_rvalue (gfc_expr **result)
       e->expr_type = EXPR_FUNCTION;
 
       if (!sym->attr.function
-         && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
+         && !gfc_add_function (&sym->attr, sym->name, NULL))
        {
          m = MATCH_ERROR;
          break;
@@ -3031,7 +3385,7 @@ gfc_match_rvalue (gfc_expr **result)
 
       m = gfc_match_actual_arglist (0, &e->value.function.actual);
       if (m == MATCH_NO)
-       gfc_error ("Missing argument list in function '%s' at %C", sym->name);
+       gfc_error ("Missing argument list in function %qs at %C", sym->name);
 
       if (m != MATCH_YES)
        {
@@ -3049,13 +3403,19 @@ gfc_match_rvalue (gfc_expr **result)
       break;
 
     generic_function:
-      gfc_get_sym_tree (name, NULL, &symtree, false);  /* Can't fail */
+      /* Look for symbol first; if not found, look for STRUCTURE type symbol
+         specially. Creates a generic symbol for derived types.  */
+      gfc_find_sym_tree (name, NULL, 1, &symtree);
+      if (!symtree)
+        gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
+      if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
+        gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
 
       e = gfc_get_expr ();
       e->symtree = symtree;
       e->expr_type = EXPR_FUNCTION;
 
-      if (sym->attr.flavor == FL_DERIVED)
+      if (gfc_fl_struct (sym->attr.flavor))
        {
          e->value.function.esym = sym;
          e->symtree->n.sym->attr.generic = 1;
@@ -3064,6 +3424,10 @@ gfc_match_rvalue (gfc_expr **result)
       m = gfc_match_actual_arglist (0, &e->value.function.actual);
       break;
 
+    case FL_NAMELIST:
+      m = MATCH_ERROR;
+      break;
+
     default:
       gfc_error ("Symbol at %C is not appropriate for an expression");
       return MATCH_ERROR;
@@ -3095,10 +3459,10 @@ gfc_match_rvalue (gfc_expr **result)
 static match
 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
 {
-  gfc_symbol *sym;
+  gfc_symbol *sym, *dt_sym;
   gfc_symtree *st;
   gfc_expr *expr;
-  locus where;
+  locus where, old_loc;
   match m;
 
   /* Since nothing has any business being an lvalue in a module
@@ -3108,6 +3472,7 @@ match_variable (gfc_expr **result, int equiv_flag, int host_flag)
      of keywords, such as 'end', being turned into variables by
      failed matching to assignments for, e.g., END INTERFACE.  */
   if (gfc_current_state () == COMP_MODULE
+      || gfc_current_state () == COMP_SUBMODULE
       || gfc_current_state () == COMP_INTERFACE
       || gfc_current_state () == COMP_CONTAINS)
     host_flag = 0;
@@ -3128,6 +3493,17 @@ match_variable (gfc_expr **result, int equiv_flag, int host_flag)
   sym->attr.implied_index = 0;
 
   gfc_set_sym_referenced (sym);
+
+  /* STRUCTUREs may share names with variables, but derived types may not.  */
+  if (sym->attr.flavor == FL_PROCEDURE && sym->generic
+      && (dt_sym = gfc_find_dt_in_generic (sym)))
+    {
+      if (dt_sym->attr.flavor == FL_DERIVED)
+        gfc_error ("Derived type '%s' cannot be used as a variable at %C",
+                   sym->name);
+      return MATCH_ERROR;
+    }
+
   switch (sym->attr.flavor)
     {
     case FL_VARIABLE:
@@ -3156,7 +3532,7 @@ match_variable (gfc_expr **result, int equiv_flag, int host_flag)
          flavor = FL_VARIABLE;
 
        if (flavor != FL_UNKNOWN
-           && gfc_add_flavor (&sym->attr, flavor, sym->name, NULL) == FAILURE)
+           && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
          return MATCH_ERROR;
       }
       break;
@@ -3192,13 +3568,14 @@ match_variable (gfc_expr **result, int equiv_flag, int host_flag)
        }
 
       if (sym->attr.proc_pointer
-         || replace_hidden_procptr_result (&sym, &st) == SUCCESS)
+         || replace_hidden_procptr_result (&sym, &st))
        break;
 
       /* Fall through to error */
+      gcc_fallthrough ();
 
     default:
-      gfc_error ("'%s' at %C is not a variable", sym->name);
+      gfc_error ("%qs at %C is not a variable", sym->name);
       return MATCH_ERROR;
     }
 
@@ -3214,10 +3591,12 @@ match_variable (gfc_expr **result, int equiv_flag, int host_flag)
       else
        implicit_ns = sym->ns;
        
-      if (gfc_peek_ascii_char () == '%'
+      old_loc = gfc_current_locus;
+      if (gfc_match_member_sep (sym) == MATCH_YES
          && sym->ts.type == BT_UNKNOWN
          && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
        gfc_set_default_type (sym, 0, implicit_ns);
+      gfc_current_locus = old_loc;
     }
 
   expr = gfc_get_expr ();