+/**
+ * Checks if a compound type has constant fields.
+ */
+static bool has_const_fields(const compound_type_t *type)
+{
+ const scope_t *scope = &type->declaration->scope;
+ const declaration_t *declaration = scope->declarations;
+
+ for (; declaration != NULL; declaration = declaration->next) {
+ if (declaration->namespc != NAMESPACE_NORMAL)
+ continue;
+
+ const type_t *decl_type = skip_typeref(declaration->type);
+ if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
+ return true;
+ }
+ /* TODO */
+ return false;
+}
+
+static bool is_lvalue(const expression_t *expression)
+{
+ switch (expression->kind) {
+ case EXPR_REFERENCE:
+ case EXPR_ARRAY_ACCESS:
+ case EXPR_SELECT:
+ case EXPR_UNARY_DEREFERENCE:
+ return true;
+
+ default:
+ return false;
+ }
+}
+
+static bool is_valid_assignment_lhs(expression_t const* const left)
+{
+ type_t *const orig_type_left = revert_automatic_type_conversion(left);
+ type_t *const type_left = skip_typeref(orig_type_left);
+
+ if (!is_lvalue(left)) {
+ errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
+ left);
+ return false;
+ }
+
+ if (is_type_array(type_left)) {
+ errorf(HERE, "cannot assign to arrays ('%E')", left);
+ return false;
+ }
+ if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
+ errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
+ orig_type_left);
+ return false;
+ }
+ if (is_type_incomplete(type_left)) {
+ errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
+ left, orig_type_left);
+ return false;
+ }
+ if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
+ errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
+ left, orig_type_left);
+ return false;
+ }
+
+ return true;
+}
+