def toposort_classes(classes):
    def get_class_topolist(class_name, name_to_class, processed_classes, current_trace):
        if class_name in processed_classes:
            return []
        if class_name in current_trace:
            raise AssertionError(
                'Encountered self-reference in dependency chain of {}'.format(class_name))
        cls = name_to_class[class_name]
        dependencies = _list_superclasses(cls)
        properties = cls['properties'] if 'properties' in cls else []
        for prop in properties:
            if 'linkedClass' in prop:
                dependencies.append(prop['linkedClass'])
        class_list = []
        current_trace.add(class_name)
        for dependency in dependencies:
            class_list.extend(get_class_topolist(
                dependency, name_to_class, processed_classes, current_trace))
        current_trace.remove(class_name)
        class_list.append(name_to_class[class_name])
        processed_classes.add(class_name)
        return class_list
    class_map = {c['name']: c for c in classes}
    seen_classes = set()
    toposorted = []
    for name in class_map.keys():
        toposorted.extend(get_class_topolist(name, class_map, seen_classes, set()))
    return toposorted